Problem with adding enchants on itemstack

Discussion in 'Plugin Development' started by JavaNaza, Feb 5, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    JavaNaza

    Hey guys, I have an problem with adding enchants to the ItemStack.
    When player uses the command he gets that item but without enchant that I added to it.

    Here is my code:
    Code:
    package me.grantis.invetorygui.abilities;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Abilities {
     
        public ItemStack protector(ItemStack iT) {
         
            ItemStack protector = new ItemStack(Material.DIAMOND_CHESTPLATE);
            ItemMeta protectorMeta = protector.getItemMeta();
            protectorMeta.setDisplayName(ChatColor.GREEN + "Protector");
            protector.addUnsafeEnchantment(Enchantment.PROTECTION_FIRE, 4);
            protector.setItemMeta(protectorMeta);
            return iT = protector;
         
        }
    
     
    }
    
    Can somebody show me how to use multiple classes for things like ItemStack if its not right?
    Thank you!
     
    Last edited: Feb 5, 2018
  2. Offline

    AyWuzzUp

    To add an enchantment to an item you would do something like the following -

    Code:
    ItemStack testEnchant = new ItemStack (Material.BOW, 1);
    ItemMeta testEnchantMeta = testEnchant.getItemMeta();
    testEnchantMeta.addEnchant(Enchantment.ARROW_FIRE, 10, true);
    testEnchant.setItemMeta(testEnchantMeta);
    

    So simply change:
    Code:
    protector.addUnsafeEnchantment(Enchantment.PROTECTION_FIRE, 4);
    
    To this:
    Code:
    protectorMeta.addEnchant(Enchantment.PROTECTION_FIRE, 4, true);
    

    Also if you want to access information from one class to another I suggest looking into Dependency Injections, better than using the static modifier in my opinion, anyways if you would like help with dependency injections feel free to ask I can help you out !
     
    Last edited: Feb 5, 2018
  3. Offline

    AyWuzzUp

    If your issue has been solved, please remember to mark the thread as solved ! Thank you.
     
  4. Offline

    PlayerWinEvent

    no dude, his problem is not an enchantment. if you read into his method, you will find that he messed up input and output. the solution is below. (untested, sorry...
    Code:
    public ItemStack protector(ItemStack iT) {
        
            ItemMeta protectorMeta =  iT.getItemMeta();
            protectorMeta.setDisplayName(ChatColor.GREEN + "Protector");
             iT.addUnsafeEnchantment(Enchantment.PROTECTION_FIRE, 4);
             iT.setItemMeta(protectorMeta);
            return iT;
        
        }
     
    JavaNaza likes this.
  5. Offline

    AyWuzzUp

    Okay first of all, you do use 'addUnsafeEnchantment' to enchant your item you simply use the ItemMeta and use 'addEnchantment' like I had said above.

    And to 'fix' his problem which I should not even be needing to give code for this because this is a very simple concept you would do the following:

    Code:
    public ItemStack protector(ItemStack itemStack) {
    
        ItemMeta itemMeta = itemStack.getItemMeta();
        itemMeta.addEnchant(Enchantment.PROTECTION_FIRE, 4, true);
        itemStack.setItemMeta(itemMeta); 
    
        return itemStack;
    }
    
     
Thread Status:
Not open for further replies.

Share This Page