Library Creating easier Custom Items

Discussion in 'Resources' started by Agentleader1, Jul 8, 2015.

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

    Agentleader1

    I understand this is fairly simple. This was created more of really to make people's lives easier so they don't have to code their own library for this or dig through.

    Source Code

    The source code is fairly long for a simple util but has a ton of constructors.
    Long source code (open)

    Code:
    package com.Agentleader1.DarkThunder;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Item;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.MaterialData;
    
    public class CustomItem {
    
        private ItemStack item;
        private ItemMeta meta;
        private String name;
        private String[] lore;
        private Object[] assoc;
      
        public CustomItem(Material arg0, String name, String... lore){
            this.item = new ItemStack(arg0);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        public CustomItem(Material arg0, int arg1, String name, String... lore){
            this.item = new ItemStack(arg0, arg1);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
    
        public CustomItem(Material arg0, byte arg1, String name, String... lore){
            this.item = new ItemStack(arg0, 1, (byte) arg1);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        public CustomItem(Material arg0, int arg1, int arg2, String name, String... lore){
            this.item = new ItemStack(arg0, arg1, (byte) arg2);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        @Deprecated
        public CustomItem(int arg0, String name, String... lore){
            this.item = new ItemStack(arg0);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        @Deprecated
        public CustomItem(int arg0, int arg1, String name, String... lore){
            this.item = new ItemStack(arg0, arg1);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
    
        @Deprecated
        public CustomItem(int arg0, byte arg1, String name, String... lore){
            this.item = new ItemStack(arg0, 1, (byte) arg1);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        @Deprecated
        public CustomItem(int arg0, int arg1, int arg2, String name, String... lore){
            this.item = new ItemStack(arg0, arg1, (byte) arg2);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        public CustomItem(Material arg0, String name, String[] lore, Object... assoc){
            this.item = new ItemStack(arg0);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
            this.assoc = assoc;
        }
      
        public CustomItem(Material arg0, int arg1, String name, String[] lore, Object... assoc){
            this.item = new ItemStack(arg0, arg1);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
    
        public CustomItem(Material arg0, byte arg1, String name, String[] lore, Object... assoc){
            this.item = new ItemStack(arg0, 1, (byte) arg1);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        public CustomItem(Material arg0, int arg1, int arg2, String name, String[] lore, Object... assoc){
            this.item = new ItemStack(arg0, arg1, (byte) arg2);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        @Deprecated
        public CustomItem(int arg0, String name, String[] lore, Object... assoc){
            this.item = new ItemStack(arg0);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        @Deprecated
        public CustomItem(int arg0, int arg1, String name, String[] lore, Object... assoc){
            this.item = new ItemStack(arg0, arg1);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
    
        @Deprecated
        public CustomItem(int arg0, byte arg1, String name, String[] lore, Object... assoc){
            this.item = new ItemStack(arg0, 1, (byte) arg1);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        @Deprecated
        public CustomItem(int arg0, int arg1, int arg2, String name, String[] lore, Object... assoc){
            this.item = new ItemStack(arg0, arg1, (byte) arg2);
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        public CustomItem(ItemStack arg0, String name, String... lore){
            this.item = arg0;
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
        }
      
        public CustomItem(ItemStack arg0, String name, String[] lore, Object... assoc){
            this.item = arg0;
            this.meta = this.item.getItemMeta();
            this.name = name;
            this.lore = lore;
            this.assoc = assoc;
        }
      
        public ItemStack toItemStack(){
            if(this.name != null){
                this.meta.setDisplayName(this.name);
            }
            if(this.lore != null){
                this.meta.setLore(Arrays.asList(this.lore));
            }
            this.item.setItemMeta(this.meta);
            return this.item;
        }
      
        public String getName(){
            return this.name;
        }
      
        @Deprecated
        public String[] getLore(){
            return this.lore;
        }
      
        public List<String> getLoreList(){
            return Arrays.asList(this.lore);
        }
      
        public Object getAssoc(int arg0){
            return this.assoc[arg0];
        }
      
        public Object[] getAssocs(){
            return this.assoc;
        }
      
        @Override
        public String toString(){
            return this.item.toString();
        }
      
        public void setName(String arg0){
            this.name = arg0;
        }
      
        public void setLore(String... lore){
            this.lore = lore;
        }
      
        @Deprecated
        public void setLore(ArrayList<String> lore){
            this.lore = (String[]) lore.toArray();
        }
      
        public Item drop(World arg0, Location arg1){
            return arg0.dropItem(arg1, toItemStack());
        }
      
        public Item dropNaturally(World arg0, Location arg1){
            return arg0.dropItemNaturally(arg1, toItemStack());
        }
      
        public int setAmount(int arg0){
            this.item.setAmount(arg0);
            return arg0;
        }
      
        public int getAmount(){
            int a = this.item.getAmount();
            return a;
        }
      
        @Deprecated
        public void setMaterialData(MaterialData arg0){
            this.item.setData(arg0);
        }
      
        public void setDura(short arg0){
            this.item.setDurability(arg0);
        }
    
        public void setMaterial(Material arg0){
            this.item.setType(arg0);
        }
      
        @Deprecated
        public void setId(int id){
            this.item.setTypeId(id);
        }
      
    }
    


    How to use
    All you need to know is to provide the item (id, material, or a new itemstack all work) and a custom name.
    Lore and association is optional. The association is to provide something a more accurate boolean check for your personal use.
    Creating new custom items (open)

    Code:
    CustomItem item = new CustomItem(Material.DIAMOND_SWORD, ChatColor.RED + "Sword of Vigilance");  //name only
    CustomItem item = new CustomItem(Material.DIAMOND_SWORD, ChatColor.RED + "Sword of Vigilance", "Lore 1", "Lore 2");  //name + lore
    CustomItem item = new CustomItem(Material.DIAMOND_SWORD, ChatColor.RED + "Sword of Vigilance", lore, object);  //name + lore + association

    Other uses (open)

    Code:
    CustomItem item = new CustomItem(Material.DIAMOND_SWORD, ChatColor.RED + "Sword of Vigilance", lore, object);  //name + lore + association
            if(item.getAssoc(0).equals(obj)){
                //see if your association matches
            }
    item.drop(world, loc); //drop the item
    item.setName("Sword of failure"); //set name
    item.setLore("lore 1", "lore 2", "lore 3"); //set lore
    ItemStack stack = item.toItemStack(); //compile the item and grab it


    Hope you enjoy! :D
     
    sethrem likes this.
  2. Offline

    sethrem

    This can be done 100% better. But GJ.
     
    Last edited: Jul 8, 2015
  3. Offline

    ChipDev

    Nice job on the association.
     
  4. Offline

    sethrem

    @ChipDev if you look at the code the association does nothing (facepalm).
     
  5. Offline

    ChipDev

    Its different =p
    Most other lib.s don't have that. Even though it isn't the best, its cool.
     
  6. Offline

    kampai

    Cool! I used this in my plugin and it saved me a LOT of time!
     
  7. Offline

    Agentleader1

    @kampai Thanks, glad I could help.
     
Thread Status:
Not open for further replies.

Share This Page