Search in sources :

Example 1 with ClipContext

use of net.minecraft.world.level.ClipContext in project SpongeCommon by SpongePowered.

the class ServerGamePacketListenerImplMixin method impl$throwAnimationAndInteractEvents.

@SuppressWarnings("ConstantConditions")
@Inject(method = "handleAnimate", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;resetLastActionTime()V"), cancellable = true)
private void impl$throwAnimationAndInteractEvents(final ServerboundSwingPacket packetIn, final CallbackInfo ci) {
    if (PhaseTracker.getInstance().getPhaseContext().isEmpty()) {
        return;
    }
    final InteractionHand hand = packetIn.getHand();
    if (!((ServerPlayerGameModeAccessor) this.player.gameMode).accessor$isDestroyingBlock()) {
        if (this.impl$ignorePackets > 0) {
            this.impl$ignorePackets--;
        } else {
            if (ShouldFire.INTERACT_ITEM_EVENT_PRIMARY) {
                final Vec3 startPos = this.player.getEyePosition(1);
                // TODO hook for blockReachDistance?
                final Vec3 endPos = startPos.add(this.player.getLookAngle().scale(5d));
                HitResult result = this.player.getLevel().clip(new ClipContext(startPos, endPos, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, this.player));
                if (result.getType() == HitResult.Type.MISS) {
                    final ItemStack heldItem = this.player.getItemInHand(hand);
                    SpongeCommonEventFactory.callInteractItemEventPrimary(this.player, heldItem, hand);
                }
            }
        }
    }
    if (ShouldFire.ANIMATE_HAND_EVENT) {
        final HandType handType = (HandType) (Object) hand;
        final ItemStack heldItem = this.player.getItemInHand(hand);
        try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
            frame.addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(heldItem));
            frame.addContext(EventContextKeys.USED_HAND, handType);
            final AnimateHandEvent event = SpongeEventFactory.createAnimateHandEvent(frame.currentCause(), handType, (Humanoid) this.player);
            if (SpongeCommon.post(event)) {
                ci.cancel();
            }
        }
    }
}
Also used : HitResult(net.minecraft.world.phys.HitResult) ClipContext(net.minecraft.world.level.ClipContext) HandType(org.spongepowered.api.data.type.HandType) CauseStackManager(org.spongepowered.api.event.CauseStackManager) InteractionHand(net.minecraft.world.InteractionHand) Vec3(net.minecraft.world.phys.Vec3) AnimateHandEvent(org.spongepowered.api.event.entity.living.AnimateHandEvent) ItemStack(net.minecraft.world.item.ItemStack) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

InteractionHand (net.minecraft.world.InteractionHand)1 ItemStack (net.minecraft.world.item.ItemStack)1 ClipContext (net.minecraft.world.level.ClipContext)1 HitResult (net.minecraft.world.phys.HitResult)1 Vec3 (net.minecraft.world.phys.Vec3)1 HandType (org.spongepowered.api.data.type.HandType)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 AnimateHandEvent (org.spongepowered.api.event.entity.living.AnimateHandEvent)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1