Jump to content

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


cadendavidson

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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