Advertisement
Guest User

ItemBuilder dla Adyoo

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