Overhaul Village API

@Cushaw_BlueMelon
- Add @Nullable
- Double won't be null, so a @NotNull is no need
- Format codes
- Add missing village API (Should not use implementation codes on API!)
- Brings the API to world and entity
This commit is contained in:
Sotr
2019-04-22 14:27:48 +08:00
parent 64238d53a4
commit 16801d98a0
11 changed files with 134 additions and 67 deletions

View File

@@ -0,0 +1,7 @@
package io.akarin.server.api.structure;
/**
* Represents a village structure
*/
public interface Village {
}

View File

@@ -33,6 +33,22 @@ import org.jetbrains.annotations.Nullable;
*/
public interface World extends PluginMessageRecipient, Metadatable {
// Akarin start
/**
* Get the nearest village of the location in range.
*
* @return The nearest village, null if there is no village in range.
*/
public io.akarin.server.api.structure.Village getNearestVillage(@NotNull Location location, double xRadius, double yRadius, double zRadius);
/**
* Get villages which are near by the location in range.
*
* @return All the villages in range, an empty list if there is no village in range.
*/
public List<io.akarin.server.api.structure.Village> getVillagesInRange(@NotNull Location location, double xRadius, double yRadius, double zRadius);
// Akarin end
// Paper start
/**
* @return The amount of Entities in this world

View File

@@ -652,4 +652,22 @@ public interface Entity extends Metadatable, CommandSender, Nameable {
@NotNull
org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason getEntitySpawnReason();
// Paper end
// Akarin start
/**
* Get the nearest village of this entity in range.
*
* @return The nearest village, null if there is no village in range.
*/
@Nullable
public io.akarin.server.api.structure.Village getNearestVillage(double xRadius, double yRadius, double zRadius);
/**
* Get villages which are near by this entity in range.
*
* @return All the villages in range, an empty list if there is no village in range.
*/
@NotNull
public List<io.akarin.server.api.structure.Village> getVillagesInRange(double xRadius, double yRadius, double zRadius);
// Akarin end
}

View File

