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

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

Thread Status:
Not open for further replies.
  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
    (p.getItemInHand()==TeleportItem)

    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:
    Code:
    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. Offline

    timtower Administrator Administrator Moderator

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

    Tim_M

    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.
     
Thread Status:
Not open for further replies.

Share This Page