use permission() in Command annotation

This commit is contained in:
aPunch 2012-02-07 05:31:22 -06:00
parent 455e49b4eb
commit ba8ee44769
4 changed files with 75 additions and 77 deletions

View File

@ -36,7 +36,6 @@ import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.trait.Owner; import net.citizensnpcs.api.npc.trait.trait.Owner;
import net.citizensnpcs.command.annotation.Command; import net.citizensnpcs.command.annotation.Command;
import net.citizensnpcs.command.annotation.NestedCommand; import net.citizensnpcs.command.annotation.NestedCommand;
import net.citizensnpcs.command.annotation.Permission;
import net.citizensnpcs.command.annotation.Requirements; import net.citizensnpcs.command.annotation.Requirements;
import net.citizensnpcs.command.annotation.ServerCommand; import net.citizensnpcs.command.annotation.ServerCommand;
import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.CommandException;
@ -64,13 +63,6 @@ public class CommandManager {
*/ */
private final Map<Method, Map<CommandIdentifier, Method>> commands = new HashMap<Method, Map<CommandIdentifier, Method>>(); private final Map<Method, Map<CommandIdentifier, Method>> commands = new HashMap<Method, Map<CommandIdentifier, Method>>();
/*
* Mapping of commands (not including aliases) with a description. This is
* only for top level commands.
*/
// private final Map<CommandIdentifier, String> descs = new
// HashMap<CommandIdentifier, String>();
private final Map<String, List<Command>> subCommands = new HashMap<String, List<Command>>(); private final Map<String, List<Command>> subCommands = new HashMap<String, List<Command>>();
// Stores the injector used to getInstance. // Stores the injector used to getInstance.
@ -199,10 +191,10 @@ public class CommandManager {
return cmds.toArray(new String[cmds.size()]); return cmds.toArray(new String[cmds.size()]);
} }
/* // Get a list of command descriptions. This is only for root commands. /*
public Map<CommandIdentifier, String> getCommands() { * // Get a list of command descriptions. This is only for root commands.
return descs; * public Map<CommandIdentifier, String> getCommands() { return descs; }
}*/ */
// Get the usage string for a nested command. // Get the usage string for a nested command.
private String getNestedUsage(String[] args, int level, Method method, Player player) throws CommandException { 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. // Returns whether a player has access to a command.
private boolean hasPermission(Method method, Player player) { private boolean hasPermission(Method method, Player player) {
Permission permission = method.getAnnotation(Permission.class); Command cmd = method.getAnnotation(Command.class);
if (permission == null) if (cmd.permission().isEmpty())
return true; return true;
if (hasPermission(player, permission.value())) if (hasPermission(player, cmd.permission()))
return true; return true;
return false; return false;
@ -370,15 +362,15 @@ public class CommandManager {
instances.put(method, obj); instances.put(method, obj);
} }
/*// Build a list of commands and their usage details, at least for /*
// root level commands * // Build a list of commands and their usage details, at least for
if (parent == null) * // root level commands if (parent == null) if
if (cmd.usage().length() == 0) * (cmd.usage().length() == 0) descs.put(new
descs.put(new CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]), cmd.desc()); * CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]),
else * cmd.desc()); else descs.put(new
descs.put(new CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]), * CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]),
cmd.usage() + " - " + cmd.desc()); * cmd.usage() + " - " + cmd.desc());
*/ */
// Look for nested commands -- if there are any, those have // Look for nested commands -- if there are any, those have
// to be cached too so that they can be quickly looked // to be cached too so that they can be quickly looked

View File

@ -37,4 +37,6 @@ public @interface Command {
String[] modifiers() default ""; String[] modifiers() default "";
String usage() default ""; String usage() default "";
String permission() default "";
} }

View File

@ -1,29 +0,0 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package net.citizensnpcs.command.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Permission {
String value();
}

View File

@ -9,7 +9,6 @@ import net.citizensnpcs.api.npc.trait.trait.Owner;
import net.citizensnpcs.api.npc.trait.trait.Spawned; import net.citizensnpcs.api.npc.trait.trait.Spawned;
import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.CommandContext;
import net.citizensnpcs.command.annotation.Command; import net.citizensnpcs.command.annotation.Command;
import net.citizensnpcs.command.annotation.Permission;
import net.citizensnpcs.command.annotation.Requirements; import net.citizensnpcs.command.annotation.Requirements;
import net.citizensnpcs.npc.CitizensNPCManager; import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.trait.LookClose; import net.citizensnpcs.trait.LookClose;
@ -31,9 +30,14 @@ public class NPCCommands {
this.characterManager = characterManager; this.characterManager = characterManager;
} }
@Command(aliases = { "npc" }, usage = "create [name] --type (type) --char (character)", desc = "Create a new NPC", @Command(
modifiers = { "create" }, min = 2, max = 4) aliases = { "npc" },
@Permission("npc.create") usage = "create [name] --type (type) --char (character)",
desc = "Create a new NPC",
modifiers = { "create" },
min = 2,
max = 4,
permission = "npc.create")
@Requirements @Requirements
public void createNPC(CommandContext args, Player player, NPC npc) { public void createNPC(CommandContext args, Player player, NPC npc) {
String name = args.getString(1); String name = args.getString(1);
@ -77,19 +81,28 @@ public class NPCCommands {
Messaging.send(player, successMsg); Messaging.send(player, successMsg);
} }
@Command(aliases = { "npc" }, usage = "despawn", desc = "Despawn an NPC", modifiers = { "despawn" }, min = 1, @Command(
max = 1) aliases = { "npc" },
@Permission("npc.despawn") usage = "despawn",
desc = "Despawn an NPC",
modifiers = { "despawn" },
min = 1,
max = 1,
permission = "npc.despawn")
public void despawnNPC(CommandContext args, Player player, NPC npc) { public void despawnNPC(CommandContext args, Player player, NPC npc) {
npc.getTrait(Spawned.class).setSpawned(false); // TODO: move this to npc.getTrait(Spawned.class).setSpawned(false);
// despawn() / spawn()?
npc.despawn(); npc.despawn();
Messaging.send(player, ChatColor.GREEN + "You despawned " + StringHelper.wrap(npc.getName()) + "."); 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", @Command(
modifiers = { "select" }, min = 2, max = 2) aliases = { "npc" },
@Permission("npc.select") usage = "select [id]",
desc = "Selects an NPC with the given ID",
modifiers = { "select" },
min = 2,
max = 2,
permission = "npc.select")
@Requirements(ownership = true) @Requirements(ownership = true)
public void selectNPC(CommandContext args, Player player, NPC npc) { public void selectNPC(CommandContext args, Player player, NPC npc) {
NPC toSelect = npcManager.getNPC(args.getInteger(1)); NPC toSelect = npcManager.getNPC(args.getInteger(1));
@ -105,9 +118,14 @@ public class NPCCommands {
Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), toSelect); Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), toSelect);
} }
@Command(aliases = { "npc" }, usage = "spawn [id]", desc = "Spawn an existing NPC", modifiers = { "spawn" }, @Command(
min = 2, max = 2) aliases = { "npc" },
@Permission("npc.spawn") usage = "spawn [id]",
desc = "Spawn an existing NPC",
modifiers = { "spawn" },
min = 2,
max = 2,
permission = "npc.spawn")
@Requirements @Requirements
public void spawnNPC(CommandContext args, Player player, NPC npc) { public void spawnNPC(CommandContext args, Player player, NPC npc) {
NPC respawn = npcManager.getNPC(args.getInteger(1)); 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", @Command(
modifiers = { "tphere" }, min = 1, max = 1) aliases = { "npc" },
@Permission("npc.tphere") 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) { public void teleportNPCToPlayer(CommandContext args, Player player, NPC npc) {
npc.getBukkitEntity().teleport(player, TeleportCause.COMMAND); npc.getBukkitEntity().teleport(player, TeleportCause.COMMAND);
Messaging.send(player, StringHelper.wrap(npc.getName()) + " was teleported to your location."); 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, @Command(
max = 1) aliases = { "npc" },
@Permission("npc.tp") 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) { public void teleportToNPC(CommandContext args, Player player, NPC npc) {
player.teleport(npc.getBukkitEntity(), TeleportCause.COMMAND); player.teleport(npc.getBukkitEntity(), TeleportCause.COMMAND);
Messaging.send(player, ChatColor.GREEN + "You teleported to " + StringHelper.wrap(npc.getName()) + "."); 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 = { @Command(
"lookclose", "look", "rotate" }, min = 1, max = 1) aliases = { "npc" },
@Permission("npc.look-close") 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) { public void toggleNPCLookClose(CommandContext args, Player player, NPC npc) {
LookClose trait = npc.getTrait(LookClose.class); LookClose trait = npc.getTrait(LookClose.class);
trait.toggle(); trait.toggle();