I was recently playing around with ItemMeta and things, and I noticed how much of a pain it is to give players items with ItemMeta and Lores, ect. I came up with a method to make it easy. This is the code: PHP: public ItemStack setNameAndLore(Material material, int amount, String name, String... lore) { ItemStack item = new ItemStack(material, amount); ItemMeta meta = item.getItemMeta(); meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name)); ArrayList<String> lorez = new ArrayList<String>(); for(String mylores : lore) { lorez.add(ChatColor.translateAlternateColorCodes('&', mylores)); } meta.setLore(lorez); item.setItemMeta(meta); return item; } To use this you can do PHP: //something like player.getInventory().addItem, or something else, like dropping it on the ground.setNameAndLore(Material.YOUR_MATERIAl, amount, "Name", "Lore 1", "Lore 2", "And as many more lores as you want!"); You can use colors, like "&6" for gold. An example would be: PHP: player.getInventory().addItem(setNameAndLore(Material.DIAMOND_BLOCK, 1, "&aMy awesome diamond block", "&6It's so cool", "&4You cry when you see it!")); let me know if it's useful Change Log (Click to See) (Move your mouse to reveal the content) Change Log (Click to See) (open) Change Log (Click to See) (close) v1.0 First release v1.1 Added amount
Hahaah. I just got lucky that I noticed it. I just roam the plugin development while waiting for help with one of my own . It's fun helping others and I get to learn more from it too.
sgavster I think if you've learned java and know how to deal with the Bukkit API it should be no problem to think of a method like that... And I don't really think it's a pain to get the ItemMeta object from the ItemStack, use 2 methods and set the ItemMeta of the ItemStack again. If you would have to deal with NMS it's more likely a pain to change the NBTTagCompound, but since Bukkit implemented this it isn't^^
What I would do, is change this line: Code: public ItemStack setNameAndLore(Material material, String name, String... lore) To this: Code: public ItemStack setNameAndLore(Material material, String name, String[] lore) So that you could use it like this: Code: //something like player.getInventory().addItem, or something else, like dropping it on the ground. setNameAndLore(Material.YOUR_MATERIAl, "Name", new String[] { "lore", "lore 2" }); But that's my opinion, haha.
DarkBladee12 - Yes, it's simple to do, but if you're messing with a ton of items with ItemMeta, this makes it easy. sebasju1234 - I could, but I like how you can do "item, name, lore, lore, lore, lore" Skyost - Thank you
I would break this down to even 2 attributes (ItemStack item, String... name) That will make it able to use the same method for items without a lore.
iZanax That's not true, you can even use "setNameAndLore(ItemStack item, String name, String... lore)" without a lore, since "Object..." doesn't require an input, so you could simply do "setNameAndLore(new ItemStack(Material.APPLE), "Super Apple")"!
sgavster Your post pretty much the exact tutorial/post I made. Also, it's not hard to think of these.
JPG2000 The reason I made this was because I needed to make 50+ items with itemmeta on my server. I never said this was super complex or anything, just a little bit useful. And I don't know if you're implying that I copied you, but I've never even seen your post
sgavster You can make the code faster and shorter without the loop: Code: public ItemStack setNameAndLore(Material material, String name, String... lore) { ItemStack item = new ItemStack(material); ItemMeta meta = item.getItemMeta(); meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name)); meta.setLore(Arrays.asList(lore)); item.setItemMeta(meta); return item; } I just found this out, since I never knew that adding the ... (forgot the name) was actually making an Array
sgavster Add the lore like normal (how I said). Get rid of the translate feature. In my opinion it's stupid and worthless, be a use most developers I know just use the chat color enum. And mine is ssignificantly faster
Isn't this just the case of micro-optimization? Iterating an ArrayList is O(n), Arrays.asList is O(1), but you can't perform operations while using asList, unless you asList and then iterate over that (which is really pointless).