1.17.1 Disable using left hand when right clicking a block

Discussion in 'Spigot Plugin Development' started by CreativGraphics, Aug 5, 2021.

  1. 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):
    public void onBlockPlace(BlockPlaceEvent e) {
        Player p = e.getPlayer();
        PlayerInventory inv = p.getInventory();
        ItemStack tool = inv.getItemInMainHand();
        ItemMeta im = tool.getItemMeta();

            tool.getType() == Material.STICK &&
            im != null &&
            im.hasCustomModelData() &&
            this.logs.containsKey(im.getCustomModelData()) // this.logs is HashMap with possible CustomModelData as a key
        // Other not important stuff here
  2. This is not how to do a if else statement in java. Why do you have "if {" without condition ?
  3. It is how you do an if statement. They have

    Code (Text):
    if (
        condition1 &&
        condition2 &&
    ) {
        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?
  4. 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.
  5. The PlayerInteractEvent as well as the BlockPlaceEvent provides a #getHand (See [PIE] and [BPE]) method. Take advantage of it

    Code (Java):
    public void onBlockPlace(BlockPlaceEvent event) {

        if(event.getHand() == EquipmentSlot.OFF_HAND) {
        // 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.
    #5 GodCipher, Aug 5, 2021
    Last edited: Aug 5, 2021
    • Agree Agree x 1
  6. 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?
  7. yes
  8. is that because your offhand check contains a check to see if the main hand still has the custom item in it?