From 7610f03bb1f006fed629b0b2898d68c3de687526 Mon Sep 17 00:00:00 2001 From: fullwall Date: Sun, 16 Sep 2012 14:35:04 +0800 Subject: [PATCH] Fix up trait command requirements, add /trait add|remove --- .../command/command/TraitCommands.java | 124 +++++++++++++----- 1 file changed, 94 insertions(+), 30 deletions(-) diff --git a/src/main/java/net/citizensnpcs/command/command/TraitCommands.java b/src/main/java/net/citizensnpcs/command/command/TraitCommands.java index 4fbd0a2bb..bb4b6766c 100644 --- a/src/main/java/net/citizensnpcs/command/command/TraitCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/TraitCommands.java @@ -8,6 +8,7 @@ import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.command.Command; import net.citizensnpcs.command.CommandConfigurable; import net.citizensnpcs.command.CommandContext; +import net.citizensnpcs.command.Requirements; import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.NoPermissionsException; import net.citizensnpcs.util.Messaging; @@ -20,18 +21,106 @@ import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.collect.Lists; +@Requirements(selected = true, ownership = true) public class TraitCommands { @Command( aliases = { "trait", "tr" }, - usage = "[trait name], [trait name]...", + usage = "add [trait name]...", desc = "Adds traits to the NPC", - modifiers = { "*" }, + modifiers = { "add", "a" }, min = 1, - max = 1, - flags = "r", permission = "npc.trait") public void add(CommandContext args, CommandSender sender, NPC npc) throws CommandException { + List added = Lists.newArrayList(); + List failed = Lists.newArrayList(); + for (String traitName : Splitter.on(',').split(args.getJoinedStrings(0))) { + if (!sender.hasPermission("citizens.npc.trait." + traitName)) + failed.add(String.format("%s: No permission", traitName)); + + Class clazz = CitizensAPI.getTraitFactory().getTraitClass(traitName); + if (clazz == null) { + failed.add(String.format("%s: Trait not found", traitName)); + continue; + } + if (npc.hasTrait(clazz)) { + failed.add(String.format("%s: Already added", traitName)); + continue; + } + npc.addTrait(clazz); + added.add(StringHelper.wrap(traitName)); + } + if (added.size() > 0) + Messaging.sendF(sender, ChatColor.GREEN + "Added %s successfully.", Joiner.on(", ").join(added)); + if (failed.size() > 0) + Messaging.sendF(sender, ChatColor.GRAY + "Couldn't add %s.", Joiner.on(", ").join(failed)); + } + + @Command( + aliases = { "traitc", "trc", "tc" }, + usage = "[trait name] [flags]", + desc = "Configures a trait", + modifiers = { "*" }, + min = 2, + flags = "*", + permission = "npc.trait-configure") + public void configure(CommandContext args, CommandSender sender, NPC npc) throws CommandException { + String traitName = args.getString(0); + if (!sender.hasPermission("citizens.npc.trait-configure." + traitName)) + throw new NoPermissionsException(); + Class clazz = CitizensAPI.getTraitFactory().getTraitClass(args.getString(0)); + if (clazz == null) + throw new CommandException("Trait not found."); + if (!clazz.isAssignableFrom(CommandConfigurable.class)) + throw new CommandException("That trait is not configurable"); + if (!npc.hasTrait(clazz)) + throw new CommandException("The NPC doesn't have that trait."); + CommandConfigurable trait = (CommandConfigurable) npc.getTrait(clazz); + trait.configure(args); + } + + @Command( + aliases = { "trait", "tr" }, + usage = "remove [trait name]...", + desc = "Removes traits on the NPC", + modifiers = { "remove", "rem", "r" }, + min = 1, + permission = "npc.trait") + public void remove(CommandContext args, CommandSender sender, NPC npc) throws CommandException { + List removed = Lists.newArrayList(); + List failed = Lists.newArrayList(); + for (String traitName : Splitter.on(',').split(args.getJoinedStrings(0))) { + if (!sender.hasPermission("citizens.npc.trait." + traitName)) + failed.add(String.format("%s: No permission", traitName)); + + Class clazz = CitizensAPI.getTraitFactory().getTraitClass(traitName); + if (clazz == null) { + failed.add(String.format("%s: Trait not found", traitName)); + continue; + } + boolean hasTrait = npc.hasTrait(clazz); + if (!hasTrait) { + failed.add(String.format("%s: Trait not attached", traitName)); + continue; + } + npc.removeTrait(clazz); + removed.add(StringHelper.wrap(traitName)); + } + if (removed.size() > 0) + Messaging.sendF(sender, ChatColor.GREEN + "Removed %s successfully.", + Joiner.on(", ").join(removed)); + if (failed.size() > 0) + Messaging.sendF(sender, ChatColor.GRAY + "Couldn't change %s.", Joiner.on(", ").join(failed)); + } + + @Command( + aliases = { "trait", "tr" }, + usage = "[trait name], [trait name]...", + desc = "Toggles traits on the NPC", + modifiers = { "*" }, + min = 1, + permission = "npc.trait") + public void toggle(CommandContext args, CommandSender sender, NPC npc) throws CommandException { List added = Lists.newArrayList(); List removed = Lists.newArrayList(); List failed = Lists.newArrayList(); @@ -59,31 +148,6 @@ public class TraitCommands { Messaging.sendF(sender, ChatColor.GREEN + "Removed %s successfully.", Joiner.on(", ").join(removed)); if (failed.size() > 0) - Messaging - .send(sender, ChatColor.GRAY + "Couldn't change the following traits - " - + Joiner.on(", ").join(failed)); - } - - @Command( - aliases = { "traitc", "trc", "tc" }, - usage = "[trait name] [flags]", - desc = "Configures a trait", - modifiers = { "*" }, - min = 2, - flags = "*", - permission = "npc.trait-configure") - public void configure(CommandContext args, CommandSender sender, NPC npc) throws CommandException { - String traitName = args.getString(0); - if (!sender.hasPermission("citizens.npc.trait-configure." + traitName)) - throw new NoPermissionsException(); - Class clazz = CitizensAPI.getTraitFactory().getTraitClass(args.getString(0)); - if (clazz == null) - throw new CommandException("Trait not found."); - if (!clazz.isAssignableFrom(CommandConfigurable.class)) - throw new CommandException("That trait is not configurable"); - if (!npc.hasTrait(clazz)) - throw new CommandException("The NPC doesn't have that trait."); - CommandConfigurable trait = (CommandConfigurable) npc.getTrait(clazz); - trait.configure(args); + Messaging.sendF(sender, ChatColor.GRAY + "Couldn't change %s.", Joiner.on(", ").join(failed)); } }