From ba8ee44769c506dbe4c1ce8ce381aea991e99e29 Mon Sep 17 00:00:00 2001 From: aPunch Date: Tue, 7 Feb 2012 05:31:22 -0600 Subject: [PATCH] use permission() in Command annotation --- .../citizensnpcs/command/CommandManager.java | 40 ++++----- .../command/annotation/Command.java | 2 + .../command/annotation/Permission.java | 29 ------- .../command/command/NPCCommands.java | 81 +++++++++++++------ 4 files changed, 75 insertions(+), 77 deletions(-) delete mode 100644 src/net/citizensnpcs/command/annotation/Permission.java diff --git a/src/net/citizensnpcs/command/CommandManager.java b/src/net/citizensnpcs/command/CommandManager.java index 9045eb606..f09ee414c 100644 --- a/src/net/citizensnpcs/command/CommandManager.java +++ b/src/net/citizensnpcs/command/CommandManager.java @@ -36,7 +36,6 @@ import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.trait.trait.Owner; import net.citizensnpcs.command.annotation.Command; import net.citizensnpcs.command.annotation.NestedCommand; -import net.citizensnpcs.command.annotation.Permission; import net.citizensnpcs.command.annotation.Requirements; import net.citizensnpcs.command.annotation.ServerCommand; import net.citizensnpcs.command.exception.CommandException; @@ -64,13 +63,6 @@ public class CommandManager { */ private final Map> commands = new HashMap>(); - /* - * Mapping of commands (not including aliases) with a description. This is - * only for top level commands. - */ - // private final Map descs = new - // HashMap(); - private final Map> subCommands = new HashMap>(); // Stores the injector used to getInstance. @@ -199,10 +191,10 @@ public class CommandManager { return cmds.toArray(new String[cmds.size()]); } - /* // Get a list of command descriptions. This is only for root commands. - public Map getCommands() { - return descs; - }*/ + /* + * // Get a list of command descriptions. This is only for root commands. + * public Map getCommands() { return descs; } + */ // Get the usage string for a nested command. private String getNestedUsage(String[] args, int level, Method method, Player player) throws CommandException { @@ -284,11 +276,11 @@ public class CommandManager { // Returns whether a player has access to a command. private boolean hasPermission(Method method, Player player) { - Permission permission = method.getAnnotation(Permission.class); - if (permission == null) + Command cmd = method.getAnnotation(Command.class); + if (cmd.permission().isEmpty()) return true; - if (hasPermission(player, permission.value())) + if (hasPermission(player, cmd.permission())) return true; return false; @@ -370,15 +362,15 @@ public class CommandManager { instances.put(method, obj); } - /*// Build a list of commands and their usage details, at least for - // root level commands - if (parent == null) - if (cmd.usage().length() == 0) - descs.put(new CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]), cmd.desc()); - else - descs.put(new CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]), - cmd.usage() + " - " + cmd.desc()); - */ + /* + * // Build a list of commands and their usage details, at least for + * // root level commands if (parent == null) if + * (cmd.usage().length() == 0) descs.put(new + * CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]), + * cmd.desc()); else descs.put(new + * CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]), + * cmd.usage() + " - " + cmd.desc()); + */ // Look for nested commands -- if there are any, those have // to be cached too so that they can be quickly looked diff --git a/src/net/citizensnpcs/command/annotation/Command.java b/src/net/citizensnpcs/command/annotation/Command.java index 085e49ecb..e44126a66 100644 --- a/src/net/citizensnpcs/command/annotation/Command.java +++ b/src/net/citizensnpcs/command/annotation/Command.java @@ -37,4 +37,6 @@ public @interface Command { String[] modifiers() default ""; String usage() default ""; + + String permission() default ""; } \ No newline at end of file diff --git a/src/net/citizensnpcs/command/annotation/Permission.java b/src/net/citizensnpcs/command/annotation/Permission.java deleted file mode 100644 index 894117bcf..000000000 --- a/src/net/citizensnpcs/command/annotation/Permission.java +++ /dev/null @@ -1,29 +0,0 @@ -// $Id$ -/* - * WorldEdit - * Copyright (C) 2010 sk89q - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.citizensnpcs.command.annotation; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -@Retention(RetentionPolicy.RUNTIME) -public @interface Permission { - - String value(); -} \ No newline at end of file diff --git a/src/net/citizensnpcs/command/command/NPCCommands.java b/src/net/citizensnpcs/command/command/NPCCommands.java index 4d4933d88..dcf3ff326 100644 --- a/src/net/citizensnpcs/command/command/NPCCommands.java +++ b/src/net/citizensnpcs/command/command/NPCCommands.java @@ -9,7 +9,6 @@ import net.citizensnpcs.api.npc.trait.trait.Owner; import net.citizensnpcs.api.npc.trait.trait.Spawned; import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.annotation.Command; -import net.citizensnpcs.command.annotation.Permission; import net.citizensnpcs.command.annotation.Requirements; import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.trait.LookClose; @@ -31,9 +30,14 @@ public class NPCCommands { this.characterManager = characterManager; } - @Command(aliases = { "npc" }, usage = "create [name] --type (type) --char (character)", desc = "Create a new NPC", - modifiers = { "create" }, min = 2, max = 4) - @Permission("npc.create") + @Command( + aliases = { "npc" }, + usage = "create [name] --type (type) --char (character)", + desc = "Create a new NPC", + modifiers = { "create" }, + min = 2, + max = 4, + permission = "npc.create") @Requirements public void createNPC(CommandContext args, Player player, NPC npc) { String name = args.getString(1); @@ -77,19 +81,28 @@ public class NPCCommands { Messaging.send(player, successMsg); } - @Command(aliases = { "npc" }, usage = "despawn", desc = "Despawn an NPC", modifiers = { "despawn" }, min = 1, - max = 1) - @Permission("npc.despawn") + @Command( + aliases = { "npc" }, + usage = "despawn", + desc = "Despawn an NPC", + modifiers = { "despawn" }, + min = 1, + max = 1, + permission = "npc.despawn") public void despawnNPC(CommandContext args, Player player, NPC npc) { - npc.getTrait(Spawned.class).setSpawned(false); // TODO: move this to - // despawn() / spawn()? + npc.getTrait(Spawned.class).setSpawned(false); npc.despawn(); Messaging.send(player, ChatColor.GREEN + "You despawned " + StringHelper.wrap(npc.getName()) + "."); } - @Command(aliases = { "npc" }, usage = "select [id]", desc = "Selects an NPC with the given ID", - modifiers = { "select" }, min = 2, max = 2) - @Permission("npc.select") + @Command( + aliases = { "npc" }, + usage = "select [id]", + desc = "Selects an NPC with the given ID", + modifiers = { "select" }, + min = 2, + max = 2, + permission = "npc.select") @Requirements(ownership = true) public void selectNPC(CommandContext args, Player player, NPC npc) { NPC toSelect = npcManager.getNPC(args.getInteger(1)); @@ -105,9 +118,14 @@ public class NPCCommands { Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), toSelect); } - @Command(aliases = { "npc" }, usage = "spawn [id]", desc = "Spawn an existing NPC", modifiers = { "spawn" }, - min = 2, max = 2) - @Permission("npc.spawn") + @Command( + aliases = { "npc" }, + usage = "spawn [id]", + desc = "Spawn an existing NPC", + modifiers = { "spawn" }, + min = 2, + max = 2, + permission = "npc.spawn") @Requirements public void spawnNPC(CommandContext args, Player player, NPC npc) { NPC respawn = npcManager.getNPC(args.getInteger(1)); @@ -131,25 +149,40 @@ public class NPCCommands { } } - @Command(aliases = { "npc" }, usage = "tphere", desc = "Teleport an NPC to your location", - modifiers = { "tphere" }, min = 1, max = 1) - @Permission("npc.tphere") + @Command( + aliases = { "npc" }, + usage = "tphere", + desc = "Teleport an NPC to your location", + modifiers = { "tphere" }, + min = 1, + max = 1, + permission = "npc.tphere") public void teleportNPCToPlayer(CommandContext args, Player player, NPC npc) { npc.getBukkitEntity().teleport(player, TeleportCause.COMMAND); Messaging.send(player, StringHelper.wrap(npc.getName()) + " was teleported to your location."); } - @Command(aliases = { "npc" }, usage = "tp", desc = "Teleport to an NPC", modifiers = { "tp", "teleport" }, min = 1, - max = 1) - @Permission("npc.tp") + @Command( + aliases = { "npc" }, + usage = "tp", + desc = "Teleport to an NPC", + modifiers = { "tp", "teleport" }, + min = 1, + max = 1, + permission = "npc.tp") public void teleportToNPC(CommandContext args, Player player, NPC npc) { player.teleport(npc.getBukkitEntity(), TeleportCause.COMMAND); Messaging.send(player, ChatColor.GREEN + "You teleported to " + StringHelper.wrap(npc.getName()) + "."); } - @Command(aliases = { "npc" }, usage = "lookclose", desc = "Toggle an NPC's look-close state", modifiers = { - "lookclose", "look", "rotate" }, min = 1, max = 1) - @Permission("npc.look-close") + @Command( + aliases = { "npc" }, + usage = "lookclose", + desc = "Toggle an NPC's look-close state", + modifiers = { "lookclose", "look", "rotate" }, + min = 1, + max = 1, + permission = "npc.look-close") public void toggleNPCLookClose(CommandContext args, Player player, NPC npc) { LookClose trait = npc.getTrait(LookClose.class); trait.toggle();