GetItemInHand not working?

Discussion in 'Spigot Plugin Development' started by avighnash, May 23, 2016.

  1. Hello! I am using this
    Code (Text):
    if (p.getItemInHand() != null) {
                if (p.getItemInHand() == star) {
    to open an inventory. However, whenever I right click or left click a block or right click the air, the same inventory is opened. Why is this happening?

  2. P.S This was working after I reloaded the server.
  3. -
    #3 AdamJLE, May 23, 2016
    Last edited: Mar 16, 2022
  4. First: Are you using 1.9?
    If so, it's either getItemInMainHand() OR getItemInOffHand()

    Second: Define the same inventory is opened.
  5. I am not using 1.9, infact, I am using 1.4.2.

    Lol jk, I am using 1.8.
    What does that mean? I already defined the inventory, otherwise I would be getting a NPE.
  6. -
    #7 AdamJLE, May 23, 2016
    Last edited: Mar 16, 2022
  7. I mean define what you mean. I don't understand "the same inventory is opened". What exactly is the error?

    EDIT: Exactly what AdamJLE said.
  8. The same inventory is opened, meaning the inventory "inv" is also being opened when you right click/left click blocks or right click the air.
  9. There are no errors too.
  10. Check for the action instead of item in hand if u want it to be different (event.getAction())
    • Informative Informative x 1
  11. I want it, so when the player clicks the itemstack "star" in there hand, it would open the inventory "inv".
  12. Exactly, otherwise the event will fire when right/left clicking blocks as well as right/left clicking air.
  13. Clicks meaning left click? (Pressing e then left clicking the item)? if so use InventoryClickEvent and to get the item do event.getCurrentItem()
  14. The itemstack "star" Is in the players hotbar...
  15. Do you mean like this?

    Code (Text):
    if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
                if (p.getItemInHand() != null) {
                    if (p.getItemInHand().isSimilar(star)) {
  16. -
    #17 AdamJLE, May 23, 2016
    Last edited: Mar 16, 2022
  17. Still does not work. Meaning, I can still open the inventory "inv" when right clicking air or any blocks.
  18. -
    #19 AdamJLE, May 23, 2016
    Last edited: Mar 16, 2022
  19. I stopped the server, recompiled the jar and It does not work.