Item Name Changing Class

Discussion in 'Resources' started by stirante, Oct 6, 2012.

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

    stirante

    Now, this is handled by API. Don't use this class.

    I created simple class which *should* change name of item in version 1.4. I think there are many bugs in code, but I haven't chance to test it.
    Code:
    package ;
     
    import java.util.ArrayList;
     
    import net.minecraft.server.NBTTagCompound;
    import net.minecraft.server.NBTTagList;
    import net.minecraft.server.NBTTagString;
     
    import org.bukkit.craftbukkit.inventory.CraftItemStack;
    import org.bukkit.inventory.ItemStack;
     
    public class ItemNamer {
     
        private static CraftItemStack                    craftStack;
        private static net.minecraft.server.ItemStack    itemStack;
     
        public static void load(ItemStack item) {
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                ItemNamer.itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                ItemNamer.itemStack = craftStack.getHandle();
            }
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("display", new NBTTagCompound());
                itemStack.tag = tag;
            }
        }
     
        public static void setName(String name) {
            NBTTagCompound tag = itemStack.tag.getCompound("display");
            tag.setString("Name", name);
            itemStack.tag.setCompound("display", tag);
        }
     
        public static String getName() {
            NBTTagCompound tag = itemStack.tag.getCompound("display");
            return tag.getString("Name");
        }
     
        public static void addLore(String lore) {
            NBTTagCompound tag = itemStack.tag.getCompound("display");
            NBTTagList list = tag.getList("Lore");
            if (list == null) list = new NBTTagList();
            list.add(new NBTTagString(lore));
            tag.set("Lore", list);
            itemStack.tag.setCompound("display", tag);
        }
     
        public static void setLore(String lore) {
            NBTTagCompound tag = itemStack.tag.getCompound("display");
            NBTTagList list = new NBTTagList();
            list.add(new NBTTagString(lore));
            tag.set("Lore", list);
            itemStack.tag.setCompound("display", tag);
        }
     
        public static String[] getLore() {
            NBTTagCompound tag = itemStack.tag;
            NBTTagList list = tag.getCompound("display").getList("Lore");
            ArrayList<String> strings = new ArrayList<String>();
            String[] lores = new String[] {};
            for (int i = 0; i < strings.size(); i++)
                strings.add(((NBTTagString) list.get(i)).data);
            strings.toArray(lores);
            return lores;
        }
     
        public static org.bukkit.inventory.ItemStack getItemStack() {
            return craftStack;
        }
    }
    
    Maybe someone will find it usefull.
     
    nate0927 and lol768 like this.
  2. Offline

    Icyene

    If this works, then holy.... this + BlockPatcher = new items!
     
    ZeusAllMighty11 likes this.
  3. Offline

    Armadillo

    Did you create this off of the API that they are making on Github????
     
  4. Offline

    stirante

    I'm just adding tags in NBT. In newst snapshot(12W40B) you can change name of all items just by adding in tags compound "display". In this compound you add String "Name" and list of strings "Lore". You can test it now by editing save with NBTEdit. It works!

    EDIT:I don't think it needs BlockPatcher to create new blocks.You can simply check Name tag.
     
  5. Offline

    Icyene

    But with BlockPatcher the actions of the block can be set different from other blocks.
     
  6. Offline

    stirante

    Ok, so I will test it and reply if it works.

    It almost works. The only issue is Name tag should be in compound display but Name tag is just in compound tag. I don't know where is problem, becouse everywhere I put tags in display.

    EDIT: I see whats wrong. In couple of minutes I will edit code in first post.

    EDIT 2:Code works. I also updated code in first post.

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

    Taurhuzz

    How would you test it if 1.4 isn't out yet?
     
  8. Offline

    nisovin

    I believe this should work, but I feel making it all static would make it a bit awkward to use if you want to handle multiple items at a time. This feels a bit nicer:

    Code:
    import java.util.ArrayList;
     
    import net.minecraft.server.NBTTagCompound;
    import net.minecraft.server.NBTTagList;
    import net.minecraft.server.NBTTagString;
     
    import org.bukkit.craftbukkit.inventory.CraftItemStack;
    import org.bukkit.inventory.ItemStack;
     
    public class NamedItemStack {
        private CraftItemStack                    craftStack;
        private net.minecraft.server.ItemStack    itemStack;
     
        public NamedItemStack(ItemStack item) {
            if (item instanceof CraftItemStack) {
                craftStack = (CraftItemStack) item;
                itemStack = craftStack.getHandle();
            }
            else if (item instanceof ItemStack) {
                craftStack = new CraftItemStack(item);
                itemStack = craftStack.getHandle();
            }
            NBTTagCompound tag = itemStack.tag;
            if (tag == null) {
                tag = new NBTTagCompound();
                tag.setCompound("display", new NBTTagCompound());
                itemStack.tag = tag;
            }
        }
     
        public NamedItemStack setName(String name) {
            NBTTagCompound tag = itemStack.tag.getCompound("display");
            tag.setString("Name", name);
            itemStack.tag.setCompound("display", tag);
            return this;
        }
     
        public String getName() {
            NBTTagCompound tag = itemStack.tag.getCompound("display");
            return tag.getString("Name");
        }
     
        public NamedItemStack addLore(String lore) {
            NBTTagCompound tag = itemStack.tag.getCompound("display");
            NBTTagList list = tag.getList("Lore");
            if (list == null) list = new NBTTagList();
            list.add(new NBTTagString(lore));
            tag.set("Lore", list);
            itemStack.tag.setCompound("display", tag);
            return this;
        }
     
        public NamedItemStack setLore(String... lore) {
            NBTTagCompound tag = itemStack.tag.getCompound("display");
            NBTTagList list = new NBTTagList();
            for (String l : lore) {
                list.add(new NBTTagString(l));
            }
            tag.set("Lore", list);
            itemStack.tag.setCompound("display", tag);
            return this;
        }
     
        public String[] getLore() {
            NBTTagCompound tag = itemStack.tag;
            NBTTagList list = tag.getCompound("display").getList("Lore");
            ArrayList<String> strings = new ArrayList<String>();
            String[] lores = new String[] {};
            for (int i = 0; i < strings.size(); i++)
                strings.add(((NBTTagString) list.get(i)).data);
            strings.toArray(lores);
            return lores;
        }
     
        public ItemStack getItemStack() {
            return craftStack;
        }
    }
    
    Then you can use it something like this:

    Code:
    ItemStack item = new NamedItemStack(new ItemStack(Material.BLAZE_ROD)).setName("A Name").setLore("Line 1", "Line 2", "Line 3").getItemStack();
    
     
    Cowboys1919 and xGhOsTkiLLeRx like this.
  9. Offline

    stirante

    I opened .dat file in NBTEdit and it works.

    I think the best option is to make it static and use like this

    ItemNamer.setName(itemStack, "name");

    EDIT:It works. You can use it like that:
    player.setItemInHand(Namer.setName(player.getItemInHand(), "Custom name"));
    Becouse it's new class I will post code here.
    Code:java
    1. package ;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import net.minecraft.server.NBTTagCompound;
    6. import net.minecraft.server.NBTTagList;
    7. import net.minecraft.server.NBTTagString;
    8.  
    9. import org.bukkit.craftbukkit.inventory.CraftItemStack;
    10. import org.bukkit.inventory.ItemStack;
    11.  
    12. public class Namer {
    13.  
    14. private static CraftItemStack craftStack;
    15. private static net.minecraft.server.ItemStack itemStack;
    16.  
    17. public static ItemStack setName(ItemStack item, String name) {
    18. if (item instanceof CraftItemStack) {
    19. craftStack = (CraftItemStack) item;
    20. Namer.itemStack = craftStack.getHandle();
    21. }
    22. else if (item instanceof ItemStack) {
    23. craftStack = new CraftItemStack(item);
    24. Namer.itemStack = craftStack.getHandle();
    25. }
    26. NBTTagCompound tag = itemStack.tag;
    27. if (tag == null) {
    28. tag = new NBTTagCompound();
    29. tag.setCompound("display", new NBTTagCompound());
    30. itemStack.tag = tag;
    31. }
    32.  
    33. tag = itemStack.tag.getCompound("display");
    34. tag.setString("Name", name);
    35. itemStack.tag.setCompound("display", ChatColor.RESET + tag);
    36. return craftStack;
    37. }
    38.  
    39. public static String getName(ItemStack item) {
    40. if (item instanceof CraftItemStack) {
    41. craftStack = (CraftItemStack) item;
    42. Namer.itemStack = craftStack.getHandle();
    43. }
    44. else if (item instanceof ItemStack) {
    45. craftStack = new CraftItemStack(item);
    46. Namer.itemStack = craftStack.getHandle();
    47. }
    48. NBTTagCompound tag = itemStack.tag;
    49. if (tag == null) {
    50. return null;
    51. }
    52. tag = itemStack.tag.getCompound("display");
    53. return tag.getString("Name");
    54. }
    55.  
    56. public ItemStack setLore(ItemStack item, String... lore) {
    57. if (item instanceof CraftItemStack) {
    58. craftStack = (CraftItemStack) item;
    59. Namer.itemStack = craftStack.getHandle();
    60. }
    61. else if (item instanceof ItemStack) {
    62. craftStack = new CraftItemStack(item);
    63. Namer.itemStack = craftStack.getHandle();
    64. }
    65. NBTTagCompound tag = itemStack.tag;
    66. if (tag == null) {
    67. tag = new NBTTagCompound();
    68. tag.setCompound("display", new NBTTagCompound());
    69. itemStack.tag = tag;
    70. }
    71. tag = itemStack.tag.getCompound("display");
    72. NBTTagList list = new NBTTagList();
    73. for (String l : lore) {
    74. list.add(new NBTTagString("", ChatColor.RESET + l));
    75. }
    76. tag.set("Lore", list);
    77. itemStack.tag.setCompound("display", tag);
    78. return craftStack;
    79. }
    80.  
    81. public static ItemStack addLore(ItemStack item, String lore) {
    82. if (item instanceof CraftItemStack) {
    83. craftStack = (CraftItemStack) item;
    84. Namer.itemStack = craftStack.getHandle();
    85. }
    86. else if (item instanceof ItemStack) {
    87. craftStack = new CraftItemStack(item);
    88. Namer.itemStack = craftStack.getHandle();
    89. }
    90. NBTTagCompound tag = itemStack.tag;
    91. if (tag == null) {
    92. tag = new NBTTagCompound();
    93. tag.setCompound("display", new NBTTagCompound());
    94. tag.getCompound("display").set("Lore", new NBTTagList());
    95. itemStack.tag = tag;
    96. }
    97.  
    98. tag = itemStack.tag.getCompound("display");
    99. NBTTagList list = tag.getList("Lore");
    100. list.add(new NBTTagString("", lore));
    101. tag.set("Lore", list);
    102. itemStack.tag.setCompound("display", ChatColor.RESET + tag);
    103. return craftStack;
    104. }
    105.  
    106. public static String[] getLore(ItemStack item) {
    107. if (item instanceof CraftItemStack) {
    108. craftStack = (CraftItemStack) item;
    109. Namer.itemStack = craftStack.getHandle();
    110. }
    111. else if (item instanceof ItemStack) {
    112. craftStack = new CraftItemStack(item);
    113. Namer.itemStack = craftStack.getHandle();
    114. }
    115. NBTTagCompound tag = itemStack.tag;
    116. if (tag == null) {
    117. tag = new NBTTagCompound();
    118. tag.setCompound("display", new NBTTagCompound());
    119. tag.getCompound("display").set("Lore", new NBTTagList());
    120. itemStack.tag = tag;
    121. }
    122. tag = itemStack.tag;
    123. NBTTagList list = tag.getCompound("display").getList("Lore");
    124. ArrayList<String> strings = new ArrayList<String>();
    125. String[] lores = new String[] {};
    126. for (int i = 0; i < strings.size(); i++)
    127. strings.add(((NBTTagString) list.get(i)).data);
    128. strings.toArray(lores);
    129. return lores;
    130. }
    131. }
    132.  
    133.  


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

    stirante

    Finally 1.4!
    Now you can test item naming ingame(source included).
    Link
    To name item put it in your hand and type "/setname here type your custom name"
    Now I'm plannig to create something to control fuse and explosion radius in creeper and another class which uses nbt(CustomPotionEffects).

    EDIT:I forgot about how to use it, so here is example :D
    Code:java
    1.  
    2. player.setItemInHand(Namer.setName(player.getItemInHand(), "Some custom name"));
    3. player.setItemInHand(Namer.addLore(player.getItemInHand(), "Lore 1"));
    4. player.setItemInHand(Namer.addLore(player.getItemInHand(), "Lore 2"));
    5.  
     
  11. Offline

    Shiny Quagsire

    Tip: If you want your item to look more real, add a "§r" before the item name in the code. This resets the italics placed by Minecraft.

    Also, I get an exception saying "Empty String not allowed" when trying to replace lore, but it doesn't trace back to my code. I'm not sure why that is.
     
  12. Offline

    stirante

    I added "§r", but I was too lazy to update the code :p. I'll update code now.

    I'll check whats wrong with this exception

    EDIT:
    I found code which throws exception, but i don't know why. This is from NBTTagString class.
    Code:java
    1.  
    2. public NBTTagString(String par1Str)
    3. {
    4. super(par1Str);
    5. }
    6.  
    7. public NBTTagString(String par1Str, String par2Str)
    8. {
    9. super(par1Str);
    10. this.data = par2Str;
    11.  
    12. if (par2Str == null)
    13. {
    14. throw new IllegalArgumentException("Empty string not allowed");
    15. }
    16. }
    17.  

    But I pass only 1 parameter and this parameter can't be null or empty string.
     
  13. Offline

    nisovin

    Change this:

    Code:
    list.add(new NBTTagString(lore));
    to this:

    Code:
    list.add(new NBTTagString("", lore));
     
  14. Offline

    stirante

    You're genius :D
     
  15. Offline

    Malikk

    Using your class with the 'Empty String' bits fixed, it only works for items placed directly into a player's inventory. If you use, say 'world.dropItemNaturally', it doesn't work.

    Code:
    //The Item spawned by this line doesn't have the name or lore,
    world.dropItemNaturally(loc, myNamedItem);
     
    //While this one does
    player.getInventory().addItem(myNamedItem);
    
    What do you think is the issue?
     
  16. Offline

    nisovin

    I've noticed this as well. The secret to fix it is this:

    Code:
    Item item = world.dropItemNaturally(loc, myNamedItem);
    item.setItemStack(myNamedItem);
    
     
    Malikk likes this.
  17. Offline

    xxNightlordx

    Is there anyway to use this to create a renamed item from a custom recipe?
     
  18. Offline

    Malikk

    Awesome. That would have taken me a lot of guess-and-check to figure out.

    Thanks
     
  19. Offline

    stirante

    Sure. If you can get from crafting armor with nbt tags then you can get renamed item.
    Code:java
    1.  
    2. public void onEnable(){
    3. ItemStack item = new ItemStack(Material.DIAMOND_PICKAXE);
    4. Namer.setName(item, "SuperPickaxe");
    5. ShapelessRecipe recipe = new ShapelessRecipe(item).addIngredient(Material.INK_SACK).addIngredient(Material.DIAMOND_PICKAXE);
    6. getServer().addRecipe(recipe);
    7. }
    8. public void onDisable(){
    9. getServer().clearRecipes();
    10. }
    11.  
     
  20. Offline

    Limeth

    Hello, my goal is to allow people craft lsd, but the name won't show up:
    Code:java
    1.  
    2. ItemStack lsdItemStack = new ItemStack(Material.PAPER, 1, (short) 512);
    3. Namer.setName(lsdItemStack, "LSD");
    4.  
    5. ShapelessRecipe lsdRecipe = new ShapelessRecipe(lsdItemStack).addIngredient(Material.PAPER).addIngredient(Material.RED_MUSHROOM);
    6.  
    7. getServer().addRecipe(lsdRecipe);
    8.  
     
  21. Offline

    stirante

    Maybe, try changing name in PrepareItemCraftEvent
     
  22. thanks a lot for this, i have updated my plugin using it this is the result :



    the only issue i have (is not related to the item naming) is i can't find a way to have a unique view per player for the custom inventory (so if somebody else use it you will see the change on your inventory view too)...
     
  23. Offline

    xxNightlordx

    Can you give an example? I'm having the same problem as Limeth
     
  24. Offline

    stirante

    Awesome!

    Code:java
    1. public void onCraft(PrepareItemCraftEvent e){
    2. if (e.getRecipe().equals(headRecipe)){
    3. e.getInventory().setResult(Namer.setName(e.getInventory().getResult(), e.getViewers().get(0).getName()));
    4. }
    5. }
     
  25. Offline

    xxNightlordx

    Still doesn't work :(
     
  26. Offline

    rmh4209

    Hey, stirante, just wanted to say thanks for the work you do, man. It's been a great help for the DiabloDrops project. :)
     
  27. Offline

    stirante

    If you want more check out my PrettyScaryLib :)
     
  28. Offline

    Hoolean

    So could someone point out the code that works please? xD

    Also, I'd like to include colours! Can anyone show me how to do that?
     
  29. Offline

    rmh4209

    Colors are done by using ChatColor or by using a string replace method. Check out the DiabloDrops source if you want to see some of how it's done.
     
  30. Offline

    flatbmx

    Is it just me, but once you throw away the original minecraft servers itemstack and you then try and get another handle from the same bukkit itemstack it gives you a totally new handle. I can get setname to work fine, however getting the tags once my own nameditem class is gone bukkit doesnt save the tags. Anyone else having this issue aswell?
     
Thread Status:
Not open for further replies.

Share This Page