Search in sources :

Example 1 with UseItemStackEvent

use of org.spongepowered.api.event.item.inventory.UseItemStackEvent in project SpongeCommon by SpongePowered.

the class LivingEntityMixin method impl$onSetActiveItemStack.

// Data delegated methods
// Start implementation of UseItemstackEvent
@Inject(method = "startUsingItem", cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD, at = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/LivingEntity;useItem:Lnet/minecraft/world/item/ItemStack;"))
private void impl$onSetActiveItemStack(final InteractionHand hand, final CallbackInfo ci, final ItemStack stack) {
    if (this.level.isClientSide) {
        return;
    }
    final UseItemStackEvent.Start event;
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(stack);
        final HandType handType = (HandType) (Object) hand;
        this.impl$addSelfToFrame(frame, snapshot, handType);
        final Ticks useDuration = Ticks.of(stack.getUseDuration());
        event = SpongeEventFactory.createUseItemStackEventStart(PhaseTracker.getCauseStackManager().currentCause(), useDuration, useDuration, snapshot);
    }
    if (SpongeCommon.post(event)) {
        ci.cancel();
    } else {
        this.useItemRemaining = (int) event.remainingDuration().ticks();
    }
}
Also used : UseItemStackEvent(org.spongepowered.api.event.item.inventory.UseItemStackEvent) CauseStackManager(org.spongepowered.api.event.CauseStackManager) HandType(org.spongepowered.api.data.type.HandType) Ticks(org.spongepowered.api.util.Ticks) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

HandType (org.spongepowered.api.data.type.HandType)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 UseItemStackEvent (org.spongepowered.api.event.item.inventory.UseItemStackEvent)1 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)1 Ticks (org.spongepowered.api.util.Ticks)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1