coolio85
coolio85

Reputation: 171

Bukkit setItemStackName not working

Hey I am trying to make a gamemodeGUI plugin and change the item name to make it customised.

It works but it changes the first block in the inventory to white stained glass which says cancel.

Here is my inventory and StackName code

    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

    ItemStack iron = new ItemStack(Material.IRON_BLOCK, 1);
    ItemStack diamond = new ItemStack(Material.DIAMOND_BLOCK, 1);
    ItemStack gold = new ItemStack(Material.GOLD_BLOCK, 1);
    ItemStack lapis = new ItemStack(Material.LAPIS_BLOCK, 1);
    ItemStack cancel = new ItemStack(Material.STAINED_GLASS, 1);

    setItemStackName(iron, "Survival");
    myInventory.setItem(0, iron);

    setItemStackName(diamond, "Creative");
    myInventory.setItem(0, diamond);

    setItemStackName(gold, "Adventure");
    myInventory.setItem(0, gold);

    setItemStackName(lapis, "Spectator");
    myInventory.setItem(0, lapis);

    setItemStackName(cancel, "Cancel");
    myInventory.setItem(0, cancel);


}

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

Upvotes: 2

Views: 70

Answers (1)

LeoColman
LeoColman

Reputation: 7143

Let's take a look at your code:

setItemStackName(diamond, "Creative");
myInventory.setItem(0, diamond);

setItemStackName(gold, "Adventure");
myInventory.setItem(0, gold);

setItemStackName(lapis, "Spectator");
myInventory.setItem(0, lapis);

setItemStackName(cancel, "Cancel");
myInventory.setItem(0, cancel);

If we notice your inventory, you're setting all the Items to the slot 0.

If we look at a Bukkit Inventory setup, you'll see that Slot 0 is indeed the first slot:

enter image description here

The problem you're facing is:

  1. You put a Diamond Block named "Creative" at the first slot
  2. You put a Gold Block named "Gold" at the first slot (Notice that you put at slot 0 again)
  3. You put a Lapis Lazulli Block named "Spectator" at the first slot (same thing again)
  4. You put a Glass Pane named "Cancel" at the first slot.

If we see what's happening here, it becomes pretty clear why the item naming is failing! You're always setting the first item to something different. The last value is the one that will be final, so Cancel will be there.


To Fix this, you could simply set the new values to new slots. If you want all in a row, you could do

setItemStackName(diamond, "Creative");
myInventory.setItem(0, diamond);

setItemStackName(gold, "Adventure");
myInventory.setItem(1, gold);

setItemStackName(lapis, "Spectator");
myInventory.setItem(2, lapis);

setItemStackName(cancel, "Cancel");
myInventory.setItem(3, cancel);

Or any variety of this pattern.

Upvotes: 2

Related Questions