Check if a player has permission to a command

Discussion in 'Plugin Development' started by bubba1234119, Aug 8, 2014.

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

    bubba1234119

    I am trying to figure out how to make it when a person executes a command to have it check to see if they have access to it. If not I want to manually cancel it and send a message. Here is what I currently have.

    The error is on the line with the if statement. I am getting a null pointer exception.
    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void cmdListener(PlayerCommandPreprocessEvent e)
        {
            if(!plugin.getServer().getPluginCommand(e.getMessage()).testPermission(e.getPlayer()))
            {
                e.getPlayer().sendMessage(plugin.string.translate(plugin.resource.getString("messages.yml","cmd-cancelled")));
                e.setCancelled(true);
            }
        }
     
  2. Offline

    Gamecube762

    Player.hasPermission("command.perm") returns true or false.
     
  3. Offline

    123ang

    Code:java
    1. if (player.hasPermission("your.permission.name"){
    2. // Do Something
    3. } else{
    4. player.sendMessage("no permission");
    5. }


    Hope I helped :)!
     
  4. Offline

    _Filip

    bubba1234119 The problem is that you have to get get string.substring(1);, as commands don't have the "/".
     
  5. Offline

    bubba1234119

    Hold on looking into this.
     
Thread Status:
Not open for further replies.

Share This Page