mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-10-01 08:37:57 +02:00
added command requirements and NPC owners
This commit is contained in:
parent
8dd91596ae
commit
ad58821188
Binary file not shown.
@ -17,6 +17,7 @@ import net.citizensnpcs.api.npc.trait.Character;
|
|||||||
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
|
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
|
||||||
import net.citizensnpcs.api.npc.trait.InstanceFactory;
|
import net.citizensnpcs.api.npc.trait.InstanceFactory;
|
||||||
import net.citizensnpcs.api.npc.trait.Trait;
|
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.api.npc.trait.trait.SpawnLocation;
|
||||||
import net.citizensnpcs.command.CommandManager;
|
import net.citizensnpcs.command.CommandManager;
|
||||||
import net.citizensnpcs.command.Injector;
|
import net.citizensnpcs.command.Injector;
|
||||||
@ -52,7 +53,7 @@ public class Citizens extends JavaPlugin {
|
|||||||
private CitizensNPCManager npcManager;
|
private CitizensNPCManager npcManager;
|
||||||
private final InstanceFactory<Character> characterManager = new DefaultInstanceFactory<Character>();
|
private final InstanceFactory<Character> characterManager = new DefaultInstanceFactory<Character>();
|
||||||
private final InstanceFactory<Trait> traitManager = new DefaultInstanceFactory<Trait>();
|
private final InstanceFactory<Trait> traitManager = new DefaultInstanceFactory<Trait>();
|
||||||
private final CommandManager cmdManager = new CommandManager();
|
private CommandManager cmdManager;
|
||||||
private Settings config;
|
private Settings config;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -180,6 +181,7 @@ public class Citizens extends JavaPlugin {
|
|||||||
|
|
||||||
private void setupNPCs() throws NPCLoadException {
|
private void setupNPCs() throws NPCLoadException {
|
||||||
traitManager.register("location", SpawnLocation.class);
|
traitManager.register("location", SpawnLocation.class);
|
||||||
|
traitManager.register("owner", Owner.class);
|
||||||
|
|
||||||
for (DataKey key : getNPCStorage().getKey("npc").getIntegerSubKeys()) {
|
for (DataKey key : getNPCStorage().getKey("npc").getIntegerSubKeys()) {
|
||||||
int id = Integer.parseInt(key.name());
|
int id = Integer.parseInt(key.name());
|
||||||
@ -228,8 +230,10 @@ public class Citizens extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void registerCommands() {
|
private void registerCommands() {
|
||||||
|
cmdManager = new CommandManager(npcManager);
|
||||||
cmdManager.setInjector(new Injector(npcManager, characterManager));
|
cmdManager.setInjector(new Injector(npcManager, characterManager));
|
||||||
|
|
||||||
|
// cmdManager.register(AdminCommands.class);
|
||||||
cmdManager.register(NPCCommands.class);
|
cmdManager.register(NPCCommands.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,6 +30,8 @@ import java.util.Set;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
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.Command;
|
||||||
import net.citizensnpcs.command.annotation.Requirements;
|
import net.citizensnpcs.command.annotation.Requirements;
|
||||||
import net.citizensnpcs.command.annotation.NestedCommand;
|
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.CommandUsageException;
|
||||||
import net.citizensnpcs.command.exception.MissingNestedCommandException;
|
import net.citizensnpcs.command.exception.MissingNestedCommandException;
|
||||||
import net.citizensnpcs.command.exception.NoPermissionsException;
|
import net.citizensnpcs.command.exception.NoPermissionsException;
|
||||||
|
import net.citizensnpcs.command.exception.RequirementMissingException;
|
||||||
import net.citizensnpcs.command.exception.ServerCommandException;
|
import net.citizensnpcs.command.exception.ServerCommandException;
|
||||||
import net.citizensnpcs.command.exception.UnhandledCommandException;
|
import net.citizensnpcs.command.exception.UnhandledCommandException;
|
||||||
import net.citizensnpcs.command.exception.WrappedCommandException;
|
import net.citizensnpcs.command.exception.WrappedCommandException;
|
||||||
|
import net.citizensnpcs.npc.CitizensNPCManager;
|
||||||
import net.citizensnpcs.util.Messaging;
|
import net.citizensnpcs.util.Messaging;
|
||||||
|
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
@ -55,8 +59,8 @@ public class CommandManager {
|
|||||||
/*
|
/*
|
||||||
* Mapping of commands (including aliases) with a description. Root commands
|
* Mapping of commands (including aliases) with a description. Root commands
|
||||||
* are stored under a key of null, whereas child commands are cached under
|
* 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
|
* their respective Method. The child map has the key of the command name
|
||||||
* name (one for each alias) with the method.
|
* (one for each alias) with the method.
|
||||||
*/
|
*/
|
||||||
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>>();
|
||||||
|
|
||||||
@ -76,10 +80,16 @@ public class CommandManager {
|
|||||||
|
|
||||||
private final Map<Method, ServerCommand> serverCommands = new HashMap<Method, ServerCommand>();
|
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
|
* Register an class that contains commands (denoted by Command. If no
|
||||||
* no dependency injector is specified, then the methods of the class will
|
* dependency injector is specified, then the methods of the class will be
|
||||||
* be registered to be called statically. Otherwise, new instances will be
|
* registered to be called statically. Otherwise, new instances will be
|
||||||
* created of the command classes and methods will not be called statically.
|
* created of the command classes and methods will not be called statically.
|
||||||
*/
|
*/
|
||||||
public void register(Class<?> clazz) {
|
public void register(Class<?> clazz) {
|
||||||
@ -313,11 +323,19 @@ public class CommandManager {
|
|||||||
executeMethod(method, args, player, methodArgs, level + 1);
|
executeMethod(method, args, player, methodArgs, level + 1);
|
||||||
else if (methodArgs[1] instanceof Player) {
|
else if (methodArgs[1] instanceof Player) {
|
||||||
Requirements cmdRequirements = requirements.get(method);
|
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.selected() && npc == null)
|
||||||
if (cmdRequirements != null)
|
throw new RequirementMissingException("You must have an NPC selected to execute that command.");
|
||||||
Messaging.debug("");
|
if (cmdRequirements.ownership() && !npc.getTrait(Owner.class).getOwner().equals(player.getName()))
|
||||||
else
|
throw new RequirementMissingException(
|
||||||
|
"You must be the owner of this NPC to execute that command.");
|
||||||
|
}
|
||||||
|
} else
|
||||||
Messaging.debug("No annotation present.");
|
Messaging.debug("No annotation present.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -368,7 +386,7 @@ public class CommandManager {
|
|||||||
|
|
||||||
// Returns whether a player has permission.
|
// Returns whether a player has permission.
|
||||||
private boolean hasPermission(Player player, String perm) {
|
private boolean hasPermission(Player player, String perm) {
|
||||||
return ((Player) player).hasPermission("citizens." + perm);
|
return player.hasPermission("citizens." + perm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getAllCommandModifiers(String command) {
|
public String[] getAllCommandModifiers(String command) {
|
||||||
|
@ -6,7 +6,7 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Requirements {
|
public @interface Requirements {
|
||||||
|
|
||||||
boolean requireSelected() default false;
|
boolean selected() default false;
|
||||||
|
|
||||||
boolean requireOwnership() default false;
|
boolean ownership() default false;
|
||||||
}
|
}
|
15
src/net/citizensnpcs/command/command/AdminCommands.java
Normal file
15
src/net/citizensnpcs/command/command/AdminCommands.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -6,15 +6,17 @@ import org.bukkit.entity.Player;
|
|||||||
import net.citizensnpcs.api.npc.NPC;
|
import net.citizensnpcs.api.npc.NPC;
|
||||||
import net.citizensnpcs.api.npc.trait.Character;
|
import net.citizensnpcs.api.npc.trait.Character;
|
||||||
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
|
import net.citizensnpcs.api.npc.trait.DefaultInstanceFactory;
|
||||||
|
import net.citizensnpcs.api.npc.trait.trait.Owner;
|
||||||
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.Permission;
|
||||||
|
import net.citizensnpcs.command.annotation.Requirements;
|
||||||
import net.citizensnpcs.npc.CitizensNPC;
|
import net.citizensnpcs.npc.CitizensNPC;
|
||||||
import net.citizensnpcs.npc.CitizensNPCManager;
|
import net.citizensnpcs.npc.CitizensNPCManager;
|
||||||
import net.citizensnpcs.util.Messaging;
|
import net.citizensnpcs.util.Messaging;
|
||||||
import net.citizensnpcs.util.StringHelper;
|
import net.citizensnpcs.util.StringHelper;
|
||||||
|
|
||||||
// TODO add requirements
|
@Requirements(selected = true, ownership = true)
|
||||||
public class NPCCommands {
|
public class NPCCommands {
|
||||||
private final CitizensNPCManager npcManager;
|
private final CitizensNPCManager npcManager;
|
||||||
private final DefaultInstanceFactory<Character> characterManager;
|
private final DefaultInstanceFactory<Character> characterManager;
|
||||||
@ -32,6 +34,7 @@ public class NPCCommands {
|
|||||||
min = 2,
|
min = 2,
|
||||||
max = 3)
|
max = 3)
|
||||||
@Permission("npc.create")
|
@Permission("npc.create")
|
||||||
|
@Requirements
|
||||||
public void createNPC(CommandContext args, Player player, NPC npc) {
|
public void createNPC(CommandContext args, Player player, NPC npc) {
|
||||||
CitizensNPC create = (CitizensNPC) npcManager.createNPC(args.getString(1));
|
CitizensNPC create = (CitizensNPC) npcManager.createNPC(args.getString(1));
|
||||||
String msg = ChatColor.GREEN + "You created " + StringHelper.wrap(create.getName());
|
String msg = ChatColor.GREEN + "You created " + StringHelper.wrap(create.getName());
|
||||||
@ -41,6 +44,10 @@ public class NPCCommands {
|
|||||||
}
|
}
|
||||||
msg += " at your location.";
|
msg += " at your location.";
|
||||||
|
|
||||||
|
// Set the owner
|
||||||
|
create.addTrait(new Owner(player.getName()));
|
||||||
|
create.getTrait(Owner.class).setOwner(player.getName());
|
||||||
|
|
||||||
create.spawn(player.getLocation());
|
create.spawn(player.getLocation());
|
||||||
npcManager.selectNPC(player, create);
|
npcManager.selectNPC(player, create);
|
||||||
Messaging.send(player, msg);
|
Messaging.send(player, msg);
|
||||||
@ -54,6 +61,7 @@ public class NPCCommands {
|
|||||||
min = 2,
|
min = 2,
|
||||||
max = 2)
|
max = 2)
|
||||||
@Permission("npc.spawn")
|
@Permission("npc.spawn")
|
||||||
|
@Requirements(ownership = true)
|
||||||
public void spawnNPC(CommandContext args, Player player, NPC npc) {
|
public void spawnNPC(CommandContext args, Player player, NPC npc) {
|
||||||
CitizensNPC respawn = (CitizensNPC) npcManager.getNPC(args.getInteger(1));
|
CitizensNPC respawn = (CitizensNPC) npcManager.getNPC(args.getInteger(1));
|
||||||
if (respawn == null) {
|
if (respawn == null) {
|
||||||
@ -66,7 +74,8 @@ public class NPCCommands {
|
|||||||
Messaging.send(player, ChatColor.GREEN + "You respawned " + StringHelper.wrap(respawn.getName())
|
Messaging.send(player, ChatColor.GREEN + "You respawned " + StringHelper.wrap(respawn.getName())
|
||||||
+ " at your location.");
|
+ " at your location.");
|
||||||
} else
|
} 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(
|
@Command(
|
||||||
@ -78,9 +87,8 @@ public class NPCCommands {
|
|||||||
max = 1)
|
max = 1)
|
||||||
@Permission("npc.despawn")
|
@Permission("npc.despawn")
|
||||||
public void despawnNPC(CommandContext args, Player player, NPC npc) {
|
public void despawnNPC(CommandContext args, Player player, NPC npc) {
|
||||||
CitizensNPC despawn = (CitizensNPC) npcManager.getSelectedNPC(player);
|
npc.despawn();
|
||||||
despawn.despawn();
|
|
||||||
npcManager.deselectNPC(player);
|
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()) + ".");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,6 +11,7 @@ import net.citizensnpcs.api.CitizensAPI;
|
|||||||
import net.citizensnpcs.api.npc.NPC;
|
import net.citizensnpcs.api.npc.NPC;
|
||||||
import net.citizensnpcs.api.npc.NPCManager;
|
import net.citizensnpcs.api.npc.NPCManager;
|
||||||
import net.citizensnpcs.api.npc.trait.Character;
|
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.api.npc.trait.trait.SpawnLocation;
|
||||||
import net.citizensnpcs.resources.lib.CraftNPC;
|
import net.citizensnpcs.resources.lib.CraftNPC;
|
||||||
import net.citizensnpcs.storage.Storage;
|
import net.citizensnpcs.storage.Storage;
|
||||||
@ -160,7 +161,8 @@ public class CitizensNPCManager implements NPCManager {
|
|||||||
|
|
||||||
public boolean canSelectNPC(Player player, NPC npc) {
|
public boolean canSelectNPC(Player player, NPC npc) {
|
||||||
if (player.hasPermission("citizens.npc.select"))
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user