Item Name Changing Class

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

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

    stirante

    Honestly, I tested only functions that sets something.
     
  2. Offline

    TechTeller96

  3. Offline

    stirante

    Better use code from this post
    Better use code from this post.
     
  4. Offline

    TechTeller96

    Also, how do I see if a player's inventory contains a snowball called "Playername"?
     
  5. Offline

    phondeux

    I'm seeing the same thing and it's driving me nuts; I'm trying to get mobs to drop whatever they're wearing, and it works if I don't give them an item but if given an item they drop two of it. It's like there's a ghost itemstack that's immutable.
     
  6. Offline

    rmh4209

    The ghost ItemStack is an issue that Bukkit has. You can check out DiabloDrops' source if you need any assistance with what you need, however.
     
  7. I've used this class in my plugin to create an item, but now I'm trying to take 1 of this item from a players inventory.
    How can I do this?
     
  8. How would I use this? :eek:
    Code:
    ItemStack itemstack = event.getCurrentItem();
    String itemname = new NamedItemStack(new ItemStack(itemstack)).getName();
    
    I've tried this, but that doesn't work.
     
  9. Offline

    stirante

    You're using wrong class. I mean it's not my class. Classes always up to date are on github.
     
  10. Yeah I know, it's from this post.
     
  11. Offline

    iZanax

    stirante

    The "public static String[] getLore(ItemStack item)" seems to return always
    an empty String[], even I really set a Lore with Namer.setLore(item,"test");
    So do I do something wrong?
    Also I only need 1 line of the Lore is there no easier way for getting the Lore?
     
  12. Offline

    stirante

    I found whats wrong :D I'll fix this. It will be updated here.

    EDIT:Code fixed :D
     
  13. Offline

    iZanax

    Thanks a lot! :)
     
  14. Offline

    JeroenV

    stirante I keep getting a NullPointerException with this
    Code:
    itemStack.c(ChatColor.RESET + name);
    after using:
    Code:
    player.setItemInHand(Namer.setName(player.getItemInHand(), "Some custom name"));
    What's wrong?
     
  15. Offline

    stirante

    Can you post whole error? I've looked through code and I don't know what can cause NPE.
     
  16. Offline

    JeroenV

    stirante
    NodesMain
    Code:
    package Nodes;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.PlayerInventory;
     
    public class NodesMain {
     
     
        public static void BuyNode(Player player){
        PlayerInventory Inventory = player.getInventory();
        int ItemId = player.getItemInHand().getTypeId();
        if(ItemId == 0){
        player.getItemInHand().setType(Material.CHEST);
        player.setItemInHand(Namer.setName(player.getItemInHand(), "Food Node 1"));
        }
        else{
        }
        }
    }
    
    Your Classnamer Class
    Code:
    package Nodes;
     
    import net.minecraft.server.NBTTagCompound;
    import net.minecraft.server.NBTTagList;
    import net.minecraft.server.NBTTagString;
     
    import org.bukkit.ChatColor;
    import org.bukkit.craftbukkit.inventory.CraftItemStack;
    import org.bukkit.inventory.ItemStack;
     
    /**
    * Class, that allows setting and getting name and lore of item.
    */
    public class Namer {
     
    /** craft stack. */
    private static CraftItemStack    craftStack;
     
    /** item stack. */
    private static net.minecraft.server.ItemStack    itemStack;
     
    /**
    * Sets name.
    *
    * @param item item
    * @param name name
    * @return item stack
    */
    public static ItemStack setName(ItemStack item, String name) {
    if (item instanceof CraftItemStack) {
    craftStack = (CraftItemStack) item;
    itemStack = craftStack.getHandle();
    }
    else if (item instanceof ItemStack) {
    craftStack = new CraftItemStack(item);
    itemStack = craftStack.getHandle();
    }
    itemStack.c(ChatColor.RESET + name);
    return craftStack;
    }
     
    /**
    * Gets name.
    *
    * @param item item
    * @return name
    */
    public static String getName(ItemStack item) {
    if (item instanceof CraftItemStack) {
    craftStack = (CraftItemStack) item;
    Namer.itemStack = craftStack.getHandle();
    }
    else if (item instanceof ItemStack) {
    craftStack = new CraftItemStack(item);
    Namer.itemStack = craftStack.getHandle();
    }
    return itemStack.r();
    }
     
    /**
    * Sets lore.
    *
    * @param item item
    * @param lore lore
    * @return item stack
    */
    public ItemStack setLore(ItemStack item, String... lore) {
    if (item instanceof CraftItemStack) {
    craftStack = (CraftItemStack) item;
    Namer.itemStack = craftStack.getHandle();
    }
    else if (item instanceof ItemStack) {
    craftStack = new CraftItemStack(item);
    Namer.itemStack = craftStack.getHandle();
    }
    NBTTagCompound tag = itemStack.tag;
    if (tag == null) {
    tag = new NBTTagCompound();
    tag.setCompound("display", new NBTTagCompound());
    itemStack.tag = tag;
    }
            tag = itemStack.tag.getCompound("display");
            NBTTagList list = new NBTTagList();
            for (String l : lore) {
                list.add(new NBTTagString(ChatColor.RESET + l));
            }
            tag.set("Lore", list);
            itemStack.tag.setCompound("display", tag);
            return craftStack;
        }
     
    /**
    * Adds lore.
    *
    * @param item item
    * @param lore lore
    * @return item stack
    */
    public static ItemStack addLore(ItemStack item, String lore) {
    if (item instanceof CraftItemStack) {
    craftStack = (CraftItemStack) item;
    Namer.itemStack = craftStack.getHandle();
    }
    else if (item instanceof ItemStack) {
    craftStack = new CraftItemStack(item);
    Namer.itemStack = craftStack.getHandle();
    }
    NBTTagCompound tag = itemStack.tag;
    if (tag == null) {
    tag = new NBTTagCompound();
    tag.setCompound("display", new NBTTagCompound());
    tag.getCompound("display").set("Lore", new NBTTagList());
    itemStack.tag = tag;
    }
     
    tag = itemStack.tag.getCompound("display");
    NBTTagList list = tag.getList("Lore");
    list.add(new NBTTagString(ChatColor.RESET + lore));
    tag.set("Lore", list);
    itemStack.tag.setCompound("display", tag);
    return craftStack;
    }
     
    /**
    * Gets lore.
    *
    * @param item item
    * @return lore
    */
    public static String[] getLore(ItemStack item) {
    if (item instanceof CraftItemStack) {
    craftStack = (CraftItemStack) item;
    Namer.itemStack = craftStack.getHandle();
    }
    else if (item instanceof ItemStack) {
    craftStack = new CraftItemStack(item);
    Namer.itemStack = craftStack.getHandle();
    }
    NBTTagCompound tag = itemStack.tag;
    if (tag == null) {
    tag = new NBTTagCompound();
    tag.setCompound("display", new NBTTagCompound());
    tag.getCompound("display").set("Lore", new NBTTagList());
    itemStack.tag = tag;
    }
    tag = itemStack.tag;
    NBTTagList list = tag.getCompound("display").getList("Lore");
    String[] lores = new String[list.size()];
    for (int i = 0; i < list.size(); i++)
    lores[i] = ((NBTTagString) list.get(i)).data;
    return lores;
    }
    }

    --------
    I'm using the latest bukkit beta build, with my external jar as craftbukkit itself.

    @stirante Can you find what's wrong here? I can't really continue my project if this doesn't work out.

    When I change the code so the iteminhand isn't air, I get this:

    code:
    Code:
        public static void BuyNode(Player player){
        PlayerInventory Inventory = player.getInventory();
        int ItemId = player.getItemInHand().getTypeId();
        if(ItemId == 3){
        player.getItemInHand().setType(Material.CHEST);
        player.setItemInHand(Namer.setName(player.getItemInHand(), "Food fvvfdv"));   
        player.setItemInHand(Namer.addLore(player.getItemInHand(), "Lore 1"));
        player.setItemInHand(Namer.addLore(player.getItemInHand(), "Lore 2"));   
        }
        else{
        }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  17. Offline

    stirante

    For now you can use code which changes name in nbt tags from earlier posts. Bug is in method from nms I think.
     
  18. Offline

    JeroenV

    stirante

    I used the code from the first post and adjusted my code in MainNode class:
    Code:
    public class NodesMain {
     
     
        public static void BuyNode(Player player){
        PlayerInventory Inventory = player.getInventory();
        int ItemId = player.getItemInHand().getTypeId();
        if(ItemId == 3){
        ItemStack item = new ItemStack(new ItemStack(Material.DIRT)).setName("A Name").setLore("Line 1", "Line 2", "Line 3").getItemStack();
        Namer.setName("FoodNode1");
        }
        else{
        }
        }
    }
    But here .setName() shows up red with the error:
    I can see that this is because of switching with minecraft.net.server.ItemStack and bukkit.org.Itemstack.
    This error was with bukkit.org.Itemstack.

    However if I use minecraft.net.server.Itemstack it says:
    No matter how many times I tell it to use Itemstack(Itemstack) it keeps showing up together with the first error.
     
  19. Offline

    stirante

    Better use this.
     
  20. Offline

    JeroenV

    Ah sorry, I thought that was the class I was using before but there are differences.

    I do get some code showing up in red colour here that I'm unsure about how to solve without breaking any of your code:
    Code:
    itemStack.tag.setCompound("display", ChatColor.RESET + tag);
    For ChatColor.RESET + tag.
    Sorry to be bothering you with this.



    EDIT: I just removed ChatColor.RESET and it all worked out fine :) Tyvm
     
  21. Offline

    stirante

    It's old code. If you're using getLore method better paste function from github.
     
  22. Offline

    JeroenV

    Alright, thanks alot again.

    I'm trying to figure out how to use this with a BlockPlace/Break event without deleting the name.
    Code:
        @EventHandler
        public void BlockPlaceEvent(org.bukkit.event.block.BlockPlaceEvent event){
        Player player = event.getPlayer();
        String playername = player.getPlayerListName();
        Material mat = event.getBlock().getType();
     
        //CHEST
        if(mat == Material.CHEST){
        String node = Namer.getName(new ItemStack(mat));
        player.sendMessage(node);
        }
    When I use new ItemStack, it's going to delete the itemstack that had the name and create a new one with the default name, right? That's where I have a problem because these events to have a get...() that would return an itemstack.
     
  23. Offline

    stirante

    Yes, If you want to save name you will have to set metadata for every block.
     
  24. Offline

    JeroenV

    stirante
    How would I go about that? I can't figure anyway to get data from an itemstack that I can use to get it back. Other than perhaps setting up a hashmap with (itemstack:playername+int). To track it around.. not sure if that's such a good idea.

    EDIT: I would only need the name on the blockplaceevent to figure out if they're placing down a certain named item. And then do stuff with that. I suppose getItemInHand() wont work cause it's already placed.
     
  25. Offline

    stirante

    Maybe store data on PlayerInteractEvent. Check if event.getPlayer().getItemInHand() is your named block and save to HashMap location, where this blockwill be placed.
     
  26. Awesome !!! But everything is broken now, right? :(
     
  27. Offline

    lol768

    This is implemented in Bukkit

    See item.getMeta();
     
  28. Offline

    ImDeJay

    i was on the github page the other day and this Namer.java was there. but i hadnt upgraded my craftbukkit so i didnt get the class, but now i've updated my craftbukkit and that class is gone.

    will it be back?
     
  29. Offline

    aviator14

    See the post above yours. This lib is still very useful, but stirante removed things that got added to Bukkit. Item naming, book writing, armor coloring, wolf collar dying, skeleton api and zombie api have all been added to Bukkit, so those have all been taken out of here. He did leave his own skele/zombie type changing class in here though, because he did it in a way that updates the AI (Bukkit's doesn't apparently). Bukkit added api for skull item, but apparently not the block, so that class is still here too.
     
    stirante likes this.
  30. Offline

    ImDeJay

    yeah, i read that post after i posted and realised, so i looked into how bukkit done it and done it their way, thanks though.
     
Thread Status:
Not open for further replies.

Share This Page