Posted November 15, 20204 yr 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 November 15, 20204 yr by cadendavidson
November 15, 20204 yr 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 November 15, 20204 yr by poopoodice
November 16, 20204 yr 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.