checking if the player is holding and right-clicking an item

Discussion in 'Plugin Development' started by Bambus UwU, Oct 15, 2021.

  1. Offline

    Bambus UwU

    so my plan was to start with a simple idea for getting into programming Plugins.
    I found a post of someone talking about an item that will teleport you some blocks forward if you right click it.
    I started by giving the player a stick if a command is used.
    I named the item TeleportItem in order to only make the player teleport if the one special item is held.
    The Problems started when I tried checking if the player is holding the item. I first went with PlayerItemHeldEvent but I realised it is used to check if the player has been holding an item. So I tried the getItemInHand which worked

    The only problem is that i can't really find a way to check if the player is actually right-clicking. I have tried the PlayerInteractEvent and getAction(which I have found somewhere on this forum). Neither of them worked for me though. Is there any major mistake i have made?
    The code I am stuck with atm looks something like this:
    if(p.getItemInHand()==TeleportItem && PlayerInteractEvent()){
                //Get coordinates and add 5 blocks of space depending on where the player is looking (Haven't started yet)
  2. Online

    timtower Administrator Administrator Moderator

    @Bambus UwU PlayerInteractEvent isn't a function.
    You need a listener for it.
  3. Offline


    You need to create a function that takes in a PlayerInteractEvent as an argument with the EventListener annotation inside a class that implements Listener.

    You will also have to register that class.

Share This Page