[CraftBukkit] Spawning NPC with armor

Discussion in 'Plugin Development' started by Freelix2000, Oct 5, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Freelix2000

    I'm a bit new to CraftBukkit API and packet sending stuffs, and I've been playing around with NPC spawning and I plan on eventually adding some NPC stuff to my Halo plugin. I have successfully followed a tutorial so that I can safely spawn an NPC with a name and skin and a few other stuff, but I'm a bit stuck on how to edit the NPC's armor. I would like to not only find a method for setting the NPC's armor, but I am also wondering if it is possible to convert Bukkit's ItemStack class to CraftBukkit in such a way that would allow me to set the NPC's armor identical to a player's. I am mostly looking for a method in the EntityPlayer class to accomplish this.
     
  2. Offline

    Fuzzybear04

    This would set a Skeletons armor, P being Player.

    Code:java
    1. Skeleton s = p.getWorld().spawn(p.getLocation(), Skeleton.class);
    2. s.getEquipment().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
    3. s.getEquipment().setChestplateDropChance(1);
     
  3. Offline

    MnMaxon

    Freelix2000
    Can you use
    Code:java
    1. npc.getBukkitEntity().getInventory().setArmorContents(player.getInventory().getArmorContents());

    I'm not completely sure how you made the npcs, but this works using NPCFactory
     
  4. Offline

    Freelix2000

    Fuzzybear04
    I understand Bukkit's API. I'm talking about CraftBukkit API and packet spawning NPC stuff.
    MnMaxon
    I tried that, but it didn't work. No crashes or errors, the entity just didn't appear to have armor.
     
  5. Offline

    MnMaxon

    Freelix2000
    I went back and looked at my code. I saw this line of code on my NPC.create() method:
    Code:java
    1. NPCLoop(npc, player.getArmorContents())


    It turns out I was looping this every 1.5 seconds;
    Code:java
    1.  
    2. public static void NpcLoop(final NPC npc, final ItemStack[] armor) {
    3. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    4. @Override
    5. public void run() {
    6. if (npc.getBukkitEntity().isValid()) {
    7. npc.setEquipment(EquipmentSlot.BOOTS, armor[0]);
    8. npc.setEquipment(EquipmentSlot.LEGGINGS, armor[1]);
    9. npc.setEquipment(EquipmentSlot.CHESTPLATE, armor[2]);
    10. npc.setEquipment(EquipmentSlot.HELMET, armor[3]);
    11. NpcLoop(npc, armor);
    12. }
    13. }
    14. }, 30L);
    15. }

    Honestly, I don't remember why I was doing this, but I might have been running into your problem. Again, this might only work with NPCFactory. I hope this helps.
     
  6. Offline

    Fuzzybear04

    Ah, Sorry, Didn't see the "CraftBukkit" part ;)
     
  7. Offline

    Freelix2000

    MnMaxon
    Okay, but I don't use an NPC object in my code, here's what I have:
    Code:
    CraftPlayer p1 = (CraftPlayer) p;
                GameProfile gp = new GameProfile(UUID.randomUUID(), p.getName());
                EntityPlayer handle = p1.getHandle();
                final EntityPlayer h = new EntityPlayer(handle.server, handle.server.getWorldServer(0), gp, new PlayerInteractManager(handle.world));
                h.setHealth(0);
                h.setLocation(p1.getLocation().getX(), p1.getLocation().getY(), p1.getLocation().getZ(), p1.getLocation().getPitch(), p1.getLocation().getYaw());
                PacketPlayOutNamedEntitySpawn npc = new PacketPlayOutNamedEntitySpawn(h);
                try{
                    Field field = npc.getClass().getDeclaredField("a");
                    field.setAccessible(true);
                    field.setInt(npc, 123);
                    field.setAccessible(!field.isAccessible());
                }catch(Exception x){
                    x.printStackTrace();
                }
                p1.getHandle().playerConnection.sendPacket(npc);
                return true;
     
Thread Status:
Not open for further replies.

Share This Page