Help Renaming an Item

Discussion in 'Plugin Development' started by GoldSoda, Jun 8, 2014.

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

    GoldSoda

    I am trying to make a plugin that gives you a stick named BanHammer, with some subtext that says something like: The Hammer has spoken. I have tried but nothing has worked. If someone could either tell me how to do it or just remake the code and give it to me that would be great.
    Code:java
    1. package BanHammer;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.inventory.PlayerInventory;
    10. import org.bukkit.inventory.meta.ItemMeta;
    11. import org.bukkit.permissions.Permission;
    12. import org.bukkit.plugin.PluginManager;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15.  
    16. public class BanHammer extends JavaPlugin {
    17.  
    18. public Permission playerPermission = new Permission("banhammer.allowed");
    19.  
    20. @Override
    21. public void onEnable() {
    22. getLogger().info("BanHammer v1.0 has been enabled!");
    23. PluginManager pm = getServer().getPluginManager();
    24. pm.addPermission(playerPermission);
    25.  
    26. }
    27.  
    28. @Override
    29. public void onDisable() {
    30.  
    31. }
    32.  
    33. public boolean onCommand(CommandSender sender, Command cmd, String label,
    34. String[] args) {
    35.  
    36. if (cmd.getName().equalsIgnoreCase("banhammer")
    37. && sender instanceof Player) {
    38. Player player = (Player) sender;
    39.  
    40.  
    41. if (player.hasPermission(playerPermission)) {
    42.  
    43. ItemStack stick = new ItemStack(Material.STICK, 1);
    44.  
    45. ItemMeta meta = (ItemMeta) stick.getItemMeta();
    46. meta.setDisplayName("Ban Hammer");
    47. //This is supposed to rename the stick to "Ban Hammer"
    48. //I would also like some subtext that says: "The Hammer has spoken"
    49. PlayerInventory pi = player.getInventory();
    50. pi.addItem(stick);
    51.  
    52.  
    53.  
    54.  
    55. player.sendMessage(ChatColor.RED
    56. + String.format("Here is your BanHammer, %s!",
    57. player.getName()));
    58. } else { player.sendMessage(ChatColor.DARK_RED + "You don't have permission to use this command, ya' dingus");
    59.  
    60. }
    61. }
    62.  
    63. return true;
    64.  
    65. }
    66.  
    67. }
    68.  
    69.  
    70.  
     
  2. Offline

    aninsanellama

    If the problem is that the display name is not appearing, it is because you did not set the ItemStack's meta again once you had changed it. You can use the stick.setItemMeta(meta);.

    To add text beneath that (lore), use the .setLore() method within the ItemMeta class and pass in your array of strings.
     
  3. Offline

    _feedDz

    @aninsanellamayou should never do it like that as you are setting all metadata instead of one piece of it use this:
    Code:java
    1. ItemStack stick = new ItemStack(Material.STICK,1);
    2. //set the name
    3. stick.getItemMeta().setDisplayName("DisplayName");
    4. //set the enchants
    5. stick.addEnchantment(Enchantment.DAMAGE_ALL, 5);
     
Thread Status:
Not open for further replies.

Share This Page