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.ItemInteractionResult)
,
then the normal interaction behavior for that phase will not run,
and the specified InteractionResult
will be returned instead.
-
Nested Class Summary
Nested classes/interfaces inherited from class net.neoforged.neoforge.event.entity.player.PlayerInteractEvent
PlayerInteractEvent.EntityInteract, PlayerInteractEvent.EntityInteractSpecific, PlayerInteractEvent.LeftClickBlock, PlayerInteractEvent.LeftClickEmpty, PlayerInteractEvent.RightClickBlock, PlayerInteractEvent.RightClickEmpty, PlayerInteractEvent.RightClickItem
Nested classes/interfaces inherited from class net.neoforged.neoforge.event.entity.player.PlayerEvent
PlayerEvent.BreakSpeed, PlayerEvent.Clone, PlayerEvent.HarvestCheck, PlayerEvent.ItemCraftedEvent, PlayerEvent.ItemSmeltedEvent, PlayerEvent.LoadFromFile, PlayerEvent.NameFormat, PlayerEvent.PlayerChangedDimensionEvent, PlayerEvent.PlayerChangeGameModeEvent, PlayerEvent.PlayerLoggedInEvent, PlayerEvent.PlayerLoggedOutEvent, PlayerEvent.PlayerRespawnEvent, PlayerEvent.SaveToFile, PlayerEvent.StartTracking, PlayerEvent.StopTracking, PlayerEvent.TabListNameFormat
Nested classes/interfaces inherited from class net.neoforged.neoforge.event.entity.living.LivingEvent
LivingEvent.LivingJumpEvent, LivingEvent.LivingVisibilityEvent
Nested classes/interfaces inherited from class net.neoforged.neoforge.event.entity.EntityEvent
EntityEvent.EnteringSection, EntityEvent.EntityConstructing, EntityEvent.Size
-
Field Summary
Modifier and TypeFieldDescriptionprivate ItemInteractionResult
private final UseOnContext
private final UseItemOnBlockEvent.UsePhase
-
Constructor Summary
ConstructorDescriptionUseItemOnBlockEvent
(UseOnContext context, UseItemOnBlockEvent.UsePhase usePhase) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Cancels the use interaction (preventing the block or item's use behavior from running) and provides the specified result to the interaction logic instead.Returns The Use Phase of the interaction.void
Set theItemInteractionResult
that will be returned to vanilla if the event is cancelled, instead of calling the relevant method of the event.Methods inherited from class net.neoforged.neoforge.event.entity.player.PlayerInteractEvent
getFace, getHand, getItemStack, getLevel, getPos, getSide
Methods inherited from class net.neoforged.neoforge.event.entity.player.PlayerEvent
getEntity
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.neoforged.bus.api.ICancellableEvent
isCanceled, setCanceled
-
Field Details
-
context
-
usePhase
-
cancellationResult
-
-
Constructor Details
-
UseItemOnBlockEvent
-
-
Method Details
-
getUseOnContext
- Returns:
- context
-
getUsePhase
Returns The Use Phase of the interaction.- Returns:
- The Use Phase of the interaction
- See Also:
-
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 logicItemInteractionResult.SUCCESS
,ItemInteractionResult.CONSUME
,ItemInteractionResult.CONSUME_PARTIAL
, and FAIL will prevent further types of interaction attempts when provided from the ITEM_BEFORE_BLOCK phase.ItemInteractionResult.SUCCESS
,ItemInteractionResult.CONSUME
, andItemInteractionResult.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.ItemInteractionResult.SUCCESS
will trigger the arm-swinging mechanic.ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION
will always allow proceeding to the next phase.ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION
will not call the block'suse method
-
getCancellationResult
- Returns:
- The
ItemInteractionResult
that will be returned to vanilla if the event is cancelled, instead of calling the relevant method of the event. By default, this isItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION
, meaning cancelled events will cause the client to keep trying more interactions until something works.
-
setCancellationResult
Set theItemInteractionResult
that will be returned to vanilla if the event is cancelled, instead of calling the relevant method of the event.
-