Search code examples
javaminecraftbukkit

Bukkit myInventory change item names


I have made a minecraft bukkit plugin which is a gamemode changer GUI but I can't figure out how to change the block names in the inventory that I opened up. At the moment all they say is Iron_Block, Gold_block ...etc

If anyone could help me that would be very helpful

This is the inventory code bit

public static Inventory myInventory = Bukkit.createInventory(null, 9, "GamemodeGUI");

static {
    myInventory.setItem(0, new ItemStack(Material.IRON_BLOCK, 1)); //Survival
    myInventory.setItem(1, new ItemStack(Material.DIAMOND_BLOCK, 1)); //Creative
    myInventory.setItem(2, new ItemStack(Material.GOLD_BLOCK, 1)); //Adventure
    myInventory.setItem(3, new ItemStack(Material.LAPIS_BLOCK, 1)); //Spectator

    myInventory.setItem(8, new ItemStack(Material.STAINED_GLASS, 1)); //Cancel
}

Solution

  • Create your ItemStack out of the method call by assigning it to a variable:

    ItemStack iron = new ItemStack(Material.IRON_BLOCK, 1); //Can be added to your static block
    

    Customize it to change the name for what you want

    public static void setItemStackName(ItemStack renamed, String customName) {
        ItemMeta renamedMeta = renamed.getItemMeta();
        renamedMeta.setDisplayName(customName);
        renamed.setItemMeta(renamedMeta);
    }
    

    To the ItemMeta you can also add a lore. This is the Item's description

    renamedMeta.setLore(Arrays.asList("Line 1 lore", "Line 2", "..."));
    

    Then add the custom ItemStack to the inventory

    setItemStackName(iron, "Survival");
    inventory.setItem(0, iron);    //Pass your customized ItemStack to the method now