From cfeabdff0d4ab2cdf11ba3b57e134e122557d3c3 Mon Sep 17 00:00:00 2001 From: fullwall Date: Thu, 18 Apr 2013 22:03:27 +0800 Subject: [PATCH] Check for both types of NPCCreateEvent --- .../java/net/citizensnpcs/EventListen.java | 55 +++++++++++-------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/citizensnpcs/EventListen.java b/src/main/java/net/citizensnpcs/EventListen.java index 769067739..5aa1e35f6 100644 --- a/src/main/java/net/citizensnpcs/EventListen.java +++ b/src/main/java/net/citizensnpcs/EventListen.java @@ -18,6 +18,7 @@ import net.citizensnpcs.api.event.NPCDeathEvent; import net.citizensnpcs.api.event.NPCDespawnEvent; import net.citizensnpcs.api.event.NPCLeftClickEvent; import net.citizensnpcs.api.event.NPCRightClickEvent; +import net.citizensnpcs.api.event.PlayerCreateNPCEvent; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPCRegistry; import net.citizensnpcs.api.trait.trait.Owner; @@ -67,6 +68,32 @@ public class EventListen implements Listener { this.registries = registries; } + private void checkCreationEvent(CommandSenderCreateNPCEvent event) { + if (event.getCreator().hasPermission("citizens.admin.avoid-limits")) + return; + int limit = Setting.DEFAULT_NPC_LIMIT.asInt(); + int maxChecks = Setting.MAX_NPC_LIMIT_CHECKS.asInt(); + for (int i = maxChecks; i >= 0; i--) { + if (!event.getCreator().hasPermission("citizens.npc.limit." + i)) + continue; + limit = i; + break; + } + if (limit < 0) + return; + int owned = 0; + for (NPC npc : npcRegistry) { + if (!event.getNPC().equals(npc) && npc.hasTrait(Owner.class) + && npc.getTrait(Owner.class).isOwnedBy(event.getCreator())) + owned++; + } + int wouldOwn = owned + 1; + if (wouldOwn >= limit) { + event.setCancelled(true); + event.setCancelReason(Messaging.tr(Messages.OVER_NPC_LIMIT, limit)); + } + } + private Iterable getAllNPCs() { return Iterables. concat(npcRegistry, Iterables.concat(registries.values())); } @@ -100,28 +127,7 @@ public class EventListen implements Listener { @EventHandler(ignoreCancelled = true) public void onCommandSenderCreateNPC(CommandSenderCreateNPCEvent event) { - if (event.getCreator().hasPermission("citizens.admin.avoid-limits")) - return; - int limit = Setting.DEFAULT_NPC_LIMIT.asInt(); - int maxChecks = Setting.MAX_NPC_LIMIT_CHECKS.asInt(); - for (int i = maxChecks; i >= 0; i--) { - if (!event.getCreator().hasPermission("citizens.npc.limit." + i)) - continue; - limit = i; - break; - } - if (limit < 0) - return; - int owned = 0; - for (NPC npc : npcRegistry) { - if (!event.getNPC().equals(npc) && npc.getTrait(Owner.class).isOwnedBy(event.getCreator())) - owned++; - } - int wouldOwn = owned + 1; - if (wouldOwn >= limit) { - event.setCancelled(true); - event.setCancelReason(Messaging.tr(Messages.OVER_NPC_LIMIT, limit)); - } + checkCreationEvent(event); } @EventHandler(ignoreCancelled = true) @@ -220,6 +226,11 @@ public class EventListen implements Listener { // undesirable as player NPCs are not real players and confuse plugins. } + @EventHandler(ignoreCancelled = true) + public void onPlayerCreateNPC(PlayerCreateNPCEvent event) { + checkCreationEvent(event); + } + @EventHandler public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { NPC npc = npcRegistry.getNPC(event.getRightClicked());