1.8.8 Add enchantment to itemstack not working.

Discussion in 'Spigot Plugin Development' started by SammyProXCoding, Sep 25, 2021.

  1. Hey all! So i am currently coding a practice plugin but the enchantment doesnt get added ingame, also, no errors in console...
    Code (Text):
                        ItemStack[] inv = new ItemStack[6];
                        inv[0] = new ItemStack(Material.DIAMOND_BOOTS, 1);
                        inv[1] = new ItemStack(Material.DIAMOND_LEGGINGS, 1);
                        inv[2] = new ItemStack(Material.DIAMOND_CHESTPLATE, 1);
                        inv[3] = new ItemStack(Material.DIAMOND_HELMET, 1);
                        inv[4] = new ItemStack(Material.DIAMOND_SWORD, 1);
                        inv[5] = new ItemStack(Material.ENDER_PEARL, 8);
                        //Add enchantments to sword and armor
                        inv[4].addEnchantment(Enchantment.FIRE_ASPECT, 2);
                        inv[4].addEnchantment(Enchantment.DAMAGE_ALL, 2);
                        inv[0].addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
                        inv[1].addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
                        inv[2].addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
                        inv[3].addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
                        //Set the itemmetas

                        p.getInventory().clear();
                        p.getInventory().setBoots(inv[0]);
                        p.getInventory().setLeggings(inv[1]);
                        p.getInventory().setChestplate(inv[2]);
                        p.getInventory().setHelmet(inv[3]);
                        p.removePotionEffect(PotionEffectType.DAMAGE_RESISTANCE);
                        p.getInventory().setItem(0, inv[4]);
                        p.getInventory().setItem(1, inv[5]);
                        for(int i=0; i<p.getInventory().getSize(); i++){
                            if(p.getInventory().getItem(i) == null){
                                p.getInventory().setItem(i, pitemstack);
                            }
                        }
                        p.updateInventory();
                        queuedfirst.getInventory().clear();
                        queuedfirst.getInventory().setBoots(inv[0]);
                        queuedfirst.getInventory().setLeggings(inv[1]);
                        queuedfirst.getInventory().setChestplate(inv[2]);
                        queuedfirst.getInventory().setHelmet(inv[3]);
                        queuedfirst.getInventory().setItem(0, inv[4]);
                        queuedfirst.getInventory().setItem(1, inv[5]);
                        for(int i=0; i<queuedfirst.getInventory().getSize(); i++){
                            if(queuedfirst.getInventory().getItem(i) == null){
                                queuedfirst.getInventory().setItem(i, pitemstack);
                            }
                        }
                        queuedfirst.updateInventory();
                        queuedfirst.removePotionEffect(PotionEffectType.DAMAGE_RESISTANCE);
                        p.removePotionEffect(PotionEffectType.DAMAGE_RESISTANCE);
                        return;
    Thanks!
     
  2. Have you tried to debug? printing out each ItemStack and its enchantments both before and after you call the addEnchantment method?
    Also, if you're always using indexes to obtain/modify the ItemStacks in the array, what is the purpose of it being in the array apposed to having 6 ItemStack variables which you can assign variable names instead of remembering which index means what?
     
    • Agree Agree x 1
  3. Get the ItemMeta, then, meta.addEnchant(Enchant.YOUR_ENCHANT);
    inv.setItemMeta(meta);
     
  4. I already tried that, it was the same outcome.
     
  5. Try with only
    Code (Java):
    ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    sword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
    player.getInventory().setItemInHand(sword);
    and see if that works
     
  6. You forgot inventory.update();
     
  7. try doing a player.updateInventory(); at the end
     
  8. It is there but not included in the posted code...
     
  9. Try adding a false to the end of the addEnchantment like this
    Code (Java):

    inv[0].addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1, false);
     
    also the addEnchantment should return true if it succeed or false if fails so check the output