Search in sources :

Example 1 with HandType

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

the class CodecPlayInUseEntity method decode.

@Override
public MessagePlayInUseEntity decode(CodecContext context, ByteBuffer buf) throws CodecException {
    final int entityId = buf.readVarInt();
    final int action = buf.readVarInt();
    if (action == 1) {
        return new MessagePlayInUseEntity.Attack(entityId);
    } else if (action == 0 || action == 2) {
        Vector3d position = null;
        if (action == 2) {
            final double x = buf.readFloat();
            final double y = buf.readFloat();
            final double z = buf.readFloat();
            position = new Vector3d(x, y, z);
        }
        final HandType hand = buf.readVarInt() == 0 ? HandTypes.MAIN_HAND : HandTypes.OFF_HAND;
        return new MessagePlayInUseEntity.Interact(entityId, hand, position);
    } else {
        throw new DecoderException("Received a UseEntity message with a unknown action: " + action);
    }
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) Vector3d(com.flowpowered.math.vector.Vector3d) HandType(org.spongepowered.api.data.type.HandType) MessagePlayInUseEntity(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInUseEntity)

Example 2 with HandType

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

the class CodecPlayInPlayerBlockPlacement method decode.

@Override
public MessagePlayInPlayerBlockPlacement decode(CodecContext context, ByteBuffer buf) throws CodecException {
    final Vector3i position = buf.read(Types.VECTOR_3_I);
    final Direction face = fromFace(buf.readVarInt());
    final HandType hand = buf.readVarInt() == 0 ? HandTypes.MAIN_HAND : HandTypes.OFF_HAND;
    final double ox = buf.readFloat();
    final double oy = buf.readFloat();
    final double oz = buf.readFloat();
    final Vector3d offset = new Vector3d(ox, oy, oz);
    return new MessagePlayInPlayerBlockPlacement(position, offset, face, hand);
}
Also used : Vector3d(com.flowpowered.math.vector.Vector3d) HandType(org.spongepowered.api.data.type.HandType) MessagePlayInPlayerBlockPlacement(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerBlockPlacement) Vector3i(com.flowpowered.math.vector.Vector3i) Direction(org.spongepowered.api.util.Direction)

Example 3 with HandType

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

the class PlayerInteractionHandler method handleFinishItemInteraction.

public void handleFinishItemInteraction(MessagePlayInOutFinishUsingItem message) {
    final Optional<HandType> activeHand = this.player.get(LanternKeys.ACTIVE_HAND).orElse(Optional.empty());
    // The player is already interacting
    if (!activeHand.isPresent() || this.activeHandStartTime == -1L) {
        return;
    }
    // Try the action of the hotbar item first
    final AbstractSlot slot = activeHand.get() == HandTypes.MAIN_HAND ? this.player.getInventory().getHotbar().getSelectedSlot() : this.player.getInventory().getOffhand();
    final ItemStack rawItemStack = slot.getRawItemStack();
    if (rawItemStack == null) {
        return;
    }
    // Require a minimum amount of ticks for the interaction to succeed
    final MinimumUseDurationProperty property = rawItemStack.getProperty(MinimumUseDurationProperty.class).orElse(null);
    if (property != null) {
        final long time = LanternGame.currentTimeTicks();
        if (time - this.activeHandStartTime < property.getValue()) {
            resetItemUseTime();
            return;
        }
    }
    handleFinishItemInteraction0(slot, activeHand.get());
}
Also used : AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) HandType(org.spongepowered.api.data.type.HandType) MinimumUseDurationProperty(org.lanternpowered.server.item.property.MinimumUseDurationProperty) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 4 with HandType

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

the class PlayerInteractionHandler method handleHandItemInteraction.

private boolean handleHandItemInteraction(BehaviorContextImpl context, HandType handType, AbstractSlot slot, @Nullable BehaviorContext.Snapshot snapshot) {
    final Optional<HandType> activeHand = this.player.get(LanternKeys.ACTIVE_HAND).orElse(Optional.empty());
    // The player is already interacting
    if (activeHand.isPresent()) {
        return true;
    }
    final Optional<ItemStack> handItem = slot.peek();
    if (handItem.isPresent()) {
        final LanternItemType itemType = (LanternItemType) handItem.get().getType();
        context.addContext(ContextKeys.USED_ITEM_STACK, handItem.get());
        context.addContext(ContextKeys.USED_SLOT, slot);
        context.addContext(ContextKeys.INTERACTION_HAND, handType);
        context.addContext(ContextKeys.ITEM_TYPE, itemType);
        final BehaviorResult result = context.process(itemType.getPipeline().pipeline(InteractWithItemBehavior.class), (ctx, behavior) -> behavior.tryInteract(itemType.getPipeline(), ctx));
        if (result.isSuccess()) {
            return true;
        }
        if (snapshot != null) {
            context.popSnapshot(snapshot);
        }
    }
    return false;
}
Also used : InteractWithItemBehavior(org.lanternpowered.server.item.behavior.types.InteractWithItemBehavior) HandType(org.spongepowered.api.data.type.HandType) BehaviorResult(org.lanternpowered.server.behavior.BehaviorResult) LanternItemType(org.lanternpowered.server.item.LanternItemType) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 5 with HandType

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

the class PlayerInteractionHandler method pulse.

/**
 * Pulses the interaction handler.
 */
void pulse() {
    if (this.diggingBlock != null) {
        final int breakState = (int) Math.round(((double) Math.max(0, this.diggingEndTime - System.nanoTime()) / (double) this.diggingDuration) * 10.0);
        if (this.lastBreakState != breakState) {
            sendBreakUpdate(breakState);
            this.lastBreakState = breakState;
        }
    }
    final HandType activeHand = this.player.get(LanternKeys.ACTIVE_HAND).orElse(Optional.empty()).orElse(null);
    final AbstractSlot slot = activeHand == null ? null : activeHand == HandTypes.MAIN_HAND ? this.player.getInventory().getHotbar().getSelectedSlot() : this.player.getInventory().getOffhand();
    // The interaction just started
    if (!Objects.equals(activeHand, this.lastActiveHand)) {
        this.lastActiveHand = activeHand;
        this.lastActiveItemStack = slot == null ? null : slot.getRawItemStack();
    } else if (activeHand != null) {
        if (this.activeHandStartTime == -1L) {
            this.activeHandStartTime = LanternGame.currentTimeTicks();
        }
        final ItemStack itemStack = slot.getRawItemStack();
        if (itemStack == null || this.lastActiveItemStack != itemStack) {
            // Stop the interaction
            resetItemUseTime();
        } else {
            final MaximumUseDurationProperty property = itemStack.getProperty(MaximumUseDurationProperty.class).orElse(null);
            if (property != null) {
                // Check if the interaction reached it's max time
                final long time = LanternGame.currentTimeTicks();
                if (time - this.activeHandStartTime > property.getValue()) {
                    handleFinishItemInteraction0(slot, activeHand);
                }
            }
        }
    }
}
Also used : MaximumUseDurationProperty(org.lanternpowered.server.item.property.MaximumUseDurationProperty) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) HandType(org.spongepowered.api.data.type.HandType) 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