Advertisement
imBEheAR

Item Builder

May 5th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.78 KB | None | 0 0
  1. package eu.galaxyhc.Utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.Color;
  9. import org.bukkit.Material;
  10. import org.bukkit.enchantments.Enchantment;
  11. import org.bukkit.inventory.ItemStack;
  12. import org.bukkit.inventory.meta.ItemMeta;
  13. import org.bukkit.inventory.meta.LeatherArmorMeta;
  14. import org.bukkit.potion.Potion;
  15.  
  16. public class ItemBuilder {
  17.   private Material mat;
  18.  
  19.   private int amount;
  20.  
  21.   private final short data;
  22.  
  23.   private String title;
  24.  
  25.   private final List<String> lore;
  26.  
  27.   private final HashMap<Enchantment, Integer> enchants;
  28.  
  29.   private Color color;
  30.  
  31.   private Potion potion;
  32.  
  33.   public ItemBuilder(Material mat) {
  34.     this(mat, 1);
  35.   }
  36.  
  37.   public ItemBuilder(Material mat, int amount) {
  38.     this(mat, amount, (short)0);
  39.   }
  40.  
  41.   public ItemBuilder(Material mat, short data) {
  42.     this(mat, 1, data);
  43.   }
  44.  
  45.   public ItemBuilder(Material mat, int amount, short data) {
  46.     this.title = null;
  47.     this.lore = new ArrayList<>();
  48.     this.enchants = new HashMap<>();
  49.     this.mat = mat;
  50.     this.amount = amount;
  51.     this.data = data;
  52.   }
  53.  
  54.   public ItemBuilder setType(Material mat) {
  55.     this.mat = mat;
  56.     return this;
  57.   }
  58.  
  59.   public ItemBuilder setTitle(String title) {
  60.     this.title = title;
  61.     return this;
  62.   }
  63.  
  64.   public ItemBuilder addLores(List<String> lores) {
  65.     this.lore.addAll(lores);
  66.     return this;
  67.   }
  68.  
  69.   public ItemBuilder addLore(String lore) {
  70.     this.lore.add(lore);
  71.     return this;
  72.   }
  73.  
  74.   public ItemBuilder addEnchantment(Enchantment enchant, int level) {
  75.     if (this.enchants.containsKey(enchant))
  76.       this.enchants.remove(enchant);
  77.     this.enchants.put(enchant, Integer.valueOf(level));
  78.     return this;
  79.   }
  80.  
  81.   public ItemBuilder setColor(Color color) {
  82.     if (!this.mat.name().contains("LEATHER_"))
  83.       throw new IllegalArgumentException("Can only dye leather armor!");
  84.     this.color = color;
  85.     return this;
  86.   }
  87.  
  88.   public ItemBuilder setPotion(Potion potion) {
  89.     if (this.mat != Material.POTION)
  90.       this.mat = Material.POTION;
  91.     this.potion = potion;
  92.     return this;
  93.   }
  94.  
  95.   public ItemBuilder setAmount(int amount) {
  96.     this.amount = amount;
  97.     return this;
  98.   }
  99.  
  100.   public ItemStack build() {
  101.     Material mat = this.mat;
  102.     if (mat == null) {
  103.       mat = Material.AIR;
  104.       Bukkit.getLogger().warning("Null material!");
  105.     }
  106.     ItemStack item = new ItemStack(this.mat, this.amount, this.data);
  107.     ItemMeta meta = item.getItemMeta();
  108.     if (this.title != null)
  109.       meta.setDisplayName(this.title);
  110.     if (!this.lore.isEmpty())
  111.       meta.setLore(this.lore);
  112.     if (meta instanceof LeatherArmorMeta)
  113.       ((LeatherArmorMeta)meta).setColor(this.color);
  114.     item.setItemMeta(meta);
  115.     item.addUnsafeEnchantments(this.enchants);
  116.     if (this.potion != null)
  117.       this.potion.apply(item);
  118.     return item;
  119.   }
  120.  
  121.   public ItemBuilder clone() {
  122.     ItemBuilder newBuilder = new ItemBuilder(this.mat);
  123.     newBuilder.setTitle(this.title);
  124.     for (String lore : this.lore)
  125.       newBuilder.addLore(lore);
  126.     for (Map.Entry<Enchantment, Integer> entry : this.enchants.entrySet())
  127.       newBuilder.addEnchantment(entry.getKey(), ((Integer)entry.getValue()).intValue());
  128.     newBuilder.setColor(this.color);
  129.     newBuilder.potion = this.potion;
  130.     return newBuilder;
  131.   }
  132.  
  133.   public Material getType() {
  134.     return this.mat;
  135.   }
  136.  
  137.   public String getTitle() {
  138.     return this.title;
  139.   }
  140.  
  141.   public List<String> getLore() {
  142.     return this.lore;
  143.   }
  144.  
  145.   public Color getColor() {
  146.     return this.color;
  147.   }
  148.  
  149.   public boolean hasEnchantment(Enchantment enchant) {
  150.     return this.enchants.containsKey(enchant);
  151.   }
  152.  
  153.   public int getEnchantmentLevel(Enchantment enchant) {
  154.     return ((Integer)this.enchants.get(enchant)).intValue();
  155.   }
  156.  
  157.   public HashMap<Enchantment, Integer> getAllEnchantments() {
  158.     return this.enchants;
  159.   }
  160.  
  161.   public boolean isItem(ItemStack item) {
  162.     return isItem(item, false);
  163.   }
  164.  
  165.   public boolean isItem(ItemStack item, boolean strictDataMatch) {
  166.     ItemMeta meta = item.getItemMeta();
  167.     if (item.getType() != getType())
  168.       return false;
  169.     if (!meta.hasDisplayName() && getTitle() != null)
  170.       return false;
  171.     if (!meta.getDisplayName().equals(getTitle()))
  172.       return false;
  173.     if (!meta.hasLore() && !getLore().isEmpty())
  174.       return false;
  175.     if (meta.hasLore())
  176.       for (String lore : meta.getLore()) {
  177.         if (!getLore().contains(lore))
  178.           return false;
  179.       }  
  180.     for (Enchantment enchant : item.getEnchantments().keySet()) {
  181.       if (!hasEnchantment(enchant))
  182.         return false;
  183.     }
  184.     return true;
  185.   }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement