Search in sources :

Example 1 with MessagePlayOutEntityAnimation

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityAnimation in project LanternServer by LanternPowered.

the class PlayerInteractionHandler method handleItemInteraction.

public void handleItemInteraction(MessagePlayInPlayerUseItem message) {
    // Prevent duplicate messages
    final long time = System.currentTimeMillis();
    if (this.lastInteractionTime != -1L && time - this.lastInteractionTime < 40) {
        return;
    }
    this.lastInteractionTime = time;
    final CauseStack causeStack = CauseStack.current();
    try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
        final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
        frame.pushCause(this.player);
        frame.addContext(ContextKeys.PLAYER, this.player);
        final BehaviorContext.Snapshot snapshot = context.pushSnapshot();
        if (!handleItemInteraction(context, snapshot)) {
            if (!this.player.get(LanternKeys.CAN_DUAL_WIELD).orElse(false)) {
                return;
            }
            final AbstractSlot offHandSlot = this.player.getInventory().getOffhand();
            final Optional<ItemStack> handItem = offHandSlot.peek();
            if (handItem.isPresent()) {
                final DualWieldProperty property = handItem.get().getProperty(DualWieldProperty.class).orElse(null);
                // noinspection ConstantConditions
                if (property != null && property.getValue()) {
                    /*
                    final Vector3d position = this.player.getPosition().add(0, this.player.get(Keys.IS_SNEAKING).get() ? 1.54 : 1.62, 0);
                    final Optional<BlockRayHit<LanternWorld>> hit = BlockRay.from(this.player.getWorld(), position)
                            .direction(this.player.getDirectionVector())
                            .distanceLimit(5)
                            // Is this supposed to be inverted?
                            .skipFilter(Predicates.not(BlockRay.onlyAirFilter()))
                            .build()
                            .end();
                    if (hit.isPresent() && hit.get().getLocation().getBlock().getType() != BlockTypes.AIR) {
                        return;
                    }
                    */
                    this.player.getConnection().send(new MessagePlayOutEntityAnimation(this.player.getNetworkId(), 3));
                    this.player.triggerEvent(SwingHandEntityEvent.of(HandTypes.OFF_HAND));
                /*
                    final CooldownTracker cooldownTracker = this.player.getCooldownTracker();
                    cooldownTracker.set(handItem.get().getType(), 15);
                    */
                }
            }
        }
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) BehaviorContextImpl(org.lanternpowered.server.behavior.BehaviorContextImpl) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) MessagePlayOutEntityAnimation(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityAnimation) BehaviorContext(org.lanternpowered.server.behavior.BehaviorContext) ItemStack(org.spongepowered.api.item.inventory.ItemStack) DualWieldProperty(org.lanternpowered.server.item.property.DualWieldProperty)

Aggregations

BehaviorContext (org.lanternpowered.server.behavior.BehaviorContext)1 BehaviorContextImpl (org.lanternpowered.server.behavior.BehaviorContextImpl)1 CauseStack (org.lanternpowered.server.event.CauseStack)1 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)1 DualWieldProperty (org.lanternpowered.server.item.property.DualWieldProperty)1 MessagePlayOutEntityAnimation (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityAnimation)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1