Solved How to give a item an enchantment

Discussion in 'Spigot Plugin Development' started by soldier711, Aug 19, 2017.

  1. Hello fellow spigot developers I am trying to add an enchantment to a diamond_sword wich doesnt work, I just get a error when I left click my sign

    Code (Text):
                    ItemStack sword = setItem(2, (new ItemStack(Material.DIAMOND_SWORD)));
                    sword.addEnchantment(Enchantment.DAMAGE_ALL, 2);
                    player.getInventory().addItem(sword);
                   
    but it doesnt work help me please :3
     
  2. Give us the error please :)
     
  3. I think its this [​IMG]
     
  4. Code (Text):
                    ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
                    sword.addEnchantment(Enchantment.DAMAGE_ALL, 2);
                    player.getInventory().addItem(sword);
    This works but how do I set the slot its going to be in?
     
  5. Code (Text):
    player.getInventory().setItem(slot, item);
    Warning: Slots number starts at 0! First hotbar inventory slot is slot 0, the next to the right is 1 etc.

    Here is an image of the slots.
     
  6. Inventory#setItem(slot, item);

    EDIT: Ninja'd!
     
    • Funny Funny x 1