added command requirements and NPC owners

This commit is contained in:
aPunch 2012-01-26 15:08:32 -06:00
parent 86b84c259a
commit 2bd13d1eb0
7 changed files with 66 additions and 19 deletions

Binary file not shown.

View File

@ -17,6 +17,7 @@ import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
import net.citizensnpcs.api.npc.trait.InstanceFactory;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.api.npc.trait.trait.Owner;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.command.CommandManager;
import net.citizensnpcs.command.Injector;
@ -52,7 +53,7 @@ public class Citizens extends JavaPlugin {
private CitizensNPCManager npcManager;
private final InstanceFactory<Character> characterManager = new DefaultInstanceFactory<Character>();
private final InstanceFactory<Trait> traitManager = new DefaultInstanceFactory<Trait>();
private final CommandManager cmdManager = new CommandManager();
private CommandManager cmdManager;
private Settings config;
@Override
@ -180,6 +181,7 @@ public class Citizens extends JavaPlugin {
private void setupNPCs() throws NPCLoadException {
traitManager.register("location", SpawnLocation.class);
traitManager.register("owner", Owner.class);
for (DataKey key : getNPCStorage().getKey("npc").getIntegerSubKeys()) {
int id = Integer.parseInt(key.name());
@ -228,8 +230,10 @@ public class Citizens extends JavaPlugin {
}
private void registerCommands() {
cmdManager = new CommandManager(npcManager);
cmdManager.setInjector(new Injector(npcManager, characterManager));
// cmdManager.register(AdminCommands.class);
cmdManager.register(NPCCommands.class);
}

View File

@ -30,6 +30,8 @@ import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
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.Requirements;
import net.citizensnpcs.command.annotation.NestedCommand;
@ -39,9 +41,11 @@ import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.command.exception.CommandUsageException;
import net.citizensnpcs.command.exception.MissingNestedCommandException;
import net.citizensnpcs.command.exception.NoPermissionsException;
import net.citizensnpcs.command.exception.RequirementMissingException;
import net.citizensnpcs.command.exception.ServerCommandException;
import net.citizensnpcs.command.exception.UnhandledCommandException;
import net.citizensnpcs.command.exception.WrappedCommandException;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.util.Messaging;
import org.bukkit.command.ConsoleCommandSender;
@ -55,8 +59,8 @@ public class CommandManager {
/*
* Mapping of commands (including aliases) with a description. Root commands
* are stored under a key of null, whereas child commands are cached under
* their respective {@link Method}. The child map has the key of the command
* name (one for each alias) with the method.
* their respective Method. The child map has the key of the command name
* (one for each alias) with the method.
*/
private final Map<Method, Map<CommandIdentifier, Method>> commands = new HashMap<Method, Map<CommandIdentifier, Method>>();
@ -76,10 +80,16 @@ public class CommandManager {
private final Map<Method, ServerCommand> serverCommands = new HashMap<Method, ServerCommand>();
private final CitizensNPCManager npcManager;
public CommandManager(CitizensNPCManager npcManager) {
this.npcManager = npcManager;
}
/*
* Register an class that contains commands (denoted by {@link Command}. If
* no dependency injector is specified, then the methods of the class will
* be registered to be called statically. Otherwise, new instances will be
* Register an class that contains commands (denoted by Command. If no
* dependency injector is specified, then the methods of the class will be
* registered to be called statically. Otherwise, new instances will be
* created of the command classes and methods will not be called statically.
*/
public void register(Class<?> clazz) {
@ -313,11 +323,19 @@ public class CommandManager {
executeMethod(method, args, player, methodArgs, level + 1);
else if (methodArgs[1] instanceof Player) {
Requirements cmdRequirements = requirements.get(method);
if (cmdRequirements != null) {
if (methodArgs[2] instanceof NPC) {
NPC npc = npcManager.getSelectedNPC(player);
if (npc != null)
methodArgs[2] = npc;
// TODO add requirements
if (cmdRequirements != null)
Messaging.debug("");
else
if (cmdRequirements.selected() && npc == null)
throw new RequirementMissingException("You must have an NPC selected to execute that command.");
if (cmdRequirements.ownership() && !npc.getTrait(Owner.class).getOwner().equals(player.getName()))
throw new RequirementMissingException(
"You must be the owner of this NPC to execute that command.");
}
} else
Messaging.debug("No annotation present.");
}
@ -368,7 +386,7 @@ public class CommandManager {
// Returns whether a player has permission.
private boolean hasPermission(Player player, String perm) {
return ((Player) player).hasPermission("citizens." + perm);
return player.hasPermission("citizens." + perm);
}
public String[] getAllCommandModifiers(String command) {

View File

@ -6,7 +6,7 @@ import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Requirements {
boolean requireSelected() default false;
boolean selected() default false;
boolean requireOwnership() default false;
boolean ownership() default false;
}

View File

@ -0,0 +1,15 @@
package net.citizensnpcs.command.command;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
import net.citizensnpcs.npc.CitizensNPCManager;
public class AdminCommands {
private final CitizensNPCManager npcManager;
private final DefaultInstanceFactory<Character> characterManager;
public AdminCommands(CitizensNPCManager npcManager, DefaultInstanceFactory<Character> characterManager) {
this.npcManager = npcManager;
this.characterManager = characterManager;
}
}

View File

@ -6,15 +6,17 @@ import org.bukkit.entity.Player;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
import net.citizensnpcs.api.npc.trait.trait.Owner;
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.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.util.Messaging;
import net.citizensnpcs.util.StringHelper;
// TODO add requirements
@Requirements(selected = true, ownership = true)
public class NPCCommands {
private final CitizensNPCManager npcManager;
private final DefaultInstanceFactory<Character> characterManager;
@ -32,6 +34,7 @@ public class NPCCommands {
min = 2,
max = 3)
@Permission("npc.create")
@Requirements
public void createNPC(CommandContext args, Player player, NPC npc) {
CitizensNPC create = (CitizensNPC) npcManager.createNPC(args.getString(1));
String msg = ChatColor.GREEN + "You created " + StringHelper.wrap(create.getName());
@ -41,6 +44,10 @@ public class NPCCommands {
}
msg += " at your location.";
// Set the owner
create.addTrait(new Owner(player.getName()));
create.getTrait(Owner.class).setOwner(player.getName());
create.spawn(player.getLocation());
npcManager.selectNPC(player, create);
Messaging.send(player, msg);
@ -54,6 +61,7 @@ public class NPCCommands {
min = 2,
max = 2)
@Permission("npc.spawn")
@Requirements(ownership = true)
public void spawnNPC(CommandContext args, Player player, NPC npc) {
CitizensNPC respawn = (CitizensNPC) npcManager.getNPC(args.getInteger(1));
if (respawn == null) {
@ -66,7 +74,8 @@ public class NPCCommands {
Messaging.send(player, ChatColor.GREEN + "You respawned " + StringHelper.wrap(respawn.getName())
+ " at your location.");
} else
Messaging.sendError(player, respawn.getName() + " is already spawned at another location.");
Messaging.sendError(player, respawn.getName()
+ " is already spawned at another location. Use '/npc tp' to teleport the NPC to your location.");
}
@Command(
@ -78,9 +87,8 @@ public class NPCCommands {
max = 1)
@Permission("npc.despawn")
public void despawnNPC(CommandContext args, Player player, NPC npc) {
CitizensNPC despawn = (CitizensNPC) npcManager.getSelectedNPC(player);
despawn.despawn();
npc.despawn();
npcManager.deselectNPC(player);
Messaging.send(player, ChatColor.GREEN + "You despawned " + StringHelper.wrap(despawn.getName()) + ".");
Messaging.send(player, ChatColor.GREEN + "You despawned " + StringHelper.wrap(npc.getName()) + ".");
}
}

View File

@ -11,6 +11,7 @@ import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCManager;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.trait.Owner;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.resources.lib.CraftNPC;
import net.citizensnpcs.storage.Storage;
@ -160,7 +161,8 @@ public class CitizensNPCManager implements NPCManager {
public boolean canSelectNPC(Player player, NPC npc) {
if (player.hasPermission("citizens.npc.select"))
return player.getItemInHand().getTypeId() == Setting.SELECTION_ITEM.getInt();
return player.getItemInHand().getTypeId() == Setting.SELECTION_ITEM.getInt()
&& npc.getTrait(Owner.class).getOwner().equals(player.getName());
return false;
}