Add a custom item extending ItemStack to the Inventory

Discussion in 'Plugin Development' started by sebcio98, Dec 31, 2015.

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

    sebcio98

    Hi. I created a class which extends ItemStack and I would like to add this as an item to player's inventory. Is it possible to add and later get this item to/from the inventory?

    Here are the item classes (I skipped the imports):
    Code:
    public abstract class MagicItem extends ItemStack
    {
        protected MagicItem(Material mat, int count, String name, String lore, Map<Enchantment, Integer> enchs)
        {
            if (count < 1) count = 1;
        
            this.setType(mat);
            this.setAmount(count);
        
            ItemMeta meta = this.getItemMeta();
            if (name != "")
                meta.setDisplayName(name);
            if (lore != "")
            {
                List<String> llore = new ArrayList<String>();
                llore = Arrays.asList(lore.split("\n"));
                meta.setLore(llore);
            }
            this.setItemMeta(meta);
         
            if (enchs != null && !enchs.isEmpty())
                this.addUnsafeEnchantments(enchs);
        }
    
        protected void use()
        {
        
        }
    }
    Code:
    public class FireWand extends MagicItem
    {
        public FireWand()
        {
            super(Material.BLAZE_ROD, 1, "§4Fire Wand", "Default MagicItem lore", null);
        }
    
        @Override
        public void use()
        {
        
        }
    }
    I thought that I can do just player.getInventory().addItem(new FireWand()); but then it just adds ItemStack.
    So, again, is there any way to do this?
     
    Last edited: Dec 31, 2015
  2. Offline

    mcdorli

    What's the problem. What have you imported? nms itemStack or bukkit itemStack?
    You could probably just use the playerInteractEvent, and check if the item the player's holding is the wand.
     
  3. @sebcio98 You should probably call super's constructor in your MagicItem constructor, eh?
     
  4. Offline

    sebcio98

    The imports are:
    Code:
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    The PlayerInteractEvent.getItem() gives me a plain ItemStack, just like all the other events. What I want is to put an instance of my class extending ItemStack into an inventory and my question is is that possible?

    I can't set the lore and enchantments this way, but yeah I should probably use it to shorten the code ^^

    @EDIT: Wait, no. I can't call super's constructor if I want to check if "count" is less than 1, because it must be the first line in this constructor;
     
    Last edited: Dec 31, 2015
  5. Just call constructor and then do your checks? Unless you're doing everything the constructor does.
     
  6. Offline

    sebcio98

    I'm doing everything the constructor would do, so it's fine. Actually I'm doing even more, because the constructor would set only the material and the amount.
     
Thread Status:
Not open for further replies.

Share This Page