Advertisement
KIZAFOX

ItemBuilder

Jan 20th, 2022
1,065
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package your.package;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import org.bukkit.Color;
  9. import org.bukkit.DyeColor;
  10. import org.bukkit.Material;
  11. import org.bukkit.enchantments.Enchantment;
  12. import org.bukkit.inventory.ItemStack;
  13. import org.bukkit.inventory.meta.ItemMeta;
  14. import org.bukkit.inventory.meta.LeatherArmorMeta;
  15. import org.bukkit.inventory.meta.SkullMeta;
  16.  
  17. /**
  18.  * Easily create itemstacks, without messing your hands.
  19.  * <i>Note that if you do use this in one of your projects, leave this notice.</i>
  20.  * <i>Please do credit me if you do use this in one of your projects.</i>
  21.  * @author NonameSL
  22.  */
  23. public class ItemBuilder {
  24.     private ItemStack is;
  25.     /**
  26.      * Create a new ItemBuilder from scratch.
  27.      * @param m The material to create the ItemBuilder with.
  28.      */
  29.     public ItemBuilder(Material m){
  30.         this(m, 1);
  31.     }
  32.     /**
  33.      * Create a new ItemBuilder over an existing itemstack.
  34.      * @param is The itemstack to create the ItemBuilder over.
  35.      */
  36.     public ItemBuilder(ItemStack is){
  37.         this.is=is;
  38.     }
  39.     /**
  40.      * Create a new ItemBuilder from scratch.
  41.      * @param m The material of the item.
  42.      * @param amount The amount of the item.
  43.      */
  44.     public ItemBuilder(Material m, int amount){
  45.         is= new ItemStack(m, amount);
  46.     }
  47.     /**
  48.      * Create a new ItemBuilder from scratch.
  49.      * @param m The material of the item.
  50.      * @param amount The amount of the item.
  51.      * @param durability The durability of the item.
  52.      */
  53.     public ItemBuilder(Material m, int amount, byte durability){
  54.         is = new ItemStack(m, amount, durability);
  55.     }
  56.     /**
  57.      * Clone the ItemBuilder into a new one.
  58.      * @return The cloned instance.
  59.      */
  60.     public ItemBuilder clone(){
  61.         return new ItemBuilder(is);
  62.     }
  63.     /**
  64.      * Change the durability of the item.
  65.      * @param dur The durability to set it to.
  66.      */
  67.     public ItemBuilder setDurability(short dur){
  68.         is.setDurability(dur);
  69.         return this;
  70.     }
  71.     /**
  72.      * Set the displayname of the item.
  73.      * @param name The name to change it to.
  74.      */
  75.     public ItemBuilder setName(String name){
  76.         ItemMeta im = is.getItemMeta();
  77.         im.setDisplayName(name);
  78.         is.setItemMeta(im);
  79.         return this;
  80.     }
  81.     /**
  82.      * Add an unsafe enchantment.
  83.      * @param ench The enchantment to add.
  84.      * @param level The level to put the enchant on.
  85.      */
  86.     public ItemBuilder addUnsafeEnchantment(Enchantment ench, int level){
  87.         is.addUnsafeEnchantment(ench, level);
  88.         return this;
  89.     }
  90.     /**
  91.      * Remove a certain enchant from the item.
  92.      * @param ench The enchantment to remove
  93.      */
  94.     public ItemBuilder removeEnchantment(Enchantment ench){
  95.         is.removeEnchantment(ench);
  96.         return this;
  97.     }
  98.     /**
  99.      * Set the skull owner for the item. Works on skulls only.
  100.      * @param owner The name of the skull's owner.
  101.      */
  102.     public ItemBuilder setSkullOwner(String owner){
  103.         try{
  104.             SkullMeta im = (SkullMeta)is.getItemMeta();
  105.             im.setOwner(owner);
  106.             is.setItemMeta(im);
  107.         }catch(ClassCastException expected){}
  108.         return this;
  109.     }
  110.     /**
  111.      * Add an enchant to the item.
  112.      * @param ench The enchant to add
  113.      * @param level The level
  114.      */
  115.     public ItemBuilder addEnchant(Enchantment ench, int level){
  116.         ItemMeta im = is.getItemMeta();
  117.         im.addEnchant(ench, level, true);
  118.         is.setItemMeta(im);
  119.         return this;
  120.     }
  121.     /**
  122.      * Add multiple enchants at once.
  123.      * @param enchantments The enchants to add.
  124.      */
  125.     public ItemBuilder addEnchantments(Map<Enchantment, Integer> enchantments){
  126.         is.addEnchantments(enchantments);
  127.         return this;
  128.     }
  129.     /**
  130.      * Sets infinity durability on the item by setting the durability to Short.MAX_VALUE.
  131.      */
  132.     public ItemBuilder setInfinityDurability(){
  133.         is.setDurability(Short.MAX_VALUE);
  134.         return this;
  135.     }
  136.     /**
  137.      * Re-sets the lore.
  138.      * @param lore The lore to set it to.
  139.      */
  140.     public ItemBuilder setLore(String... lore){
  141.         ItemMeta im = is.getItemMeta();
  142.         im.setLore(Arrays.asList(lore));
  143.         is.setItemMeta(im);
  144.         return this;
  145.     }
  146.     /**
  147.      * Re-sets the lore.
  148.      * @param lore The lore to set it to.
  149.      */
  150.     public ItemBuilder setLore(List<String> lore) {
  151.         ItemMeta im = is.getItemMeta();
  152.         im.setLore(lore);
  153.         is.setItemMeta(im);
  154.         return this;
  155.     }
  156.     /**
  157.      * Remove a lore line.
  158.      * @param lore The lore to remove.
  159.      */
  160.     public ItemBuilder removeLoreLine(String line){
  161.         ItemMeta im = is.getItemMeta();
  162.         List<String> lore = new ArrayList<>(im.getLore());
  163.         if(!lore.contains(line))return this;
  164.         lore.remove(line);
  165.         im.setLore(lore);
  166.         is.setItemMeta(im);
  167.         return this;
  168.     }
  169.     /**
  170.      * Remove a lore line.
  171.      * @param index The index of the lore line to remove.
  172.      */
  173.     public ItemBuilder removeLoreLine(int index){
  174.         ItemMeta im = is.getItemMeta();
  175.         List<String> lore = new ArrayList<>(im.getLore());
  176.         if(index<0||index>lore.size())return this;
  177.         lore.remove(index);
  178.         im.setLore(lore);
  179.         is.setItemMeta(im);
  180.         return this;
  181.     }
  182.     /**
  183.      * Add a lore line.
  184.      * @param line The lore line to add.
  185.      */
  186.     public ItemBuilder addLoreLine(String line){
  187.         ItemMeta im = is.getItemMeta();
  188.         List<String> lore = new ArrayList<>();
  189.         if(im.hasLore())lore = new ArrayList<>(im.getLore());
  190.         lore.add(line);
  191.         im.setLore(lore);
  192.         is.setItemMeta(im);
  193.         return this;
  194.     }
  195.     /**
  196.      * Add a lore line.
  197.      * @param line The lore line to add.
  198.      * @param pos The index of where to put it.
  199.      */
  200.     public ItemBuilder addLoreLine(String line, int pos){
  201.         ItemMeta im = is.getItemMeta();
  202.         List<String> lore = new ArrayList<>(im.getLore());
  203.         lore.set(pos, line);
  204.         im.setLore(lore);
  205.         is.setItemMeta(im);
  206.         return this;
  207.     }
  208.     /**
  209.      * Sets the dye color on an item.
  210.      * <b>* Notice that this doesn't check for item type, sets the literal data of the dyecolor as durability.</b>
  211.      * @param color The color to put.
  212.      */
  213.     @SuppressWarnings("deprecation")
  214.     public ItemBuilder setDyeColor(DyeColor color){
  215.         this.is.setDurability(color.getData());
  216.         return this;
  217.     }
  218.     /**
  219.      * Sets the dye color of a wool item. Works only on wool.
  220.      * @deprecated As of version 1.2 changed to setDyeColor.
  221.      * @see ItemBuilder@setDyeColor(DyeColor)
  222.      * @param color The DyeColor to set the wool item to.
  223.      */
  224.     @Deprecated
  225.     public ItemBuilder setWoolColor(DyeColor color){
  226.         if(!is.getType().equals(Material.WOOL))return this;
  227.         this.is.setDurability(color.getData());
  228.         return this;
  229.     }
  230.     /**
  231.      * Sets the armor color of a leather armor piece. Works only on leather armor pieces.
  232.      * @param color The color to set it to.
  233.      */
  234.     public ItemBuilder setLeatherArmorColor(Color color){
  235.         try{
  236.             LeatherArmorMeta im = (LeatherArmorMeta)is.getItemMeta();
  237.             im.setColor(color);
  238.             is.setItemMeta(im);
  239.         }catch(ClassCastException expected){}
  240.         return this;
  241.     }
  242.     /**
  243.      * Retrieves the itemstack from the ItemBuilder.
  244.      * @return The itemstack created/modified by the ItemBuilder instance.
  245.      */
  246.     public ItemStack toItemStack(){
  247.         return is;
  248.     }
  249. }
  250.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement