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 2a33588a85
commit 2b08e7dbeb
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.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<net.citizensnpcs.command.Command> getCommands(String base) {
public Iterable<CommandInfo> getCommands(String base) {
return commands.getCommands(base);
}

View File

@ -53,8 +53,6 @@ public class CommandManager {
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
* (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<Command> getCommands(String command) {
if (subCommands.containsKey(command))
return subCommands.get(command);
List<Command> cmds = new ArrayList<Command>();
public List<CommandInfo> getCommands(String command) {
List<CommandInfo> cmds = new ArrayList<CommandInfo>();
for (Entry<String, Method> 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();

View File

@ -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<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
Set<Command> cmds = new HashSet<Command>();
Set<CommandInfo> processed = Sets.newHashSet();
List<String> lines = new ArrayList<String>();
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>/<c>" + cmd.aliases()[0] + (cmd.usage().isEmpty() ? "" : " " + cmd.usage())
+ " <7>- <e>" + 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>/<c>" + command.aliases()[0]
+ (command.usage().isEmpty() ? "" : " " + command.usage()) + " <7>- <e>" + 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.");