Solved Item not being named when added to inventory

Discussion in 'Plugin Development' started by TheKingElessar, Jul 23, 2018.

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

    TheKingElessar

    I'm trying to add a named poisonous potato to a player's inventory. Here's what I've got. As far as I know, and from what I've read in my searching, this should work.
    Code:
    ItemStack(Material.POISONOUS_POTATO, 1);
    ItemMeta potatoMeta = potato.getItemMeta();  // Gets the item's metadata
    potatoMeta.setDisplayName("Cheaty Potato");  // Changes the metadata
    potato.setItemMeta(potatoMeta);  // Set the metadata back to the item
    
    if(player.hasPermission("potato.add")) {
        player.getInventory().addItem(potato);  // Adds the item to the player's inventory
    } else {
        return true;
    When this is processed, it adds a poisonous potato to the player's inventory, but it's not renamed. It's just a normal poisonous potato.

    What's going wrong? Thanks for the help!
     
  2. try to remove the 1 from the Constructor of the ItemStack:

    new ItemStack(Material.POISONOUS_POTATO);
     
  3. Offline

    MasOS

    The reason might be that you did not set the itemstack you've created via
    Code:
    ItemStack(Material.POISONOUS_POTATO, 1);
    to the variable "potato".

    Try this:
    Code:
    // Might be a more efficient way of checking permissions
    // If the player does not have perms, it won't even create the itemstack
    if(!player.hasPermission("potato.add"))
        return true;
    
    ItemStack potato = new ItemStack(Material.POISONOUS_POTATO, 1); // Creates the itemstack "Potato"
    ItemMeta potatoMeta = potato.getItemMeta();  // Gets the item's metadata
    potatoMeta.setDisplayName("Cheaty Potato");  // Changes the metadata
    potato.setItemMeta(potatoMeta);  // Set the metadata back to the item
    
    player.getInventory().addItem(potato);  // Adds the item to the player's inventory
    Alternatively, create a method that names items for you:
    Code:
    public static ItemStack nameItem(ItemStack item, String name) {
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(name);
        item.setItemMeta(meta);
        return item;
    }
    Hope it helped.
     
  4. Offline

    InstanceofDeath

    1.I don‘t know where you define your potato object, but certainly not in the first line xD

    org.bukkit.ItemStack potato = new ItemStack(Material.Poisonous_Potato)


    Otherwise the code should be correct.
     
  5. Offline

    TheKingElessar

    @InstanceofDeath @MasOS @susy01

    Sorry, for some reason I wasn't alerted until now that there were replies.

    I did set the itemstack to the variable potato, but I think when copying over the code to my post it must have been deleted. Here's where I define it:
    Code:
    ItemStack potato = new ItemStack(Material.POISONOUS_POTATO, 1);
    
    I removed the amount, but haven't had a chance to test it (my Minecraft won't launch!). Thanks for the help!

    Edit: Removing the amount worked perfectly, thanks!
     
    Last edited: Jul 27, 2018
  6. Offline

    KarimAKL

Thread Status:
Not open for further replies.

Share This Page