Class UseItemOnBlockEvent

All Implemented Interfaces:
net.neoforged.bus.api.ICancellableEvent

public class UseItemOnBlockEvent extends PlayerInteractEvent implements net.neoforged.bus.api.ICancellableEvent

Fires on both the client and server thread when a player interacts with a block.

The event fires in three phases, corresponding with the three interaction behaviors: IItemExtension.onItemUseFirst(net.minecraft.world.item.ItemStack, net.minecraft.world.item.context.UseOnContext), BlockBehaviour.use(net.minecraft.world.level.block.state.BlockState, net.minecraft.world.level.Level, net.minecraft.core.BlockPos, net.minecraft.world.entity.player.Player, net.minecraft.world.InteractionHand, net.minecraft.world.phys.BlockHitResult), and Item.useOn(net.minecraft.world.item.context.UseOnContext).

The event fires after the interaction logic decides to run the particular interaction behavior, as opposed to PlayerInteractEvent.RightClickBlock which fires once-per-right-click, before the behavior-choosing logic.

If the event is cancelled via cancelWithResult(net.minecraft.world.InteractionResult), then the normal interaction behavior for that phase will not run, and the specified InteractionResult will be returned instead.

  • Field Details

  • Constructor Details

  • Method Details

    • getUseOnContext

      public UseOnContext getUseOnContext()
      Returns:
      context
    • getUsePhase

      public UseItemOnBlockEvent.UsePhase getUsePhase()
      Returns The Use Phase of the interaction.
      Returns:
      The Use Phase of the interaction
      See Also:
    • cancelWithResult

      public void cancelWithResult(InteractionResult result)

      Cancels the use interaction (preventing the block or item's use behavior from running) and provides the specified result to the interaction logic instead.

      Invoke this if you intend to prevent the default interaction behavior and replace it with your own.

      Parameters:
      result - InteractionResult to return to the interaction logic
      • SUCCESS, CONSUME, CONSUME_PARTIAL, and FAIL will prevent further types of interaction attempts when provided from the ITEM_BEFORE_BLOCK phase.
      • SUCCESS, CONSUME, and CONSUME_PARTIAL will trigger advancements on the server (except in the ITEM_BEFORE_BLOCK phase), and will also prevent the ITEM_AFTER_BLOCK item interaction from occurring if provided during the BLOCK phase.
      • SUCCESS will trigger the arm-swinging mechanic.
      • PASS will always allow proceeding to the next phase.