So in a class called: 'ItemStacks' I use this Code: public static ItemStack createItemStack(Material m, int amount, String displayname, byte data, List<String> lore) { And then in another class I want to use the method so for example I am in a class called "PlayerJoin" I use this Code: @EventHandler public void onPlayerJoin(PlayerJoinEvent e) { e.getPlayer().getInventory().addItem(ItemStacks.createItemStack(m, amount, displayname, data, lore)); Can anyone tell me what I should do with lore? As you can tell I'm not the best Bukkit Developer but I'm trying to learn it by experimenting Also sorry for my bad English, I hope that I'm clear enough I have figured it out I'm using this: Code: @SuppressWarnings("deprecation") @EventHandler public void onPlayerInteract(PlayerInteractEvent e) { Action a = e.getAction(); ItemStack is = e.getPlayer().getItemInHand(); if (a == Action.PHYSICAL || is == null || is.getType() == Material.AIR) return; if ((e.getPlayer().getItemInHand() != null) && (e.getPlayer().getItemInHand().hasItemMeta()) && (e.getPlayer().getItemInHand().getItemMeta().hasDisplayName())) { String ItemName = is.getItemMeta().getDisplayName(); if ((ItemName.contains(ChatColor.RED + "Kit Selector"))) { Menu(e.getPlayer()); } } }
@Ezrab_ You have a method that can make a new ItemStack for you, has all the arguments that you need. Why not use that method? Don't see your problem to be honest.
Okay uhm... Code: e.getPlayer().getInventory().addItem(ItemStacks.createItemStack(Material.DIAMOND_SWORD, 1, "", (byte) 0, lore)); So my problem is, is that I don't know what to put at lore... Code: e.getPlayer().getInventory().addItem(ItemStacks.createItemStack(Material.DIAMOND_SWORD, 1, "", (byte) 0, lore /*<-- here*/)); Because if I do Code: e.getPlayer().getInventory().addItem(ItemStacks.createItemStack(Material.DIAMOND_SWORD, 1, "", (byte) 0, "")); It will say that it's not a String.
@Ezrab_ https://www.tutorialspoint.com/java/util/arrays_aslist.htm Or you make a method that doesn't require the lore.
That's the problem I want to use the lore here take a look: Code: public static ItemStack createItemStack(Material m, int amount, String displayname, byte data, List<String> lore) { ItemStack is = new ItemStack(m, amount, data); ItemMeta im = is.getItemMeta(); im.setDisplayName(displayname); im.setLore(lore); is.setItemMeta(im); return is; } I hope you understand what I mean now...
@Ezrab_ Then you input a list. You can make that on a different line of code just fine. Before the createItemStack call in the event.
@Ezrab_ Code:java Arrays.asList("This is my lore", "and I can add as many", "strings as I want"); Put that in for lore parameter and change string stuff to what you want. That should work.
You could edit the method to accept String... instead of a list, so you can put multiple lines of lore. Just create a List<String> inside the method, and run through the String... and put them all in there, then set the lore. NOTE: String... is the variable name
Thanks that did the job! I have another question why doesn't this work? Code: if (is.getType() == Material.FEATHER && (is.getItemMeta().equals(ChatColor.RED + "Kit Selector"))) So I'm making an inventory that you can only open with a feather that has this ^ name. But it doesn't work if I use .equals(ChatColor.RED + "Kit Selector") Anyone knows what else I could do? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
@Ezrab_ Are you gettings errors? Also is.getItemMeta() returns a itemmeta, not a string! You would do is.getItemMeta().getDisplayName() or getting the lore, I recommend that you learn how ItemMeta works and it's functions. If you want me to explain it just tell me.
@Ezrab_ ItemMeta is an Object that has functions like set displayName(), setLore(), setEnchants(), etc. You then have to set that ItemMeta to an ItemStack, or you can compare it to other ItemMetas.
Okay so I did this Code: @EventHandler public void onPlayerInteract(PlayerInteractEvent e) { Action a = e.getAction(); ItemStack is = e.getItem(); if (a == Action.PHYSICAL || is == null || is.getType() == Material.AIR) return; if (is.getType() == Material.FEATHER && is.getItemMeta().getDisplayName().contains(ChatColor.RED + "Kit Selector")) { Menu(e.getPlayer()); } else { return; } } It does work now, but if you just use a normal feather it gives a NullPointerException and I dont know why that is
No, I don't give code. https://jd.bukkit.org/org/bukkit/inventory/ItemStack.html#hasItemMeta() https://jd.bukkit.org/org/bukkit/inventory/meta/ItemMeta.html#hasDisplayName()
I linked you the methods that you need to make the checks. Should be able to figure out how they work by looking at the description.
Then look at the links, last word in the link is the method. Look at the method name, look at what it returns. You have a method to check if there is item meta, and a method to check if there is a display name.
Ok here: Code: ITEM.hasItemMeta(); // Returns a boolean seeing if it HAS ItemMeta ITEM.getItemMeta(); // Gets ItemMeta, if this ^ is false returns null ITEM.getItemMeta().hasDisplayName(); // Returns a boolean seeing if it has a display name in ItemMeta ITEM.getItemMeta().getDisplayName(); // Gets DisplayName, if this ^ is false returns material name ITEM.getItemMeta().hasLore(); // Returns a boolean seeing if it has a lore in ItemMeta ITEm.getItemMeta().getLore(); // Gets the Lore in a List<String>, if this ^ is false probably returns null or zero length List A layout of the basic methods of the ItemMeta class.
@TheEnderman The getDisplayName() Will not return null (I think) plus you check it with hasDisplayName()... If it returns false, then just get the Material then get the name.