Search in sources :

Example 6 with HandType

use of org.spongepowered.api.data.type.HandType in project SpongeCommon by SpongePowered.

the class PacketUtil method firePreEvents.

private static boolean firePreEvents(Packet<?> packetIn, EntityPlayerMP playerMP) {
    if (packetIn instanceof CPacketAnimation) {
        CPacketAnimation packet = (CPacketAnimation) packetIn;
        SpongeCommonEventFactory.lastAnimationPacketTick = SpongeImpl.getServer().getTickCounter();
        SpongeCommonEventFactory.lastAnimationPlayer = new WeakReference<>(playerMP);
        HandType handType = packet.getHand() == EnumHand.MAIN_HAND ? HandTypes.MAIN_HAND : HandTypes.OFF_HAND;
        final ItemStack heldItem = playerMP.getHeldItem(packet.getHand());
        Sponge.getCauseStackManager().addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(heldItem));
        AnimateHandEvent event = SpongeEventFactory.createAnimateHandEvent(Sponge.getCauseStackManager().getCurrentCause(), handType, (Humanoid) playerMP);
        if (SpongeImpl.postEvent(event)) {
            return true;
        }
        return false;
    } else if (packetIn instanceof CPacketPlayerDigging) {
        SpongeCommonEventFactory.lastPrimaryPacketTick = SpongeImpl.getServer().getTickCounter();
        CPacketPlayerDigging packet = (CPacketPlayerDigging) packetIn;
        ItemStack stack = playerMP.getHeldItemMainhand();
        Sponge.getCauseStackManager().addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(stack));
        switch(packet.getAction()) {
            case DROP_ITEM:
            case DROP_ALL_ITEMS:
                if (!stack.isEmpty() && !playerMP.isSpectator()) {
                    ((IMixinEntityPlayerMP) playerMP).setPacketItem(stack.copy());
                }
                return false;
            case START_DESTROY_BLOCK:
            case ABORT_DESTROY_BLOCK:
            case STOP_DESTROY_BLOCK:
                final BlockPos pos = packet.getPosition();
                final Vector3d interactionPoint = VecHelper.toVector3d(pos);
                final BlockSnapshot blockSnapshot = new Location<>((World) playerMP.world, interactionPoint).createSnapshot();
                final RayTraceResult result = SpongeImplHooks.rayTraceEyes(playerMP, SpongeImplHooks.getBlockReachDistance(playerMP));
                if (SpongeCommonEventFactory.callInteractItemEventPrimary(playerMP, stack, EnumHand.MAIN_HAND, result == null ? null : VecHelper.toVector3d(result.hitVec), blockSnapshot).isCancelled()) {
                    ((IMixinEntityPlayerMP) playerMP).sendBlockChange(pos, playerMP.world.getBlockState(pos));
                    return true;
                }
                double d0 = playerMP.posX - ((double) pos.getX() + 0.5D);
                double d1 = playerMP.posY - ((double) pos.getY() + 0.5D) + 1.5D;
                double d2 = playerMP.posZ - ((double) pos.getZ() + 0.5D);
                double d3 = d0 * d0 + d1 * d1 + d2 * d2;
                double dist = SpongeImplHooks.getBlockReachDistance(playerMP) + 1;
                dist *= dist;
                if (d3 > dist) {
                    return true;
                } else if (pos.getY() >= SpongeImpl.getServer().getBuildLimit()) {
                    return true;
                }
                if (packet.getAction() == CPacketPlayerDigging.Action.START_DESTROY_BLOCK) {
                    if (SpongeCommonEventFactory.callInteractBlockEventPrimary(playerMP, blockSnapshot, EnumHand.MAIN_HAND, packet.getFacing(), result == null ? null : VecHelper.toVector3d(result.hitVec)).isCancelled()) {
                        ((IMixinEntityPlayerMP) playerMP).sendBlockChange(pos, playerMP.world.getBlockState(pos));
                        return true;
                    }
                }
                return false;
            default:
                break;
        }
    } else if (packetIn instanceof CPacketPlayerTryUseItem) {
        CPacketPlayerTryUseItem packet = (CPacketPlayerTryUseItem) packetIn;
        SpongeCommonEventFactory.lastSecondaryPacketTick = SpongeImpl.getServer().getTickCounter();
        long packetDiff = System.currentTimeMillis() - lastTryBlockPacketTimeStamp;
        // If the time between packets is small enough, use the last result.
        if (packetDiff < 100) {
            // Use previous result and avoid firing a second event
            return lastTryBlockPacketItemResult;
        }
        final ItemStack heldItem = playerMP.getHeldItem(packet.getHand());
        Sponge.getCauseStackManager().addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(heldItem));
        final RayTraceResult result = SpongeImplHooks.rayTraceEyes(playerMP, SpongeImplHooks.getBlockReachDistance(playerMP));
        final boolean isCancelled = SpongeCommonEventFactory.callInteractItemEventSecondary(playerMP, heldItem, packet.getHand(), result == null ? null : VecHelper.toVector3d(result.hitVec), BlockSnapshot.NONE).isCancelled();
        SpongeCommonEventFactory.callInteractBlockEventSecondary(playerMP, heldItem, result == null ? null : VecHelper.toVector3d(result.hitVec), BlockSnapshot.NONE, Direction.NONE, packet.getHand());
        if (isCancelled) {
            // Multiple slots may have been changed on the client. Right
            // clicking armor is one example - the client changes it
            // without the server telling it to.
            playerMP.sendAllContents(playerMP.openContainer, playerMP.openContainer.getInventory());
            return true;
        }
    } else if (packetIn instanceof CPacketPlayerTryUseItemOnBlock) {
        CPacketPlayerTryUseItemOnBlock packet = (CPacketPlayerTryUseItemOnBlock) packetIn;
        lastTryBlockPacketTimeStamp = System.currentTimeMillis();
        SpongeCommonEventFactory.lastSecondaryPacketTick = SpongeImpl.getServer().getTickCounter();
        Vector3d interactionPoint = VecHelper.toVector3d(packet.getPos());
        final RayTraceResult result = SpongeImplHooks.rayTraceEyes(playerMP, SpongeImplHooks.getBlockReachDistance(playerMP));
        BlockSnapshot blockSnapshot = new Location<>((World) playerMP.world, interactionPoint).createSnapshot();
        final ItemStack heldItem = playerMP.getHeldItem(packet.getHand());
        Sponge.getCauseStackManager().addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(heldItem));
        boolean isCancelled = SpongeCommonEventFactory.callInteractItemEventSecondary(playerMP, heldItem, packet.getHand(), result == null ? null : VecHelper.toVector3d(result.hitVec), blockSnapshot).isCancelled();
        lastTryBlockPacketItemResult = isCancelled;
        if (isCancelled) {
            // update client
            BlockPos pos = packet.getPos();
            playerMP.connection.sendPacket(new SPacketBlockChange(playerMP.world, pos));
            playerMP.connection.sendPacket(new SPacketBlockChange(playerMP.world, pos.offset(packet.getDirection())));
            // See above
            playerMP.sendAllContents(playerMP.openContainer, playerMP.openContainer.getInventory());
            return true;
        }
    }
    return false;
}
Also used : CPacketPlayerDigging(net.minecraft.network.play.client.CPacketPlayerDigging) HandType(org.spongepowered.api.data.type.HandType) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) RayTraceResult(net.minecraft.util.math.RayTraceResult) AnimateHandEvent(org.spongepowered.api.event.entity.living.humanoid.AnimateHandEvent) CPacketAnimation(net.minecraft.network.play.client.CPacketAnimation) CPacketPlayerTryUseItemOnBlock(net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock) SPacketBlockChange(net.minecraft.network.play.server.SPacketBlockChange) CPacketPlayerTryUseItem(net.minecraft.network.play.client.CPacketPlayerTryUseItem) Vector3d(com.flowpowered.math.vector.Vector3d) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) Location(org.spongepowered.api.world.Location)

