From b3f151da098d58e86cfcf3954449186c94c64535 Mon Sep 17 00:00:00 2001 From: fullwall Date: Thu, 10 Jan 2013 22:40:46 +0800 Subject: [PATCH] Some javadocs, fix type coercion when checking for enabled --- .../net/citizensnpcs/command/Command.java | 38 +++++++++++++++++++ .../command/CommandAnnotationProcessor.java | 15 ++++++++ .../citizensnpcs/command/CommandManager.java | 10 ++--- .../net/citizensnpcs/npc/CitizensNPC.java | 9 ++++- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/citizensnpcs/command/Command.java b/src/main/java/net/citizensnpcs/command/Command.java index dd271593f..b6e21db5e 100644 --- a/src/main/java/net/citizensnpcs/command/Command.java +++ b/src/main/java/net/citizensnpcs/command/Command.java @@ -5,21 +5,59 @@ import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface Command { + /** + * A list of root-level command aliases that will be accepted for this + * command. For example: {"npc", "npc2"} would match both /npc + * and /npc2. + */ String[] aliases(); + /** + * A short description of the command that will be displayed with the + * command usage and help. Translatable. + */ String desc(); + /** + * Defines the flags available for this command. A flag is a single + * character such as -f that will alter the behaviour of the + * command. Each character in this string will be counted as a valid flag: + * extra flags will be discarded. Accepts * as a catch all. + */ String flags() default ""; + /** + * A longer description of the command and any flags it uses which will be + * displayed in addition to {@link desc} in help commands. Translatable. + */ String help() default ""; + /** + * The maximum number of arguments that the command will accept. Default is + * -1, or an unlimited number of arguments. + */ int max() default -1; + /** + * Minimum number of arguments that are accepted by the command. + */ int min() default 0; + /** + * The argument modifiers accepted by the command. Also accepts + * '*' as a catch all. + */ String[] modifiers() default ""; + /** + * The permission of the command. The comamnd sender will get an error if + * this is not met. + */ String permission() default ""; + /** + * Command usage string that is displayed when an error occurs with the + * command processing. + */ String usage() default ""; } \ No newline at end of file diff --git a/src/main/java/net/citizensnpcs/command/CommandAnnotationProcessor.java b/src/main/java/net/citizensnpcs/command/CommandAnnotationProcessor.java index 6d5f10886..81b8c1574 100644 --- a/src/main/java/net/citizensnpcs/command/CommandAnnotationProcessor.java +++ b/src/main/java/net/citizensnpcs/command/CommandAnnotationProcessor.java @@ -7,8 +7,23 @@ import net.citizensnpcs.command.exception.CommandException; import org.bukkit.command.CommandSender; public interface CommandAnnotationProcessor { + /** + * @return The {@link Annotation} class that this processor will accept. + */ Class getAnnotationClass(); + /** + * @param sender + * The command sender + * @param context + * The context of the command, including arguments + * @param instance + * The {@link Annotation} instance + * @param args + * The method arguments + * @throws CommandException + * If an exception occurs + */ void process(CommandSender sender, CommandContext context, Annotation instance, Object[] args) throws CommandException; } diff --git a/src/main/java/net/citizensnpcs/command/CommandManager.java b/src/main/java/net/citizensnpcs/command/CommandManager.java index 1f4745e54..d6ca540db 100644 --- a/src/main/java/net/citizensnpcs/command/CommandManager.java +++ b/src/main/java/net/citizensnpcs/command/CommandManager.java @@ -90,12 +90,12 @@ public class CommandManager { // Attempt to execute a command. private void executeMethod(String[] args, CommandSender sender, Object[] methodArgs) throws CommandException { - String cmdName = args[0]; + String cmdName = args[0].toLowerCase(); String modifier = args.length > 1 ? args[1] : ""; - Method method = commands.get(cmdName.toLowerCase() + " " + modifier.toLowerCase()); + Method method = commands.get(cmdName + " " + modifier.toLowerCase()); if (method == null) - method = commands.get(cmdName.toLowerCase() + " *"); + method = commands.get(cmdName + " *"); if (method == null) throw new UnhandledCommandException(); @@ -274,8 +274,8 @@ public class CommandManager { * @return Whether the command is handled */ public boolean hasCommand(org.bukkit.command.Command cmd, String modifier) { - return commands.containsKey(cmd.getName().toLowerCase() + " " + modifier.toLowerCase()) - || commands.containsKey(cmd.getName().toLowerCase() + " *"); + String cmdName = cmd.getName().toLowerCase(); + return commands.containsKey(cmdName + " " + modifier.toLowerCase()) || commands.containsKey(cmdName + " *"); } // Returns whether a CommandSenders has permission. diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index 53a117f10..d3e18d2a0 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -107,8 +107,12 @@ public class CitizensNPC extends AbstractNPC { } }); for (DataKey traitKey : keys) { - if (traitKey.keyExists("enabled") && !traitKey.getBoolean("enabled")) + if (traitKey.keyExists("enabled") && !traitKey.getBoolean("enabled") + && traitKey.getRaw("enabled") instanceof Boolean) { + // we want to avoid coercion here as YAML can coerce map + // existence to boolean continue; + } Class clazz = CitizensAPI.getTraitFactory().getTraitClass(traitKey.name()); Trait trait; if (hasTrait(clazz)) { @@ -173,7 +177,8 @@ public class CitizensNPC extends AbstractNPC { } if (traitNames.length() > 0) { root.setString("traitnames", traitNames.substring(0, traitNames.length() - 1)); - } + } else + root.setString("traitnames", ""); removeTraitData(root); }