Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. package me.dark.night.dkaltardinivo.utils;
  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.ItemFlag;
  13. import org.bukkit.inventory.ItemStack;
  14. import org.bukkit.inventory.meta.ItemMeta;
  15. import org.bukkit.inventory.meta.LeatherArmorMeta;
  16. import org.bukkit.inventory.meta.SkullMeta;
  17.  
  18.  
  19. public class ItemBuilder {
  20. private ItemStack is;
  21.  
  22. public ItemBuilder(Material m){
  23. this(m, 1);
  24. }
  25.  
  26. public ItemBuilder(ItemStack is){
  27. this.is=is;
  28. }
  29.  
  30. public ItemBuilder(Material m, int quantia){
  31. is= new ItemStack(m, quantia);
  32. }
  33.  
  34. public ItemBuilder(Material m, int quantia, byte durabilidade){
  35. is = new ItemStack(m, quantia, durabilidade);
  36. }
  37.  
  38. public ItemBuilder clone(){
  39. return new ItemBuilder(is);
  40. }
  41.  
  42. public ItemBuilder setDurability(short durabilidade){
  43. is.setDurability(durabilidade);
  44. return this;
  45. }
  46.  
  47. public ItemBuilder setAmount(int amount) {
  48. is.setAmount(amount);
  49. return this;
  50. }
  51.  
  52. public ItemBuilder setDurability(int durabilidade){
  53. is.setDurability(Short.valueOf(""+durabilidade));
  54. return this;
  55. }
  56.  
  57. public ItemBuilder setName(String nome){
  58. if (nome.equalsIgnoreCase("nulo")) return this;
  59. ItemMeta im = is.getItemMeta();
  60. im.setDisplayName(nome);
  61. is.setItemMeta(im);
  62. return this;
  63. }
  64.  
  65. public ItemBuilder addUnsafeEnchantment(Enchantment ench, int level){
  66. is.addUnsafeEnchantment(ench, level);
  67. return this;
  68. }
  69.  
  70. public ItemBuilder addEnchants(List<String> enchants){
  71. if (enchants.get(0).equalsIgnoreCase("nulo")) return this;
  72. for (String s : enchants) {
  73. Enchantment ench = Enchantment.getByName(s.split(":")[0]);
  74. int level = Integer.valueOf(s.split(":")[1]);
  75. is.addUnsafeEnchantment(ench, level);
  76. }
  77. return this;
  78. }
  79.  
  80. public ItemBuilder setGlow(boolean b) {
  81. if (!b) return this;
  82. is.addUnsafeEnchantment(Enchantment.DURABILITY, 1);
  83. ItemMeta im = is.getItemMeta();
  84. im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
  85. is.setItemMeta(im);
  86. return this;
  87. }
  88.  
  89. public ItemBuilder setType(Material m) {
  90. is.setType(m);
  91. return this;
  92. }
  93.  
  94. public ItemBuilder removeEnchantment(Enchantment ench){
  95. is.removeEnchantment(ench);
  96. return this;
  97. }
  98.  
  99. public ItemBuilder setSkullOwner(String dono){
  100. try{
  101. SkullMeta im = (SkullMeta)is.getItemMeta();
  102. im.setOwner(dono);
  103. is.setItemMeta(im);
  104. }catch(ClassCastException expected){}
  105. return this;
  106. }
  107.  
  108. public ItemBuilder addEnchant(Enchantment ench, int level){
  109. ItemMeta im = is.getItemMeta();
  110. im.addEnchant(ench, level, true);
  111. is.setItemMeta(im);
  112. return this;
  113. }
  114.  
  115. public ItemBuilder addEnchantments(Map<Enchantment, Integer> enchantments){
  116. is.addEnchantments(enchantments);
  117. return this;
  118. }
  119.  
  120. public ItemBuilder setInfinityDurability(){
  121. is.setDurability(Short.MAX_VALUE);
  122. return this;
  123. }
  124.  
  125. public ItemBuilder addItemFlag(ItemFlag flag){
  126. ItemMeta im = is.getItemMeta();
  127. im.addItemFlags(flag);
  128. is.setItemMeta(im);
  129. return this;
  130. }
  131.  
  132. public ItemBuilder setLore(String... lore){
  133. ItemMeta im = is.getItemMeta();
  134. im.setLore(Arrays.asList(lore));
  135. is.setItemMeta(im);
  136. return this;
  137. }
  138.  
  139. public ItemBuilder setLore(List<String> lore) {
  140. if (lore.get(0).equalsIgnoreCase("nulo")) return this;
  141. ItemMeta im = is.getItemMeta();
  142. im.setLore(lore);
  143. is.setItemMeta(im);
  144. return this;
  145. }
  146.  
  147. public ItemBuilder removeLoreLine(String linha){
  148. ItemMeta im = is.getItemMeta();
  149. List<String> lore = new ArrayList<>(im.getLore());
  150. if(!lore.contains(linha))return this;
  151. lore.remove(linha);
  152. im.setLore(lore);
  153. is.setItemMeta(im);
  154. return this;
  155. }
  156.  
  157. public ItemBuilder removeLoreLine(int index){
  158. ItemMeta im = is.getItemMeta();
  159. List<String> lore = new ArrayList<>(im.getLore());
  160. if(index<0||index>lore.size())return this;
  161. lore.remove(index);
  162. im.setLore(lore);
  163. is.setItemMeta(im);
  164. return this;
  165. }
  166.  
  167. public ItemBuilder addLoreLine(String linha){
  168. ItemMeta im = is.getItemMeta();
  169. List<String> lore = new ArrayList<>();
  170. if(im.hasLore())lore = new ArrayList<>(im.getLore());
  171. lore.add(linha);
  172. im.setLore(lore);
  173. is.setItemMeta(im);
  174. return this;
  175. }
  176.  
  177. public ItemBuilder addLoreLine(String linha, int pos){
  178. ItemMeta im = is.getItemMeta();
  179. List<String> lore = new ArrayList<>(im.getLore());
  180. lore.set(pos, linha);
  181. im.setLore(lore);
  182. is.setItemMeta(im);
  183. return this;
  184. }
  185.  
  186. @SuppressWarnings("deprecation")
  187. public ItemBuilder setDyeColor(DyeColor cor){
  188. this.is.setDurability(cor.getData());
  189. return this;
  190. }
  191.  
  192. @Deprecated
  193. public ItemBuilder setWoolColor(DyeColor cor){
  194. if(!is.getType().equals(Material.WOOL))return this;
  195. this.is.setDurability(cor.getData());
  196. return this;
  197. }
  198.  
  199. public ItemBuilder setLeatherArmorColor(Color cor){
  200. try{
  201. LeatherArmorMeta im = (LeatherArmorMeta)is.getItemMeta();
  202. im.setColor(cor);
  203. is.setItemMeta(im);
  204. }catch(ClassCastException expected){}
  205. return this;
  206. }
  207.  
  208. public ItemStack toItemStack(){
  209. return is;
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement