Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. package me.TwCrown.Addon.Utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.logging.Level;
  6.  
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.Color;
  10. import org.bukkit.DyeColor;
  11. import org.bukkit.Material;
  12. import org.bukkit.enchantments.Enchantment;
  13. import org.bukkit.inventory.ItemFlag;
  14. import org.bukkit.inventory.ItemStack;
  15. import org.bukkit.inventory.meta.ItemMeta;
  16. import org.bukkit.inventory.meta.LeatherArmorMeta;
  17. import org.bukkit.inventory.meta.SkullMeta;
  18.  
  19. import net.md_5.bungee.api.ChatColor;
  20.  
  21. public class ItemBuilder {
  22.  
  23.     private ItemStack is;
  24.  
  25.     public ItemBuilder(Material m) {
  26.         this(m, 1, (short) 0);
  27.     }
  28.  
  29.     public ItemBuilder(ItemStack is) {
  30.         this.is = is.clone();
  31.     }
  32.  
  33.     public ItemBuilder(Material m, int amount, short data) {
  34.         is = new ItemStack(m, amount, data);
  35.     }
  36.  
  37.     public ItemBuilder clone() {
  38.         return new ItemBuilder(is);
  39.     }
  40.  
  41.     public ItemBuilder durability(int dur) {
  42.         is.setDurability((short) dur);
  43.         return this;
  44.     }
  45.  
  46.     public ItemBuilder name(String name) {
  47.         ItemMeta im = is.getItemMeta();
  48.         im.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
  49.         is.setItemMeta(im);
  50.         return this;
  51.     }
  52.  
  53.     public ItemBuilder unsafeEnchantment(Enchantment ench, int level) {
  54.         is.addUnsafeEnchantment(ench, level);
  55.         return this;
  56.     }
  57.  
  58.     public ItemBuilder enchant(Enchantment ench, int level) {
  59.         ItemMeta im = is.getItemMeta();
  60.         im.addEnchant(ench, level, true);
  61.         is.setItemMeta(im);
  62.         return this;
  63.     }
  64.  
  65.     public ItemBuilder removeEnchantment(Enchantment ench) {
  66.         is.removeEnchantment(ench);
  67.         return this;
  68.     }
  69.  
  70.     public ItemBuilder owner(String owner) {
  71.         try {
  72.             SkullMeta im = (SkullMeta) is.getItemMeta();
  73.             im.setOwner(owner);
  74.             is.setItemMeta(im);
  75.         } catch (ClassCastException expected) {
  76.         }
  77.         return this;
  78.     }
  79.  
  80.     public ItemBuilder infinityDurabilty() {
  81.         is.setDurability(Short.MAX_VALUE);
  82.         return this;
  83.     }
  84.  
  85.     public ItemBuilder lore(String... lore) {
  86.         ItemMeta im = is.getItemMeta();
  87.         List<String> out = im.getLore() == null ? new ArrayList<>() : im.getLore();
  88.         for (String string : lore)
  89.             out.add(ChatColor.translateAlternateColorCodes('&', string));
  90.         im.setLore(out);
  91.         is.setItemMeta(im);
  92.         return this;
  93.     }
  94.  
  95.     @SuppressWarnings("deprecation")
  96.     public ItemBuilder woolColor(DyeColor color) {
  97.         if (!is.getType().equals(Material.WOOL))
  98.             return this;
  99.         is.setDurability(color.getDyeData());
  100.         return this;
  101.     }
  102.  
  103.     public ItemBuilder amount(int amount) {
  104.         if (amount > 64)
  105.             amount = 64;
  106.         is.setAmount(amount);
  107.         return this;
  108.     }
  109.  
  110.     public ItemBuilder removeAttributes() {
  111.         ItemMeta meta = is.getItemMeta();
  112.         meta.addItemFlags(ItemFlag.values());
  113.         is.setItemMeta(meta);
  114.         return this;
  115.     }
  116.  
  117.     public ItemStack build() {
  118.         return is;
  119.     }
  120.  
  121.     public ItemBuilder color(Color color) {
  122.         if (!is.getType().name().contains("LEATHER_"))
  123.             return this;
  124.         LeatherArmorMeta meta = (LeatherArmorMeta) is.getItemMeta();
  125.         meta.setColor(color);
  126.         is.setItemMeta(meta);
  127.         return this;
  128.     }
  129.    
  130.     public ItemBuilder head(String texture) {
  131. //      ItemStack head = new ItemStack(Material.SKULL_ITEM, 1, (short)3);
  132. //      GameProfile profile = (texture, UUID.randomUUID());
  133. //      ItemMeta headMeta = head.getItemMeta();
  134. //      Class<?> headMetaClass = headMeta.getClass();
  135. //      RefSet(headMetaClass, headMeta, "profile", profile);
  136.         return this;
  137.     }
  138.  
  139.    
  140.  
  141.    
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement