Solved Spawn ARMOR STAND with ProtocolLib packets

Discussion in 'Spigot Plugin Development' started by connection_lost, Apr 21, 2017.

  1. ** This question is solved. It is mainly because newer version of Minecraft has changed how spawning armor stands works. **

    If you are here through a forum or Google search:
    1. The question contains code that works for old Minecraft versions below 1.11.
    2. The answer contains code that works for Minecraft versions 1.12 or above. Please see the first reply.
    3. ArmorStand is a treated LivingEntity at server level. However it is treated as Entity at protocol level. Resulting in packet type as SPAWN_ENTITY instead of SPAWN_ENTITY_LIVING.

    ---------------------------------------------------

    I am currently looking for a way to spawn an armorstand that relies on ProtocolLib instead of NMS. Currently my code is:
    Code (Text):
    ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
    PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.SPAWN_ENTITY_LIVING);
    // Entity ID
    packet.getIntegers().write(0, somerandomentityid);
    // Entity Type
    packet.getIntegers().write(1, 78);
    // Set location
    packet.getIntegers().write(2, (int)(location.getX() * 32D));
    packet.getIntegers().write(3, (int)(location.getY() * 32D));
    packet.getIntegers().write(4, (int)(location.getZ() * 32D));
    // Set watcher - ↓ removing that line does not do anything.
    packet.getDataWatcherModifier().write(0, WrappedDataWatcher());
    protocolManager.sendServerPacket(player, packet);
    For the DataWatcher, I also tried using a cloned data by using this but doesn't help.
    Code (Text):
    World world = Bukkit.getWorld("world");
    Entity entity = world.spawnEntity(new Location(world, 0, 256, 0), EntityType.ARMOR_STAND);
    armorstandwatcher = WrappedDataWatcher.getEntityWatcher(entity).deepClone();
    entity.remove();
    Result is nothing happens. I have looked through the internet and sadly cannot find anything useful. Thoughts?
     
    #1 connection_lost, Apr 21, 2017
    Last edited: Jan 28, 2021
  2. This is the solution of creating an armorstand with ProtocolLib.

    Code (Text):
            PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.SPAWN_ENTITY);

            // Entity ID
            packet.getIntegers().write(0, entityid);
            // Entity Type
            packet.getIntegers().write(6, 78);
            // Set optional velocity (/8000)
            packet.getIntegers().write(1, 0);
            packet.getIntegers().write(2, 0);
            packet.getIntegers().write(3, 0);
            // Set yaw pitch
            packet.getIntegers().write(4, 0);
            packet.getIntegers().write(5, 0);
            // Set object data
            packet.getIntegers().write(7, 0);
            // Set location
            packet.getDoubles().write(0, location.getX());
            packet.getDoubles().write(1, location.getY());
            packet.getDoubles().write(2, location.getZ());
            // Set UUID
            packet.getUUIDs().write(0, UUID.randomUUID());
    EDIT as of year 2021:
    Up to a certain version of Minecraft and ProtocolLib (somewhere around 1.13 thru 1.15, unsure which exact version), the packet format has changed. Currently, entity type has to be:
    Code (Java):

            // Entity Type
            packet.getEntityTypeModifier().write(0, EntityType.ARMOR_STAND);
     
    #2 connection_lost, Apr 21, 2017
    Last edited: Jan 28, 2021
    • Useful Useful x 4
    • Agree Agree x 1