detecting when a player right clicks while holding an item

Discussion in 'Plugin Development' started by Divlocket, Sep 5, 2021.

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

    Divlocket

    I'm fairly new to plugin development, and I wanted to make something happen when a player right clicks while holding an item (bone).

    This is the code I have in my main class.
    ```
    @EventHandler(priority = EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
    Player p = event.getPlayer(); if(p.getItemInHand().getType() == Material.BONE){
    getLogger().info("ok boomer");}
    }
    ```
     
  2. Offline

    xpaintall

    You should check if the click is a right click (I believe it is event.getAction().)
     
  3. Offline

    Kars

    Just use event.getItem
     
  4. Offline

    byteful

    event.hasItem() should be ran before as well to prevent NPE.
     
Thread Status:
Not open for further replies.

Share This Page