Added LivingEntity#swingMainHand and LivingEntity#swingOffHand

This commit is contained in:
Felix Cravic 2020-05-31 16:14:27 +02:00
parent eed946e948
commit f8758d2933
2 changed files with 31 additions and 6 deletions

View File

@ -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;

View File

@ -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;
}
});
}