How to check permissions

Discussion in 'Plugin Development' started by JjPwN1, Aug 1, 2012.

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

    JjPwN1

    I am a very new bukkit plugin coder... as you can tell by this question I am asking. Where would I insert and what would I insert to check if a player has the correct permission when they type "/hat". I know is goes something like "if(player.hasPermission"BbyHelms.use");". Here is my code:​
    [​IMG]
     
  2. Offline

    Bavestry

    Code:
    if(player.hasPermission("node.here"){
    
    }
    Put it above the if(cmd.getName().stuff), but make sure all that stuff is inside the permission checking if statement's squiggly brackets.
     
    JjPwN1 likes this.
  3. Offline

    JjPwN1

    Alright, so would it look like this?
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player)sender;
            if(player.hasPermission("BbyHelms.use")){
               
            }
            if(cmd.getName().equalsIgnoreCase("hat"));
            ItemStack is = player.getInventory().getHelmet();
            player.getInventory().setHelmet(player.getItemInHand());
            player.getInventory().removeItem(player.getItemInHand());
            player.sendMessage(ChatColor.GOLD + "[BbyHelms]" + ChatColor.DARK_RED + " Enjoy your new hat!");
            player.setItemInHand(is);
            return false;
        }
    }
     
  4. Offline

    Firefly

    Code:
    if(player.hasPermission("BbyHelms.use")){
             
    }
    What you coded:

    If player has this permission: Do nothing!
     
    Pingre and JjPwN1 like this.
  5. Offline

    JjPwN1

    Firefly
    Hm, how about this:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player)sender;
            if(player.hasPermission("BbyHelms.use")){
            if(cmd.getName().equalsIgnoreCase("hat"));
            ItemStack is = player.getInventory().getHelmet();
            player.getInventory().setHelmet(player.getItemInHand());
            player.getInventory().removeItem(player.getItemInHand());
            player.sendMessage(ChatColor.GOLD + "[BbyHelms]" + ChatColor.DARK_RED + " Enjoy your new hat!");
            player.setItemInHand(is);
            }
            return false;
        }
    }
     
  6. Offline

    Bavestry

    JjPwN1
    That is, in fact, how it should be done. Good job! :D
     
    thomas15v, jflory7 and JjPwN1 like this.
  7. Offline

    turqmelon

    For user-friendliness sake, I'd add an else statement to tell the player they don't have permission, instead of doing nothing.
     
  8. Offline

    JjPwN1

    turqmelon
    To do that, would the code look like this?
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player)sender;
            if(player.hasPermission("BbyHelms.use")){
            if(cmd.getName().equalsIgnoreCase("hat"));
            ItemStack is = player.getInventory().getHelmet();
            player.getInventory().setHelmet(player.getItemInHand());
            player.getInventory().removeItem(player.getItemInHand());
            player.sendMessage(ChatColor.GOLD + "[BbyHelms]" + ChatColor.DARK_RED + " Enjoy your new hat!");
            player.setItemInHand(is);
            }else;
            player.sendMessage(ChatColor.GOLD + "[BbyHelms]" + ChatColor.DARK_RED + " You don't have enough permission!");
            return false;
        }
    }
     
  9. Offline

    Firefly

    No, since that else is linked to the if regarding the cmd.getName()

    You have some brackets missing.
     
    JjPwN1 likes this.
  10. Offline

    turqmelon

    Change this:
    Code:
    }else;
            player.sendMessage(ChatColor.GOLD + "[BbyHelms]" + ChatColor.DARK_RED + " You don't have enough permission!");
            return false;
    To:
    Code:
    }else{
            player.sendMessage(ChatColor.GOLD + "[BbyHelms]" + ChatColor.DARK_RED + " You don't have enough permission!");
    return true;
    }
    If you return false, it'll display the syntax you specify in the plugin.yml. Returning true will allow you to use your own custom messages.
     
    JjPwN1 likes this.
  11. Offline

    JjPwN1

    Alright, I got it. Works perfectly, learned a ton from you guys.
     
  12. Offline

    Bavestry

    Firefly
    There are two opening brackets, and two closing brackets, @turqmelon's solution should fix it.
     
    JjPwN1 likes this.
Thread Status:
Not open for further replies.

Share This Page