From 1f8702f072b00152a04626dc64ad26ea59aca433 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Thu, 12 Mar 2015 19:52:26 +1100 Subject: [PATCH] Entity tracking and invalid world detection --- .../plot/PlotSquared.java | 6 +- .../plot/util/ChunkManager.java | 2 + .../plot/util/MainUtil.java | 2 +- .../plot/util/bukkit/BukkitChunkManager.java | 72 +++++++++++++++++++ 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java index 54e1bb439..9c9326ba7 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java @@ -136,8 +136,10 @@ public class PlotSquared { public static Set getPlots() { final ArrayList newplots = new ArrayList<>(); - for (final HashMap world : plots.values()) { - newplots.addAll(world.values()); + for (final Entry> entry : plots.entrySet()) { + if (isPlotWorld(entry.getKey())) { + newplots.addAll(entry.getValue().values()); + } } return new LinkedHashSet<>(newplots); } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java index 6e94e3656..a007f5009 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java @@ -21,6 +21,8 @@ public abstract class ChunkManager { final int z = loc.getZ() >> 9; return new ChunkLoc(x, z); } + + public abstract int countEntities(Plot plot); public abstract boolean loadChunk(String world, ChunkLoc loc); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java index 0292e96b7..cbe2db01e 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java @@ -54,7 +54,7 @@ public class MainUtil { static long state = 1; public static HashMap lastPlot = new HashMap<>(); public static HashMap worldBorder = new HashMap<>(); - + public static ArrayList getMaxPlotSelectionIds(final String world, PlotId pos1, PlotId pos2) { final Plot plot1 = PlotSquared.getPlots(world).get(pos1); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitChunkManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitChunkManager.java index bb330e878..f96679c86 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitChunkManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitChunkManager.java @@ -865,4 +865,76 @@ public class BukkitChunkManager extends ChunkManager { clearAllEntities(MainUtil.getPlot(worldname, pos2)); // FIXME swap plots } + + @Override + public int countEntities(Plot plot) { + int count = 0; + World world = BukkitUtil.getWorld(plot.world); + + Location bot = MainUtil.getPlotBottomLoc(plot.world, plot.id).add(1, 0, 1); + Location top = MainUtil.getPlotTopLoc(plot.world, plot.id); + int bx = bot.getX() >> 4; + int bz = bot.getZ() >> 4; + + int tx = top.getX() >> 4; + int tz = top.getZ() >> 4; + + int size = (tx-bx) << 4; + + HashSet chunks = new HashSet<>(); + for (int X = bx; X <= tx; X++) { + for (int Z = bz; Z <= tz; Z++) { + chunks.add(world.getChunkAt(X,Z)); + } + } + + boolean doWhole = false; + List entities = null; + if (size > 200) { + entities = world.getEntities(); + if (entities.size() < 16 + (size * size / 64)) { + doWhole = true; + } + } + + if (doWhole) { + for (final Entity entity : entities) { + org.bukkit.Location loc = entity.getLocation(); + Chunk chunk = loc.getChunk(); + if (chunks.contains(chunk)) { + int X = chunk.getX(); + int Z = chunk.getX(); + if (X > bx && X < tx && Z > bz && Z < tz) { + count++; + } + else { + final PlotId id = MainUtil.getPlotId(BukkitUtil.getLocation(loc)); + if (plot.id.equals(id)) { + count++; + } + } + } + } + } + else { + for (Chunk chunk : chunks) { + int X = chunk.getX(); + int Z = chunk.getX(); + Entity[] ents = chunk.getEntities(); + if (X == bx || X == tx || Z == bz || Z == tz) { + for (final Entity entity : ents) { + final PlotId id = MainUtil.getPlotId(BukkitUtil.getLocation(entity)); + if (plot.id.equals(id)) { + count++; + } + } + } + else { + count += ents.length; + } + } + return count; + } + return count; + } }