Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.16.1] PlayerInteractEvent.LeftClickBlock called on both press and release

Featured Replies

Posted

Hello,

 

I'm currently writing an event to allow a player to left-click on any block while holding a block of ice in their main hand, which converts the ice into 2-3 ice cube items from my mod. I'm using Minecraft/Forge version 'net.minecraftforge:forge:1.16.1-32.0.63'.

My issue is that the event is essentially fired twice every time, once when the left-click button is pressed and once when it is released. Thus, the player always uses up two ice blocks instead of one.

I first made sure the event wasn't being fired twice due to being executed on both the client and the server, as I included the following condition:

 

if (!event.getWorld().isRemote)

 

So the problem is definitely the press-release issue. I can't find anything in the event that specifies whether the click was a press or release. Any thoughts on how I can detect this, and rule out either one or the other?

 

I've included the code for my event below. Thanks!

 

@SubscribeEvent
    public static void makeIceCubes(PlayerInteractEvent.LeftClickBlock event) {
        if (!event.getWorld().isRemote) {
            PlayerEntity playerEntity = event.getPlayer();
            ItemStack itemStack = playerEntity.getHeldItemMainhand();
            Item heldItem = itemStack.getItem();
            if (heldItem.equals(Items.ICE)) {
                itemStack.shrink(1);
                World world = event.getWorld();
                int iceCubeCount = new Random().nextInt(3) + 1;
                ItemEntity itemEntity = new ItemEntity(world, playerEntity.getPosX(), playerEntity.getPosY(), playerEntity.getPosZ(), new ItemStack(ModItems.ICE_CUBE.get(), iceCubeCount));
                itemEntity.setDefaultPickupDelay();
                world.addEntity(itemEntity);
            }
        }
    }

 

Edited by cadendavidson

I had a briefly look so I might be wrong: This event is also triggered on server side in ServerPlayNetHandler L957, when received action StartDestroyBlock/AbortDestroyBlock/StopDestroyBlock, and I think that is the reason you had the even triggered once again when player release the mouse (aborts/stops block breaking), but I can not confirm that, you may want to test and see which part has trigger the event.

 

Edited by poopoodice

A workaround to the fact that PlayerInteractEvent.LeftClickBlock is triggered multiple times could be to add a cooldown to the item, i.e. add this to your if statement:

!playerEntity.getCooldownTracker().hasCooldown(heldItem)

and then set the cooldown in the then statement, something like this:

playerEntity.getCooldownTracker().setCooldown(heldItem, 5);

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.