Permissions

Discussion in 'Plugin Development' started by ShadowDust99, Dec 28, 2014.

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

    ShadowDust99

    Hi again guys,
    I am trying to configure permissions for my plugin, but am having problems. I want to make a custom permission denied message, but the permission-message thing in the plugin.yml file doesn't work, it never displays that message I want. Therefore, I decided to try coding it, and I did this:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
         
            if (cmd.getName().equalsIgnoreCase("test")) {
                if (player.hasPermission("firstplugin.test")) {
                    player.sendMessage("Hello, command works!");
                    player.sendMessage(ChatColor.RED + "Plugin seems to work fine, and this is in Red text");
                    return true; 
                } else {
                    player.sendMessage("You do not have sufficient permissions for that command. Sorry!");
                }
                         
     
            }
         
            return false;
        }
    
    }
    Note that I did try switching lines 4 and 5 around and that still didn't work. I just want it to check if they have the permission and if they don't, say "You do not have sufficient permissions for that command. Sorry!" WHY HAS IT GOT TO BE SO HARD!

    Please tell me where I have gone wrong, and how I can fix this.

    Also, could someone please tell me how to make a plugin execute a plugin as a player, and as the console? Thanks!
     
  2. Offline

    mythbusterma

    @ShadowDust99

    I don't know how you execute a plugin as a Player, that's a new concept.

    It's also a pretty bad idea to assume the command sender is a Player, it's often not. Looks like it should work other than that.

    I'm guessing you probably forget to set the command executor.
     
  3. Offline

    ShadowDust99

    @mythbusterma
    Oops, meant to say "how to make a plugin execute a command as a player and the console"

    And, could you please explain the "command executor" in English as I am new to programming and don't know how to do that, and what code I have to use to do it.
     
    Last edited: Dec 28, 2014
  4. Offline

    JordyPwner

    Register your command in the onEnable()
     
  5. @ShadowDust99 To execute a command as console, you would do this:
    Code:
    ConsoleCommandSender console = this.getServer().getConsoleSender();
    this.getServer().dispatchCommand(console, "YourCommandHere");
    And I think this should also execute a command as a player, if he has the right permissions for it:
    Code:
    // Somewhere you first need to get your player, of course.
    this.getServer().dispatchCommand(player, "YourCommandHere");
     
  6. Offline

    ShadowDust99

    Ok, and what about registering my command in the onEnable()? Do you know the code to do that?
     
  7. Offline

    stoneminer02

    If you have it in another class, you need to register with this, else you won't need it at all:
    getCommand("test").setExecutor(new CommandClass(), this);
    "this" is the plugin where it is in the onEnable.
     
  8. @ShadowDust99 I suppose you do not have it in a different class, so you do not need to register anything.
    But if you would have a different class,
    you would neet to use
    Code:
    getCommand("yourPluginsCommandHere").setExecutor(new <YourClassNameHere>(), this);
    You would have to repeat this for every command your plugin uses.
     
  9. Offline

    mythbusterma

    @Lionhard

    Pretty sure you still need to set the command executor, even if it's in the same class.
     
  10. @mythbusterma No, you do not have to. (Atleast I do not have to. :D)
     
  11. Offline

    Konato_K

    @mythbusterma No, if the command is in the JavaPlugin then you don't need to set it.
     
  12. Offline

    ShadowDust99

    @Lionhard I have the command in the same class, but the plugin still isn't doing my custom permission denied message, it is saying the default "I believe you don't have permission for that command, if you believe this is a mistake contact an administrator" or something like that. Should I try setting the command executor and see what happens?
     
  13. @ShadowDust99 That wouldn't really help. If you don't mind, could you please send us (or atleast me per PM) your whole class so I can look through it?

    And by the way, you have one additional } in your code, I'm pretty sure that is causing it, because the if statement is probably closed by a different closing tag, leading to another else statement and therefore not working output message.
    But if you use eclipse, that shouldn't even let you compile (Or atleast notify you) and you should see a red X near the class and much more notifications.

    But try it. :)
     
  14. Offline

    ShadowDust99

    @Lionhard there are no errors with in the document according to eclipse. The } should be fine. What would you like me to PM you? The .java code or something?
     
  15. @ShadowDust99 if this is your onCommand
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
       
            if (cmd.getName().equalsIgnoreCase("test")) {
                if (player.hasPermission("firstplugin.test")) {
                    player.sendMessage("Hello, command works!");
                    player.sendMessage(ChatColor.RED + "Plugin seems to work fine, and this is in Red text");
                    return true;
                } else {
                    player.sendMessage("You do not have sufficient permissions for that command. Sorry!");
                }
                       
            }
       
            return false;
        }
    }
    
    Then the } is not fine.

    And what I want you to send me is your whole class. Just go to your class, and copy & paste everything.
     
Thread Status:
Not open for further replies.

Share This Page