Solved Code for giving a player an item with a custom name.

Discussion in 'Plugin Development' started by dandwhit, Sep 6, 2014.

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


    I'm new to java and coding.
    I'm basically hoping someone can tell me what the code would be to give someone an item with a custom name.

    Basically what I want is when you run '/mb' it gives you paper with the name MagicBand.

    Thanks for any help,

    EDIT: Also, if you know how, what is the code to make it so they cannot drop that item.
  2. Offline


  3. Offline


    teej107 Oh okay then, worth a shot though.
  4. Offline


    dandwhit We are also more likely to give harder answers to work with if you show no effort. What have you tried?
  5. Offline


  6. Offline


    Each itemstack has it's own ItemMeta. You can edit that ItemMeta, for example edit the name, lore, ect.
    Make sure to re-aplly the ItemMeta after you are done editting it.
  7. Offline


    Learn. Java. Good. God. Do. It. It. Will. Help. So. Much.

    Also, typing like that's hard.

    Anyway, learn Java before Bukkit. I tried to learn Forge before Java and it was a horrible experience, so I practiced and played with Java and now I'm not horrid at it!

    Okay that's a lie.
    But seriously, learn Java.

    Anyway, to make an item meta first make an ItemMeta from the ItemStack you want. If you want a hint, .getItemMeta(); is involved.

    If you want to edit the name, you need to altar the display name with a setter. I'm pretty sure you'll be able to figure this out.

    For lore, I usually make a new list that has the string containy thingamajiggadoodaa. Simply clear the list, and write it. Adding a comma outside the string will add a new line, I believe. Though I haven't tested so don'taskmeifthatstrue. I suppose you can also do just a new .add or something for a new line like me but eh.

    There's a few other features that I don't care about.

    Oh and make sure to set your stack's metadata to the new metadata thing when you're done okay bye.
  8. Offline


    Reddeh OracleTarget Thanks and I will. I have made basic plugins. But I mean basic, literally you just run a command and it gives you a message from the config file. Also now I think I know what went wrong when I made it earlier. (I deleted it all because it didn't work)

    Reddeh OracleTarget teej107 Hi again, I seem to have another problem now. I've changed the plugin completely now and basically when you right click a brewing stand it opens a gui. The code is not complete but I'm getting an error at the bottom. It says "Unreachable Code". Could you tell me what is wrong with it?

    Thanks again,

    Show Spoiler
    Show Spoiler

    1. package me.dandwhit.RideStatus;
    3. import java.util.logging.Logger;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.inventory.InventoryClickEvent;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.event.player.PlayerJoinEvent;
    16. import org.bukkit.inventory.Inventory;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.meta.ItemMeta;
    19. import org.bukkit.plugin.PluginDescriptionFile;
    20. import;
    22. public class Main extends JavaPlugin implements Listener {
    24. Logger log;
    26. public void onEnable() {
    27. PluginDescriptionFile pdfFile = this.getDescription();
    28. log = this.getLogger();
    29. + " Has been started! Version: v " + pdfFile.getVersion());
    32. setConfig();
    33. }
    35. public void setConfig(){
    36. this.saveDefaultConfig();
    37. }
    39. public void onDisable() {
    40. getLogger().info("Plugin Disabled");
    41. }
    43. private void teleportInWord(Player player, int x, int y, int z) {
    44. player.teleport(new Location(player.getWorld(), x, y, z));
    45. }
    47. public void openGui(Player player) {
    48. Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GOLD + "Ride Status");
    50. ItemStack wds = new ItemStack(Material.NOTE_BLOCK);
    51. ItemMeta wdsMeta = wds.getItemMeta();
    52. ItemStack wdw = new ItemStack(Material.DIAMOND);
    53. ItemMeta wdwMeta = wdw.getItemMeta();
    54. ItemStack td = new ItemStack(Material.FIREWORK);
    55. ItemMeta tdMeta = td.getItemMeta();
    56. ItemStack dl = new ItemStack(Material.BEACON);
    57. ItemMeta dlMeta = dl.getItemMeta();
    59. wdsMeta.setDisplayName(ChatColor.RED + "Walt Disney Studios");
    60. wds.setItemMeta(wdsMeta);
    62. wdwMeta.setDisplayName(ChatColor.GREEN + "Walt Disney World");
    63. wdw.setItemMeta(wdwMeta);
    65. tdMeta.setDisplayName(ChatColor.GOLD + "Tokyo Disneyland");
    66. td.setItemMeta(tdMeta);
    68. dlMeta.setDisplayName(ChatColor.AQUA + "Disneyland Resort");
    69. dl.setItemMeta(dlMeta);
    71. inv.setItem(1, wds);
    72. inv.setItem(3, wdw);
    73. inv.setItem(5, td);
    74. inv.setItem(7, dl);
    76. player.openInventory(inv);
    77. }
    79. @EventHandler
    80. public void onInventoryClick(InventoryClickEvent event) {
    81. if(ChatColor.stripColor(event.getInventory().getName())
    82. .equalsIgnoreCase("Ride Status"))
    84. return;
    85. Player player = (Player) event.getWhoClicked();
    86. event.setCancelled(true);
    88. if(event.getCurrentItem() == null
    89. || event.getCurrentItem().getType() == Material.AIR
    90. || !event.getCurrentItem().hasItemMeta()){
    91. player.closeInventory();
    92. return;
    93. }
    95. switch (event.getCurrentItem().getType()) {
    96. case NOTE_BLOCK:
    97. player.sendMessage(ChatColor.GOLD + "wdsstatus");
    98. }
    99. }
    101. @EventHandler
    102. public void onPlayerJoin(PlayerJoinEvent event){
    103. event.getPlayer().getInventory()
    104. .addItem(new ItemStack (Material.BREWING_STAND));
    105. }
    108. @EventHandler
    109. public void onPlayerInteract(PlayerInteractEvent event) {
    110. Action a= event.getAction();
    111. ItemStack is= event.getItem();
    113. if(a == Action.PHYSICAL || is == null || is.getType() == Material.AIR);
    114. return;
    116. //from here
    117. if(is.getType() == Material.BREWING_STAND)
    118. openGui(event.getPlayer());
    119. //until here is underlined, saying "Unreachable Code"
    120. }
    122. }

    Wait never mind fixed it.

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


    So you gave your item a custom name and lore yeah?

    If you want to make it so you can't drop that specific items then make sure you have an eventlistener. A simple google tells you how to do this.

    Then in your eventlistener, wherever you set it to (Make sure it implements Listener) make an eventhandler running "PlayerDropItemEvent". Call this onPlayerDropItem or something idk. Set the event priority to highest as well because it's a topkek habit to do that for important stuff.

    Then check event.getItemDrop() if it equals the item your thing is, then if it has lore and a name something like
    if(event.getItemDrop().getItemMeta.getDisplayName() != null && event.getItemDrop().getItemMeta.getLore() != null) and then check if the name is the same as your custom item's name and then the lore is the same as your custom item's lore. Use the same method you used to give it lore to check it's lore, using a list. So like if it == loreList() or something and if it passes all those tests then set the event to cancelled. If it doesn't work SHOW ME YOUR MOVES code and I'll say what you did wrong c:
Thread Status:
Not open for further replies.

Share This Page