From 7b55fda4fad1f94b4f84609b7f9a57c38b2c03de Mon Sep 17 00:00:00 2001 From: aPunch Date: Tue, 20 Mar 2012 14:38:37 -0500 Subject: [PATCH] Added ability to set the server as the owner of an NPC. --- .../java/net/citizensnpcs/EventListen.java | 4 +-- src/main/java/net/citizensnpcs/Settings.java | 1 + .../command/command/NPCCommands.java | 25 ++++++++++++------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/citizensnpcs/EventListen.java b/src/main/java/net/citizensnpcs/EventListen.java index 38efba508..f385f56b9 100644 --- a/src/main/java/net/citizensnpcs/EventListen.java +++ b/src/main/java/net/citizensnpcs/EventListen.java @@ -121,9 +121,7 @@ public class EventListen implements Listener { if (!player.hasMetadata("selected") || player.getMetadata("selected").size() == 0 || player.getMetadata("selected").get(0).asInt() != npc.getId()) { - if (isSettingFulfilled(player, Setting.SELECTION_ITEM) - && (npc.getTrait(Owner.class).getOwner().equals(player.getName()) || player - .hasPermission("citizens.admin"))) { + if (isSettingFulfilled(player, Setting.SELECTION_ITEM) && (npc.getTrait(Owner.class).isOwner(player))) { npcManager.selectNPC(player, npc); Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), npc); if (!Setting.QUICK_SELECT.asBoolean()) diff --git a/src/main/java/net/citizensnpcs/Settings.java b/src/main/java/net/citizensnpcs/Settings.java index 5f430ea3b..0683265c2 100644 --- a/src/main/java/net/citizensnpcs/Settings.java +++ b/src/main/java/net/citizensnpcs/Settings.java @@ -46,6 +46,7 @@ public class Settings { QUICK_SELECT("npc.selection.quick-select", false), SELECTION_ITEM("npc.selection.item", "280"), SELECTION_MESSAGE("npc.selection.message", "You selected !"), + SERVER_OWNS_NPCS("npc.server-ownership", false), TALK_CLOSE_MAXIMUM_COOLDOWN("npc.text.max-talk-cooldown", 60), TALK_CLOSE_MINIMUM_COOLDOWN("npc.text.min-talk-cooldown", 30), TALK_ITEM("npc.text.talk-item", "340"), diff --git a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java index e74409e4a..1594663d1 100644 --- a/src/main/java/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/NPCCommands.java @@ -122,7 +122,9 @@ public class NPCCommands { msg += "."; // Initialize necessary traits - npc.getTrait(Owner.class).setOwner(player.getName()); + npc.addTrait(traitManager.getTrait(Owner.class)); + if (!Setting.SERVER_OWNS_NPCS.asBoolean()) + npc.getTrait(Owner.class).setOwner(player.getName()); npc.getTrait(MobType.class).setType(type.toString()); npc.addTrait(traitManager.getTrait(LookClose.class, npc)); npc.addTrait(traitManager.getTrait(Text.class, npc)); @@ -154,13 +156,13 @@ public class NPCCommands { npcs.add(add); } else if (args.getValueFlags().size() == 0 && args.argsLength() == 1 || args.argsLength() == 2) { for (NPC add : npcManager) - if (add.getTrait(Owner.class).getOwner().equalsIgnoreCase(player.getName())) + if (add.getTrait(Owner.class).isOwner(player)) npcs.add(add); } else { if (args.hasValueFlag("owner")) { String name = args.getFlag("owner"); for (NPC add : npcManager) - if (add.getTrait(Owner.class).getOwner().equalsIgnoreCase(name)) + if (add.getTrait(Owner.class).getOwner().equals(name)) npcs.add(add); } @@ -225,14 +227,19 @@ public class NPCCommands { Messaging.send(player, " Type: " + npc.getTrait(MobType.class).getType()); } - @Command(aliases = { "npc" }, usage = "owner [name]", desc = "Set the owner of an NPC", modifiers = { "owner" }, min = 2, max = 2, permission = "npc.owner") + @Command(aliases = { "npc" }, usage = "owner [name]", desc = "Set the owner of an NPC", modifiers = { "owner" }, min = 1, max = 2, permission = "npc.owner") public void owner(CommandContext args, Player player, NPC npc) throws CommandException { + if (args.argsLength() == 1) { + Messaging.send(player, StringHelper.wrap(npc.getName() + "'s Owner: ") + + npc.getTrait(Owner.class).getOwner()); + return; + } String name = args.getString(1); if (npc.getTrait(Owner.class).getOwner().equals(name)) throw new CommandException("'" + name + "' is already the owner of " + npc.getName() + "."); - npc.getTrait(Owner.class).setOwner(name); - Messaging.send(player, StringHelper.wrap(name) + " is now the owner of " + StringHelper.wrap(npc.getName()) - + "."); + npc.getTrait(Owner.class).setOwner(name.equalsIgnoreCase("server") ? "server" : name); + Messaging.send(player, (name.equalsIgnoreCase("server") ? "The server" : StringHelper.wrap(name)) + + " is now the owner of " + StringHelper.wrap(npc.getName()) + "."); } @Command(aliases = { "npc" }, usage = "remove (all)", desc = "Remove a NPC", modifiers = { "remove" }, min = 1, max = 2) @@ -249,7 +256,7 @@ public class NPCCommands { } if (npc == null) throw new CommandException("You must have an NPC selected to execute that command."); - if (!npc.getTrait(Owner.class).getOwner().equals(player.getName()) && !player.hasPermission("citizens.admin")) + if (!npc.getTrait(Owner.class).isOwner(player)) throw new CommandException("You must be the owner of this NPC to execute that command."); if (!player.hasPermission("citizens.npc.remove") && !player.hasPermission("citizens.admin")) throw new NoPermissionsException(); @@ -290,7 +297,7 @@ public class NPCCommands { if (respawn == null) throw new CommandException("No NPC with the ID '" + args.getInteger(1) + "' exists."); - if (!respawn.getTrait(Owner.class).getOwner().equals(player.getName())) + if (!respawn.getTrait(Owner.class).isOwner(player)) throw new CommandException("You must be the owner of this NPC to execute that command."); if (respawn.spawn(player.getLocation())) {