Util Custom Items and Stuff

Discussion in 'Resources' started by Agentleader1, May 14, 2015.

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

    Agentleader1

    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: (open)

    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 (open)

    Starting off: Required in every class (open)
    Code:
    private CustomItemManager CIM = new CustomItemManager();

    Creating a custom item (open)

    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 (open)

    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
    }
     
    teej107 likes this.
  2. Offline

    teej107

    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.
     
    KingFaris11 and mine-care like this.
  3. Offline

    Agentleader1

    @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.
     
  4. Invisible

    nverdier

    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.
     
  5. Offline

    teej107

    That is a terrible reason IMHO. If you are worried about that, at least have it be a singleton class.
     
    timtower, Goblom and nverdier like this.
  6. Offline

    MineStein

    Wouldn't something like this be preferable?
     
  7. Offline

    ChipDev

    Please do not advertise your thread in others.
    And yes, it would. But to others it would not.
     
  8. Offline

    MineStein

    @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.
     
Thread Status:
Not open for further replies.

Share This Page