From e2f372927d36108e7ca6f9690ce73ee7fbed06d9 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sat, 21 Mar 2015 00:09:51 +1100 Subject: [PATCH] Added animal-cap, hostile-cap, mob-cap --- .../plot/PlotSquared.java | 2 +- .../plot/commands/Merge.java | 5 +- .../plot/listeners/PlayerEvents.java | 47 ++++++++++++++--- .../plot/util/ChunkManager.java | 2 +- .../plot/util/bukkit/BukkitChunkManager.java | 50 ++++++++++++++++--- 5 files changed, 89 insertions(+), 17 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java index 53399b903..ab388e4a8 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java @@ -676,7 +676,7 @@ public class PlotSquared { final List booleanFlags = Arrays.asList("notify-enter", "notify-leave", "item-drop", "invincible", "instabreak", "drop-protection", "forcefield", "titles", "pve", "pvp", "no-worldedit"); final List intervalFlags = Arrays.asList("feed", "heal"); final List stringFlags = Arrays.asList("greeting", "farewell"); - final List intFlags = Arrays.asList("mob-cap"); + final List intFlags = Arrays.asList("entity-cap", "animal-cap", "hostile-cap"); for (final String flag : stringFlags) { FlagManager.addFlag(new AbstractFlag(flag)); } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Merge.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Merge.java index a4d3bbca3..ceb41623c 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Merge.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Merge.java @@ -159,7 +159,9 @@ public class Merge extends SubCommand { } if (multiMerge) { for (final UUID uuid : multiUUID) { - CmdConfirm.addPending(UUIDHandler.getPlayer(uuid), "merge request", new Runnable() { + PlotPlayer accepter = UUIDHandler.getPlayer(uuid); + MainUtil.sendMessage(accepter, "ADDING REQUEST FOR: " + accepter.getName()); + CmdConfirm.addPending(accepter, "merge request", new Runnable() { @Override public void run() { PlotPlayer accepter = UUIDHandler.getPlayer(uuid); @@ -197,6 +199,7 @@ public class Merge extends SubCommand { } }); } + MainUtil.sendMessage(plr, "SENT MERGE REQUEST!"); return true; } final PlotWorld plotWorld = PlotSquared.getPlotWorld(world); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java index c8671f1be..22c312b0b 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -682,7 +682,8 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public static void MobSpawn(final CreatureSpawnEvent event) { - if (event.getEntity() instanceof Player) { + Entity entity = event.getEntity(); + if (entity instanceof Player) { return; } final Location loc = BukkitUtil.getLocation(event.getLocation()); @@ -707,14 +708,46 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi } Plot plot = MainUtil.getPlot(loc); if (plot != null && plot.owner != null) { - Flag capFlag = FlagManager.getPlotFlag(plot, "mob-cap"); - if (capFlag == null) { + Flag entityFlag = FlagManager.getPlotFlag(plot, "entity-cap"); + Flag animalFlag = FlagManager.getPlotFlag(plot, "animal-cap"); + Flag monsterFlag = FlagManager.getPlotFlag(plot, "hostile-cap"); + if (!(entity instanceof Creature)) { return; } - int cap = ((Integer) capFlag.getValue()); - int mobs = ChunkManager.manager.countEntities(plot); - if (mobs >= cap) { - event.setCancelled(true); + if (entityFlag == null) { + if (animalFlag == null && (entity instanceof Animals)) { + return; + } + if (monsterFlag == null && (entity instanceof Monster)) { + return; + } + } + int[] mobs = ChunkManager.manager.countEntities(plot); + if (entity instanceof Creature) { + if (entityFlag != null) { + int cap = ((Integer) entityFlag.getValue()); + if (mobs[0] >= cap) { + event.setCancelled(true); + return; + } + } + } + if (entity instanceof Animals) { + if (animalFlag != null) { + int cap = ((Integer) animalFlag.getValue()); + if (mobs[1] >= cap) { + event.setCancelled(true); + } + } + return; + } + if (entity instanceof Monster) { + if (monsterFlag != null) { + int cap = ((Integer) monsterFlag.getValue()); + if (mobs[2] >= cap) { + event.setCancelled(true); + } + } } } } 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 a007f5009..3cffaf91f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java @@ -22,7 +22,7 @@ public abstract class ChunkManager { return new ChunkLoc(x, z); } - public abstract int countEntities(Plot plot); + 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/bukkit/BukkitChunkManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitChunkManager.java index 7dde64ef3..6defeac07 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 @@ -34,6 +34,8 @@ import org.bukkit.block.Sign; import org.bukkit.block.Skull; import org.bukkit.block.banner.Pattern; import org.bukkit.block.banner.PatternType; +import org.bukkit.entity.Animals; +import org.bukkit.entity.Creature; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -867,8 +869,8 @@ public class BukkitChunkManager extends ChunkManager { } @Override - public int countEntities(Plot plot) { - int count = 0; + public int[] countEntities(Plot plot) { + int[] count = new int[3]; World world = BukkitUtil.getWorld(plot.world); Location bot = MainUtil.getPlotBottomLoc(plot.world, plot.id).add(1, 0, 1); @@ -899,18 +901,33 @@ public class BukkitChunkManager extends ChunkManager { if (doWhole) { for (final Entity entity : entities) { + if (!(entity instanceof Creature)) { + continue; + } 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++; + count[0]++; + if (entity instanceof Animals) { + count[1]++; + } + else { + count[2]++; + } } else { final PlotId id = MainUtil.getPlotId(BukkitUtil.getLocation(loc)); if (plot.id.equals(id)) { - count++; + count[0]++; + if (entity instanceof Animals) { + count[1]++; + } + else { + count[2]++; + } } } } @@ -923,17 +940,36 @@ public class BukkitChunkManager extends ChunkManager { Entity[] ents = chunk.getEntities(); if (X == bx || X == tx || Z == bz || Z == tz) { for (final Entity entity : ents) { + if (!(entity instanceof Creature)) { + continue; + } final PlotId id = MainUtil.getPlotId(BukkitUtil.getLocation(entity)); if (plot.id.equals(id)) { - count++; + count[0]++; + if (entity instanceof Animals) { + count[1]++; + } + else { + count[2]++; + } } } } else { - count += ents.length; + for (final Entity entity : ents) { + if (!(entity instanceof Creature)) { + continue; + } + count[0]++; + if (entity instanceof Animals) { + count[1]++; + } + else { + count[2]++; + } + } } } - return count; } return count; }