Solved [HELP] Giving mob (zombie) armour after spawn.

Discussion in 'Plugin Development' started by Steffion, Dec 4, 2012.

Thread Status:
Not open for further replies.
  1. Hello everyone,
    I need help with my plugin :)!

    What I want: I want to give a mob (zombie or something) armour after spawn.
    This is what I do have now:
    Code:java
    1. Entity zombie1 = player.getWorld().spawnEntity(player.getLocation(), EntityType.ZOMBIE);
    2. LivingEntity zombie2 = (LivingEntity) zombie1;


    (not much xd)

    I don't know how to do it at all.
    Please help me!
     
  2. Please search the fora. There should be at least like 10 (if not more) threads about this
     
    TwoPointDuck likes this.
  3. fireblast709

    I tried but I could not find it how to do this exactly.
     
  4. Steffion There are threads where they take ItemStack and LivingEntity as input of those methods
     
  5. The other threads make things fairly complicated. You can use one line of code to make an entity wear an item.
    Code:
    ((CraftLivingEntity) zombie2).getHandle().setEquipment(i, CraftItemStack.createNMSItemStack(item))
    'i' is where you want to put the item. If you put it as '0', it will put the item in the boots slot. '1' is for leggings, '2' is for chestplate, '3' is for helmet, and '4' is for hand.
     
  6. 0 is for hand, 1 for boots, 2 for leggings, 3 for chestplate and 4 for head
     
  7. kabbage lol nice profile pic :p Packets?
     
  8. Courtesy to Rprrr
    Code:java
    1. public static void setWeapon(LivingEntity mob, ItemStack item){
    2. CraftItemStack cis = new CraftItemStack(item);
    3. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    4. net.minecraft.server.ItemStack itemStack = cis.getHandle();
    5. ent.setEquipment(0, itemStack);
    6. }
    7. public static ItemStack getWeapon(LivingEntity mob){
    8. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    9. return new CraftItemStack(ent.getEquipment(0));
    10. }
    11. public static void setHelmet(LivingEntity mob, ItemStack item){
    12. CraftItemStack cis = new CraftItemStack(item);
    13. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    14. net.minecraft.server.ItemStack itemStack = cis.getHandle();
    15. ent.setEquipment(1, itemStack);
    16. }
    17. public static ItemStack getHelmet(LivingEntity mob){
    18. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    19. return new CraftItemStack(ent.getEquipment(1));
    20. }
    21. public static void setChestplate(LivingEntity mob, ItemStack item){
    22. CraftItemStack cis = new CraftItemStack(item);
    23. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    24. net.minecraft.server.ItemStack itemStack = cis.getHandle();
    25. ent.setEquipment(2, itemStack);
    26. }
    27. public static ItemStack getChestplate(LivingEntity mob){
    28. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    29. return new CraftItemStack(ent.getEquipment(2));
    30. }
    31. public static void setPants(LivingEntity mob, ItemStack item){
    32. CraftItemStack cis = new CraftItemStack(item);
    33. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    34. net.minecraft.server.ItemStack itemStack = cis.getHandle();
    35. ent.setEquipment(3, itemStack);
    36. }
    37. public static ItemStack getPants(LivingEntity mob){
    38. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    39. return new CraftItemStack(ent.getEquipment(3));
    40. }
    41. public static void setBoots(LivingEntity mob, ItemStack item){
    42. CraftItemStack cis = new CraftItemStack(item);
    43. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    44. net.minecraft.server.ItemStack itemStack = cis.getHandle();
    45. ent.setEquipment(4, itemStack);
    46. }
    47. public static ItemStack getBoots(LivingEntity mob){
    48. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    49. return new CraftItemStack(ent.getEquipment(4));
    50. }

    You could use those methods. And to make zombies wear diamond armor on spawn:
    Code:java
    1.  
    2. ItemStack weapon = new ItemStack(Material.DIAMOND_SWORD, 1);
    3. ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS, 1);
    4. ItemStack pants = new ItemStack(Material.DIAMOND_LEGGINGS, 1);
    5. ItemStack chestplate = new ItemStack(Material.DIAMOND_CHESTPLATE, 1);
    6. ItemStack helmet = new ItemStack(Material.DIAMOND_HELMET, 1);
    7. @EventHandler
    8. public void onSpawn(CreatureSpawnEvent event) {
    9. if(event.getEntity() instanceof Zombie) {
    10. LivingEntity le = event.getEntity();
    11. setWeapon(le, weapon);
    12. setBoots(le, boots);
    13. setPants(le, pants);
    14. setChestplate(le, chestplate);
    15. setHelmet(le, helmet);
    16. }
    17. }
     
  9. Hi, I know im a wee bit late for this post, but could you update the code to 1.5.1.

    I'm trying to hook this up to event.getPlayer().getWorld().spawnCreature(spawnLoc, EntityType.ZOMBIE);

    Thanks for any and all help.
     
  10. TwoPointDuck
    Code:
    public void setHelmet(LivingEntity e, ItemStack helmet)
    {
        EntityEquipment ee = e.getEquipment();
        ee.setHelmet(helmet);
    }
    Same for the rest of the armor, you can also set the dropchances in EntityEquipment (just note that this does not work for all entities, only for mobs that can actually wear it. Also, Players just use the PlayerInventory like usual)
     
Thread Status:
Not open for further replies.

Share This Page