Solved Help with setting custom names in kits!

Discussion in 'Spigot Plugin Development' started by JJpro321, Jun 26, 2016.

  1. So I'm currently working on a plugin the plugin includes 3 tiers of kits here's one of the kits:

    Code (Text):
    package com.jjpro321.essentialslite.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    public class Daily implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
                String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to use this command!");
                return false;
            }
            Player player = (Player) sender;
            Inventory inv = Bukkit.createInventory(null, 54, "Daily kit items");
           
            ItemStack diamondAxe = new ItemStack(Material.DIAMOND_AXE);
            ItemStack diamondSword = new ItemStack(Material.DIAMOND_SWORD);
            ItemStack diamondPickaxe = new ItemStack(Material.DIAMOND_PICKAXE);
            ItemStack diamondBoots = new ItemStack(Material.DIAMOND_BOOTS);
            ItemStack bedrock = new ItemStack(Material.BEDROCK);
           
            diamondBoots.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1000);
            diamondAxe.addUnsafeEnchantment(Enchantment.DIG_SPEED, 1000);
            diamondAxe.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
            diamondAxe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1000);
            bedrock.addUnsafeEnchantment(Enchantment.THORNS, 1000);
            diamondPickaxe.addUnsafeEnchantment(Enchantment.DIG_SPEED, 1000);
            diamondPickaxe.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
            diamondPickaxe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1000);
            diamondSword.addUnsafeEnchantment(Enchantment.DIG_SPEED, 1000);
            diamondSword.addUnsafeEnchantment(Enchantment.DURABILITY, 1000);
            diamondSword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1000);
           
            inv.addItem(diamondAxe, diamondSword, diamondPickaxe, diamondBoots, bedrock);
           
            player.openInventory(inv);
           
            return true;
        }
    }
     
    I would just like to know if anybody knows how to add custom names/lores to the items that the players get? Thanks.
     
  2. You can set a custom lore using the Item's MetaData.
    e.g.
    Code (Text):
    ItemMeta meta = itemStack.getItemMeta();
    meta.setDisplayName(ChatColor.RED + "Sword");
    meta.setLore(Arrays.asList("Line1", "Line2"));
    itemStack.setItemMeta(meta);
     
    • Agree Agree x 1
    • Winner Winner x 1
  3. Make it an ItemMeta :)

    In your case:
    ItemStack diamondAxe = new ItemStack (Material.DIAMOND_AXE);
    ItemMeta diamondAxeMeta = diamondAxe.getItemMeta();
    and then:
    diamondAxeMeta.setDisplayName(ChatColor.BLUE + "A Diamond Axe");
    and:
    diamondAxeMeta.setLore(Arrays.asList(ChatColor.RED + "This is a Lore!"));
    after that:
    diamondAxe.setItemMeta(diamondAxeMeta);
    With the method im using (i think, never tried using 2 lores) you can go up to 1 lore, i think you can use \n for spaces.
     
  4. Thanks so much :)