This is fairly basic, what it does is it creates custom items, or gives you a boolean to check if an item has a certain display name or lore. Really simple ;-; Here's the library class: (Move your mouse to reveal the content) Here's the library class: (open) Here's the library class: (close) Code: public class CustomItemManager { public CustomItemManager(){ } public ItemStack customItem(ItemStack item, String displayName){ ItemMeta meta = item.getItemMeta(); meta.setDisplayName(displayName); item.setItemMeta(meta); return item; } public ItemStack customItem(Player player, ItemStack item, String displayName){ ItemMeta meta = item.getItemMeta(); meta.setDisplayName(displayName); item.setItemMeta(meta); player.updateInventory(); return item; } public ItemStack customItem(ItemStack item, String displayName, List<String> lore){ ItemMeta meta = item.getItemMeta(); meta.setDisplayName(displayName); meta.setLore(lore); item.setItemMeta(meta); return item; } public ItemStack customItem(Player player, ItemStack item, String displayName, List<String> lore){ ItemMeta meta = item.getItemMeta(); meta.setDisplayName(displayName); meta.setLore(lore); item.setItemMeta(meta); player.updateInventory(); return item; } public boolean isCustomItem(ItemStack item){ if(item.hasItemMeta()){ if((item.getItemMeta().hasDisplayName()) || item.getItemMeta().hasLore()){ return true; }else{ return false; } }else{ return false; } } public boolean isCustomItem(ItemStack item, String displayName){ if(item.hasItemMeta()){ if((item.getItemMeta().hasDisplayName())){ if(item.getItemMeta().getDisplayName().equalsIgnoreCase(displayName)){ return true; }else{ return false; } }else{ return false; } }else{ return false; } } public boolean isCustomItem(ItemStack item, List<String> lore){ if(item.hasItemMeta()){ if((item.getItemMeta().hasLore())){ if(item.getItemMeta().getLore().equals(lore)){ return true; }else{ return false; } }else{ return false; } }else{ return false; } } public boolean isCustomItem(ItemStack item, String displayName, List<String> lore){ if(item.hasItemMeta()){ if((item.getItemMeta().hasDisplayName()) && item.getItemMeta().hasLore()){ if(item.getItemMeta().getDisplayName().equalsIgnoreCase(displayName) && item.getItemMeta().getLore().equals(lore)){ return true; }else{ return false; } }else{ return false; } }else{ return false; } } } How to use it (Move your mouse to reveal the content) How to use it (open) How to use it (close) Starting off: Required in every class (Move your mouse to reveal the content) Starting off: Required in every class (open) Starting off: Required in every class (close) Code: private CustomItemManager CIM = new CustomItemManager(); Creating a custom item (Move your mouse to reveal the content) Creating a custom item (open) Creating a custom item (close) Code: ItemStack item = new ItemStack(1); List<String> lore = Arrays.asList("Lore 1","2", "etc."); ItemStack SwagStone = CIM.customItem(item, "Swag Stone", lore); //lore is optional. Or with self player inventory update: Code: Player player = Bukkit.getPlayer("Notch"); ItemStack item = new ItemStack(1); List<String> lore = Arrays.asList("Lore 1","2", "etc."); ItemStack SwagStone = CIM.customItem(player, item, "Swag Stone", lore); Checking if an item is a custom one (Move your mouse to reveal the content) Checking if an item is a custom one (open) Checking if an item is a custom one (close) If it is at all (named/lored or not): Code: if(CIM.isCustomItem(anyItem)){ //do stuff } If it has a certain data: Code: if(CIM.isCustomItem(anyItem, displayName, lore)){ //do stuff }
You can improve that code by Making your methods static. They are utility methods and they aren't using anything from the object. It would be better this way since you don't have to create a new object just to access those methods. Your bunches of return statements within the else braces are a bit unnecessary. Your isCustomItem method name for checking the lore and/or display name seems a bit misleading. Some of your methods that do the work of two methods should call those methods.
@teej107 Kid, I rush. The reason it's not static is because the variable names to get the instance of it would be too long. Using a new instance of it allows you to rename the class to something shorter.
What do you mean by 'variable names to get the instance of it'? You would rather create an instance, and use that instance to use the methods, rather than just call a static method? What @teej107 said is completely correct, and I would recommend fixing what he pointed out.
That is a terrible reason IMHO. If you are worried about that, at least have it be a singleton class.
@ChipDev I didn't advertise any threads. If you refer to the link, it was merely a snippet of code. The thread I made for that particular snippet was created after the above post made by me.