From 1cd4ff2926a64334fe66bea258f8b38288639969 Mon Sep 17 00:00:00 2001 From: fullwall Date: Fri, 7 Sep 2012 20:52:33 +0800 Subject: [PATCH] Filter out commands when the player does not own the NPC --- src/main/java/net/citizensnpcs/Citizens.java | 3 +- .../citizensnpcs/command/CommandManager.java | 58 ++++++++++++++++--- .../command/command/HelpCommands.java | 36 +++++++----- 3 files changed, 73 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/citizensnpcs/Citizens.java b/src/main/java/net/citizensnpcs/Citizens.java index 723208e15..3973c3b85 100644 --- a/src/main/java/net/citizensnpcs/Citizens.java +++ b/src/main/java/net/citizensnpcs/Citizens.java @@ -24,6 +24,7 @@ import net.citizensnpcs.api.util.NBTStorage; import net.citizensnpcs.api.util.Storage; import net.citizensnpcs.api.util.YamlStorage; import net.citizensnpcs.command.CommandManager; +import net.citizensnpcs.command.CommandManager.CommandInfo; import net.citizensnpcs.command.Injector; import net.citizensnpcs.command.command.AdminCommands; import net.citizensnpcs.command.command.EditorCommands; @@ -94,7 +95,7 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { } } - public Iterable getCommands(String base) { + public Iterable getCommands(String base) { return commands.getCommands(base); } diff --git a/src/main/java/net/citizensnpcs/command/CommandManager.java b/src/main/java/net/citizensnpcs/command/CommandManager.java index e6b03df82..7b8899300 100644 --- a/src/main/java/net/citizensnpcs/command/CommandManager.java +++ b/src/main/java/net/citizensnpcs/command/CommandManager.java @@ -53,8 +53,6 @@ public class CommandManager { private final Set serverCommands = new HashSet(); - private final Map> subCommands = new HashMap>(); - /* * Attempt to execute a command. This version takes a separate command name * (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()]); } - public List getCommands(String command) { - if (subCommands.containsKey(command)) - return subCommands.get(command); - List cmds = new ArrayList(); + public List getCommands(String command) { + List cmds = new ArrayList(); for (Entry entry : commands.entrySet()) { - if (!entry.getKey().split(" ")[0].equalsIgnoreCase(command) - || !entry.getValue().isAnnotationPresent(Command.class)) + if (!entry.getKey().split(" ")[0].equalsIgnoreCase(command)) 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; } + 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. private String getUsage(String[] args, Command cmd) { StringBuilder command = new StringBuilder(); diff --git a/src/main/java/net/citizensnpcs/command/command/HelpCommands.java b/src/main/java/net/citizensnpcs/command/command/HelpCommands.java index b61c40ce3..5b4718301 100644 --- a/src/main/java/net/citizensnpcs/command/command/HelpCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/HelpCommands.java @@ -1,20 +1,23 @@ package net.citizensnpcs.command.command; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Set; import net.citizensnpcs.Citizens; import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.trait.trait.Owner; import net.citizensnpcs.command.Command; import net.citizensnpcs.command.CommandContext; +import net.citizensnpcs.command.CommandManager.CommandInfo; import net.citizensnpcs.command.Requirements; import net.citizensnpcs.command.exception.CommandException; import net.citizensnpcs.util.Paginator; import org.bukkit.command.CommandSender; +import com.google.common.collect.Sets; + @Requirements public class HelpCommands { private final Citizens plugin; @@ -35,26 +38,31 @@ public class HelpCommands { public void citizensHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException { int page = args.argsLength() == 2 ? args.getInteger(1) : 1; Paginator paginator = new Paginator().header("Citizens Help"); - for (String line : getLines(sender, "citizens")) + for (String line : getLines(sender, npc, "citizens")) paginator.addLine(line); if (!paginator.sendPage(sender, page)) throw new CommandException("The page '" + page + "' does not exist."); } - private List getLines(CommandSender sender, String baseCommand) { + private List getLines(CommandSender sender, NPC npc, String baseCommand) { // Ensures that commands with multiple modifiers are only added once - Set cmds = new HashSet(); + Set processed = Sets.newHashSet(); List lines = new ArrayList(); - for (Command cmd : plugin.getCommands(baseCommand)) { - if (cmds.contains(cmd) + for (CommandInfo info : plugin.getCommands(baseCommand)) { + Command command = info.getCommandAnnotation(); + if (processed.contains(info) || (!sender.hasPermission("citizens.admin") && !sender.hasPermission("citizens." - + cmd.permission()))) + + command.permission()))) continue; - - lines.add("<7>/" + cmd.aliases()[0] + (cmd.usage().isEmpty() ? "" : " " + cmd.usage()) - + " <7>- " + cmd.desc()); - if (cmd.modifiers().length > 1) - cmds.add(cmd); + Requirements requirements = info.getRequirements(); + if (requirements != null && npc != null) { + if (requirements.ownership() && !npc.getTrait(Owner.class).isOwnedBy(sender)) + continue; + } + lines.add("<7>/" + command.aliases()[0] + + (command.usage().isEmpty() ? "" : " " + command.usage()) + " <7>- " + command.desc()); + if (command.modifiers().length > 1) + processed.add(info); } return lines; } @@ -71,7 +79,7 @@ public class HelpCommands { public void npcHelp(CommandContext args, CommandSender sender, NPC npc) throws CommandException { int page = args.argsLength() == 2 ? args.getInteger(1) : 1; Paginator paginator = new Paginator().header("NPC Help"); - for (String line : getLines(sender, "npc")) + for (String line : getLines(sender, npc, "npc")) paginator.addLine(line); if (!paginator.sendPage(sender, page)) 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 { int page = args.argsLength() == 2 ? args.getInteger(1) : 1; Paginator paginator = new Paginator().header("Script Help"); - for (String line : getLines(sender, "script")) + for (String line : getLines(sender, npc, "script")) paginator.addLine(line); if (!paginator.sendPage(sender, page)) throw new CommandException("The page '" + page + "' does not exist.");