Only show nested commands in the Bukkit help if the player has permission to use at least one of the children

This commit is contained in:
zml 2013-08-06 20:09:47 -07:00
parent 63f2f96a8c
commit bf2c8ebed4
3 changed files with 33 additions and 11 deletions

View File

@ -27,7 +27,9 @@ import org.bukkit.plugin.Plugin;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author zml2008 * @author zml2008
@ -52,13 +54,22 @@ public class CommandsManagerRegistration extends CommandRegistration {
public boolean registerAll(List<Command> registered) { public boolean registerAll(List<Command> registered) {
List<CommandInfo> toRegister = new ArrayList<CommandInfo>(); List<CommandInfo> toRegister = new ArrayList<CommandInfo>();
for (Command command : registered) { for (Command command : registered) {
String[] permissions = null; List<String> permissions = null;
Method cmdMethod = commands.getMethods().get(null).get(command.aliases()[0]); Method cmdMethod = commands.getMethods().get(null).get(command.aliases()[0]);
Map<String, Method> childMethods = commands.getMethods().get(cmdMethod);
if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) { if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) {
permissions = cmdMethod.getAnnotation(CommandPermissions.class).value(); permissions = Arrays.asList(cmdMethod.getAnnotation(CommandPermissions.class).value());
} else if (cmdMethod != null && childMethods != null && childMethods.size() > 0) {
permissions = new ArrayList<String>();
for (Method m : childMethods.values()) {
if (m.isAnnotationPresent(CommandPermissions.class)) {
permissions.addAll(Arrays.asList(m.getAnnotation(CommandPermissions.class).value()));
}
}
} }
toRegister.add(new CommandInfo(command.usage(), command.desc(), command.aliases(), commands, permissions)); toRegister.add(new CommandInfo(command.usage(), command.desc(), command.aliases(), commands, permissions == null ? null : permissions.toArray(new String[permissions.size()])));
} }
return register(toRegister); return register(toRegister);

View File

@ -31,7 +31,7 @@ import org.bukkit.plugin.Plugin;
import java.util.Arrays; import java.util.Arrays;
/** /**
* @author zml2008 * An implementation of a dynamically registered {@link org.bukkit.command.Command} attached to a plugin
*/ */
public class DynamicPluginCommand extends org.bukkit.command.Command implements PluginIdentifiableCommand { public class DynamicPluginCommand extends org.bukkit.command.Command implements PluginIdentifiableCommand {

View File

@ -21,7 +21,9 @@ package com.sk89q.worldedit.bukkit;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import com.sk89q.bukkit.util.CommandInfo; import com.sk89q.bukkit.util.CommandInfo;
import com.sk89q.bukkit.util.CommandRegistration; import com.sk89q.bukkit.util.CommandRegistration;
@ -96,13 +98,22 @@ public class BukkitServerInterface extends ServerInterface {
public void onCommandRegistration(List<Command> commands, CommandsManager<LocalPlayer> manager) { public void onCommandRegistration(List<Command> commands, CommandsManager<LocalPlayer> manager) {
List<CommandInfo> toRegister = new ArrayList<CommandInfo>(); List<CommandInfo> toRegister = new ArrayList<CommandInfo>();
for (Command command : commands) { for (Command command : commands) {
String[] permissions = null; List<String> permissions = null;
Method cmdMethod = manager.getMethods().get(null).get(command.aliases()[0]); Method cmdMethod = manager.getMethods().get(null).get(command.aliases()[0]);
Map<String, Method> childMethods = manager.getMethods().get(cmdMethod);
if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) { if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) {
permissions = cmdMethod.getAnnotation(CommandPermissions.class).value(); permissions = Arrays.asList(cmdMethod.getAnnotation(CommandPermissions.class).value());
} else if (cmdMethod != null && childMethods != null && childMethods.size() > 0) {
permissions = new ArrayList<String>();
for (Method m : childMethods.values()) {
if (m.isAnnotationPresent(CommandPermissions.class)) {
permissions.addAll(Arrays.asList(m.getAnnotation(CommandPermissions.class).value()));
}
}
} }
toRegister.add(new CommandInfo(command.usage(), command.desc(), command.aliases(), manager, permissions)); toRegister.add(new CommandInfo(command.usage(), command.desc(), command.aliases(), commands, permissions == null ? null : permissions.toArray(new String[permissions.size()])));
} }
dynamicCommands.register(toRegister); dynamicCommands.register(toRegister);