Please Help! How to bind a command to a item?

Discussion in 'Spigot Plugin Development' started by ZevoGaems, May 5, 2017.

  1. I need to add a command to a item for my staff mode plugin so how would I bind a command so when you right click the item it does a command? Please help and thank you for helping!
  2. I noticed you're posting a lot of questions of here, when I feel you could be google this stuff and moreover, learning Java.
  3. You would want to:
    1. Make a PlayerInteractEvent
    2. Check if the action was an RIGHT_CLICK_AIR
    3. Check if the item clicked was your item.
    4. Perform what you want to do!
    5. Done!
    (You could try googling this)
    #3 Zain20, May 5, 2017
    Last edited: May 5, 2017
  4. This is how i would do it:
    • Add a NBTTagString by a custom name (recommend to use the plugin name as key) on the item tag, containing the command
    • Listen to the PlayerInteractEvent
    • Once the event gets triggered, check if the item contains a NBTTagString by the name of your plugin
      • if so, let the player execute the string as command
  5. Offtopic: You are very lucky that people are still helping you, even though you are making so many threads.
    Ontopic: I think @Zain20 's suggestion is the best, but I don't think it is RIGHT_CLICK_EVENT. I think you have to do Event.getAction().equals(Action.RIGHT_CLICK_AIR) || Event.getAction().equals(Action.RIGHT_CLICK_BLOCK);.
  6. Oh yes, you are correct, I was not thinking lol!
  7. 1: I am using a ender pearl which would that still be classified as a block?
    2: It says when I do getAction() it says it is undefined for the type event?
  8. Well, ask yourself, is an ender pearl a block?

    Because Event#getAction doesn't exist.