Making custom item names

Discussion in 'Plugin Development' started by Mineblemone, Jun 22, 2013.

Thread Status:
Not open for further replies.
  1. How can I make it so when I give a player an item it has a custom name? I've seen it before, so I know it's possible.
     
  2. Offline

    Alex3543

    ItemMeta

    ItemStack bow = new ItemStack(Material.BOW, 1);
    ItemMeta bm = bow.getItemMeta();
    bm.setDisplayName("§2Special Bow");
    bm.setLore(Arrays.asList("§4Its a Bow."));
    bow.setItemMeta(bm);

    player.getInventory().addItem(new ItemStack(bow));
     
  3. Offline

    Alex5657

    There is no need for bow.setItemMeta(bm). You already have the object reference and you are modify it. Its just how java works
     
  4. Offline

    Alex3543



    Ok thanks :)
     
  5. Where do I put this?
     
  6. Offline

    pkt

    Yes you do... how else will the meta be set to what you have edited it to be?

    There is no need for the New ItemStack(bow) at the bottom, as bow is already an itemstack/has already been set

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
    TheTrixsta likes this.
  7. pkt
    No you don't it works fine without it.

    Question:
    How do I make it so that every player that joins gets this item?
     
  8. Offline

    pkt

    Again, how will it be assigned to the meta that you edited without using that method?

    Use the PlayerJoinEvent to give it to every player that joins:
    Code:
        @EventHandler
        public void join(PlayerJoinEvent event) {
            ItemStack bow = new ItemStack(Material.BOW, 1);
            ItemMeta bm = bow.getItemMeta();
            bm.setDisplayName(ChatColor.GREEN + "Special Bow");
            bm.setLore(Arrays.asList(ChatColor.RED + "Its a Bow."));
            bow.setItemMeta(bm);
            event.getPlayer().getInventory().addItem(bow);
        }
     
  9. thabks

    pkt
    It doesn't work.
    Here is my code:
    @EventHandler
    public void join(PlayerJoinEvent event) {
    ItemStack emerald = new ItemStack(Material.EMERALD, 1);
    ItemMeta bm = emerald.getItemMeta();
    bm.setDisplayName(ChatColor.GREEN + "Click to go to"+ ChatColor.RED+"Infected");
    emerald.setItemMeta(bm);
    event.getPlayer().getInventory().addItem(emerald);
    }
    It does nothing.

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

    pkt

    It does, you just didn't register your Listener
     
  11. pkt
    How do I do that?
     
  12. Offline

    pkt

    getServer().getPluginManager().registerEvents(class, plugin);
     
  13. pkt
    Im being a noob. but I havent done events before. Where do I put this?
     
  14. Offline

    Alex5657

    You want me to teach you java? When you type emerald.getMeta() it returns a reference to the object in memory. WHATEVER you do to this object will be done to the original one. That, sir, is one of the most important concepts of java. Please don't comment on something unless you know for sure
     
  15. Offline

    pkt

    In a class that implements Listeners
     
  16. Offline

    Alex5657

    You do:
    getServer().getPluginManager().registerEvents(new <your class name>(), this);

    in the onEnable method
     
  17. Offline

    pkt

    Lol, don't get mad bro, I think you need to test it yourself, then say sorry to me
     
  18. Offline

    Alex5657

    You are being the noob now. You put this in your plugin class

    I think you do. I have a book right here that says so.

    Plus, I have tested this, not only here.

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

    pkt

    Being a noob? I told him to put it in a class that implements listener.. is that not true? Yes it is true

    Omg, you so dumb, can we get a staff member here to shut this kid up?

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

    Alex5657

    He is asking where to put:
    getServer().getPluginManager().registerEvents(new <your class name>(), this);

    or am I just mistaken?
     
  21. Offline

    pkt

    Yea ? what's your point?
    He can either put that in his onEnable or he can put it in a classes constructor
     
  22. Offline

    Alex5657

    Listen, kid, you are stubborn and immature. You proved that by this statement. What I recommend you do, is test it out. I know that for sure.

    Wow, you said put it into your listener class. Take the time to read at least what you wrote

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

    pkt

    I have -_- go test that exact code, and tell me what happens... or Mineblemone can test it so you won't lie
     
  24. Offline

    Alex5657

  25. Offline

    pkt

    Do you not know what a listener class is? It's a class that implements Listener. Example:
    public class Stuff implements Listener {
     
  26. Offline

    TheTrixsta


    Its a completely new instance, So you are not directly editing the item's metadata
     
  27. Offline

    Alex5657

    I know. You said put the getServer().getPluginManager().registerEvents(new <your class name>(), this); into the listener. Clearly wrong.
     
  28. Offline

    pkt

    Thank you for clearifing
     
  29. Offline

    Alex5657

    Ah, thank you. I was wrong pkt, you were right.
     
  30. Offline

    pkt

    Thank you :)
     
Thread Status:
Not open for further replies.

Share This Page