Search code examples
javaminecraftbukkitspigot

How can I send Animation-Packet with ProtocolLib in Spigot?


I want to show a damage animation on a entity without damage. NOT:

entity.damage(2);

and then

public void onEntityDamage(EntityDamageEvent event) {
    event.setDamage(0);

more like that:

entity.setHealth(entity.getHealth - 2);
"Send a packet to Player Damage Animation IF POSSIBLE WITH ProtocolLib"

Solution

  • The method Player.damage method to make everything as "normal".

    If you want to send your own packet, yous should use the PacketPlayOutAnimation, with the value 1 (Why?)

    For 1.16 and lower:

    import net.minecraft.server.MC_VERSION.PacketPlayOutAnimation;
    
    EntityPlayer ep = ((CraftPlayer) p).getHandle();
    ep.playerConnection.sendPacket(new PacketPlayOutAnimation(ep, 1));
    

    For 1.17 and more:

    import net.minecraft.network.protocol.game.PacketPlayOutAnimation;
    
    EntityPlayer ep = ((CraftPlayer) p).getHandle();
    ep.b.sendPacket(new PacketPlayOutAnimation(ep, 1));
    

    Here is a version with import of direct minecraft version. You can use reflection to use those NMS for all versions.

    Or with ProtocolLib, it seems to be something like that:

    PacketContainer packetContainer = protocolManager.createPacket(Play.Server.ANIMATION);
    packetContainer.getIntegers().write(0, entityId);
    packetContainer.getIntegers().write(0, 1);
    manager.sendServerPacket(p, packetContainer); // seems to be how you send packet
    

    More informations about the packet in ProtocolLib here