Is it possible to rename items?

Discussion in 'Plugin Development' started by linkrock4, Nov 30, 2012.

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

    linkrock4

    Hey, just wanted to know if its possible to rename items, and if it is, do any of you have any example so i can see how it works? Thanks!
     
  2. Offline

    MCForger

  3. Offline

    linkrock4

    Thanks! :D

    I dont know what to write in the public void, no idea honestly o.o

    public void (--->What should i write in here?<---);
    ItemStack anvil = new ItemStack(Material.DIAMOND_ORE);
    NamedItemStack namedItemStack = new NamedItemStack(anvil);
    // Give it a name
    namedItemStack.setName("Present");
    // Get the item's name (returns null if it hasn't been renamed)
    namedItemStack.getName();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  4. public NamedItemStack nameItem(ItemStack is, String name){
    NamedItemStack nis = new NamedItemStack(is);
    nis.setName(name);
    return nis;
    }

    Did you read any java tutorials?

    Edit: this is a little bit more lightweight:

    item is your CraftItemStack and name is a String, return item.

    NBTTagCompound tag = new NBTTagCompound();
    NBTTagCompound display = new NBTTagCompound();
    tag.setCompound("display", display);
    display.setString("Name", name);
    item.getHandle().tag = tag;
     
  5. Offline

    whitehooder

    I've got one question for you, mncat77. How may I use this so that I don't rename all other items of the same type too?
    Example: I rename a diamond sword to "The awesome sword" and the other diamond swords I've got in the inventory changes too.
    Thanks in advance.
     
  6. Offline

    d33k40

    You rename the ItemStack

    [​IMG]

    i made a few weeks ago a plugin for naming items with formatting, MAGIC too.
     
  7. Use the naming method only on one of the diamond swords and not all the itemstacks that are diamond swords.
    I guess you are looping through the inventory and rename every diamond sword, so you could just add a break; after renaming one. Code would be helpful to know what's the error
     
  8. Offline

    whitehooder

    I have renamed the ItemStack. I then tried adding the ItemStack to the players inventory and then adding another ItemStack of the same item (without the custom name). What I ended up with was two (stacked) items with the custom name.
    Note:
    I am spawning the items. The error is that they stack and therefore recieves the same name. I am not adding them at the same line, just to let you know.
    Code:
    Code:java
    1. CraftItemStack chair = new CraftItemStack(Material.WOOD_STAIRS, 1);
    2. NBTTagCompound tag = new NBTTagCompound();
    3. NBTTagCompound display = new NBTTagCompound();
    4. tag.setCompound("display", display);
    5. display.setString("Name", "Wooden chair");
    6. chair.getHandle().tag = tag;
    7.  
    8. //adding...
    9.  
    10. player.getInventory().addItem(chair);
    11. player.getInventory().addItem(new ItemStack(Material.WOOD_STAIRS, 1));
     
  9. Offline

    d33k40

    why do that? so weird.
     
  10. Offline

    fireblast709

    More lightweight, with ItemStack item:
    ((CraftItemStack)item).getHandle().c("your name here")
     
    whitehooder and mncat77 like this.
  11. Oh nice, didn't know that there was a method in the native minecraft code for that. I'll use that from now on ^^.
     
  12. Offline

    whitehooder

    Thanks fireblast709 :)
    Problem still there tho ;)
     
  13. Offline

    fireblast709

    You could try creating an ItemStack, loop through the inventory and if it is a diamond sword replace it with a clone of the renamed one
     
  14. Offline

    whitehooder

    Huh? Either I do not fully understand you or you misunderstood me ;)
    What I am trying to achieve is to add an item with a custom name to the players inventory without changing the name of all other items of the same type. This happens only with stacking items so a diamond sword should be no big deal.
    The error/bug is that when I give a player a stackable item (such as a stoneblock) that is renamed and the player already has stone in his inventory, the item I add stacks with them and makes them change name too. I would like it to appear in two different stacks. I could certainly add it to the inventory and make sure it does not stack with other items, but the player will then be able to drag it over to the stack and rename all the other blocks himself.
    Get my point, eh?

    EDIT: I now discovered that by adding the item to the players inventory and letting it stack does not change the name of it. However, I still want to be able to spawn an item with custom name without this happening. Maybe I could add a custom data value to the block so it won't be able to stack?

    Thanks.
     
  15. Offline

    linkrock4

    Does that work? :confused:
    Code:
    item is your CraftItemStack and name is a String,
     
    return Material.COMMAND
    NBTTagCompound tag = new NBTTagCompound();
    NBTTagCompound display = new NBTTagCompound();
    tag.setCompound("Present", display);
    display.setString("Present", name);
    item.getHandle().tag = tag;
     
  16. There is no 'Present' tag, if that was supposed to be the name you'd have to change name into "Present" (the second).
    For simlicity use the one fireblast recommended : ((CraftItemStack)item).getHandle().c("Present").
     
  17. Offline

    linkrock4

    Sorry for my noobness but where do i place ((CraftItemStack)item).getHandle().c("Present"); ?
     
  18. You put this line of code after you have defined item and before you add item to the players inventory (or whatever you want to do with it).
     
  19. Offline

    linkrock4

    I want to simply just rename it :confused:
     
  20. That's what that line of code does....
     
  21. Offline

    linkrock4

    Yes i know but i dont know where to place it .-.

    Im a VERY newbie to java :p
     
  22. Offline

    Bobfan

    Does this rename the item itself or the specific itemstack?
     
  23. Offline

    whitehooder

    Specific itemstack. only problem is with stackables
     
    Bobfan likes this.
  24. Offline

    gomeow

    This is sample code that I tested that could be run when a player picks an item up. Code from above.
    Code:
    @EventHandler
        public void onPickup(PlayerPickupItemEvent event) {
            ((CraftItemStack)event.getItem().getItemStack()).getHandle().c("MyName");
        }
     
Thread Status:
Not open for further replies.

Share This Page