Filter out commands when the player does not own the NPC

This commit is contained in:
fullwall 2012-09-07 20:52:33 +08:00
parent 319efd3fd5
commit 1cd4ff2926
3 changed files with 73 additions and 24 deletions

View File

@ -24,6 +24,7 @@ import net.citizensnpcs.api.util.NBTStorage;
import net.citizensnpcs.api.util.Storage; import net.citizensnpcs.api.util.Storage;
import net.citizensnpcs.api.util.YamlStorage; import net.citizensnpcs.api.util.YamlStorage;
import net.citizensnpcs.command.CommandManager; import net.citizensnpcs.command.CommandManager;
import net.citizensnpcs.command.CommandManager.CommandInfo;
import net.citizensnpcs.command.Injector; import net.citizensnpcs.command.Injector;
import net.citizensnpcs.command.command.AdminCommands; import net.citizensnpcs.command.command.AdminCommands;
import net.citizensnpcs.command.command.EditorCommands; import net.citizensnpcs.command.command.EditorCommands;
@ -94,7 +95,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
} }
} }
public Iterable<net.citizensnpcs.command.Command> getCommands(String base) { public Iterable<CommandInfo> getCommands(String base) {
return commands.getCommands(base); return commands.getCommands(base);
} }

View File

@ -53,8 +53,6 @@ public class CommandManager {
private final Set<Method> serverCommands = new HashSet<Method>(); private final Set<Method> serverCommands = new HashSet<Method>();
private final Map<String, List<Command>> subCommands = new HashMap<String, List<Command>>();
/* /*
* Attempt to execute a command. This version takes a separate command name * Attempt to execute a command. This version takes a separate command name
* (for the root command) and then a list of following arguments. * (for the root command) and then a list of following arguments.
@ -168,19 +166,61 @@ public class CommandManager {
return cmds.toArray(new String[cmds.size()]); return cmds.toArray(new String[cmds.size()]);
} }
public List<Command> getCommands(String command) { public List<CommandInfo> getCommands(String command) {
if (subCommands.containsKey(command)) List<CommandInfo> cmds = new ArrayList<CommandInfo>();
return subCommands.get(command);
List<Command> cmds = new ArrayList<Command>();
for (Entry<String, Method> entry : commands.entrySet()) { for (Entry<String, Method> entry : commands.entrySet()) {
if (!entry.getKey().split(" ")[0].equalsIgnoreCase(command) if (!entry.getKey().split(" ")[0].equalsIgnoreCase(command))
|| !entry.getValue().isAnnotationPresent(Command.class))
continue; continue;
cmds.add(entry.getValue().getAnnotation(Command.class)); Command commandAnnotation = entry.getValue().getAnnotation(Command.class);
if (commandAnnotation == null)
continue;
cmds.add(new CommandInfo(commandAnnotation, requirements.get(entry.getValue())));
} }
return cmds; return cmds;
} }
public static class CommandInfo {
private final Command commandAnnotation;
private final Requirements requirements;
public CommandInfo(Command commandAnnotation, Requirements requirements) {
this.commandAnnotation = commandAnnotation;
this.requirements = requirements;
}
public Command getCommandAnnotation() {
return commandAnnotation;
}
public Requirements getRequirements() {
return requirements;
}
@Override
public int hashCode() {
return 31 + ((commandAnnotation == null) ? 0 : commandAnnotation.hashCode());
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
CommandInfo other = (CommandInfo) obj;
if (commandAnnotation == null) {
if (other.commandAnnotation != null) {
return false;
}
} else if (!commandAnnotation.equals(other.commandAnnotation)) {
return false;
}
return true;
}
}
// Get the usage string for a command. // Get the usage string for a command.
private String getUsage(String[] args, Command cmd) { private String getUsage(String[] args, Command cmd) {
StringBuilder command = new StringBuilder(); StringBuilder command = new StringBuilder();

View File

@ -1,20 +1,23 @@
package net.citizensnpcs.command.command; package net.citizensnpcs.command.command;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import net.citizensnpcs.Citizens; import net.citizensnpcs.Citizens;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.command.Command; import net.citizensnpcs.command.Command;
import net.citizensnpcs.command.CommandContext; import net.citizensnpcs.command.CommandContext;
import net.citizensnpcs.command.CommandManager.CommandInfo;
import net.citizensnpcs.command.Requirements; import net.citizensnpcs.command.Requirements;
import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.util.Paginator; import net.citizensnpcs.util.Paginator;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.google.common.collect.Sets;
@Requirements @Requirements
public class HelpCommands { public class HelpCommands {
private final Citizens plugin; private final Citizens plugin;
@ -35,26 +38,31 @@ public class HelpCommands {
public void citizensHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException { public void citizensHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
int page = args.argsLength() == 2 ? args.getInteger(1) : 1; int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
Paginator paginator = new Paginator().header("Citizens Help"); Paginator paginator = new Paginator().header("Citizens Help");
for (String line : getLines(sender, "citizens")) for (String line : getLines(sender, npc, "citizens"))
paginator.addLine(line); paginator.addLine(line);
if (!paginator.sendPage(sender, page)) if (!paginator.sendPage(sender, page))
throw new CommandException("The page '" + page + "' does not exist."); throw new CommandException("The page '" + page + "' does not exist.");
} }
private List<String> getLines(CommandSender sender, String baseCommand) { private List<String> getLines(CommandSender sender, NPC npc, String baseCommand) {
// Ensures that commands with multiple modifiers are only added once // Ensures that commands with multiple modifiers are only added once
Set<Command> cmds = new HashSet<Command>(); Set<CommandInfo> processed = Sets.newHashSet();
List<String> lines = new ArrayList<String>(); List<String> lines = new ArrayList<String>();
for (Command cmd : plugin.getCommands(baseCommand)) { for (CommandInfo info : plugin.getCommands(baseCommand)) {
if (cmds.contains(cmd) Command command = info.getCommandAnnotation();
if (processed.contains(info)
|| (!sender.hasPermission("citizens.admin") && !sender.hasPermission("citizens." || (!sender.hasPermission("citizens.admin") && !sender.hasPermission("citizens."
+ cmd.permission()))) + command.permission())))
continue; continue;
Requirements requirements = info.getRequirements();
lines.add("<7>/<c>" + cmd.aliases()[0] + (cmd.usage().isEmpty() ? "" : " " + cmd.usage()) if (requirements != null && npc != null) {
+ " <7>- <e>" + cmd.desc()); if (requirements.ownership() && !npc.getTrait(Owner.class).isOwnedBy(sender))
if (cmd.modifiers().length > 1) continue;
cmds.add(cmd); }
lines.add("<7>/<c>" + command.aliases()[0]
+ (command.usage().isEmpty() ? "" : " " + command.usage()) + " <7>- <e>" + command.desc());
if (command.modifiers().length > 1)
processed.add(info);
} }
return lines; return lines;
} }
@ -71,7 +79,7 @@ public class HelpCommands {
public void npcHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException { public void npcHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
int page = args.argsLength() == 2 ? args.getInteger(1) : 1; int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
Paginator paginator = new Paginator().header("NPC Help"); Paginator paginator = new Paginator().header("NPC Help");
for (String line : getLines(sender, "npc")) for (String line : getLines(sender, npc, "npc"))
paginator.addLine(line); paginator.addLine(line);
if (!paginator.sendPage(sender, page)) if (!paginator.sendPage(sender, page))
throw new CommandException("The page '" + page + "' does not exist."); throw new CommandException("The page '" + page + "' does not exist.");
@ -89,7 +97,7 @@ public class HelpCommands {
public void scriptHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException { public void scriptHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
int page = args.argsLength() == 2 ? args.getInteger(1) : 1; int page = args.argsLength() == 2 ? args.getInteger(1) : 1;
Paginator paginator = new Paginator().header("Script Help"); Paginator paginator = new Paginator().header("Script Help");
for (String line : getLines(sender, "script")) for (String line : getLines(sender, npc, "script"))
paginator.addLine(line); paginator.addLine(line);
if (!paginator.sendPage(sender, page)) if (!paginator.sendPage(sender, page))
throw new CommandException("The page '" + page + "' does not exist."); throw new CommandException("The page '" + page + "' does not exist.");