Setting display name/lores of ItemStacks

Discussion in 'Resources' started by CraftThatBlock, May 14, 2013.

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

    CraftThatBlock

    Basicly:
    (I made it SUPER noob friendly btw ^_^ )
    Code:
            Material material = Material.STICK;
            int ammount = 1;
            String name = "Stickman's arm";
     
            ItemStack item = new ItemStack(material, ammount);
            ItemMeta itemMeta = item.getItemMeta();
            itemMeta.setDisplayName(name);
     
            itemMeta.setLore(Arrays.asList("line1", "line2", "line3"));
     
            item.setItemMeta(itemMeta);
     
    bmzd and com. BOY like this.
  2. Offline

    SDDJ

    Thanks, this is exactly what I needed, and it works perfectly.
     
  3. Offline

    Neilnet

    What about getting the title of the itemstack?
     
  4. Offline

    CraftThatBlock

    I think it should be itemMeta.getDisplayName()
     
  5. Offline

    Neilnet

    Thanks
     
  6. Offline

    Goblom

    CraftThatBlock
    Code:java
    1. public ItemStack createItem(ItemStack item, String name, String[] lore) {
    2. ItemMeta im = item.getItemMeta();
    3. im.setDisplayName(name);
    4. im.setLore(Arrays.asList(lore));
    5. item.setItemMeta(im);
    6. return item;
    7. }


    Then all you need to do is
    Code:java
    1. createItem(new ItemStack(Material.Stone), "IMA STONE", new String[] { "Ima stone", "Ima stone", "Ima stone", "Your not a stone" });
     
  7. Offline

    CraftThatBlock

    Thats basically the points.. Just in a long line, and an extra function. Like I said, this is for noobs mainly.
     
  8. Offline

    Wombosvideo

    This is my Lib to create CustomItems really easy

    Code:java
    1. package YOUR_PACKAGE_NAME;
    2.  
    3. import java.util.Arrays;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Material;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.inventory.Inventory;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.inventory.meta.ItemMeta;
    11.  
    12.  
    13. public class CustomItem extends ItemStack {
    14.  
    15. public CustomItem(Material material){
    16. this.setType(material);
    17. this.setAmount(1);
    18. }
    19.  
    20.  
    21. public CustomItem(Material material, Integer count){
    22. this.setType(material);
    23. this.setAmount(count);
    24. }
    25.  
    26. public CustomItem(Material material, String displayName){
    27. this.setType(material);
    28. ItemMeta im = this.getItemMeta();
    29. im.setDisplayName(displayName);
    30. this.setItemMeta(im);
    31. this.setAmount(1);
    32. }
    33.  
    34. public CustomItem(Material material, List<String> lores){
    35. this.setType(material);
    36. ItemMeta im = this.getItemMeta();
    37. im.setLore(lores);
    38. this.setItemMeta(im);
    39. this.setAmount(1);
    40. }
    41.  
    42. public CustomItem(Material material, Integer count, String displayName){
    43. this.setType(material);
    44. this.setAmount(count);
    45. ItemMeta im = this.getItemMeta();
    46. im.setDisplayName(displayName);
    47. this.setItemMeta(im);
    48.  
    49. }
    50.  
    51. public CustomItem(Material material, Integer count, List<String> lores){
    52. this.setType(material);
    53. this.setAmount(count);
    54. ItemMeta im = this.getItemMeta();
    55. im.setLore(lores);
    56. this.setItemMeta(im);
    57. }
    58.  
    59. public CustomItem(Material material, String displayName, List<String> lores){
    60. this.setType(material);
    61. ItemMeta im = this.getItemMeta();
    62. im.setDisplayName(displayName);
    63. im.setLore(lores);
    64. this.setItemMeta(im);
    65. this.setAmount(1);
    66. }
    67.  
    68. public CustomItem(Material material, Integer count, String displayName, List<String> lores){
    69. this.setType(material);
    70. this.setAmount(count);
    71. ItemMeta im = this.getItemMeta();
    72. im.setDisplayName(displayName);
    73. im.setLore(lores);
    74. this.setItemMeta(im);
    75. }
    76.  
    77. public void add(Player player){
    78. player.getInventory().addItem(this);
    79. }
    80.  
    81. public void add(Inventory inventory){
    82. inventory.addItem(this);
    83. }
    84.  
    85. public void set(Player player, Integer slot){
    86. player.getInventory().setItem(slot, this);
    87. }
    88.  
    89. public void set(Inventory inventory, Integer slot){
    90. inventory.setItem(slot, this);
    91. }
    92.  
    93. }



    USE WITH:

    Code:java
    1. CustomItem ci = new CustomItem(Material.STONE, "§4Magical Stone");
    2. ci.add(player);
    3.  
    4. //OR
    5.  
    6. ItemStack cis = new CustomItem(Material.STONE, "§4Magical Stone");
    7.  
    8. //Both Handled like normal ItemStacks


    I know, i forgot the damage id

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

Share This Page