1.8.8 how to make an NPC hold an enchanted item in his hand on Citizens?

Discussion in 'Spigot Plugin Development' started by Quark09, Jun 27, 2021.

  1. Yes i know what is possible with /npc equip but i wont that work in my code
    Yes i tried way a META of Item but no effect
    My main class:
    Code (Java):

    package npc.main;
    import java.util.logging.Logger;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin{
        Logger log = Logger.getLogger("Minecraft");
       
       
        @Override
        public void onEnable() {
            Commands commands = new Commands();
            getCommand("reducebotcreate").setExecutor(new Commands());
            PluginManager pm = getServer().getPluginManager();
            Handler listener = new Handler(this);
            pm.registerEvents(listener, this);
           
                 
        }
    }
     
    and my command class:
    Code (Java):

    package npc.main;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import net.citizensnpcs.api.CitizensAPI;
    import net.citizensnpcs.api.npc.NPC;
    import net.citizensnpcs.api.trait.trait.Equipment;
    import net.citizensnpcs.api.trait.trait.Equipment.EquipmentSlot;
    public class Commands implements CommandExecutor{
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                return true;
                }
            for (Player p : Bukkit.getOnlinePlayers()) {
               
           
        Player player = (Player) sender;
        if (cmd.getName().equalsIgnoreCase("reducebotcreate")) {
            NPC npc = CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, "BangerFreezzyy");
            npc.spawn(player.getLocation());
            npc.getNavigator().getLocalParameters().attackRange(5.0).attackDelayTicks(2).pathDistanceMargin(0);
            npc.getNavigator().getLocalParameters().speed(0);
            npc.getNavigator().setTarget(p, true);
            Equipment ee = npc.getTrait(Equipment.class);
             ee.set(EquipmentSlot.HAND, new ItemStack(Material.STICK, 1)  .addEnchantment(Enchantment.KNOCKBACK, 1)); // this method not working
            }}
       
       
        return true;
               
        }
    }
     
     
  2. .addEnchantment is from a ItemMeta, not from ItemStack. so you have to

    Code (Text):
    ItemStack is = new ItemStack(Material.STICK, 1)
    ItemMeta im = is.getItemMeta();
    im.addEnchantment(...);
    is.setItemMeta(im);
    ee.set(..., is);
     
    • Useful Useful x 1
  3. You're changing the ItemMeta but not changing ItemStack's ItemMeta to the new one ( with enchant )
     
  4. Very very thanks! All work