CustomRecipe

Discussion in 'Plugin Development' started by MadMaxCookie, May 18, 2016.

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

    MadMaxCookie

    Hello so I had a customrecipe plugin seems already finished
    but I'm asking how do I add a permission

    craft.<itemname> || to craft the item name

    and what's the event name of it ?
     
  2. Offline

    Zombie_Striker

    @MadMaxCookie
    PrepareItemCraftEvent is triggered whenever a player tries to craft an item. Test if the player has the permission. If the player does not have permission, cancel the event or set the 'result' equal to air.
     
  3. Offline

    MadMaxCookie

    @Zombie_Striker what about this ?

    Code:
    public void preparecraft(PrepareItemCraftEvent e) {
            Player p = (Player)e.getViewers();
         
            if(!p.hasPermission("craft." + e.getRecipe().getResult().getItemMeta().getDisplayName())) {
                e.getRecipe().getResult().setType(Material.AIR);
                return;
            }
            return;
        }
     
  4. Offline

    mine-care

    @MadMaxCookie e.getViewers() does not return a Player implementation thus casting it to player will result in a ClassCastExceprion. Refer to the docs to see what type it returns and what method returns a player.
    Then, you get the name of the item along with the ItemMeta that can both be null and result in a bull pointer exception, add a null check.
    Other than this your code seems alright.
     
Thread Status:
Not open for further replies.

Share This Page