How to add a custom item name? (And maybe lore)

Discussion in 'Plugin Development' started by FirecatHD, Nov 14, 2013.

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

    FirecatHD

    sgavster SkillSam

    Hi, i need a little bit help with this :confused:

    I need to set the name of the DIAMOND_SWORD to "Basic sword",
    the BOW name to "Basic bow" and the WHEAT name to "Hay"*

    I dont know much about itemMeta so i am stuck here, so if you can, please help :)

    *Note: YOU dont need to code it for me, just show me how (I wanna learn it) :rolleyes:

    Source code:

    Code:java
    1. package me.FirecatHD.TBS_PvP;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Color;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.enchantments.Enchantment;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.inventory.PlayerInventory;
    12. import org.bukkit.inventory.meta.LeatherArmorMeta;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.potion.PotionEffect;
    15. import org.bukkit.potion.PotionEffectType;
    16.  
    17. public class Kits extends JavaPlugin {
    18.  
    19. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    20. if (!(sender instanceof Player)) {
    21. sender.sendMessage(ChatColor.RED + "Only players can get kits!");
    22. return true;
    23. }
    24.  
    25. Player p = (Player) sender;
    26. PlayerInventory pi = p.getInventory();
    27.  
    28. if (cmd.getName().equalsIgnoreCase("basic")) {
    29.  
    30. pi.clear();
    31. pi.setArmorContents(null);
    32.  
    33. for(PotionEffect effect : p.getActivePotionEffects())
    34. {
    35. p.removePotionEffect(effect.getType());
    36. }
    37.  
    38. ItemStack Sword = new ItemStack(Material.DIAMOND_SWORD, 1);
    39. Sword.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    40. Sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
    41. pi.addItem(Sword);
    42.  
    43. ItemStack Bow = new ItemStack(Material.BOW, 1);
    44. Bow.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    45. Bow.addUnsafeEnchantment(Enchantment.ARROW_INFINITE, 1000);
    46. Bow.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 1);
    47. pi.addItem(Bow);
    48.  
    49. pi.setHelmet(new ItemStack(Material.IRON_HELMET));
    50. pi.setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
    51. pi.setLeggings(new ItemStack(Material.IRON_LEGGINGS));
    52. pi.setBoots(new ItemStack(Material.IRON_BOOTS));
    53.  
    54. pi.getHelmet().addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    55. pi.getChestplate().addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    56. pi.getLeggings().addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    57. pi.getBoots().addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    58.  
    59. pi.getHelmet().addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    60. pi.getChestplate().addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    61. pi.getLeggings().addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    62. pi.getBoots().addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    63.  
    64.  
    65. for(int i = 0; i < 15; i++) {
    66. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP , 1));
    67. }
    68. pi.addItem(new ItemStack(Material.ARROW , 1));
    69. for(int i = 0; i < 18; i++) {
    70. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP , 1));
    71. }
    72.  
    73. p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "TBS PvP" + ChatColor.BLACK + "]" + ChatColor.GRAY + ":" + ChatColor.GREEN + "You equipped " + ChatColor.BLUE + "Basic! " + ChatColor.GRAY + "Its balanced, but yet strong!");
    74. return true;
    75. }
    76.  
    77. {
    78.  
    79. if (cmd.getName().equalsIgnoreCase("derpy")) {
    80.  
    81. pi.clear();
    82. pi.setArmorContents(null);
    83.  
    84. for(PotionEffect effect : p.getActivePotionEffects())
    85. {
    86. p.removePotionEffect(effect.getType());
    87. }
    88.  
    89. //Potion effects
    90.  
    91. p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 1000000, 0));
    92. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 1000000, 2));
    93. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000, 1));
    94. p.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 1000000, 1));
    95.  
    96. //Enchantment's
    97.  
    98. ItemStack Sword = new ItemStack(Material.WHEAT, 1);
    99. Sword.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    100. Sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 3);
    101. pi.addItem(Sword);
    102.  
    103. //Armour
    104.  
    105. pi.setHelmet(new ItemStack(Material.LEATHER_HELMET));
    106. pi.setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE));
    107. pi.setLeggings(new ItemStack(Material.LEATHER_LEGGINGS));
    108. pi.setBoots(new ItemStack(Material.LEATHER_BOOTS));
    109.  
    110. //Armour enchantments
    111.  
    112. pi.getHelmet().addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    113. pi.getChestplate().addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    114. pi.getLeggings().addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    115. pi.getBoots().addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
    116.  
    117. pi.getHelmet().addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    118. pi.getChestplate().addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    119. pi.getLeggings().addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    120. pi.getBoots().addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    121.  
    122. //Leather armour colors
    123. ItemStack Helm = p.getInventory().getHelmet();
    124. LeatherArmorMeta im = (LeatherArmorMeta) Helm.getItemMeta();
    125. im.setColor(Color.fromRGB(255, 248, 148));
    126. Helm.setItemMeta(im);
    127.  
    128. ItemStack Chestplate = p.getInventory().getChestplate();
    129. Chestplate.getItemMeta();
    130. im.setColor(Color.fromRGB(175, 177, 250));
    131. Chestplate.setItemMeta(im);
    132.  
    133. ItemStack Leggings = p.getInventory().getLeggings();
    134. LeatherArmorMeta im1 = (LeatherArmorMeta) Leggings.getItemMeta();
    135. im1.setColor(Color.fromRGB(175, 177, 250));
    136. Leggings.setItemMeta(im1);
    137.  
    138. ItemStack Boots = p.getInventory().getBoots();
    139. Boots.getItemMeta();
    140. im1.setColor(Color.fromRGB(255, 248, 148));
    141. Boots.setItemMeta(im1);
    142.  
    143. //Items
    144.  
    145. pi.addItem(new ItemStack(Material.CAKE , 1));
    146. for(int i = 0; i < 34; i++) {
    147. pi.addItem(new ItemStack(Material.MUSHROOM_SOUP , 1));
    148. }
    149.  
    150. //On give message
    151.  
    152. p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "TBS PvP" + ChatColor.BLACK + "]" + ChatColor.GRAY + ":" + ChatColor.GREEN + "You equipped " + ChatColor.LIGHT_PURPLE + "Derpy! " + ChatColor.YELLOW + "Your left eye rolls off in another direction while you prepare for battle!");
    153.  
    154. }
    155. {
    156. return true;
    157. }
    158. }
    159. }
    160. }
    161.  
     
  2. Offline

    L33m4n123

    something along this lines. of course you need to edit it for your needs^^
    Code:java
    1. ItemStack diamondSword = new ItemStack(Material.Diamond_Sword);
    2. diamondSword.getItemMeta().setDisplayName("Basic Sword");
    3. diamondSword.getItemMeta().setLore("Your Lore");
     
  3. Offline

    Xacero

    FirecatHD
    Code:java
    1.  
    2. ItemMeta im = ItemStack.getItemMeta();
    3. im.setDisplayName("some name");
    4. setLore(Arrays.asList("l1", "l2", "l3"));
    5. ItemStack.setItemMeta(im);
    6.  


    Thats the clearest way I can explain how to do it :p Is that what you wanted?

    Edit - ninja'ed
     
  4. Offline

    FirecatHD

    Xacero and L33m4n123

    This is the code i came up with, but it dont seem to work :oops:

    Code:java
    1. ItemStack Sword = new ItemStack(Material.DIAMOND_SWORD, 1);
    2. Sword.getItemMeta().setDisplayName("Basic Sword");
    3. Sword.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    4. Sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
    5. pi.addItem(Sword);


    What did i do wrong?

    Thanks.
     
  5. you need to set the itemMeta which you don't do.
    here is how i do it, can be made easier:
    Code:java
    1. ItemStack item = new ItemStack(Material.WHATEVER,1);
    2. ItemMeta meta = p1.getItemMeta();
    3. meta.setDisplayName("Name it");
    4.  
    5. ArrayList<String> lore = new ArrayList<String>();
    6. lore.add("Line 1");
    7. lore.add("Line 2");
    8. lore.add("Line 3");
    9.  
    10. meta.setLore(lore);
    11. item.setItemMeta(meta);
     
  6. Offline

    sgavster

  7. Offline

    FirecatHD

    Ok mollekake here is my code how it looks now, but at the "p1.getItemMeta" Eclipse gives me a redline under "p1"
    and says: p1 cannot be resolved. I am probably being stupid right now but i dont know how to fix it (i am really new)...

    Code:
    Code:java
    1. ItemStack Sword = new ItemStack(Material.DIAMOND_SWORD,1);
    2. ItemMeta meta = p1.getItemMeta();
    3. meta.setDisplayName("Name it");
    4. Sword.setItemMeta(meta);
    5. Sword.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    6. Sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
    7.  


    Thanks.
     
  8. Offline

    sgavster

    FirecatHD Use my util! It's easier. With my util:
    Code:java
    1. player.getInventory().addItem(setNameAndLore(Material.DIAMOND_SWORD, "Starter Sword", "Lore"));
     
  9. Offline

    FirecatHD

    sgavster - Where do i put the util?
     
  10. Offline

    sgavster

    FirecatHD In whatever class you're doing this in.
    so if it's your main class just put it in there, like any other method.
     
  11. Offline

    FirecatHD

    Umm sgavster - It says "Illegal modifier for parameter setNameAndLore; only final is permitted" on the SetNameAndLore + at the string... it says "Syntax error, insert ";" to complete LocalVariableDeclarationStatement"

    + one more error :(
     
  12. Offline

    L33m4n123

    You are trying to get the ItemMeta from p1 but named the ItemStack variable sword.. so thats why ;)
    so it should be
    Code:java
    1. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD,1);
    2. ItemMeta meta = sword.getItemMeta();
    3. meta.setDisplayName("Name it");
    4. Sword.setItemMeta(meta);
    5. Sword.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
    6. Sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
    7.  
     
  13. Offline

    sgavster

    FirecatHD um.. show me you're code.. you must be doing it wrong.
     
  14. Offline

    FirecatHD

    L33m4n123 - Why didnt i see that xD Its so obvious! Thanks!

    sgavster and L33m4n123

    Its working now :D Thanks to you guys!
    If i get a new problem il just throw it in here :cool:

    Thanks.

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

    Garris0n

    I prefer my solution :p Simple class but I use it constantly now...

    ItemStack sword = new EZItemStack(Material.DIAMOND_SWORD).name("name").lore(Arrays.asList("This", "is", "lore")).enchant(Enchantment.DURABILITY, 1000).enchant(Enchantment.DAMAGE_ALL, 1).toItemStack();
     
Thread Status:
Not open for further replies.

Share This Page