Use a testPermission method for sub-commands.

This commit is contained in:
asofold 2013-08-10 03:05:23 +02:00
parent 875b9bdac5
commit fdf2009551
3 changed files with 20 additions and 10 deletions

View File

@ -115,7 +115,7 @@ public abstract class AbstractCommand<A> implements TabExecutor{
if (len == subCommandIndex || len == subCommandIndex + 1){
String arg = len == subCommandIndex ? "" : args[subCommandIndex].trim().toLowerCase();
for (AbstractCommand<?> cmd : subCommands.values()){
if (cmd.label.startsWith(arg) && (cmd.permission == null || sender.hasPermission(cmd.permission))){
if (cmd.label.startsWith(arg) && cmd.testPermission(sender, command, alias, args)){
// Only completes the label (!).
choices.add(cmd.label);
}
@ -124,7 +124,7 @@ public abstract class AbstractCommand<A> implements TabExecutor{
else if (len > subCommandIndex + 1){
String arg = args[subCommandIndex].trim().toLowerCase();
AbstractCommand<?> subCommand = subCommands.get(arg);
if (subCommand != null && (subCommand.permission == null || sender.hasPermission(subCommand.permission))){
if (subCommand != null && subCommand.testPermission(sender, command, alias, args)){
return subCommand.onTabComplete(sender, command, alias, args);
}
}
@ -142,7 +142,7 @@ public abstract class AbstractCommand<A> implements TabExecutor{
String arg = args[subCommandIndex].trim().toLowerCase();
AbstractCommand<?> subCommand = subCommands.get(arg);
if (subCommand != null){
if (subCommand.permission != null && !sender.hasPermission(subCommand.permission)){
if (!subCommand.testPermission(sender, command, alias, args)){
sender.sendMessage(ChatColor.DARK_RED + "You don't have permission.");
return true;
}
@ -152,5 +152,15 @@ public abstract class AbstractCommand<A> implements TabExecutor{
// Usage.
return false;
}
/**
* Test if the CommandSender has the permission necessary to run THIS command (not meant for checking sub-command permissions recursively).
* <br>Override for more complex specialized permissions.
* @param sender
* @return
*/
public boolean testPermission(CommandSender sender, Command command, String alias, String args[]){
return permission == null || sender.hasPermission(permission);
}
}

View File

@ -154,7 +154,7 @@ public class NoCheatPlusCommand extends BaseCommand{
// Check sub-commands.
if (args.length > 0){
AbstractCommand<?> subCommand = subCommands.get(args[0].trim().toLowerCase());
if (subCommand != null && sender.hasPermission(subCommand.permission)){
if (subCommand.testPermission(sender, command, commandLabel, args)){
// Sender has permission to run the command.
return subCommand.onCommand(sender, command, commandLabel, args);
}
@ -214,7 +214,7 @@ public class NoCheatPlusCommand extends BaseCommand{
// }
// else{
// final NCPCommand cmd = commands.get(subLabel);
// if (cmd != null && sender.hasPermission(cmd.permission)){
// if (cmd.testPermission...){
// // Delegate the tab-completion.
// return cmd.onTabComplete(sender, command, alias, args);
// }

View File

@ -348,14 +348,14 @@ permissions:
# Shortcut: convenient testing.
nocheatplus.tester:
description: Monitoring and debugging permissions, including removing data and exemption handling for oneself.
description: Monitoring and debugging permissions, including removing data and exemption.
children:
# TODO: Might just inherit from shortcut.info.
# TODO: Might just inherit from shortcut.info, might implement exempt.self etc.
nocheatplus.shortcut.monitor: true
nocheatplus.admin.debug: true
nocheatplus.command.exempt.self: true
nocheatplus.command.unexempt.self: true
nocheatplus.command.removeplayer.self: true
nocheatplus.command.exempt: true
nocheatplus.command.unexempt: true
nocheatplus.command.removeplayer: true
# Shortcut: full administrator without check-bypass-permissions:
nocheatplus.admin: