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