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
Well, here's how to change a mob's equipment.. Code:java package com.stirante.NBTTagAPI; import net.minecraft.server.EntityLiving; import org.bukkit.craftbukkit.entity.CraftLivingEntity;import org.bukkit.craftbukkit.inventory.CraftItemStack;import org.bukkit.entity.LivingEntity;import org.bukkit.inventory.ItemStack; public class EntityEquipment {public static void setWeapon(LivingEntity mob, ItemStack item){EntityLiving ent = ((CraftLivingEntity)mob).getHandle();net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).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){EntityLiving ent = ((CraftLivingEntity)mob).getHandle();net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).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){EntityLiving ent = ((CraftLivingEntity)mob).getHandle();net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).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){EntityLiving ent = ((CraftLivingEntity)mob).getHandle();net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).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){EntityLiving ent = ((CraftLivingEntity)mob).getHandle();net.minecraft.server.ItemStack itemStack = ((CraftItemStack)item).getHandle();ent.setEquipment(4, itemStack);}public static ItemStack getBoots(LivingEntity mob){EntityLiving ent = ((CraftLivingEntity)mob).getHandle();return new CraftItemStack(ent.getEquipment(4));}} Just use this when a zombie spawns?
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.
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?
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!
gomeow You are trying to cast the nms ItemStack instead of the bukkit ItemStack. Change: Code:java public static void setWeapon(LivingEntity mob, ItemStack item){ to Code:java public static void setWeapon(LivingEntity mob, org.bukkit.inventory.ItemStack item){ (same goes for the rest of the setters)
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
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)));
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;
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.
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)));
I like how people don't listen. Also the code above contains useless casts... Code:java LivingEntity le;CraftItemStack is = new CraftItemStack(newItemStack(Material.DIAMOND_SWORD, 1))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++){ le = (LivingEntity)world.spawnEntity(invadeLoc[i], EntityType.ZOMBIE); //Array of locs instead of 4 variables... setWeapon(le, is);}[/i]
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)
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]
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 ^^.
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
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 EntityEquipment ee = <some LivingEntity>.getEquipment();ee.setHelmet(ItemStack);// etc, the same for the rest of the equipment
um....im kind of a noob with java, so would I have to import something else? And could I have an example?
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