From bf2c8ebed4f117bd60662e019f176a99ce916374 Mon Sep 17 00:00:00 2001 From: zml Date: Tue, 6 Aug 2013 20:09:47 -0700 Subject: [PATCH] Only show nested commands in the Bukkit help if the player has permission to use at least one of the children --- .../util/CommandsManagerRegistration.java | 21 ++++++++++++++----- .../bukkit/util/DynamicPluginCommand.java | 2 +- .../bukkit/BukkitServerInterface.java | 21 ++++++++++++++----- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/sk89q/bukkit/util/CommandsManagerRegistration.java b/src/main/java/com/sk89q/bukkit/util/CommandsManagerRegistration.java index 57862edbd..5a82822c7 100644 --- a/src/main/java/com/sk89q/bukkit/util/CommandsManagerRegistration.java +++ b/src/main/java/com/sk89q/bukkit/util/CommandsManagerRegistration.java @@ -27,7 +27,9 @@ import org.bukkit.plugin.Plugin; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Map; /** * @author zml2008 @@ -52,13 +54,22 @@ public class CommandsManagerRegistration extends CommandRegistration { public boolean registerAll(List registered) { List toRegister = new ArrayList(); for (Command command : registered) { - String[] permissions = null; + List permissions = null; Method cmdMethod = commands.getMethods().get(null).get(command.aliases()[0]); - if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) { - permissions = cmdMethod.getAnnotation(CommandPermissions.class).value(); - } + Map childMethods = commands.getMethods().get(cmdMethod); - toRegister.add(new CommandInfo(command.usage(), command.desc(), command.aliases(), commands, permissions)); + if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) { + permissions = Arrays.asList(cmdMethod.getAnnotation(CommandPermissions.class).value()); + } else if (cmdMethod != null && childMethods != null && childMethods.size() > 0) { + permissions = new ArrayList(); + 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 == null ? null : permissions.toArray(new String[permissions.size()]))); } return register(toRegister); diff --git a/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java b/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java index 098e8d608..ea20a3b8f 100644 --- a/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java +++ b/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java @@ -31,7 +31,7 @@ import org.bukkit.plugin.Plugin; 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 { diff --git a/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java b/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java index d1ce298a3..6d3d5e949 100644 --- a/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java +++ b/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java @@ -21,7 +21,9 @@ package com.sk89q.worldedit.bukkit; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Map; import com.sk89q.bukkit.util.CommandInfo; import com.sk89q.bukkit.util.CommandRegistration; @@ -96,13 +98,22 @@ public class BukkitServerInterface extends ServerInterface { public void onCommandRegistration(List commands, CommandsManager manager) { List toRegister = new ArrayList(); for (Command command : commands) { - String[] permissions = null; + List permissions = null; Method cmdMethod = manager.getMethods().get(null).get(command.aliases()[0]); - if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) { - permissions = cmdMethod.getAnnotation(CommandPermissions.class).value(); - } + Map childMethods = manager.getMethods().get(cmdMethod); - toRegister.add(new CommandInfo(command.usage(), command.desc(), command.aliases(), manager, permissions)); + if (cmdMethod != null && cmdMethod.isAnnotationPresent(CommandPermissions.class)) { + permissions = Arrays.asList(cmdMethod.getAnnotation(CommandPermissions.class).value()); + } else if (cmdMethod != null && childMethods != null && childMethods.size() > 0) { + permissions = new ArrayList(); + 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 == null ? null : permissions.toArray(new String[permissions.size()]))); } dynamicCommands.register(toRegister);