How do i get the player from PrepareItemCraftEvent

Discussion in 'Plugin Development' started by Rasmase, Apr 14, 2013.

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

    Rasmase

    I am trying to limit a custom crafting recipe with permissions. How do I find out who triggers the PrepareItemCraftEvent?
     
  2. event.getView().getPlayer() and check if it's instanceof Player then cast.

    It's a HumanEntity object which means NPCs can trigger it as well.
     
    Rasmase likes this.
  3. Offline

    Rasmase

    I get this error:

    Type mismatch: cannot convert from HumanEntity to Player
     
  4. Offline

    Forseth11

    I'm not sure how to do this, but try event.getPlayer() or event.getEntry.
    I'm not at my computer so I can't test anything.
     
  5. Rasmase
    Post your code.
     
  6. Offline

    Rasmase

    Code:
        public void onPlayerCraft(PrepareItemCraftEvent event) {
            Player brewer = event.getView().getPlayer()
            if (event.getRecipe().getResult().getItemMeta().getDisplayName().equals("Beer")) {
                if (brewer.hasPermission("beer.beer.brew")) {
                    //Let him craft
                    
                } else {
                    event.getInventory().setResult(null);
                }
                
            }
        }
     
  7. Rasmase
    You're still not checking nor casting...
    Code:
    HumanEntity human =  event.getView().getPlayer();
    
    if(human instanceof Player)
    {
        Player player = (Player)human;
    
        // do stuff.
    }
     
    Rasmase likes this.
  8. Offline

    Rasmase

    Thanks! Works now.
     
Thread Status:
Not open for further replies.

Share This Page