Editing the name of Itemstacks

Discussion in 'Plugin Development' started by xXCryptoFreakXx, Nov 16, 2012.

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

    xXCryptoFreakXx

    Well, I've been looking for how to edit the names of a Stack, and if possible, to add lines of text to it (For example, the "Dyed" text that is added on a Dyed armor), but you see, I'm not very bright. Could someone show me how it's done, if at all?

    nothing?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  2. Offline

    Jogy34

    Yes it's possible. It's part of the new 1.4 NBTTags under a list called Lore. Are you looking for a plugin that does this or help with coding for it?
     
  3. Offline

    xXCryptoFreakXx

    I need help coding it :p
     
  4. Offline

    Jogy34

    This should probably be in the plugin development section for future reference. This section is for asking people to make plugins for you. Either way here's a thread that I started a while back when I wanted help with NBTTags.
     
  5. Offline

    xXCryptoFreakXx

    Woah my bad, I posted in the wrong section on accident. Thanks xD

    Once again, I feel like I'm missing a big picture. But, am I supposed to import NBTTagList? And what is it? I'm new at this so please bear with me.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  6. Offline

    Gravity

    Moved to plugin development.
     
  7. Offline

    xXCryptoFreakXx

  8. Offline

    Jogy34

    You have to first cast an ItemStack to a CraftItemStack and then use the getHandle() method to get the minecraft ItemStack and then use .tag or the .getTag() method to get the tag. You would store that into an NBTTagCompound. You would then have to check to see if that compound has the display compound which holds the color of the item(leather armor) the name and the lore for the item. If it doesn't have the display compound you have to set it to an empty compound. You then store the display compound into a different variable. Then to edit the lore you have to check to see if the diaplay compound has a lore list and if it doesn't then set it to a new blank NBTTagList. You then store the lore list into a new NBTTagList object then you add NBTTagString objects to the list and reset the lore list to the modified list as a precaution.

    The code would be like this:
    Code:
    ItemStack is = new ItemStack(Material.STONE, 1);
    CraftItemStack cis = new CraftItemStack(is);
    //Gets the NBTTagCompound for the item stack
    NBTTagCompound tag = cis.getHandle().getTag();
    //If the tag doesnt exist, create one.
    if(tag == null)
    {
    cis.getHandle().setTag(new NBTTagCompound());
    }
    tag = cis.getHandle().getTag();
     
    if(!tag.hasKey("display"))
    {
        tag.set("display", new NBTTagCompound());
    }
     
    NBTTagCompound display = tag.getCompound("display");
     
    if(!display.hasKey("Lore"))
    {
        display.set("Lore", new NBTTagList());
    }
     
    NBTTagList loreList = display.getList("Lore");
    loreList.add(new NBTTagString("", ChatColor.DARK_PURPLE + "Magic Stone");
    loreList.add(new NBTTagString("", ChatColor.DARK_PURPLE + "Something Else");
            
    display.set("Lore", loreList);
     
  9. Offline

    xXCryptoFreakXx

    It says that CraftItemStack "Cannot be resolved to a type"

    Also, is it okay if instead of ChatColor.DARK_BLUE I just put §1 before the text? It seems to be working just fine for me.
     
  10. Offline

    Jogy34

    It should be fine and you have to be using the CraftBukkit.jar not the Bukkit.Jar and this is the inport: org.bukkit.craftbukkit.inventory.CraftItemStack;
     
  11. Offline

    xXCryptoFreakXx

    Aww dang it. The whole time I've been using the bukkit jar since it was the one on the wiki. Where can I find the Craftbukkit jar, and whats the difference?
     
  12. Offline

    Drkmaster83

    So wait, by doing this, we can change the names of items without anvils? In-game?

    You can download the latest CraftBukkit.jar here. I highly recommend getting the 1.4.2-1.4.4 versions, as it's not suggested to use development builds. The difference? I have forgotten... Dx

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  13. Offline

    xXCryptoFreakXx

    wait wait wait wait wait wait. So the sever jar also works as the reference jar?!
     
  14. Offline

    Drkmaster83

    Hells yeah.
     
  15. Offline

    Jogy34

    Yes but to change the name you wouldn't get a list instead you would just do display.setString("Name", "New Name"); where display is the display NBTTagCompound
     
  16. Offline

    xXCryptoFreakXx

    So... Since I had alot of "name changing" to do I created a method for it, but um... that didn't go so well.

    Here's what I have:

    Code:
    if (itemid == 267 || itemid == 268 || itemid == 276 || itemid == 283 || itemid == 272)
                            {               
                                nameitem(stack,"Legendary");   
    
    Code:
    public void nameitem(ItemStack stack, String prefix)
        {
            senda.sendMessage("so um it's doing something?");
            CraftItemStack cis = new CraftItemStack(stack);
            //Gets the NBTTagCompound for the item stack
            NBTTagCompound tag = cis.getHandle().getTag();
            //If the tag doesnt exist, create one.
            if(tag == null)
            {
            cis.getHandle().setTag(new NBTTagCompound());
            }
            tag = cis.getHandle().getTag();
           
            if(!tag.hasKey("display"))
            {
                tag.set("display", new NBTTagCompound());
            }
           
            NBTTagCompound display = tag.getCompound("display");
           
            if(!display.hasKey("Lore"))
            {
                display.set("Lore", new NBTTagList());
            }
           
            NBTTagList loreList = display.getList("Lore");
            loreList.add(new NBTTagString("§9§l" + prefix.toUpperCase() + " " + stack.toString().toUpperCase()));
            loreList.add(new NBTTagString("Something Else"));
                   
            display.set("Lore", loreList);
        }
    
    What did I do wrong this time? :3

    Oh yeah but I know the method is doing something since the "senda.sendMessage()" actually sends it.

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

    Jogy34

    CraftItemStack cis = new CraftItemStack(stack);

    Should be:

    CraftItemStack cis = (CraftItemStack)stack;

    If it won't let you do that then you have to return the CraftItemStack object and replace the original ItemStack with it in the player's inventory
     
  18. Offline

    xXCryptoFreakXx

    Yeah I did that and it didnt go so well....
    [​IMG]
     
  19. Offline

    Jogy34

    I was having this same problem before:

    This:
    loreList.add(new NBTTagString("§9§l" + prefix.toUpperCase() + " " + stack.toString().toUpperCase()));
    loreList.add(new NBTTagString("Something Else"));

    Should be this:
    loreList.add(new NBTTagString("", "§9§l" + prefix.toUpperCase() + " " + stack.toString().toUpperCase()));
    loreList.add(new NBTTagString("", "Something Else"));
     
  20. Offline

    xXCryptoFreakXx

    Well it seems to be working, but how does on remove Lists? I seem to be having a few problems:
    [​IMG]
     
  21. Offline

    Jogy34

    I'm not really seeing what the problem is. It's doing exactly what you are telling it to do. But to remove things you have to set the Lore section to a new NBTTagList to reset everything. Before hand you would get everything from the list and store them then re-add them but without the one that you want to remove
     
  22. Offline

    xXCryptoFreakXx

    Well, I created a completely new tag list, but the name and the enchantments were still there, and my title was still at the bottom. How do I get rid of or change the title and enchantments?
     
  23. Offline

    Jogy34

    Do you want to change the name of the item? Also if you were to get rid of the enchantments it would only be a normal diamond sword but to do that you can either do it the original bukkit way or you can just do tag.set("ench", new NBTTagList()); <---- That's the original tag not the display tag
     
  24. Offline

    xXCryptoFreakXx

    Now, what is the difference between a TagList and a TagCompound?
     
  25. Offline

    fireblast709

    NMS code does the name like this:
    Code:java
    1. public void c(String s)
    2. {
    3. if (this.tag == null) {
    4. this.tag = new NBTTagCompound();
    5. }
    6.  
    7. if (!this.tag.hasKey("display")) {
    8. this.tag.setCompound("display", new NBTTagCompound());
    9. }
    10.  
    11. this.tag.getCompound("display").setString("Name", s);
    12. }


    Lore is basically the same (according to this, scroll down to item structure)
    1. get its tag
    2. if its null, set it to a new NBTTagCompound
    3. if the tag has not the key "display", create a new one (like in the code)
    4. then set its "Lore" (instead of "Name")
     
  26. Offline

    xXCryptoFreakXx

    Okay, so after looking at that useful link you gave me, I got this:

    Code:
       
            senda.sendMessage("so um it's doing something?");
            CraftItemStack cis = (CraftItemStack)stack;
            //Gets the NBTTagCompound for the item stack
            NBTTagCompound tag = cis.getHandle().getTag();
            //If the tag doesnt exist, create one.
            if(tag == null)
            {
            cis.getHandle().setTag(new NBTTagCompound());
            }
            tag = cis.getHandle().getTag();
           
            if(!tag.hasKey("display"))
            {
                tag.set("display", new NBTTagCompound());
            }
            NBTTagCompound display = tag.getCompound("display");
           
            if(!display.hasKey("Lore"))
            {
                display.set("Lore", new NBTTagList());
            }
            if(!display.hasKey("Name"))
            {
                display.set("Name", new NBTTagList());
            }
           
     
            NBTTagList newlore = new NBTTagList();
            NBTTagList newtitle = new NBTTagList();
     
            newtitle.add(new NBTTagString("", "§b§l" + prefix.toUpperCase() + " " +                 
            stack.getType().toString().toUpperCase()));   
     
            newlore.add(new NBTTagString("", "§9§nA Legendary " + stack.getType().toString().toLowerCase()));   
            newlore.add(new NBTTagString("", "§9§nonce wielded by a god."));   
     
            display.set("Lore", newlore);
            display.set("Name", newtitle);
    
    When the command is used, it still enchants the item, but the INSTANT your mouse hovers over it, you get a client-side crash:

    [​IMG]

    Oh fire, I'll try that

    Oh wow, everything works. Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page