Hello! I am using this Code (Text): if (p.getItemInHand() != null) { if (p.getItemInHand() == star) { p.openInventory(inv); } } 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? Thanks!
First: Are you using 1.9? If so, it's either getItemInMainHand() OR getItemInOffHand() Second: Define the same inventory is opened.
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.
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.
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.
I want it, so when the player clicks the itemstack "star" in there hand, it would open the inventory "inv".
Exactly, otherwise the event will fire when right/left clicking blocks as well as right/left clicking air.
Clicks meaning left click? (Pressing e then left clicking the item)? if so use InventoryClickEvent and to get the item do event.getCurrentItem()
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)) { p.openInventory(inv); } } }
Still does not work. Meaning, I can still open the inventory "inv" when right clicking air or any blocks.