@@ -4,10 +4,8 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.server.Village;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.Merchant;
@@ -19,21 +17,6 @@ import org.jetbrains.annotations.Nullable;
*/
public interface Villager extends Ageable, NPC, InventoryHolder, Merchant {
/**
* Get the nearest village of this villager in range .
*
* @return The nearest village. null if there is no village in range.
*/
public Village getNearestVillage(@NotNull double xRadius,@NotNull double yRadius,@NotNull double zRadius);
/**
* Get villages which are near by this villager in range.
*
* @return All the villages in range. Empty List if there is no village in range.
*/
public List<Village> getVillagesInRange(@NotNull double xRadius, @NotNull double yRadius, @NotNull double zRadius);
/**
* Gets the current profession of this villager.
*

View File

@@ -0,0 +1,17 @@
package io.akarin.server.api.structure;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class CraftVillage implements Village {
private final net.minecraft.server.Village village;
public Village getHandle() {
return (Village) village;
}
@Override
public String toString() {
return "CraftVillage";
}
}

View File

@@ -5,6 +5,7 @@ import com.google.common.collect.Maps;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.ProfileLookupCallback;
import io.akarin.server.api.structure.CraftVillage;
import net.minecraft.server.UserCache.UserCacheEntry;
import java.util.Iterator;
@@ -18,7 +19,7 @@ public class Village {
private World a; private World getWorld() { return a; } // Paper - OBFHELPER
private final List<VillageDoor> b = Lists.newArrayList();
private BlockPosition c;
private BlockPosition d;private BlockPosition getCenter() { return d; } // Paper - OBFHELPER
private BlockPosition d;public BlockPosition getCenter() { return d; } // Paper - OBFHELPER // Akarin - public
private int e;
private int f;
private int g;
@@ -27,6 +28,7 @@ public class Village {
private final Map<String, Integer> j;
private final List<Village.Aggressor> k;
private int l;
public CraftVillage village = new CraftVillage(this); // Akarin
private Village() { // Paper - Nothing should call this - world needs to be set.
this.c = BlockPosition.ZERO;

View File

@@ -3092,6 +3092,7 @@ public abstract class World implements IEntityAccess, GeneratorAccess, IIBlockAc
this.H = i;
}
public PersistentVillage getPersistentVillage() { return af(); } // Akarin - OBFHELPER
public PersistentVillage af() {
return this.villages;
}

View File

@@ -1,6 +1,7 @@
package org.bukkit.craftbukkit;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import it.unimi.dsi.fastutil.longs.LongSet;
import java.io.File;
@@ -15,6 +16,8 @@ import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import net.minecraft.server.*;
import org.apache.commons.lang.Validate;
@@ -70,6 +73,7 @@ import org.bukkit.util.BoundingBox;
import org.bukkit.util.Consumer;
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
public class CraftWorld implements World {
public static final int CUSTOM_DIMENSION_OFFSET = 10;
@@ -1887,4 +1891,51 @@ public class CraftWorld implements World {
return spigot;
}
// Spigot end
// Akarin start
@Nullable
@Override
public io.akarin.server.api.structure.Village getNearestVillage(@NotNull Location location, double xRadius, double yRadius, double zRadius) {
List<Village> villages = this.world.getPersistentVillage().getVillages();
double nearestRange = -1;
Village nearestVillage = null;
for (Village village : villages) {
BlockPosition center = village.getCenter();
double xRange = Math.abs(center.getX() - location.getX());
double yRange = Math.abs(center.getY() - location.getY());
double zRange = Math.abs(center.getZ() - location.getZ());
if (xRange <= xRadius && yRange <= yRadius && zRange <+ zRadius) {
double range = Math.sqrt(xRange * xRange + yRange * yRange + zRange * zRange);
if (nearestVillage == null || range < nearestRange) {
nearestVillage = village;
nearestRange = range;
}
}
}
return nearestVillage == null ? null : nearestVillage.village;
}
@Override
public List<io.akarin.server.api.structure.Village> getVillagesInRange(@NotNull Location location, double xRadius, double yRadius, double zRadius) {
List<Village> villages = this.world.getPersistentVillage().getVillages();
List<io.akarin.server.api.structure.Village> villagesInRange = Lists.newArrayList();
for (Village village : villages) {
BlockPosition center = village.getCenter();
double xRange = Math.abs(center.getX() - location.getX());
double yRange = Math.abs(center.getY() - location.getY());
double zRange = Math.abs(center.getZ() - location.getZ());
if (xRange <= xRadius && yRange <= yRadius && zRange <+ zRadius)
villagesInRange.add(village.village);
}
return villagesInRange;
}
// Akarin end
}

View File

@@ -7,6 +7,8 @@ import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.annotation.Nullable;
import net.minecraft.server.*;
import org.bukkit.Chunk;
@@ -32,6 +34,7 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.NumberConversions;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
public abstract class CraftEntity implements org.bukkit.entity.Entity {
private static PermissibleBase perm;
@@ -866,4 +869,17 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
return getHandle().spawnReason;
}
// Paper end
// Akarin start
@Nullable
@Override
public io.akarin.server.api.structure.Village getNearestVillage(@NotNull double xRadius, @NotNull double yRadius, @NotNull double zRadius) {
return entity.world.getWorld().getNearestVillage(getLocation(), xRadius, yRadius, zRadius);
}
@Override
public List<io.akarin.server.api.structure.Village> getVillagesInRange(@NotNull double xRadius, @NotNull double yRadius, @NotNull double zRadius) {
return entity.world.getWorld().getVillagesInRange(getLocation(), xRadius, yRadius, zRadius);
}
// Akarin end
}

View File

@@ -8,6 +8,7 @@ import javax.annotation.Nullable;
import net.minecraft.server.*;
import org.apache.commons.lang.Validate;
import org.apache.logging.log4j.io.IoBuilder;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.inventory.CraftInventory;
@@ -20,6 +21,9 @@ import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.MerchantRecipe;
import org.jetbrains.annotations.NotNull;
import com.google.common.collect.Lists;
import com.google.common.collect.Range;
public class CraftVillager extends CraftAgeable implements Villager, InventoryHolder {
private static final Map<Career, Integer> careerIDMap = new HashMap<>();
@@ -147,54 +151,6 @@ public class CraftVillager extends CraftAgeable implements Villager, InventoryHo
return null;
}
@Override
public Village getNearestVillage(@NotNull double xRadius, @NotNull double yRadius, @NotNull double zRadius) {
WorldServer nmsWorld = ((CraftWorld) this.getWorld()).getHandle();
PersistentVillage allVillage = nmsWorld.af();
List<Village> villageList = allVillage.getVillages();
Village nearestVillage = null;
double nearestRange = Double.NaN;
for (Village x : villageList) {
BlockPosition p = x.a();
double xRange = Math.abs(p.getX()-this.getLocation().getX());
double yRange = Math.abs(p.getY()-this.getLocation().getY());
double zRange = Math.abs(p.getZ()-this.getLocation().getZ());
if(xRange>xRadius||yRange>yRadius||zRange>zRadius){
continue;
}else {
if(nearestVillage==null){
nearestVillage = x;
nearestRange = Math.sqrt(Math.pow(xRange,2)+Math.pow(yRange,2)+Math.pow(zRange,2));
}else{
double range = Math.sqrt(Math.pow(xRange,2)+Math.pow(yRange,2)+Math.pow(zRange,2));
if(range<nearestRange) {
nearestVillage = x;
nearestRange = range;
}
}
}
}
return nearestVillage;
}
@Override
public List<Village> getVillagesInRange(@NotNull double xRadius, @NotNull double yRadius, @NotNull double zRadius) {
WorldServer nmsWorld = ((CraftWorld) this.getWorld()).getHandle();
PersistentVillage allVillage = nmsWorld.af();
List<Village> villageList = allVillage.getVillages();
List<Village> villagesInRange = new ArrayList<>();
for(Village x:villageList){
BlockPosition p = x.a();
double xRange = Math.abs(p.getX()-this.getLocation().getX());
double yRange = Math.abs(p.getY()-this.getLocation().getY());
double zRange = Math.abs(p.getZ()-this.getLocation().getZ());
if(xRange<xRadius&&yRange<yRadius&&zRange<zRadius){
villagesInRange.add(x);
}
}
return villagesInRange;
}
private static int getCareerID(Career career) {
return careerIDMap.getOrDefault(career, 0);
}