From 16801d98a0815f896fea6edb6620053b0e7c78c0 Mon Sep 17 00:00:00 2001 From: Sotr Date: Mon, 22 Apr 2019 14:27:48 +0800 Subject: [PATCH] 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 --- .../akarin/server/api/structure/Village.java | 7 +++ api/src/main/java/org/bukkit/World.java | 16 ++++++ .../main/java/org/bukkit/entity/Entity.java | 18 +++++++ .../main/java/org/bukkit/entity/Villager.java | 17 ------ .../server/api/structure/CraftVillage.java | 17 ++++++ .../java/net/minecraft/server/Village.java | 4 +- src/main/java/net/minecraft/server/World.java | 1 + .../org/bukkit/craftbukkit/CraftWorld.java | 51 ++++++++++++++++++ .../craftbukkit/entity/CraftEntity.java | 16 ++++++ .../craftbukkit/entity/CraftVillager.java | 52 ++----------------- work/Paper | 2 +- 11 files changed, 134 insertions(+), 67 deletions(-) create mode 100644 api/src/main/java/io/akarin/server/api/structure/Village.java create mode 100644 src/main/java/io/akarin/server/api/structure/CraftVillage.java diff --git a/api/src/main/java/io/akarin/server/api/structure/Village.java b/api/src/main/java/io/akarin/server/api/structure/Village.java new file mode 100644 index 000000000..85383f950 --- /dev/null +++ b/api/src/main/java/io/akarin/server/api/structure/Village.java @@ -0,0 +1,7 @@ +package io.akarin.server.api.structure; + +/** + * Represents a village structure + */ +public interface Village { +} diff --git a/api/src/main/java/org/bukkit/World.java b/api/src/main/java/org/bukkit/World.java index 107f41735..b22895c22 100644 --- a/api/src/main/java/org/bukkit/World.java +++ b/api/src/main/java/org/bukkit/World.java @@ -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 getVillagesInRange(@NotNull Location location, double xRadius, double yRadius, double zRadius); + // Akarin end + // Paper start /** * @return The amount of Entities in this world diff --git a/api/src/main/java/org/bukkit/entity/Entity.java b/api/src/main/java/org/bukkit/entity/Entity.java index 148c7b1fd..058fe67d9 100644 --- a/api/src/main/java/org/bukkit/entity/Entity.java +++ b/api/src/main/java/org/bukkit/entity/Entity.java @@ -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 getVillagesInRange(double xRadius, double yRadius, double zRadius); + // Akarin end } diff --git a/api/src/main/java/org/bukkit/entity/Villager.java b/api/src/main/java/org/bukkit/entity/Villager.java index 8cc01afd5..a3c94a17f 100644 --- a/api/src/main/java/org/bukkit/entity/Villager.java +++ b/api/src/main/java/org/bukkit/entity/Villager.java @@ -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 getVillagesInRange(@NotNull double xRadius, @NotNull double yRadius, @NotNull double zRadius); - - /** * Gets the current profession of this villager. * diff --git a/src/main/java/io/akarin/server/api/structure/CraftVillage.java b/src/main/java/io/akarin/server/api/structure/CraftVillage.java new file mode 100644 index 000000000..121e77e28 --- /dev/null +++ b/src/main/java/io/akarin/server/api/structure/CraftVillage.java @@ -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"; + } +} diff --git a/src/main/java/net/minecraft/server/Village.java b/src/main/java/net/minecraft/server/Village.java index 1f6dfe985..02b1229ff 100644 --- a/src/main/java/net/minecraft/server/Village.java +++ b/src/main/java/net/minecraft/server/Village.java @@ -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 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 j; private final List 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; diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java index 800419d70..297106747 100644 --- a/src/main/java/net/minecraft/server/World.java +++ b/src/main/java/net/minecraft/server/World.java @@ -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; } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index b7d1f9076..bc45338ec 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -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 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 getVillagesInRange(@NotNull Location location, double xRadius, double yRadius, double zRadius) { + List villages = this.world.getPersistentVillage().getVillages(); + List 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 } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java index a12dd4779..bd938b91e 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java @@ -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 getVillagesInRange(@NotNull double xRadius, @NotNull double yRadius, @NotNull double zRadius) { + return entity.world.getWorld().getVillagesInRange(getLocation(), xRadius, yRadius, zRadius); + } + // Akarin end } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java index ca4327a68..d3e6d3ee3 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftVillager.java @@ -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 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 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 getVillagesInRange(@NotNull double xRadius, @NotNull double yRadius, @NotNull double zRadius) { - WorldServer nmsWorld = ((CraftWorld) this.getWorld()).getHandle(); - PersistentVillage allVillage = nmsWorld.af(); - List villageList = allVillage.getVillages(); - List 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