Mob Armor

Discussion in 'Plugin Development' started by BeMacized, Nov 9, 2012.

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

    BeMacized

    Hi there,

    Since the last update of MC, 1.4.2, Zombies and skeletons now have a chance to spawn with armor.
    I was wondering if there was a way to spawn a zombie with a full set of leather armor.
    I've searched, but I wasn't able to find any documentation on weather this was implemented already.
    Is there a possible way I can do this? Or am I just impatient and it's just not implemented yet?

    Thank you for taking your time.

    -BeMacized
     
  2. Offline

    Rprrr

    Well, here's how to change a mob's equipment..
    Code:java
    1.  
    2. package com.stirante.NBTTagAPI;
    3.  
    4. import net.minecraft.server.EntityLiving;
    5.  
    6. import org.bukkit.craftbukkit.entity.CraftLivingEntity;
    7. import org.bukkit.craftbukkit.inventory.CraftItemStack;
    8. import org.bukkit.entity.LivingEntity;
    9. import org.bukkit.inventory.ItemStack;
    10.  
    11. public class EntityEquipment {
    12. public static void setWeapon(LivingEntity mob, ItemStack item){
    13. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    14. net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();
    15. ent.setEquipment(0, itemStack);
    16. }
    17. public static ItemStack getWeapon(LivingEntity mob){
    18. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    19. return new CraftItemStack(ent.getEquipment(0));
    20. }
    21. public static void setHelmet(LivingEntity mob, ItemStack item){
    22. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    23. net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();
    24. ent.setEquipment(1, itemStack);
    25. }
    26. public static ItemStack getHelmet(LivingEntity mob){
    27. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    28. return new CraftItemStack(ent.getEquipment(1));
    29. }
    30. public static void setChestplate(LivingEntity mob, ItemStack item){
    31. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    32. net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();
    33. ent.setEquipment(2, itemStack);
    34. }
    35. public static ItemStack getChestplate(LivingEntity mob){
    36. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    37. return new CraftItemStack(ent.getEquipment(2));
    38. }
    39. public static void setPants(LivingEntity mob, ItemStack item){
    40. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    41. net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();
    42. ent.setEquipment(3, itemStack);
    43. }
    44. public static ItemStack getPants(LivingEntity mob){
    45. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    46. return new CraftItemStack(ent.getEquipment(3));
    47. }
    48. public static void setBoots(LivingEntity mob, ItemStack item){
    49. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    50. net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();
    51. ent.setEquipment(4, itemStack);
    52. }
    53. public static ItemStack getBoots(LivingEntity mob){
    54. EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    55. return new CraftItemStack(ent.getEquipment(4));
    56. }
    57. }
    58.  

    Just use this when a zombie spawns?
     
  3. Offline

    phondeux

    What's returned on any of the gets if they're not wearing anything?

    Just confirmed that it's Material.AIR.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  4. Offline

    gomeow

    You know, I tried using this code today, and I get a stack trace saying net.minecraft.server.ItemStack cannot be cast to CraftItemStack. What can I do to fix it?
     
  5. Would be good to know where exactly this error happens but maybe you just messed up imports?
     
  6. Offline

    DarkBladee12

    V10lator I tried this too and it can't be messed up with imports because there are all 2 types of item stacks in that class... But doesn't work for me too, because he casts the Bukkit ItemStack to an CraftItemStack!
     
  7. Offline

    gomeow

    Line 14 of the code above
     
  8. Offline

    fireblast709

    gomeow
    You are trying to cast the nms ItemStack instead of the bukkit ItemStack. Change:
    Code:java
    1. public static void setWeapon(LivingEntity mob, ItemStack item){

    to
    Code:java
    1. public static void setWeapon(LivingEntity mob, org.bukkit.inventory.ItemStack item){

    (same goes for the rest of the setters)
     
  9. Offline

    lx3krypticx

    Easy fix use what I coded :)

    Code:
        private static ItemStack toCraftBukkit(ItemStack stack) {
            if (!(stack instanceof CraftItemStack))
                return new CraftItemStack(stack);
            else
                return stack;
        }
       
        public void dressMonster(LivingEntity monster, Material head, Material chest, Material legs, Material boots, Material weapon){
            ItemStack weapon1 = toCraftBukkit(new ItemStack(weapon));
            ItemStack head1 = toCraftBukkit(new ItemStack(head));
            ItemStack boots1 = toCraftBukkit(new ItemStack(boots));
            ItemStack chest1 = toCraftBukkit(new ItemStack(chest));
            ItemStack legs1 = toCraftBukkit(new ItemStack(legs));
           
            EntityEquipment.setWeapon(monster, weapon1);
            EntityEquipment.setBoots(monster, boots1);
            EntityEquipment.setChestplate(monster, chest1);
            EntityEquipment.setPants(monster, legs1);
            EntityEquipment.setHelmet(monster, head1);
        }
    Whenever you want to dress an entity use dressMonster(monster, head, chest, legs, boots, weapon)

    I got it working :)

    http://gyazo.com/8b941e768da272eb1fa8af20a8313f3d
     
  10. Offline

    joehot2000

    how would i use this if i had just spawned a monster and wanted to give him armour?
     
  11. Offline

    gomeow

    Oh I figured it out. Use the code above but make it take a CraftItemStack
    When I called, I did this: setWeapon(new CraftItemStack(new ItemStack(Material.DIAMOND_SWORD)));
     
  12. Offline

    joehot2000

    i am not quite sure where to PUT the code.

    my current code is just spawning the zombies at the locations (so where to put the sword code?) //Spawn Zombies at the four locations above
    switch (a%3) {
    case 0:
    //do

    //lmobInvasionBy.spawnEntity(EntityType.ZOMBIE);
    //mobInvasionBy.spawnEntity();

    world.spawnEntity(invadeLoc1, EntityType.ZOMBIE);
    world.spawnEntity(invadeLoc2, EntityType.ZOMBIE);
    world.spawnEntity(invadeLoc3, EntityType.ZOMBIE);
    world.spawnEntity(invadeLoc4, EntityType.ZOMBIE);
    break;
     
  13. joehot2000 Check the return value of spawnEntity and you'll see that it returns your zombie... So the best place to give it equipment would be right after spawning.

    Also if you execute exactly the same code more times use a loop.
     
  14. Offline

    gomeow

    I think this will work:
    Code:
    Zombie z1 = (Zombie) world.spawnEntity(invadeLoc1, EntityType.ZOMBIE);
    Zombie z2 = (Zombie) world.spawnEntity(invadeLoc2, EntityType.ZOMBIE);
    Zombie z3 = (Zombie) world.spawnEntity(invadeLoc3, EntityType.ZOMBIE);
    Zombie z4 = (Zombie) world.spawnEntity(invadeLoc4, EntityType.ZOMBIE);
    setWeapon((LivingEntity)z1, new CraftItemStack(newItemStack(Material.DIAMOND_SWORD, 1)));
    setWeapon((LivingEntity)z2, new CraftItemStack(newItemStack(Material.DIAMOND_SWORD, 1)));
    setWeapon((LivingEntity)z3, new CraftItemStack(newItemStack(Material.DIAMOND_SWORD, 1)));
    setWeapon((LivingEntity)z4, new CraftItemStack(newItemStack(Material.DIAMOND_SWORD, 1)));
     
  15. I like how people don't listen. Also the code above contains useless casts...
    Code:java
    1. LivingEntity le;
    2. CraftItemStack is = new CraftItemStack(newItemStack(Material.DIAMOND_SWORD, 1))
    3. for(int i = 0; i < invadeLoc.length /* This "magic" makes it possible to spawn a configurable amount of zombies... if not needed hardcode to 4 */; i++)
    4. {
    5. le = (LivingEntity)world.spawnEntity(invadeLoc[i], EntityType.ZOMBIE); //Array of locs instead of 4 variables...
    6. setWeapon(le, is);
    7. }[/i]
     
  16. Fancy, 1+ for using Gyazo!
     
  17. Offline

    joehot2000

    so, my code is now this:









    //Spawn Zombies at the four locations above
    switch
    (a%3) {
    case
    0:
    //do
    // mobInvasionBy.spawnEntity();
    Entity z1 = world.spawnEntity(invadeLoc1, EntityType.
    ZOMBIE
    );
    Entity z2 = world.spawnEntity(invadeLoc2, EntityType.
    ZOMBIE
    );
    Entity z3 = world.spawnEntity(invadeLoc3, EntityType.
    ZOMBIE
    );
    Entity z4 = world.spawnEntity(invadeLoc4, EntityType.
    ZOMBIE
    );
    setWeapon((LivingEntity)z1,
    new CraftItemStack(newItemStack(Material.DIAMOND_SWORD
    , 1)));
    setWeapon((LivingEntity)z2,
    new CraftItemStack(newItemStack(Material.DIAMOND_SWORD
    , 1)));
    setWeapon((LivingEntity)z3,
    new CraftItemStack(newItemStack(Material.DIAMOND_SWORD
    , 1)));
    setWeapon((LivingEntity)z4,
    new CraftItemStack(newItemStack(Material.DIAMOND_SWORD
    , 1)));
    break
    ;
    (i hope i got that right)

    it still dosent seem to want to work. it was asking me to make it a method :( (tried with method, still didnt work)
     
  18. Offline

    gomeow

    You should use the syntax tags:
    [syntax=java]code here[/syntax]
     
  19. Offline

    stirante

    It would be better for our eyes if code would be in [.code][./code]
     
  20. You're code is what? Sorry, but either its broken or forum messed it up. Please put it into
    Code:
    [syntax=java]Code here[/syntax]
     
  21. Offline

    Destro168

    Completely copy-pastable version: http://pastebin.com/vDBB0kKN

    Just use: MobEquipment.setHelmet(event.getEntity(), new ItemStack(Material.DIAMOND_HELMET,1)); in your entity spawn event to put whatever you want on the mobs. Thanks for the help guys, this problem was bugging me ^^.
     
  22. Offline

    joehot2000

    with your pastebin, where is the mob you are giving armour?



    how do i do that?
     
  23. Offline

    nitrousspark

    Code:
     
    public static void setWeapon(LivingEntity mob, ItemStack item){
    CraftItemStack cis = new CraftItemStack(item);
    EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    net.minecraft.server.ItemStack itemStack = cis.getHandle();
    ent.setEquipment(0, itemStack);
    }
    public static ItemStack getWeapon(LivingEntity mob){
    EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    return new CraftItemStack(ent.getEquipment(0));
    }
    public static void setHelmet(LivingEntity mob, ItemStack item){
    CraftItemStack cis = new CraftItemStack(item);
    EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    net.minecraft.server.ItemStack itemStack = cis.getHandle();
    ent.setEquipment(1, itemStack);
    }
    public static ItemStack getHelmet(LivingEntity mob){
    EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    return new CraftItemStack(ent.getEquipment(1));
    }
    public static void setChestplate(LivingEntity mob, ItemStack item){
    CraftItemStack cis = new CraftItemStack(item);
    EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    net.minecraft.server.ItemStack itemStack = cis.getHandle();
    ent.setEquipment(2, itemStack);
    }
    public static ItemStack getChestplate(LivingEntity mob){
    EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    return new CraftItemStack(ent.getEquipment(2));
    }
    public static void setPants(LivingEntity mob, ItemStack item){
    CraftItemStack cis = new CraftItemStack(item);
    EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    net.minecraft.server.ItemStack itemStack = cis.getHandle();
    ent.setEquipment(3, itemStack);
    }
    public static ItemStack getPants(LivingEntity mob){
    EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    return new CraftItemStack(ent.getEquipment(3));
    }
    public static void setBoots(LivingEntity mob, ItemStack item){
    CraftItemStack cis = new CraftItemStack(item);
    EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    net.minecraft.server.ItemStack itemStack = cis.getHandle();
    ent.setEquipment(4, itemStack);
    }
    public static ItemStack getBoots(LivingEntity mob){
    EntityLiving ent = ((CraftLivingEntity)mob).getHandle();
    return new CraftItemStack(ent.getEquipment(4));
    }
    net.minecraft.server.ItemStack is an error now and it says net.minecraft.server.ItemStack cannot be resolved to a type

    new CraftItemStack(item) is an error too The constructor CraftItemStack(ItemStack) is not visible

    AND getHandle() The method getHandle() is undefined for the type CraftItemStack
     
  24. Offline

    fireblast709

    nitrousspark That is due the fact Craftbukkit has renamed the packages. Also, the CraftItemStack constructor has been privatized and the getHandle() method has been removed. Use EntityEquipment, which has been added somewhat recently.
    Code:java
    1. EntityEquipment ee = <some LivingEntity>.getEquipment();
    2. ee.setHelmet(ItemStack);
    3. // etc, the same for the rest of the equipment
    4.  
     
  25. Offline

    bossomeness

    does this support dyed leather armor?
     
  26. Offline

    Drkmaster83

    You'd have to modify the LeatherArmorMeta before adding it to the entity.
     
  27. Offline

    bossomeness

    um....im kind of a noob with java, so would I have to import something else? And could I have an example?
     
  28. Offline

    Drkmaster83

    ItemMeta was added in 1.4.5-R1.0.
    Code:
    ItemMeta meta = (ItemMeta) (itemstack here).getItemMeta();
    LeatherArmorMeta lmeta = (LeatherArmorMeta) meta;
    lmeta.setColor(Color.BLUE);
    (itemstack here).setItemMeta(lmeta);
    
    Makes blue leather armor
     
Thread Status:
Not open for further replies.

Share This Page