Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. package me.wsman217.eternal_enchants.enchantments;
  2.  
  3. import static org.bukkit.ChatColor.*;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.List;
  8.  
  9. import org.bukkit.Material;
  10. import org.bukkit.inventory.ItemStack;
  11. import org.bukkit.inventory.meta.ItemMeta;
  12.  
  13. public enum Enchants {
  14.  
  15. // Display name, min level, max level, applicable items
  16. AUTO_SMELT("Auto Smelt", 1, 1, Arrays.asList(Material.DIAMOND_PICKAXE, Material.DIAMOND_AXE), -1);
  17.  
  18. private String displayName;
  19. private int minLvl, maxLvl;
  20. private ArrayList<Material> applicableItems = new ArrayList<Material>();
  21. private int lvl;
  22.  
  23. Enchants(String displayName, int minLvl, int maxLvl, List<Material> applicableItems, int lvl) {
  24. this.displayName = displayName;
  25. this.minLvl = minLvl;
  26. this.maxLvl = maxLvl;
  27. this.lvl = lvl;
  28. }
  29.  
  30. public int getLvlCurrentLvl() {
  31. return lvl;
  32. }
  33.  
  34. public Enchants setCurrentLvl(int newLvl) {
  35. this.lvl = newLvl;
  36. return this;
  37. }
  38.  
  39. public String getDisplayName() {
  40. return displayName;
  41. }
  42.  
  43. public int getMinLvl() {
  44. return minLvl;
  45. }
  46.  
  47. public int getMaxLvl() {
  48. return maxLvl;
  49. }
  50.  
  51. public ArrayList<Material> getApplicableItems() {
  52. return applicableItems;
  53. }
  54.  
  55. public static boolean isApplicable(Enchants enchant, Material toApply) {
  56. for (Material appItem : enchant.getApplicableItems())
  57. if (appItem == toApply)
  58. return true;
  59. return false;
  60. }
  61.  
  62. public static Enchants findByName(String name) {
  63. for (Enchants ench : Enchants.values())
  64. if (stripColor(ench.getDisplayName()).equals(name))
  65. return ench;
  66. return null;
  67. }
  68.  
  69. /**
  70. * Adds an enchantment to the item that is in within the highest and lowest
  71. * level of the enchant.
  72. *
  73. * @param item
  74. * @param enchant
  75. * @param lvl
  76. * @return The item with the enchant.
  77. */
  78. public static ItemStack addSafeEnchant(ItemStack item, Enchants enchant, int lvl) {
  79. if (!isWithinLvl(enchant, lvl))
  80. lvl = enchant.getMaxLvl();
  81. ItemMeta im = item.getItemMeta();
  82. List<String> itemLore = new ArrayList<String>();
  83. if (im.hasLore())
  84. itemLore = im.getLore();
  85. String ench = GRAY + enchant.getDisplayName();
  86.  
  87. if (lvl <= 10)
  88. ench += lvlToRoman(lvl);
  89. else
  90. ench += ".level." + lvl;
  91.  
  92. int index = 0;
  93.  
  94. for (String loreLine : itemLore) {
  95. index++;
  96. if (loreLine.startsWith(enchant.getDisplayName())) {
  97. itemLore.set(index, ench);
  98. im.setLore(itemLore);
  99. item.setItemMeta(im);
  100. return item;
  101. }
  102. }
  103.  
  104. itemLore.add(ench);
  105. im.setLore(itemLore);
  106. item.setItemMeta(im);
  107. return item;
  108. }
  109.  
  110. /**
  111. * Adds an enchantment to the item that can be out of the highest and lowest
  112. * level of the enchant.
  113. *
  114. * @param item
  115. * @param enchant
  116. * @param lvl
  117. * @return The item with the enchant.
  118. */
  119. public static ItemStack addUnsafeEnchant(ItemStack item, Enchants enchant, int lvl) {
  120. ItemMeta im = item.getItemMeta();
  121. List<String> itemLore = new ArrayList<String>();
  122. if (im.hasLore())
  123. itemLore = im.getLore();
  124. String ench = GRAY + enchant.getDisplayName();
  125.  
  126. if (lvl <= 10)
  127. ench += lvlToRoman(lvl);
  128. else
  129. ench += ".level." + lvl;
  130.  
  131. int index = 0;
  132.  
  133. for (String loreLine : itemLore) {
  134. index++;
  135. if (loreLine.startsWith(enchant.getDisplayName())) {
  136. itemLore.set(index, ench);
  137. im.setLore(itemLore);
  138. item.setItemMeta(im);
  139. return item;
  140. }
  141. }
  142.  
  143. itemLore.add(ench);
  144. im.setLore(itemLore);
  145. item.setItemMeta(im);
  146. return item;
  147. }
  148.  
  149. public static ArrayList<Enchants> getCustomEnchantsFromItem(ItemStack item) {
  150. ItemMeta im = item.getItemMeta();
  151. if (!im.hasLore())
  152. return new ArrayList<Enchants>();
  153. List<String> itemLore = im.getLore();
  154. ArrayList<Enchants> customEnchantsOnItem = new ArrayList<Enchants>();
  155.  
  156. for (String loreLine : itemLore)
  157. for (Enchants enchants : Enchants.values())
  158. if (loreLine.startsWith(enchants.getDisplayName()))
  159. customEnchantsOnItem.add(enchants.setCurrentLvl(romanToLvl(loreLine.substring(loreLine.indexOf(" ")))));
  160.  
  161. return customEnchantsOnItem;
  162. }
  163.  
  164. private static int romanToLvl(String roman) {
  165. switch (roman) {
  166. case "I":
  167. return 1;
  168. case "II":
  169. return 2;
  170. case "III":
  171. return 3;
  172. case "IV":
  173. return 4;
  174. case "V":
  175. return 5;
  176. case "VI":
  177. return 6;
  178. case "VII":
  179. return 7;
  180. case "VIII":
  181. return 8;
  182. case "IX":
  183. return 9;
  184. case "X":
  185. return 10;
  186. default:
  187. return 0;
  188. }
  189. }
  190.  
  191. private static String lvlToRoman(int lvl) {
  192. switch (lvl) {
  193. case 1:
  194. return " I";
  195. case 2:
  196. return " II";
  197. case 3:
  198. return " III";
  199. case 4:
  200. return " IV";
  201. case 5:
  202. return " V";
  203. case 6:
  204. return " VI";
  205. case 7:
  206. return " VII";
  207. case 8:
  208. return " VIII";
  209. case 9:
  210. return " IX";
  211. default:
  212. return " X";
  213. }
  214. }
  215.  
  216. private static boolean isWithinLvl(Enchants enchant, int lvl) {
  217. return enchant.getMinLvl() <= lvl && enchant.getMaxLvl() >= lvl;
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement