Solved Item Name Color

Discussion in 'Plugin Development' started by rohan576, Mar 22, 2014.

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

    rohan576

    Okay, so I'm trying to change the color (and name) of an item, but when I use § or ChatColor.SOMETHING, it doesn't work. It only works without ChatColor or §; so I can't figure out how to use color in the name.

    This is what I'm doing:
    Code:java
    1. inv.setItem(0, createItem(Material.ICE, 1, (short) 0, "§b§lOpen Freeze Tag Shop", ""));


    And the CreateItem snippet that sets the ItemMeta (in case you don't trust that I'm setting the DisplayName properly : p)
    Code:java
    1. public static ItemStack createItem(Material material, int amount, short shrt, String displayname, String lore) {
    2. ItemStack item = new ItemStack(material, amount, (short) shrt);
    3. ItemMeta meta = item.getItemMeta();
    4. meta.setDisplayName(displayname);
    5. ArrayList<String> Lore = new ArrayList<String>();
    6. Lore.add(lore);
    7. meta.setLore(Lore);
    8.  
    9. item.setItemMeta(meta);
    10. return item;
    11. }
     
  2. Offline

    tommyhoogstra

    I'm pretty sure you have to use ChatColor for this
    So: inv.setItem(0, createItem(Material.ICE, 1, (short) 0, ChatColor.BOLD + ChatColor.YELLOW + "Item NAME");
     
  3. Offline

    rohan576

    As I said, I've already tried doing it. :/
     
  4. Offline

    tommyhoogstra

    I edited my post if you havent tried that
     
  5. Offline

    Barnyard_Owl

    rohan576
    What does it end up giving you? What's the name of the item?
    Also, the ChatColor is essentially the same as using the section sign, there should be no issues using either.
     
  6. Offline

    rohan576

    Barnyard_Owl

    The result is that the block that's name is being changed (an Ice Block) is still called Ice when I use § or ChatColor.
     
  7. Offline

    tommyhoogstra

    I had this issue in the past
    And i had to make one change:
    Code:java
    1. ItemMeta im = i.getItemMeta();
    2. im.setDisplayName(name);
    3. i.setItemMeta(im);


    Which is assigning the item meta to the itemstack

    Edit: which you have, sorry didnt see it -/-
     
  8. Offline

    Barnyard_Owl

    rohan576
    I don't think the error lies in the code. You may be incorrectly exporting or forgetting to reload. Either way, here's how I did it in the past, and it works:
    Code:java
    1. ItemStack ironChest = new ItemStack( Material.IRON_CHESTPLATE, 1 );
    2. ItemMeta im = ironChest.getItemMeta( );
    3. im.setDisplayName( ChatColor.BLUE + "" + ChatColor.BOLD + "Tough" + ChatColor.RESET + ChatColor.BLUE + " Armor" );
    4. im.addEnchant( Enchantment.PROTECTION_ENVIRONMENTAL, 1, true );
    5. ironChest.setItemMeta( im );
     
  9. Offline

    rohan576

    Barnyard_Owl
    I'm most certain that I'm not forgetting to export properly, update, or reload.
     
  10. Offline

    tommyhoogstra

    rohan576 Why do you need a short for ice?

    There is only one type the last time i checked
     
  11. Offline

    rohan576

    tommyhoogstra
    I don't, it's just part of the function so that I can use it for any item. Either way, that's beside what I'm trying to figure out. :p
     
  12. Offline

    tommyhoogstra

    Try remove the short shrt completely just for this test, if its unaffected change it back :p
     
  13. Offline

    rohan576

    tommyhoogstra
    That seems like a very silly idea, but I'll test it anyway...

    Results: Nope, no change!
     
  14. Offline

    tommyhoogstra

    rohan576 ill try this on my test server now, other wise could you send where your inventory is created?
     
  15. Offline

    rohan576

    tommyhoogstra
    Just use some kind of event like a join event. Then just do player.getInventory() to get the inventory
     
  16. Offline

    tommyhoogstra

    I have mine in my public class when creating my inventory, ill try when getting inventory

    NVM: ill stick with join event just for you

    Ok I think I found the problem, It wasn't creating the item at all from what i saw, but then I did p.openInventory(inv); and it shows 9 ice blocks (from me spamming) with yellow names).

    And THEN it added it to my hot bar


    Edit: Instead of using p.openInventory, just use p.updateInventory();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  17. Offline

    rohan576

    tommyhoogstra
    Is there a way of doing updateInventory that isn't Deprecated though? I know it's silly, but I simply don't like using deprecations.
     
  18. Offline

    tommyhoogstra

    You could try something like p.setinventory(p.getInventory) or something along those lines
    or: Player.gerInventory().update(); maybe?
     
  19. Offline

    rohan576

    tommyhoogstra
    I tried two ways; they both don't work.

    EDIT: I also tried updateInventory... and it didn't work. :confused: Did you change anything else?
     
  20. Offline

    tommyhoogstra

    rohan576 I did some research and the only way to do it is with p.updateinventory() it was deprecated for an old bug in 2012 which i'm not sure exactly what it is.
     
  21. Offline

    rohan576

    tommyhoogstra
    I know, but I tried updateInventory, and nothing happened. Did you change something else too?
     
  22. Offline

    tommyhoogstra


    Code:java
    1. @EventHandler
    2. public void playerJoin(PlayerJoinEvent e){
    3. Player p = e.getPlayer();
    4. Inventory inv = p.getInventory();
    5. inv.setItem(0, createItem(Material.ICE,1, ChatColor.YELLOW + "TEST"));
    6. p.updateInventory()
    7. }
    8.  
    9. public ItemStack createItem(Material mat,int quantity ,String name){
    10. ItemStack i = new ItemStack(mat, quantity);
    11. ItemMeta im = i.getItemMeta();
    12. im.setDisplayName(name);
    13. i.setItemMeta(im);
    14. return i;
    15. }


    Mine doesn't include lore or shorts
     
  23. Offline

    rohan576

    tommyhoogstra
    I managed to fix this. Thank you so much for your help; have a wonderful day. :)
     
  24. Offline

    tommyhoogstra

    No worries, PM me if you have any more problems
     
Thread Status:
Not open for further replies.

Share This Page