Question

I am trying my hand at Bukkit plugins and im having some difficulty on my first. My console for the server is incapable of running the command, if i could get your review, an explanation to whats going on would be helpfull.

Class Code: This is just the relevant code chunk.

//Overrides bukkits onCommand with modified code
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (args.length > 1) {//No more then 1 argument
        sender.sendMessage("[ConsoleFilter] Too many arguments!"); //Sends player a message
        return false;
    } 
    if (args.length < 1) {//No less then 1 argument
        sender.sendMessage("[ConsoleFilter] Not enough arguments!"); //Sends player a message
        return false;
    }
    if (cmd.getName().equalsIgnoreCase("CF")) { //Checks for /CF
        if (args[0].equalsIgnoreCase("Reload")){ //Checks for /CF Reload
            Player player = null; //Sets player
            if (sender instanceof Player){ //if sender is a player entity
                player = (Player) sender; //player = sender
                //plugin.yml will actually validate permissions, however additional provisions allows for 
                //further security and further extendability.
                if (player.isOp() || player.hasPermission("ConsoleFilter.Reload")){ //Is the player an Op or have specified Permission
                    this.reloadConfig(); //Reloads config file back into memory
                    player.sendMessage(ChatColor.DARK_GREEN + "Config Reloaded!"); //Sends player a message
                    getLogger().info("[ConsoleFilter] Config Reloaded");
                    return true; //Close True
                }
            }
            else{
                this.reloadConfig(); //Reloads config file back into memory
                getLogger().info("[ConsoleFilter] Config Reloaded");
                return true; //Close True
            }
        }
    }
    return false; 
}

Server output: [Server] INFO You don't have ConsoleFilter Permissions node- ConsoleFilter.Reload

Plugin.yml: This file parses properly any spacing errors is due to posting.

name: ConsoleFilter
main: com.dirtyredz.ConsoleFilter.ConsoleFilter
version: 0.0.1
commands:
   CF:
      description: This is a demo reload command.
      usage: /CF Reload
      permission: ConsoleFilter.Reload
      permission-message: You don't have ConsoleFilter Permissions node- ConsoleFilter.Reload
permissions:
   ConsoleFilter.*:
      description: Complete access to consolefilter commands
   children:
         ConsoleFilter.reload: false
   ConsoleFilter.reload:
      description: Test reload
      default: false
Was it helpful?

Solution

So figured this one out on my own. The issue was not that the plugin.yml was set to false, infact you want to keep it that way so that users by defualt do not have access to the command. My problem was that i left the Permission-Node under the command block in the plugin.yml. I was attempting to utilize the native Bukkit api (command block permisions) and the Updated Bukkit api (permissions block permissions). the Command Block permissions were taking precedence and overwriting any code i had in place. By removing:

permission: ConsoleFilter.Reload
permission-message: You don't have ConsoleFilter Permissions node- ConsoleFilter.Reload

from the plugin.yml I allowed my code to do its work.

Also a few code changes may have contributed to the end result:

//Overrides bukkits onCommand with modified code
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (args.length > 1) {//No more then 1 argument
        sender.sendMessage("[ConsoleFilter] Too many arguments!"); //Sends player a message
        return false;
    } 
    if (args.length < 1) {//No less then 1 argument
        sender.sendMessage("[ConsoleFilter] Not enough arguments!"); //Sends player a message
        return false;
    }
    //plugin.yml will actually validate permissions, however additional provisions allows for 
    //further security and further extendability.
    if (sender instanceof Player){ //if sender is a player entity
        Player player = (Player) sender; //Sets player to sender
        if (player.isOp() || player.hasPermission("ConsoleFilter.Reload")){//Is the player an Op or have specified Permission
            if (cmd.getName().equalsIgnoreCase("CF")) { //Checks for /CF
                if (args[0].equalsIgnoreCase("Reload")){ //Checks for /CF Reload
                    this.reloadConfig(); //Reloads config file back into memory
                    player.sendMessage(ChatColor.DARK_GREEN + "Config Reloaded!"); //Sends player a message
                    getLogger().info("[ConsoleFilter] Config Reloaded");
                    return true; //Close True
                }
            }
        }else{
            player.sendMessage("You do not have the appropriate Permissions. (ConsoleFilter.Reload)");;
        }
    }else{
        this.reloadConfig(); //Reloads config file back into memory
        getLogger().info("[ConsoleFilter] Config Reloaded");
        return true; //Close True
    }
    return false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top