Class UseItemOnBlockEvent
- All Implemented Interfaces:
net.neoforged.bus.api.ICancellableEvent
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.useItemOn(net.minecraft.world.item.ItemStack, 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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate InteractionResultprivate final UseOnContextprivate final @Nullable Directionprivate final InteractionHandprivate final ItemStackprivate final Levelprivate final @Nullable Playerprivate final BlockPosprivate final UseItemOnBlockEvent.UsePhase -
Constructor Summary
ConstructorsConstructorDescriptionUseItemOnBlockEvent(UseOnContext context, UseItemOnBlockEvent.UsePhase usePhase) -
Method Summary
Modifier and TypeMethodDescriptionvoidcancelWithResult(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.@Nullable DirectiongetFace()getHand()getLevel()@Nullable PlayergetPos()If the interaction was on an entity, will be a BlockPos centered on the entity.getSide()Returns The Use Phase of the interaction.voidSet theInteractionResultthat will be returned to vanilla if the event is cancelled, instead of calling the relevant method of the event.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.neoforged.bus.api.ICancellableEvent
isCanceled, setCanceled
-
Field Details
-
level
-
player
-
hand
-
heldItem
-
pos
-
face
-
context
-
usePhase
-
cancellationResult
-
-
Constructor Details
-
UseItemOnBlockEvent
-
-
Method Details
-
getPlayer
- Returns:
- player or null
-
getHand
- Returns:
- The hand involved in this interaction. Will never be null.
-
getItemStack
- Returns:
- The itemstack involved in this interaction,
ItemStack.EMPTYif the hand was empty.
-
getPos
If the interaction was on an entity, will be a BlockPos centered on the entity. If the interaction was on a block, will be the position of that block. Otherwise, will be a BlockPos centered on the player. Will never be null.- Returns:
- The position involved in this interaction.
-
getFace
- Returns:
- The face involved in this interaction. For all non-block interactions, this will return null.
-
getLevel
- Returns:
- Convenience method to get the level of this interaction.
-
getUseOnContext
- Returns:
- context
-
getUsePhase
Returns The Use Phase of the interaction.- Returns:
- The Use Phase of the interaction
- See Also:
-
getSide
- Returns:
- The effective, i.e. logical, side of this interaction. This will be
LogicalSide.CLIENTon the client thread, andLogicalSide.SERVERon the server thread.
-
cancelWithResult
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 logicInteractionResult.SUCCESS,InteractionResult.CONSUME, and FAIL will prevent further types of interaction attempts when provided from the ITEM_BEFORE_BLOCK phase.InteractionResult.SUCCESS,InteractionResult.CONSUMEwill 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.InteractionResult.SUCCESSwill trigger the arm-swinging mechanic.InteractionResult.TRY_WITH_EMPTY_HANDwill always allow proceeding to the next phase.InteractionResult.PASSwill not call the block'suse method
-
getCancellationResult
- Returns:
- The
InteractionResultthat will be returned to vanilla if the event is cancelled, instead of calling the relevant method of the event. By default, this isInteractionResult.TRY_WITH_EMPTY_HAND, meaning cancelled events will cause the client to keep trying more interactions until something works.
-
setCancellationResult
Set theInteractionResultthat will be returned to vanilla if the event is cancelled, instead of calling the relevant method of the event.
-