Solved Spawn an entity with an enchanted item

Discussion in 'Spigot Plugin Development' started by Gusta_, Jan 19, 2021.

  1. I want to make a plugin that makes entities spawn with armour, and enchanted items, in this case, I want to make the skeleton spawn with an enchanted bow, with punch 2, it spawns perfectly with the armour, but the bow is not enchanted, couldn't figure out how to do it.

    Code (Java):
    if(event.getEntityType() == EntityType.SKELETON ) {
             
                Skeleton skeleton = (Skeleton) event.getEntity();
             
                skeleton.getEquipment().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
                skeleton.getEquipment().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
                skeleton.getEquipment().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
                skeleton.getEquipment().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
             
             
                ItemStack punchBow = new ItemStack(Material.BOW);
                punchBow.addEnchantment(Enchantment.ARROW_KNOCKBACK, 2);

            }
    The full code:

    Code (Java):
    package me.gusta.creaturespawn.creaturespawnevent;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Creeper;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Skeleton;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.inventory.ItemStack;

    public class CreatureSpawn implements Listener {
     
     
        @EventHandler
        public void event(CreatureSpawnEvent event) {
         
            if(event.getEntityType() == EntityType.CREEPER ) {
             
                Creeper creeper = (Creeper) event.getEntity();
             
                creeper.setPowered(true);
             
            }
         
            if(event.getEntityType() == EntityType.ZOMBIE ) {
             
                Zombie zombie = (Zombie) event.getEntity();
             
                zombie.getEquipment().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
                zombie.getEquipment().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
                zombie.getEquipment().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
                zombie.getEquipment().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
             
            }
         
            if(event.getEntityType() == EntityType.SKELETON ) {
             
                Skeleton skeleton = (Skeleton) event.getEntity();
             
                skeleton.getEquipment().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
                skeleton.getEquipment().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
                skeleton.getEquipment().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
                skeleton.getEquipment().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
             
             
                ItemStack punchBow = new ItemStack(Material.BOW);
                punchBow.addEnchantment(Enchantment.ARROW_KNOCKBACK, 2);

            }
         
        }

    }
     
    #1 Gusta_, Jan 19, 2021
    Last edited: Jan 20, 2021
  2. Try

    Code (Java):
            ItemStack punchBow = new ItemStack(Material.BOW);
            ItemMeta punchBowMeta = punchBow.getItemMeta();
            punchBowMeta.addEnchant(Enchantment.ARROW_KNOCKBACK, 2, true);
            punchBow.setItemMeta(punchBowMeta);
    and obviously you need to set the Skeletons item to this in their hand.
     
  3. I've tried using
    Code (Java):
    skeleton.getEquipment().getItemInMainHand(punchBow);
    to set the bow to the main hand, but I get a warning:
    "The method getItemInMainHand() in the type EntityEquipment is not applicable for the arguments (ItemStack)
     
  4. You're using "getItemInMainHand"
    use setItemInMainHand
     
    • Like Like x 1
  5. Still not working, the skeleton spawns in with the armour, but the bow is not enchanted
    Screenshot by Lightshot (prnt.sc)
    Screenshot by Lightshot (prnt.sc)
    [​IMG]
    [​IMG]
    Code (Java):
    if(event.getEntityType() == EntityType.SKELETON ) {
             
                Skeleton skeleton = (Skeleton) event.getEntity();
             
                skeleton.getEquipment().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
                skeleton.getEquipment().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
                skeleton.getEquipment().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
                skeleton.getEquipment().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
             
             
                 ItemStack punchBow = new ItemStack(Material.BOW);
                    ItemMeta punchBowMeta = punchBow.getItemMeta();
                    punchBowMeta.addEnchant(Enchantment.ARROW_KNOCKBACK, 2, true);
                    punchBow.setItemMeta(punchBowMeta);
             
                skeleton.getEquipment().setItemInMainHand(punchBow);

            }
     
  6. Ah, maybe because the event fires before the equipment is set, not sure. Try clearing his equipment and/or setting the bow a tick later.
     
  7. I implemented
    Code (Text):
    instance=this;
            getServer().getPluginManager().registerEvents((Listener)new CreatureSpawn(), (Plugin)this);
    in the core class, that for some reason, only affected the bow enchantment, as before that, the armour was still working properly, now it all works.