Hi, I am currently developing a plugin and I found an issue that I don't know how to solve. Basically I have PlayerInteractEvent, where I'm checking if player right clicked a block with specific item in main hand. Issue is when player have torches in left hand, because the torches get placed. Is there any way to prevent this? I've tried Listening for BlockPlaceEvent and canceling it whenever player holds that item in main hand, but it's not working. Code (Java): @EventHandler public void onBlockPlace(BlockPlaceEvent e) { Player p = e.getPlayer(); PlayerInventory inv = p.getInventory(); ItemStack tool = inv.getItemInMainHand(); ItemMeta im = tool.getItemMeta(); if( tool.getType() == Material.STICK && im != null && im.hasCustomModelData() && this.logs.containsKey(im.getCustomModelData()) // this.logs is HashMap with possible CustomModelData as a key ){ e.setCancelled(true); } // Other not important stuff here }
It is how you do an if statement. They have Code (Text): if ( condition1 && condition2 && condition3 ) { effect when true; } For OP: so your issue is that whenever someone uses the stick to right click on the block while offhanding a placeable block, the block gets placed?
My bad, though it was the first part of the if statement, not condition. I think the best way to know what's the issue is to use debug message, you print each condition separatly and you will know where a false is there.
The PlayerInteractEvent as well as the BlockPlaceEvent provides a #getHand (See [PIE] and [BPE]) method. Take advantage of it Spoiler: Spoonfeed Code (Java): @EventHandler public void onBlockPlace(BlockPlaceEvent event) { if(event.getHand() == EquipmentSlot.OFF_HAND) { event.setCancelled(true); return; } // other stuff } NOTE: The PlayerInteractEvent is called for each hand once. That means, that you can simply just return, if the used hand is the OFF_HAND.
This is working pretty well, but the problem is that when I click with my custom item, it consumes it, so when I have multiple items it's fine, but when I use the last one, the torch gets places, is there any way around it?
is that because your offhand check contains a check to see if the main hand still has the custom item in it?