Item Generation - JustKato/BukkitWiki GitHub Wiki

ItemStacks

These hold the information for an itemstack, some of the information it hold is ( Material, Amount, ItemMeta ), these are mainly what we are looking for when creating an ItemStack.

ItemMeta

Hold all the information about an item, name, localized name, enchantments, durability, etc.. There are also other kind of ItemMeta for example ( CrossBow, Trident, Arrow, etc.. ), these have special properties that not all the items in game have, so they need their own class that extends ItemMeta.

Creating Items

To create an ItemStack with an amount of 32 of diamonds, we will have to run the code bellow

// The amount is optional, if you don't set it it will default to 1
ItemStack diamond = new ItemStack( Material.DIAMOND, 32 ); 

Customization

Now if we want to customize it we will have to change the item meta!

public static ItemStack GenerateCoolSword() {
    ItemStack sword = new ItemStack( Material.DIAMOND_SWORD, 1 ); // creating the sword stack
    ItemMeta meta = sword.getItemMeta(); // This will copy/clone the pre-generated item meta from the sword
    meta.setDisplayName(ChatColor.GREEN + "Cool sword"); // Creating the custom name
    meta.addEnchantment(Enchantment.DAMAGE_ALL, 5, false); // Adding sharpness 5
    List<String> lore = new ArrayList<String>(); // Initializing the lore variable
    lore.add("Some lore"); // adding a line to the lore
    meta.setLore(lore); // setting the lore

    item.setItemMeta(meta); // Applying the new meta DO NOT FORGET!!!
    return item;
}

Faking the enchantment glow

Making an item have the enchantment glow but with no real actual enchantments it's quite easy, the only down-side that you will have work around for this is, if you want people to be able to enchant said item, if so you will have to handle some enchantment events and fix the item.

Firstly I will wrap this all around an example function that will work on any item and will make it glow without any enchantments, look line-by-line and read the comments to get what's happening :)

public static ItemStack FakeGlowItem(Itemstack input) {
    ItemStack newItem = input;
    ItemMeta meta = input.getItemMeta();
    
    // If the item already has enchantments, we can't make it have a fake-glow.
    if ( meta.hasEnchants() ) return input; 
    // We pick a random enchantment that doesn't affect most items 
    // and use it as our dummy-enchantment (You can change it, keep 
    // in mind that it still affects items though )
    meta.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1, false);
    // We now use an item-flag to hide ALL enchantments
    meta.AddItemFlags(ItemFlag.HIDE_ENCHANTS);
    
    newItem.setItemMeta(meta);
    return newItem;
}
⚠️ **GitHub.com Fallback** ⚠️