Example 7 with HandType

use of org.spongepowered.api.data.type.HandType in project LanternServer by LanternPowered.

the class PlayerInteractionHandler method handleBlockPlacing.

public void handleBlockPlacing(MessagePlayInPlayerBlockPlacement message) {
    final HandType handType = message.getHandType();
    // the main hand message.
    if (handType == HandTypes.OFF_HAND) {
        return;
    }
    // Try the action of the hotbar item first
    final AbstractSlot hotbarSlot = this.player.getInventory().getHotbar().getSelectedSlot();
    final AbstractSlot offHandSlot = this.player.getInventory().getOffhand();
    // The offset can round up to 1, causing
    // an incorrect clicked block location
    final Vector3d pos2 = message.getClickOffset();
    final double dx = Math.min(pos2.getX(), 0.999);
    final double dy = Math.min(pos2.getY(), 0.999);
    final double dz = Math.min(pos2.getZ(), 0.999);
    final Location<World> clickedLocation = new Location<>(this.player.getWorld(), message.getPosition().toDouble().add(dx, dy, dz));
    final Direction face = message.getFace();
    final CauseStack causeStack = CauseStack.current();
    try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
        frame.pushCause(this.player);
        // Add context
        frame.addContext(ContextKeys.INTERACTION_FACE, face);
        frame.addContext(ContextKeys.INTERACTION_LOCATION, clickedLocation);
        frame.addContext(ContextKeys.BLOCK_LOCATION, new Location<>(clickedLocation.getExtent(), message.getPosition()));
        frame.addContext(ContextKeys.PLAYER, this.player);
        final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
        final BehaviorContext.Snapshot snapshot = context.pushSnapshot();
        if (!this.player.get(Keys.IS_SNEAKING).orElse(false)) {
            final BlockState blockState = this.player.getWorld().getBlock(message.getPosition());
            final LanternBlockType blockType = (LanternBlockType) blockState.getType();
            frame.addContext(ContextKeys.BLOCK_TYPE, blockState.getType());
            frame.addContext(ContextKeys.USED_BLOCK_STATE, blockState);
            BehaviorContext.Snapshot snapshot1 = context.pushSnapshot();
            // Try first with the main hand
            hotbarSlot.peek().ifPresent(stack -> frame.addContext(ContextKeys.USED_ITEM_STACK, stack));
            frame.addContext(ContextKeys.USED_SLOT, hotbarSlot);
            frame.addContext(ContextKeys.INTERACTION_HAND, HandTypes.MAIN_HAND);
            BehaviorResult result = context.process(blockType.getPipeline().pipeline(InteractWithBlockBehavior.class), (ctx, behavior) -> behavior.tryInteract(blockType.getPipeline(), ctx));
            if (!result.isSuccess()) {
                context.popSnapshot(snapshot1);
                snapshot1 = context.pushSnapshot();
                // Try again with the off hand
                offHandSlot.peek().ifPresent(stack -> frame.addContext(ContextKeys.USED_ITEM_STACK, stack));
                frame.addContext(ContextKeys.USED_SLOT, offHandSlot);
                frame.addContext(ContextKeys.INTERACTION_HAND, HandTypes.OFF_HAND);
                result = context.process(blockType.getPipeline().pipeline(InteractWithBlockBehavior.class), (ctx, behavior) -> behavior.tryInteract(blockType.getPipeline(), ctx));
            }
            if (result.isSuccess()) {
                snapshot1 = context.pushSnapshot();
                // We can still continue, doing other operations
                if (result == BehaviorResult.CONTINUE) {
                    handleMainHandItemInteraction(context, snapshot1);
                }
                context.accept();
                return;
            }
            context.popSnapshot(snapshot1);
            snapshot1 = context.pushSnapshot();
            if (result.isSuccess()) {
                snapshot1 = context.pushSnapshot();
                // We can still continue, doing other operations
                if (result == BehaviorResult.CONTINUE) {
                    handleOffHandItemInteraction(context, snapshot1);
                }
                context.accept();
                return;
            }
            context.popSnapshot(snapshot1);
        }
        handleItemInteraction(context, snapshot);
    }
}
Also used : MessagePlayOutEntityAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityAnimation) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) MessagePlayInPlayerSwingArm(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerSwingArm) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) Vector3d(com.flowpowered.math.vector.Vector3d) MessagePlayInOutFinishUsingItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutFinishUsingItem) MessagePlayInPlayerDigging(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerDigging) HandType(org.spongepowered.api.data.type.HandType) MinimumUseDurationProperty(org.lanternpowered.server.item.property.MinimumUseDurationProperty) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) BreakBlockBehavior(org.lanternpowered.server.block.behavior.types.BreakBlockBehavior) ItemStack(org.spongepowered.api.item.inventory.ItemStack) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) CauseStack(org.lanternpowered.server.event.CauseStack) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) MessagePlayOutBlockBreakAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutBlockBreakAnimation) BehaviorResult(org.lanternpowered.server.behavior.BehaviorResult) LanternWorld(org.lanternpowered.server.world.LanternWorld) InteractWithBlockBehavior(org.lanternpowered.server.block.behavior.types.InteractWithBlockBehavior) Nullable(javax.annotation.Nullable) ContextKeys(org.lanternpowered.server.behavior.ContextKeys) FinishUsingItemBehavior(org.lanternpowered.server.item.behavior.types.FinishUsingItemBehavior) Location(org.spongepowered.api.world.Location) MaximumUseDurationProperty(org.lanternpowered.server.item.property.MaximumUseDurationProperty) LanternItemType(org.lanternpowered.server.item.LanternItemType) MessagePlayInPlayerUseItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerUseItem) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) BlockTypes(org.spongepowered.api.block.BlockTypes) SwingHandEntityEvent(org.lanternpowered.server.entity.event.SwingHandEntityEvent) Set(java.util.Set) LanternGame(org.lanternpowered.server.game.LanternGame) BlockState(org.spongepowered.api.block.BlockState) Objects(java.util.Objects) Direction(org.spongepowered.api.util.Direction) DualWieldProperty(org.lanternpowered.server.item.property.DualWieldProperty) Lantern(org.lanternpowered.server.game.Lantern) Vector3i(com.flowpowered.math.vector.Vector3i) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) MessagePlayInPlayerBlockPlacement(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerBlockPlacement) CauseStack(org.lanternpowered.server.event.CauseStack) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) HandType(org.spongepowered.api.data.type.HandType) LanternWorld(org.lanternpowered.server.world.LanternWorld) World(org.spongepowered.api.world.World) Direction(org.spongepowered.api.util.Direction) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) BlockState(org.spongepowered.api.block.BlockState) Vector3d(com.flowpowered.math.vector.Vector3d) BehaviorResult(org.lanternpowered.server.behavior.BehaviorResult) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) Location(org.spongepowered.api.world.Location) InteractWithBlockBehavior(org.lanternpowered.server.block.behavior.types.InteractWithBlockBehavior)

