How to add a permission in invclickevent?

Discussion in 'Plugin Development' started by Schaakmatth, Jun 9, 2014.

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

    Schaakmatth

    the title say it!
    Code:java
    1. @EventHandler
    2. public void inv(InventoryClickEvent e) {
    3. Player player = (Player) e.getWhoClicked();
    4. if(e.getInventory().getName().equalsIgnoreCase(ChatColor.RED + "Kit" + ChatColor.YELLOW + "Selector")) {
    5. if (e.getCurrentItem().getType() == Material.WOOD_SWORD) {
    6. player.getInventory().clear();
    7. player.getInventory().addItem(new ItemStack(Material.GOLDEN_APPLE));
    8.  
    9. ItemStack diaen = new ItemStack(Material.DIAMOND_SWORD);
    10. player.getInventory().addItem(diaen);
    11.  
    12. ItemStack bowpo = new ItemStack(Material.BOW);
    13. player.getInventory().addItem(bowpo);
    14.  
    15. player.getInventory().addItem(new ItemStack(Material.ARROW, 64));
    16.  
    17. ItemStack ironh1 = new ItemStack(Material.IRON_HELMET);
    18. player.getInventory().setHelmet(ironh1);
    19.  
    20. ItemStack ironc1 = new ItemStack(Material.IRON_CHESTPLATE);
    21. player.getInventory().setChestplate(ironc1);
    22.  
    23. ItemStack ironl1 = new ItemStack(Material.IRON_LEGGINGS);
    24. player.getInventory().setLeggings(ironl1);
    25.  
    26. ItemStack ironb1 = new ItemStack(Material.IRON_BOOTS);
    27. player.getInventory().setBoots(ironb1);
    28.  
    29. player.closeInventory();
     
  2. Offline

    MineCrashKoen

    Use:
    Code:java
    1. if(p.hasPermission("something.random"))
    2. // has permission, do stuff
    3. else //no permission
     
  3. Offline

    Cloaking_Ocean

    So on your onEnable add:
    Permission permission1 = new Permission("nameofplugin.nameofpermission");
    this.getServer().getPluginManager().addPermission(permission1);

    and then on your onDisable add:

    this.getServer().getPluginManager().addPermission(permission1) ;

    and then in your code just add an if statment like this:

    if(player.hasPermission(permission1)){
    //do code
    } else{
    player.sendMessage(ChatColor.DARK_RED + "You don't have permission for this");
    }
     
  4. Offline

    Schaakmatth

    Ok but where does i need to add the permission
     
  5. Offline

    MineCrashKoen

  6. Offline

    mine-care

    Don't use == use.equals();
    And how about creating those itemstacks onenable instead of doing it each time a player opens the inv? Onenable it loads one time and on the place you added them they load each time, may cause lag :)
     
  7. Offline

    Necrodoom

    No, you can use == for enum.

    Schaakmatth what you want to do when the permission is false or true? Put code in the if check according to that.
     
  8. Offline

    mine-care

    Wtf? Rly? I knew it is just for variables... Anyway then both work ;-)
     
  9. Offline

    Schaakmatth

  10. Offline

    Necrodoom

    Then put the kit code inside the if check? I assume you know how if checks work.
     
  11. Offline

    Schaakmatth

  12. Offline

    Garris0n

    I still wouldn't recommend it. I personally prefer using .equals, it just feels a bit more consistent. Also, it removes any confusion in a case where there are constants in a class that make it look like an enum (Bukkit's Color class, for example). That could easily confuse somebody who is new to Java/Bukkit.

    That's mostly just my opinion, though, and == is perfectly valid.
     
Thread Status:
Not open for further replies.

Share This Page