From f8758d2933eb9ff4681fe650f9538a6c2e595ff9 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Sun, 31 May 2020 16:14:27 +0200 Subject: [PATCH] Added LivingEntity#swingMainHand and LivingEntity#swingOffHand --- .../minestom/server/entity/LivingEntity.java | 22 +++++++++++++++++++ .../server/listener/AnimationListener.java | 15 ++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index a60e01404..30b7d6c69 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -414,6 +414,28 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { this.expandedBoundingBox = getBoundingBox().expand(1, 0.5f, 1); } + /** + * Send a {@link EntityAnimationPacket} to swing the main hand + * (can be used for attack animation) + */ + public void swingMainHand() { + EntityAnimationPacket animationPacket = new EntityAnimationPacket(); + animationPacket.entityId = getEntityId(); + animationPacket.animation = EntityAnimationPacket.Animation.SWING_MAIN_ARM; + sendPacketToViewers(animationPacket); + } + + /** + * Send a {@link EntityAnimationPacket} to swing the off hand + * (can be used for attack animation) + */ + public void swingOffHand() { + EntityAnimationPacket animationPacket = new EntityAnimationPacket(); + animationPacket.entityId = getEntityId(); + animationPacket.animation = EntityAnimationPacket.Animation.SWING_OFF_HAND; + sendPacketToViewers(animationPacket); + } + public void refreshActiveHand(boolean isHandActive, boolean offHand, boolean riptideSpinAttack) { this.isHandActive = isHandActive; this.offHand = offHand; diff --git a/src/main/java/net/minestom/server/listener/AnimationListener.java b/src/main/java/net/minestom/server/listener/AnimationListener.java index 3fe97240c..8e56eb321 100644 --- a/src/main/java/net/minestom/server/listener/AnimationListener.java +++ b/src/main/java/net/minestom/server/listener/AnimationListener.java @@ -3,18 +3,21 @@ package net.minestom.server.listener; import net.minestom.server.entity.Player; import net.minestom.server.event.animation.AnimationEvent; import net.minestom.server.network.packet.client.play.ClientAnimationPacket; -import net.minestom.server.network.packet.server.play.EntityAnimationPacket; public class AnimationListener { public static void animationListener(ClientAnimationPacket packet, Player player) { AnimationEvent animationEvent = new AnimationEvent(player, packet.hand); player.callCancellableEvent(AnimationEvent.class, animationEvent, () -> { - EntityAnimationPacket entityAnimationPacket = new EntityAnimationPacket(); - entityAnimationPacket.entityId = player.getEntityId(); - entityAnimationPacket.animation = animationEvent.getHand() == Player.Hand.MAIN ? - EntityAnimationPacket.Animation.SWING_MAIN_ARM : EntityAnimationPacket.Animation.SWING_OFF_HAND; - player.sendPacketToViewers(entityAnimationPacket); + Player.Hand hand = animationEvent.getHand(); + switch (hand) { + case MAIN: + player.swingMainHand(); + break; + case OFF: + player.swingOffHand(); + break; + } }); }