Example 8 with HandType

use of org.spongepowered.api.data.type.HandType in project LanternServer by LanternPowered.

the class PlayerInteractionHandler method handleFinishItemInteraction0.

private void handleFinishItemInteraction0(AbstractSlot slot, HandType handType) {
    final Optional<ItemStack> handItem = slot.peek();
    if (handItem.isPresent()) {
        final CauseStack causeStack = CauseStack.current();
        try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
            frame.pushCause(this.player);
            frame.addContext(ContextKeys.PLAYER, this.player);
            if (handItem.isPresent()) {
                final LanternItemType itemType = (LanternItemType) handItem.get().getType();
                frame.addContext(ContextKeys.USED_ITEM_STACK, handItem.get());
                frame.addContext(ContextKeys.USED_SLOT, slot);
                frame.addContext(ContextKeys.INTERACTION_HAND, handType);
                frame.addContext(ContextKeys.ITEM_TYPE, itemType);
                final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
                if (context.process(itemType.getPipeline().pipeline(FinishUsingItemBehavior.class), (ctx, behavior) -> behavior.tryUse(itemType.getPipeline(), ctx)).isSuccess()) {
                    context.accept();
                }
            }
        }
    }
    resetItemUseTime();
}
Also used : MessagePlayOutEntityAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityAnimation) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) MessagePlayInPlayerSwingArm(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerSwingArm) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) Vector3d(com.flowpowered.math.vector.Vector3d) MessagePlayInOutFinishUsingItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutFinishUsingItem) MessagePlayInPlayerDigging(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerDigging) HandType(org.spongepowered.api.data.type.HandType) MinimumUseDurationProperty(org.lanternpowered.server.item.property.MinimumUseDurationProperty) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) BreakBlockBehavior(org.lanternpowered.server.block.behavior.types.BreakBlockBehavior) ItemStack(org.spongepowered.api.item.inventory.ItemStack) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) CauseStack(org.lanternpowered.server.event.CauseStack) InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) MessagePlayOutBlockBreakAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutBlockBreakAnimation) BehaviorResult(org.lanternpowered.server.behavior.BehaviorResult) LanternWorld(org.lanternpowered.server.world.LanternWorld) InteractWithBlockBehavior(org.lanternpowered.server.block.behavior.types.InteractWithBlockBehavior) Nullable(javax.annotation.Nullable) ContextKeys(org.lanternpowered.server.behavior.ContextKeys) FinishUsingItemBehavior(org.lanternpowered.server.item.behavior.types.FinishUsingItemBehavior) Location(org.spongepowered.api.world.Location) MaximumUseDurationProperty(org.lanternpowered.server.item.property.MaximumUseDurationProperty) LanternItemType(org.lanternpowered.server.item.LanternItemType) MessagePlayInPlayerUseItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerUseItem) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) BlockTypes(org.spongepowered.api.block.BlockTypes) SwingHandEntityEvent(org.lanternpowered.server.entity.event.SwingHandEntityEvent) Set(java.util.Set) LanternGame(org.lanternpowered.server.game.LanternGame) BlockState(org.spongepowered.api.block.BlockState) Objects(java.util.Objects) Direction(org.spongepowered.api.util.Direction) DualWieldProperty(org.lanternpowered.server.item.property.DualWieldProperty) Lantern(org.lanternpowered.server.game.Lantern) Vector3i(com.flowpowered.math.vector.Vector3i) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) MessagePlayInPlayerBlockPlacement(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerBlockPlacement) CauseStack(org.lanternpowered.server.event.CauseStack) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) LanternItemType(org.lanternpowered.server.item.LanternItemType) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

HandType (org.spongepowered.api.data.type.HandType)8 Vector3d (com.flowpowered.math.vector.Vector3d)5 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)4 Vector3i (com.flowpowered.math.vector.Vector3i)3 BehaviorResult (org.lanternpowered.server.behavior.BehaviorResult)3 LanternItemType (org.lanternpowered.server.item.LanternItemType)3 InteractWithItemBehavior (org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior)3 MaximumUseDurationProperty (org.lanternpowered.server.item.property.MaximumUseDurationProperty)3 MinimumUseDurationProperty (org.lanternpowered.server.item.property.MinimumUseDurationProperty)3 MessagePlayInPlayerBlockPlacement (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerBlockPlacement)3 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 Location (org.spongepowered.api.world.Location)3 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Nullable (javax.annotation.Nullable)2 BehaviorContext (org.lanternpowered.server.behavior.BehaviorContext)2 BehaviorContextImpl (org.lanternpowered.server.behavior.BehaviorContextImpl)2 ContextKeys (org.lanternpowered.server.behavior.ContextKeys)2 LanternBlockType (org.lanternpowered.server.block.LanternBlockType)2