From 0c403ed88ed72f3e1e4b1d770384f78fbc58598d Mon Sep 17 00:00:00 2001 From: fullwall Date: Sat, 16 Nov 2013 15:08:00 +0800 Subject: [PATCH] Add /npc passive --- src/main/java/net/citizensnpcs/EventListen.java | 12 +++++++++++- .../net/citizensnpcs/commands/NPCCommands.java | 17 ++++++++++++++++- .../java/net/citizensnpcs/util/Messages.java | 2 ++ src/main/resources/messages_en.properties | 2 ++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/citizensnpcs/EventListen.java b/src/main/java/net/citizensnpcs/EventListen.java index e226a5fae..40f397e76 100644 --- a/src/main/java/net/citizensnpcs/EventListen.java +++ b/src/main/java/net/citizensnpcs/EventListen.java @@ -13,6 +13,7 @@ import net.citizensnpcs.api.event.NPCCombustByEntityEvent; import net.citizensnpcs.api.event.NPCCombustEvent; import net.citizensnpcs.api.event.NPCDamageByBlockEvent; import net.citizensnpcs.api.event.NPCDamageByEntityEvent; +import net.citizensnpcs.api.event.NPCDamageEntityEvent; import net.citizensnpcs.api.event.NPCDamageEvent; import net.citizensnpcs.api.event.NPCDeathEvent; import net.citizensnpcs.api.event.NPCDespawnEvent; @@ -152,8 +153,17 @@ public class EventListen implements Listener { @EventHandler public void onEntityDamage(EntityDamageEvent event) { NPC npc = npcRegistry.getNPC(event.getEntity()); - if (npc == null) + if (npc == null) { + if (event instanceof EntityDamageByEntityEvent) { + npc = npcRegistry.getNPC(((EntityDamageByEntityEvent) event).getDamager()); + if (npc == null) + return; + event.setCancelled(npc.data().get(NPC.DAMAGE_OTHERS_METADATA, true)); + NPCDamageEntityEvent damageEvent = new NPCDamageEntityEvent(npc, (EntityDamageByEntityEvent) event); + Bukkit.getPluginManager().callEvent(damageEvent); + } return; + } event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true)); if (event instanceof EntityDamageByEntityEvent) { NPCDamageByEntityEvent damageEvent = new NPCDamageByEntityEvent(npc, (EntityDamageByEntityEvent) event); diff --git a/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 989dc95ca..048ca54de 100644 --- a/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -807,6 +807,21 @@ public class NPCCommands { Messaging.sendTr(sender, serverOwner ? Messages.OWNER_SET_SERVER : Messages.OWNER_SET, npc.getName(), name); } + @Command( + aliases = { "npc" }, + usage = "passive (--set [true|false])", + desc = "Sets whether an NPC damages other entities or not", + modifiers = { "passive" }, + min = 1, + max = 1, + permission = "citizens.npc.passive") + public void passive(CommandContext args, CommandSender sender, NPC npc) throws CommandException { + boolean passive = args.hasValueFlag("set") ? Boolean.parseBoolean(args.getFlag("set")) : npc.data().get( + NPC.DAMAGE_OTHERS_METADATA, true); + npc.data().setPersistent(NPC.DAMAGE_OTHERS_METADATA, !passive); + Messaging.sendTr(sender, passive ? Messages.PASSIVE_SET : Messages.PASSIVE_UNSET, npc.getName()); + } + @Command( aliases = { "npc" }, usage = "pathopt --avoid-water|aw [true|false]", @@ -1196,7 +1211,7 @@ public class NPCCommands { max = 1, permission = "citizens.npc.swim") public void swim(CommandContext args, CommandSender sender, NPC npc) throws CommandException { - boolean swim = args.hasValueFlag("set") ? Boolean.parseBoolean(args.getFlag("swim")) : !npc.data().get( + boolean swim = args.hasValueFlag("set") ? Boolean.parseBoolean(args.getFlag("set")) : !npc.data().get( NPC.SWIMMING_METADATA, true); npc.data().setPersistent(NPC.SWIMMING_METADATA, swim); Messaging.sendTr(sender, swim ? Messages.SWIMMING_SET : Messages.SWIMMING_UNSET, npc.getName()); diff --git a/src/main/java/net/citizensnpcs/util/Messages.java b/src/main/java/net/citizensnpcs/util/Messages.java index 4943f669c..c7c04b1f7 100644 --- a/src/main/java/net/citizensnpcs/util/Messages.java +++ b/src/main/java/net/citizensnpcs/util/Messages.java @@ -150,6 +150,8 @@ public class Messages { public static final String OVER_NPC_LIMIT = "citizens.limits.over-npc-limit"; public static final String OWNER_SET = "citizens.commands.npc.owner.set"; public static final String OWNER_SET_SERVER = "citizens.commands.npc.owner.set-server"; + public static final String PASSIVE_SET = "citizens.commands.npc.passive.set"; + public static final String PASSIVE_UNSET = "citizens.commands.npc.passive.unset"; public static final String PATHFINDING_OPTIONS_AVOID_WATER_SET = "citizens.commands.npc.pathopt.avoid-water-set"; public static final String PATHFINDING_OPTIONS_AVOID_WATER_UNSET = "citizens.commands.npc.pathopt.avoid-water-unset"; public static final String PATHFINDING_RANGE_SET = "citizens.commands.npc.pathfindingrange.set"; diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 3552a2700..04a8d4b5b 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -64,6 +64,8 @@ citizens.commands.npc.owner.already-owner={0} is already the owner of {1}. citizens.commands.npc.owner.owner=[[{0}]]''s owner is [[{1}]]. citizens.commands.npc.owner.set-server=[[The server]] is now the owner of {0}. citizens.commands.npc.owner.set=[[{1}]] is now the owner of {0}. +citizens.commands.npc.passive.set=[[{0}]] will no longer damage entities. +citizens.commands.npc.passive.unset=[[{0}]] will now damage entities. citizens.commands.npc.pathfindingrange.set=Pathfinding range set to [[{0}]]. citizens.commands.npc.pathopt.avoid-water-set=[[{0}]] will now avoid water. citizens.commands.npc.pathopt.avoid-water-unset=[[{0}]] will no longer avoid water.