Advertisement
Guest User

Untitled

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