Solved Named Item

Discussion in 'Plugin Development' started by 567legodude, Nov 19, 2014.

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

    567legodude

    Can someone tell me what is wrong with my code, I want to give the player an item with a custom name.
    Code:java
    1. public ItemStack getrod() {
    2. ItemStack rod = new ItemStack(Material.BLAZE_ROD);
    3. ItemMeta meta = rod.getItemMeta();
    4. meta.setDisplayName(ChatColor.AQUA + "Selection stick.");
    5. rod.setItemMeta(meta);
    6. return rod;
    7. }
    8.  
    9. // Then later in my code I use
    10. player.getInventory().addItem(getrod());

    It adds the item but it doesnt have the custom name.
    Anyone tell me what i'm doing wrong?
     
  2. Offline

    lenis0012

    what hapens instead of giving them item when the code runs?
     
  3. Offline

    Zombieghost_391

    Is there any error exceptions in console and how are you adding this, when the player joins or through another event?
     
  4. Offline

    tommyhoogstra

    Check that your events are being registered, if you are even using events.
     
  5. Offline

    567legodude

    lenis0012
    There is nothing else in the method, I usually just give them a plain blaze rod, but I want to make it named.
    Zombieghost_391
    The item is added when they type a command.
    tommyhoogstra
    The event works just fine, like I said, it successfully gives them the item, but it is not named.
     
  6. Offline

    Zombieghost_391

    Is there any errors in console when the command is give?
     
  7. Offline

    tommyhoogstra

    Instead of having this method for only one itemstack, perhaps you should use something like this

    Code:java
    1.  
    2. public ItemStack createItem(Material mat, int num, String name){
    3. ItemStack i = new ItemStack(mat, num);
    4. ItemMeta im = i.getItemMeta();
    5. im.setDisplayName(name);
    6.  
    7. i.setItemMeta(im);
    8.  
    9. return i;
    10. }
    11.  

    then in your event you can do something like

    Code:java
    1.  
    2. player.getInventory().addItem(createItem(Material.YOURMATERIAL, AMOUNT OF ITEM, NAME OF ITEM));
    3.  
     
  8. Offline

    567legodude

    tommyhoogstra Zombieghost_391
    So, it seemed that solving the problem was simple.
    I changed this line.
    Code:java
    1. meta.setDisplayName(ChatColor.AQUA + "Selection stick");

    To this
    Code:java
    1. String n = ChatColor.AQUA + "Selection Stick";
    2. meta.setDisplayName(n);
     
  9. Offline

    Zombieghost_391

    Still weird, but at least you got it to work.
     
Thread Status:
Not open for further replies.

Share This Page