From facd43700d48686d071061f41667d969f4ce8388 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sun, 7 Aug 2016 04:00:50 +1000 Subject: [PATCH] Cache entity count every second --- .../bukkit/listeners/EntitySpawnListener.java | 6 +++--- .../com/plotsquared/bukkit/listeners/PlayerEvents.java | 9 +++++++-- .../com/plotsquared/bukkit/util/BukkitChunkManager.java | 4 ++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/EntitySpawnListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/EntitySpawnListener.java index 3c1ea45e1..4dedca86d 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/EntitySpawnListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/EntitySpawnListener.java @@ -22,6 +22,9 @@ public class EntitySpawnListener implements Listener { return; } Plot plot = area.getOwnedPlotAbs(location); + if (Settings.Done.RESTRICT_BUILDING && plot.hasFlag(Flags.DONE)) { + event.setCancelled(true); + } switch (entity.getType()) { case ENDER_CRYSTAL: if (plot == null) { @@ -34,8 +37,5 @@ public class EntitySpawnListener implements Listener { event.setCancelled(true); } } - if (Settings.Done.RESTRICT_BUILDING && plot.hasFlag(Flags.DONE)) { - event.setCancelled(true); - } } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java index 6297fe1b5..bd81c5b01 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java @@ -1409,15 +1409,20 @@ public class PlayerEvents extends PlotListener implements Listener { continue; } if (cap == 0) { - return false; + return true; } if (mobs == null) { mobs = plot.countEntities(); } - if (mobs[i] >= cap) { + if (mobs[i]++ >= cap) { return true; } } + if (mobs != null) { + plot.setMeta("EntityCount", mobs); + plot.setMeta("EntityCountTime", System.currentTimeMillis()); + } + return false; } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java index ba16915b4..8e008c08c 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java @@ -425,6 +425,10 @@ public class BukkitChunkManager extends ChunkManager { @Override public int[] countEntities(Plot plot) { + int[] existing = (int[]) plot.getMeta("EntityCount"); + if (existing != null && (System.currentTimeMillis() - (long) plot.getMeta("EntityCountTime") < 1000)) { + return existing; + } PlotArea area = plot.getArea(); World world = BukkitUtil.getWorld(area.worldname);