Solved Adding armor to NPCs

Discussion in 'Spigot Plugin Development' started by Rapturr, Jul 1, 2021.

  1. Hello, im trying to add armor to my NPCs but it isnt working, heres what I tried:

    NMS:
    Code (Text):
    public void setEquipmentNPC(List<Pair<EnumItemSlot, ItemStack>> equipment) {
            for (Player p : Bukkit.getOnlinePlayers()) {
                PlayerConnection connection = Utilities.getPlayerConnection(p);

                connection.sendPacket(new PacketPlayOutEntityEquipment(getNpcID(), equipment));
            }
        }
    ProtocolLib:
    Code (Text):
    public void setEquipment(List<Pair<EnumItemSlot, ItemStack>> equipment) {
            ProtocolManager pm = ProtocolLibrary.getProtocolManager();
            PacketContainer packet = pm.createPacket(PacketType.Play.Server.ENTITY_EQUIPMENT);
            packet.getModifier().writeDefaults();
            packet.getModifier().write(0, getNpcID()).write(1, equipment);

            for (Player p : Bukkit.getOnlinePlayers()) {
                try {
                    pm.sendServerPacket(p, packet);
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }

        }
    Then Ive put the method in the spawnMethod of the NPC:

    Code (Text):
     public void spawnAINPC(Location loc, List<Pair<EnumItemSlot, ItemStack>> equipments) {
            WorldServer nmsWorld = ((CraftWorld) world).getHandle();
            nmsWorld.addEntity(mob);

            npc.setEquipment(equipments);
            npc.setLocation(loc);
            npc.addNPCPacket();

            Cooldown hitDelay = new Cooldown();
            currentHealth = mob.getHealth();
    }
     
  2. as far as I remember, you must first add an NPC, then set the location and only then set the armor
     
  3. Ill give that a try
     
  4. Yup, you have to set the equipment of a NPC after the addNPCPacket (EntitySpawn, HeadRotation, etc.). Thx alot!

    https://i.imgur.com/2wcaZ6D.png

    CODE:

    Code (Text):
    public void spawnAINPC(Location loc, List<Pair<EnumItemSlot, Itemstack>> equipments) {

    npc.setlocation(loc);
    npc.addNPCPacket();
    npc.setEquipment(equipments);

    }