Add Permissions to plugin

Discussion in 'Plugin Development' started by TechG, May 29, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    TechG

    I have created my first plugin. I'm now want to limit /reforest on and /reforest off command that the player needs permission node reforest.basic to use the commands. But i can't seem to get it to work right.

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            for(String arg:args) {
                arg.toLowerCase();
            }
           
            String thisUsage = cUsage.replace("REPL", commandLabel);
            if (args.length == 1) {
                switch (args[0]) {
                case "help":
                    sender.sendMessage(thisUsage);
                    sender.sendMessage(ChatColor.GOLD + "/" + commandLabel + " <delay> <seconds>");
                    break;
                case "status":
                    sender.sendMessage(ChatColor.GOLD + "Status: \n"
                            + "Is Enabled: " + reforestConfig.getString("enabled") + "\n"
                                    + "Delay: " + reforestConfig.getString("delay"));
                    break;
                case "on":
                    if (reforestConfig.getBoolean("enabled")) {
                        sender.sendMessage(ChatColor.GOLD + "Reforest - Plugin is already enabled!");
                    } else {
                        reforestConfig.set("enabled", true);
                        reforestConfig.saveConfig();
                        sender.sendMessage(ChatColor.GOLD + "Reforest - Plugin Enabled");
                        launchTask();
                    }
                    break;
                case "off":
                    reforestConfig.set("enabled", false);
                    reforestConfig.saveConfig();
                    sender.sendMessage(ChatColor.GOLD + "Reforest - Plugin Disabled");
                    break;
                case "delay":
                   
                    break;
                }
            } else if (args.length == 2) {
                switch(args[0]) {
                case "delay":
                        try {
                            reforestConfig.set("delay", Long.valueOf(args[1]));
                            reforestConfig.saveConfig();
                           
                            sender.sendMessage(ChatColor.GOLD + "Reforest - Delay for each run set to " + args[1]);
                            sender.sendMessage(ChatColor.GOLD + "Reforest - To prevent bugs, please manually restart the \n"
                                    + "process with \"/reforest on\" in " + Long.valueOf(args[1]) + " seconds.");
                            if (reforestConfig.getBoolean("enabled"))
                                reforestConfig.set("enabled", false);
                        } catch (Exception e) {
                            sender.sendMessage(thisUsage + " <seconds>");
                        }
                        break;
                default:
                    sender.sendMessage(thisUsage);
                    break;
                }
            } else {
                sender.sendMessage(ChatColor.GOLD + "\"/Reforest help\"");
            }
            return true;
        }   
    }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @TechG sender.hasPermission("reforest.basic")?
     
  3. Offline

    TechG

    Didn't work so i'm guessing i did it wrong is not right.
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            for(String arg:args) {
                arg.toLowerCase();
            }
          
            String thisUsage = cUsage.replace("REPL", commandLabel);
            if (args.length == 1) {
                switch (args[0]) {
                case "help":
                    sender.sendMessage(thisUsage);
                    sender.sendMessage(ChatColor.GOLD + "/" + commandLabel + " <delay> <seconds>");
                    break;
                case "status":
                    sender.sendMessage(ChatColor.GOLD + "Status: \n"
                            + "Is Enabled: " + reforestConfig.getString("enabled") + "\n"
                                    + "Delay: " + reforestConfig.getString("delay"));
                    break;
                case "on":
                    if (reforestConfig.getBoolean("enabled")) {
                        sender.hasPermission("reforest.basic");
                        sender.sendMessage(ChatColor.GOLD + "Reforest - Plugin is already enabled!");
                    } else {
                        reforestConfig.set("enabled", true);
                        reforestConfig.saveConfig();
                        sender.sendMessage(ChatColor.GOLD + "Reforest - Plugin Enabled");
                        launchTask();
                    }
                    break;
                case "off":
                    reforestConfig.set("enabled", false);
                    reforestConfig.saveConfig();
                    sender.hasPermission("reforest.basic");
                    sender.sendMessage(ChatColor.GOLD + "Reforest - Plugin Disabled");
                    break;
                case "delay":
                  
                    break;
                }
            } else if (args.length == 2) {
                switch(args[0]) {
                case "delay":
                        try {
                            reforestConfig.set("delay", Long.valueOf(args[1]));
                            reforestConfig.saveConfig();
                          
                            sender.sendMessage(ChatColor.GOLD + "Reforest - Delay for each run set to " + args[1]);
                            sender.sendMessage(ChatColor.GOLD + "Reforest - To prevent bugs, please manually restart the \n"
                                    + "process with \"/reforest on\" in " + Long.valueOf(args[1]) + " seconds.");
                            if (reforestConfig.getBoolean("enabled"))
                                reforestConfig.set("enabled", false);
                        } catch (Exception e) {
                            sender.sendMessage(thisUsage + " <seconds>");
                        }
                        break;
                default:
                    sender.sendMessage(thisUsage);
                    break;
                }
            } else {
                sender.sendMessage(ChatColor.GOLD + "\"/Reforest help\"");
            }
            return true;
        }  
    }
     
  4. Offline

    timtower Administrator Administrator Moderator

    @TechG You are just calling it. You are not putting it in an if statement.
     
  5. Offline

    TechG

    Oops. So like this?
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            for(String arg:args) {
                arg.toLowerCase();
            }
          
            String thisUsage = cUsage.replace("REPL", commandLabel);
            if (args.length == 1) {
                switch (args[0]) {
                case "help":
                    sender.sendMessage(thisUsage);
                    sender.sendMessage(ChatColor.GOLD + "/" + commandLabel + " <delay> <seconds>");
                    break;
                case "status":
                    sender.sendMessage(ChatColor.GOLD + "Status: \n"
                            + "Is Enabled: " + reforestConfig.getString("enabled") + "\n"
                                    + "Delay: " + reforestConfig.getString("delay"));
                    break;
                case "on":
                    if (sender.hasPermission("reforest.basic"));
                    if (reforestConfig.getBoolean("enabled")) {
                        sender.sendMessage(ChatColor.GOLD + "Reforest - Plugin is already enabled!");
                    } else {
                        reforestConfig.set("enabled", true);
                        reforestConfig.saveConfig();
                        sender.sendMessage(ChatColor.GOLD + "Reforest - Plugin Enabled");
                        launchTask();
                    }
                    break;
                case "off":
                    if (sender.hasPermission("reforest.basic"));
                    reforestConfig.set("enabled", false);
                    reforestConfig.saveConfig();
                    sender.sendMessage(ChatColor.GOLD + "Reforest - Plugin Disabled");
                    break;
                case "delay":
                  
                    break;
                }
            } else if (args.length == 2) {
                switch(args[0]) {
                case "delay":
                        try {
                            reforestConfig.set("delay", Long.valueOf(args[1]));
                            reforestConfig.saveConfig();
                          
                            sender.sendMessage(ChatColor.GOLD + "Reforest - Delay for each run set to " + args[1]);
                            sender.sendMessage(ChatColor.GOLD + "Reforest - To prevent bugs, please manually restart the \n"
                                    + "process with \"/reforest on\" in " + Long.valueOf(args[1]) + " seconds.");
                            if (reforestConfig.getBoolean("enabled"))
                                reforestConfig.set("enabled", false);
                        } catch (Exception e) {
                            sender.sendMessage(thisUsage + " <seconds>");
                        }
                        break;
                default:
                    sender.sendMessage(thisUsage);
                    break;
                }
            } else {
                sender.sendMessage(ChatColor.GOLD + "\"/Reforest help\"");
            }
            return true;
        }  
    }
    
    I was thinking i had to do it below
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String
    but it keep erroring so maybe not. Sorry i'm new to this.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @TechG Errors are always bad. Lets fix those first.
    What errors do you have?
     
  7. Online

    KarimAKL

    @TechG About the 'CommandSender.hasPermission(String permission)' method, it returns a boolean, true if the player has the permission and false if the player does not have the permission, that's why you want to put it in an if statement like you've done now, the thing that you need to do now though is either return immediately when they don't have the permission or only execute your code in the scope where the sender has the permission.

    Return immediately if they don't have the permission:
    Code:Java
    1. // Check if the sender has the permission 'permission.to.use' and if so executes the code in the scope
    2. // Notice the '!', that means the opposite of the boolean value
    3. // In this case, if the 'hasPermission' method returns 'false' the code in the scope will execute
    4. if (!sender.hasPermission("permission.to.use")) { // Start of scope
    5. // Send them a message here to let them know they don't have permission to use the command
    6. sender.sendMessage("You do not have permission to execute this command!");
    7. // Return a boolean here because the 'onCommand' method
    8. // from the 'CommandExecutor' interface has a return type of 'boolean'
    9. // We return 'true' instead of 'false' so that the plugin knows the command succeeded
    10. // which then stops the plugin from sending
    11. // the sender of the command the usage specified in the 'plugin.yml' file
    12. return true;
    13. } // End of scope
    14. // Continue your code here (Out of the scope)

    Only execute the code in the scope:
    Code:Java
    1. // Notice the '!' is not here, this means that if the method returns 'true' the code in the scope will be executed
    2. // i.e. If they have permission, the code will run, if they don't have permission, the code won't run.
    3. if (sender.hasPermission("permission.to.use")) {
    4. // Your code here (Inside the scope)
    5. // But what if we want a message to appear when they don't have permission?
    6. // We can simply use the 'else' statement like this
    7. } else {
    8. // Let them know they don't have permission here
    9. }

    I recommend the first method because your code won't be as nested as it will be with the second method.
     
Thread Status:
Not open for further replies.

Share This Page