SkyVox

ItemAPI

Mar 18th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.41 KB | None | 0 0
  1. /**
  2. *
  3. * @author SkyVox
  4. */
  5.  
  6. public class ItemAPI {
  7. private List<StringBuilder> items = new ArrayList<>();
  8. private Logger logger;
  9.  
  10. /**
  11. * @param plugin
  12. */
  13. public ItemAPI(Plugin plugin) {
  14. this.logger = plugin.getLogger();
  15. }
  16.  
  17. /**
  18. * This will convert your file item to StringBuilder.
  19. *
  20. * @param file
  21. * @param location
  22. * @return StringBuilder
  23. */
  24. public StringBuilder convert(FileConfiguration file, String location) {
  25. String type = null; // Item Type cannot be null.
  26. short data = (file.contains(location + ".data") ? Short.parseShort(file.getString(location + ".data")) : Byte.valueOf((byte) 0));
  27. int amount = (file.contains(location + ".amount") ? Integer.parseInt(file.getString(location + ".amount")) : Integer.valueOf(0));
  28. String name = (file.contains(location + ".name") ? (name = file.getString(location + ".name").replace("§", "&")) : null);
  29.  
  30. try {
  31. type = Integer.toString(file.getInt(location + ".type"));
  32. } catch (NumberFormatException ex) {
  33. if (file.getString(location + ".type").contains(":")) {
  34. String[] typeSplit = file.getString(location + ".type").split(":");
  35.  
  36. type = typeSplit[0];
  37. try {
  38. data = Byte.valueOf((byte) Integer.parseInt(typeSplit[1]));
  39. } catch (NumberFormatException ex2) {
  40. data = Byte.valueOf((byte) 0);
  41. }
  42. } else {
  43. type = file.getString(location + ".type").toUpperCase();
  44. }
  45. }
  46.  
  47. // Create Final Item as StringBuilder.
  48. StringBuilder strFinalItem = new StringBuilder();
  49.  
  50. strFinalItem.append("<type>" + type + "</type>");
  51. strFinalItem.append("<data>" + Short.toString(data) + "</data>");
  52. strFinalItem.append("<amount>" + Integer.toString(amount) + "</amount>");
  53. strFinalItem.append("<name>" + name + "</name>");
  54.  
  55. // Add lore to Final Item if contains lore on File.
  56. if (file.contains(location + ".lore")) {
  57. int i = 0;
  58. int size = file.getStringList(location + ".lore").size();
  59.  
  60. strFinalItem.append("<lore>");
  61. for (String lore : file.getStringList(location + ".lore")) {
  62. strFinalItem.append(lore);
  63.  
  64. i+=1;
  65. if (i <= size) {
  66. strFinalItem.append("<break%lore>");
  67. }
  68. }
  69.  
  70. strFinalItem.append("</lore>");
  71. }
  72.  
  73. // Add enchant to Final Item if contains enchants on File.
  74. if (file.contains(location + ".enchants")) {
  75. int i = 0;
  76. int size = file.getStringList(location + ".enchants").size();
  77. String enchantmentName = null;
  78. Enchantment enchantment = null;
  79.  
  80. strFinalItem.append("<enchant>");
  81. for (String enchants : file.getStringList(location + ".enchants")) {
  82. try {
  83. if (enchants.contains(",")) {
  84. enchantmentName = enchants.split(",")[0].toUpperCase();
  85. enchantment = Enchantment.getByName(enchantmentName);
  86. strFinalItem.append(enchantment.getName() + "," + Integer.parseInt(enchants.split(",")[1]));
  87. } else {
  88. enchantmentName = enchants.toUpperCase();
  89. enchantment = Enchantment.getByName(enchantmentName);
  90. strFinalItem.append(enchantment.getName() + "," + Integer.toString(1));
  91. }
  92.  
  93. i+=1;
  94. if (i <= size) {
  95. strFinalItem.append("<break%enchant>");
  96. }
  97. } catch (ArrayIndexOutOfBoundsException ex) {
  98. logger.log(Level.WARNING, "Could not found enchantment name or enchantment level!");
  99. } catch (NumberFormatException ex) {
  100. logger.log(Level.WARNING, "Could not parse to integer '" + enchants.split(",")[1] + "'!");
  101. } catch (NullPointerException ex) {
  102. logger.log(Level.WARNING, "Could not found any Enchantment named by '" + enchantmentName + "'!");
  103. }
  104. }
  105.  
  106. enchantmentName = null;
  107. enchantment = null;
  108. strFinalItem.append("</enchant>");
  109. }
  110.  
  111. /*
  112. * Here some extra things that can be added.
  113. */
  114.  
  115. // Item Price.
  116. if (file.contains(location + ".price")) {
  117. strFinalItem.append("<price>" + file.getDouble(location + ".price") + "</price>");
  118. }
  119.  
  120. // Item Slot.
  121. if (file.contains(location + ".slot")) {
  122. strFinalItem.append("<slot>" + file.getInt(location + ".slot") + "</slot>");
  123. }
  124.  
  125. // Item Action. TODO: Improve this.
  126. if (file.contains(location + ".action")) {
  127. strFinalItem.append("<action>" + file.getString(location + ".action").toUpperCase() + "</action>");
  128. }
  129.  
  130. // Add glow.
  131. if (file.contains(location + ".glow")) {
  132. strFinalItem.append("<glow>" + Boolean.toString(file.getString(location + ".glow").equalsIgnoreCase("true")) + "</glow>");
  133. }
  134.  
  135. // Convert File Item to StringBuilder Item!
  136. // Return FinalItem.
  137. return strFinalItem;
  138. }
  139.  
  140. /**
  141. * Convert your ItemStack to StringBuilder.
  142. * All values will return as HTML syntax.
  143. *
  144. * @param item
  145. * @return StringBuilder
  146. */
  147. public StringBuilder serializer(ItemStack item) throws NullPointerException {
  148. if (!item.hasItemMeta()) {
  149. logger.log(Level.SEVERE, "Could not convert ItemStack to StringBuilder, This Item doesn't has ItemMeta!");
  150. return null;
  151. }
  152.  
  153. String type = item.getType().toString().toUpperCase();
  154. short data = item.getDurability();
  155. int amount = item.getAmount();
  156. String name = (item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName().replace("§", "&") : ChatColor.GRAY + "");
  157.  
  158. // Final Item, as StringBuilder.
  159. StringBuilder strFinalItem = new StringBuilder();
  160.  
  161. strFinalItem.append("<type>" + type + "</type>");
  162. strFinalItem.append("<data>" + Short.toString(data) + "</data>");
  163. strFinalItem.append("<amount>" + Integer.toString(amount) + "</amount>");
  164. strFinalItem.append("<name>" + name + "</name>");
  165.  
  166. // Add lore to Final Item.
  167. if (item.getItemMeta().hasLore()) {
  168. int i = 0;
  169. int size = item.getItemMeta().getLore().size();
  170.  
  171. strFinalItem.append("<lore>");
  172. for (String lore : item.getItemMeta().getLore()) {
  173. strFinalItem.append(lore);
  174.  
  175. i+=1;
  176. if (i <= size) {
  177. strFinalItem.append("<break%lore>");
  178. }
  179. }
  180.  
  181. strFinalItem.append("</lore>");
  182. }
  183.  
  184. // Add enchantment to Final Item.
  185. if (item.getItemMeta().hasEnchants()) {
  186. int i = 0;
  187. int size = item.getItemMeta().getEnchants().keySet().size();
  188.  
  189. strFinalItem.append("<enchant>");
  190. for (Enchantment enchant : item.getItemMeta().getEnchants().keySet()) {
  191. try {
  192. strFinalItem.append(enchant.getName().toUpperCase() + "," + item.getEnchantmentLevel(enchant));
  193.  
  194. i+=1;
  195. if (i <= size) {
  196. strFinalItem.append("<break%enchant>");
  197. }
  198. } catch (Exception ignore) {}
  199. }
  200.  
  201. strFinalItem.append("</enchant>");
  202. }
  203.  
  204. // Convert ItemStack to StringBuilder Item!
  205. // Return FinalItem.
  206. return strFinalItem;
  207. }
  208.  
  209. /**
  210. * Returns Final Item added an extra syntax.
  211. *
  212. * @param strFinalItem
  213. * @param price
  214. * @return Final Item plus extra Price
  215. */
  216. public StringBuilder extraPrice(StringBuilder strFinalItem, double price) {
  217. return strFinalItem.append("<price>" + Double.toString(price) + "</price>");
  218. }
  219.  
  220. /**
  221. * Returns Final Item added an extra syntax.
  222. *
  223. * @param strFinalItem
  224. * @param slot
  225. * @return Final Item plus extra Inventory Slot
  226. */
  227. public StringBuilder extraInventorySlot(StringBuilder strFinalItem, int slot) {
  228. return strFinalItem.append("<slot>" + Integer.toString(slot) + "</slot>");
  229. }
  230.  
  231. /**
  232. * Returns Final Item added an extra syntax.
  233. *
  234. * @param strFinalItem
  235. * @param action
  236. * @return Final Item plus extra Action
  237. */
  238. public StringBuilder extraAction(StringBuilder strFinalItem, String action) {
  239. return strFinalItem.append("<action>" + action + "</action>");
  240. }
  241.  
  242. /**
  243. * Returns Final Item added an extra syntax.
  244. *
  245. * @param strFinalItem
  246. * @param glow
  247. * @return Final Item plus extra Glow
  248. */
  249. public StringBuilder extraGlow(StringBuilder strFinalItem, boolean glow) {
  250. return strFinalItem.append("<glow>" + Boolean.toString(glow) + "</glow>");
  251. }
  252.  
  253. /**
  254. * Convert your Final Item to ItemStack.
  255. *
  256. * @param finalItem
  257. * @return FinalItem as ItemStack
  258. */
  259. public ItemStack deserializer(StringBuilder strFinalItem) {
  260. Object type = getType(strFinalItem);
  261. short data = getData(strFinalItem);
  262. int amount = getAmount(strFinalItem);
  263. String name = getName(strFinalItem);
  264. List<String> lore = getLore(strFinalItem);
  265. HashMap<Enchantment, Integer> enchantment = getEnchantments(strFinalItem);
  266.  
  267. // New ItemStack.
  268. ItemStack item = null;
  269. // Check if 'TYPE' are Integer or String.
  270. switch (type.getClass().getSimpleName().toUpperCase()) {
  271. case("STRING"):
  272. item = new ItemStack(Material.getMaterial((String) type), amount, data);
  273. break;
  274. case("INT"):
  275. case("INTEGER"):
  276. item = new ItemStack(Material.getMaterial((int) type), amount, data);
  277. break;
  278. }
  279. ItemMeta meta = item.getItemMeta();
  280.  
  281. // Set Name.
  282. if (name != null) {
  283. meta.setDisplayName(name);
  284. } else {
  285. String[] itemName = item.getType().toString().split("_");
  286. for (int i = 0; i < itemName.length; i++) {
  287. if (name != null) {
  288. name = (name + Character.toUpperCase(itemName[0].charAt(0)) + itemName[0].substring(1));
  289. } else {
  290. name = Character.toUpperCase(itemName[0].charAt(0)) + itemName[0].substring(1);
  291. }
  292. }
  293.  
  294. meta.setDisplayName(name);
  295. }
  296.  
  297. // Add lore.
  298. if (lore != null) {
  299. meta.setLore(lore);
  300. }
  301.  
  302. // Add enchantment
  303. if (enchantment != null) {
  304. for (Entry<Enchantment, Integer> entries : enchantment.entrySet()) {
  305. meta.addEnchant(entries.getKey(), entries.getValue(), true);
  306. }
  307. }
  308.  
  309. // Set Item glow.
  310. if (isGlow(strFinalItem)) {
  311. meta.addEnchant(Enchantment.OXYGEN, 1, true);
  312. meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
  313. }
  314.  
  315. // Set ItemMeta.
  316. item.setItemMeta(meta);
  317.  
  318. // Return ItemStack.
  319. return item;
  320. }
  321.  
  322. /**
  323. * This method will return the Inventory Item slot.
  324. * Will return -1 if slot does not exist on Final Item.
  325. *
  326. * @param strFinalItem
  327. * @return int slot
  328. */
  329. public int getSlot(StringBuilder strFinalItem) {
  330. try {
  331. String slot = strFinalItem.toString().split("<slot>")[1];
  332. return Integer.parseInt(slot.substring(0, slot.indexOf("</slot>")));
  333. } catch (ArrayIndexOutOfBoundsException ignore) {}
  334.  
  335. // Return -1, this Final Item does not have Slot param.
  336. return -1;
  337. }
  338.  
  339. /**
  340. * This method will return the Item Type as a Object,
  341. * And this Object can be INTEGER or STRING.
  342. *
  343. * Integer - Item ID.
  344. * String - Item Name.
  345. *
  346. * @throws NullPointerException
  347. * @param strFinalItem
  348. * @return Object type
  349. */
  350. public Object getType(StringBuilder strFinalItem) throws NullPointerException {
  351. try {
  352. String type = strFinalItem.toString().split("<type>")[1];
  353. return type.substring(0, type.indexOf("</type>"));
  354. } catch (ArrayIndexOutOfBoundsException ignore) {}
  355.  
  356. // Will return null if Final Item does not have type.
  357. return null;
  358. }
  359.  
  360. /**
  361. * This method will return the Item Data.
  362. * If Final Item does not have Item Data will return 0 (by default).
  363. *
  364. * @param strFinalItem
  365. * @return short
  366. */
  367. public short getData(StringBuilder strFinalItem) {
  368. try {
  369. String data = strFinalItem.toString().split("<data>")[1];
  370. return Short.parseShort(data.substring(0, data.indexOf("</data>")));
  371. } catch (ArrayIndexOutOfBoundsException ignore) {}
  372.  
  373. // If Final Item does not have Item Data will return 0 (by default).
  374. // Return 0.
  375. return (short) 0;
  376. }
  377.  
  378. /**
  379. * This method will return the Item amount.
  380. * If Final Item does not have Item amount will return 1 (by default).
  381. *
  382. * @param strFinalItem
  383. * @return int
  384. */
  385. public int getAmount(StringBuilder strFinalItem) {
  386. try {
  387. String amount = strFinalItem.toString().split("<amount>")[1];
  388. return Integer.parseInt(amount.substring(0, amount.indexOf("</amount>")));
  389. } catch (ArrayIndexOutOfBoundsException ignore) {}
  390.  
  391. // If Final Item does not have Item amount will return 1 (by default).
  392. return 1;
  393. }
  394.  
  395. /**
  396. * This method will return the Item name.
  397. * Will return null if name does not exist on Final Item.
  398. *
  399. * @param strFinalItem
  400. * @return String
  401. */
  402. public String getName(StringBuilder strFinalItem) throws NullPointerException {
  403. try {
  404. String name = strFinalItem.toString().split("<name>")[1];
  405. return ChatColor.translateAlternateColorCodes('&', name.substring(0, name.indexOf("</name>")));
  406. } catch (ArrayIndexOutOfBoundsException ignore) {}
  407.  
  408. // Return null if name does not exist on Final Item.
  409. return null;
  410. }
  411.  
  412. /**
  413. * This method will check if has Glow on Final Item.
  414. *
  415. * @param strFinalItem
  416. * @return
  417. */
  418. public boolean isGlow(StringBuilder strFinalItem) {
  419. try {
  420. String glow = strFinalItem.toString().split("<glow>")[1];
  421. return glow.substring(0, glow.indexOf("</amount>")).equalsIgnoreCase("true");
  422. } catch (ArrayIndexOutOfBoundsException ignore) {}
  423.  
  424. // Return false if glow does not exist on Final Item.
  425. return false;
  426. }
  427.  
  428. /**
  429. * This method will return the Item lore.
  430. *
  431. * @param strFinalItem
  432. * @return List string
  433. */
  434. public List<String> getLore(StringBuilder strFinalItem) {
  435. List<String> itemLore = new ArrayList<>();
  436. String lore = null;
  437.  
  438. try {
  439. lore = strFinalItem.toString().split("<lore>")[1];
  440. lore = lore.substring(0, lore.indexOf("</lore>"));
  441. } catch (ArrayIndexOutOfBoundsException ex) {
  442. return null;
  443. }
  444.  
  445. for (String str : lore.split("<break%lore>")) {
  446. itemLore.add(ChatColor.translateAlternateColorCodes('&', str));
  447. }
  448.  
  449. // Return itemLore.
  450. return itemLore;
  451. }
  452.  
  453. /**
  454. * This method will return Item enchantment.
  455. *
  456. * @param finalItem
  457. * @return
  458. */
  459. public HashMap<Enchantment, Integer> getEnchantments(StringBuilder finalItem) {
  460. HashMap<Enchantment, Integer> itemEnchantments = new HashMap<>();
  461. String enchantment = null;
  462.  
  463. try {
  464. enchantment = finalItem.toString().split("<enchant>")[1];
  465. enchantment = enchantment.substring(0, enchantment.indexOf("</enchant>"));
  466. } catch (ArrayIndexOutOfBoundsException ex) {
  467. return null;
  468. }
  469.  
  470. for (String enchantments : enchantment.split("<break%enchant>")) {
  471. Enchantment enchant = Enchantment.getByName(enchantments.split(",")[0].toUpperCase());
  472. int level = Integer.parseInt(enchantments.split(",")[1]);
  473.  
  474. itemEnchantments.put(enchant, level);
  475. }
  476.  
  477. // Return itemEnchantments.
  478. return itemEnchantments;
  479. }
  480.  
  481. /**
  482. * Save your Item in a List.
  483. *
  484. * @param item
  485. */
  486. public void save(StringBuilder item) {
  487. items.add(item);
  488. }
  489.  
  490. /**
  491. * Save your Item in a List.
  492. *
  493. * @param item
  494. */
  495. public void save(ItemStack item) {
  496. items.add(this.serializer(item));
  497. }
  498.  
  499. /**
  500. * Get a specific item using his name.
  501. *
  502. * @param name
  503. * @return a specific saved item as StringBuilder
  504. */
  505. public StringBuilder getItem(String name) {
  506. return getItems().stream().filter(entry -> getName(entry).equalsIgnoreCase(name)).findFirst().orElse(null);
  507. }
  508.  
  509. /**
  510. * Get a specific item using his name.
  511. *
  512. * @param name
  513. * @return a specific saved item as ItemStack
  514. */
  515. public ItemStack getItemAsItemStack(String name) {
  516. try {
  517. return getItemsAsItemStack().stream().filter(entry -> entry.getItemMeta().getDisplayName().equalsIgnoreCase(name)).findFirst().orElse(null);
  518. } catch (NullPointerException ignore) {}
  519.  
  520. // Will return null if item doesn't exists or Item doesn't has itemMeta/DisplayName.
  521. return null;
  522. }
  523.  
  524. /**
  525. * @return list with all saved Items as StringBuilder
  526. */
  527. public List<StringBuilder> getItems() {
  528. return items;
  529. }
  530.  
  531. /**
  532. * @return list with all saved Items as StringBuilder
  533. */
  534. public List<ItemStack> getItemsAsItemStack() {
  535. List<ItemStack> i = new ArrayList<>();
  536.  
  537. items.forEach(entry -> i.add(this.deserializer(entry)));
  538. return i;
  539. }
  540.  
  541. /*
  542. * -
  543. * Below here, some useful methods.
  544. * Every method here is about ItemStack.
  545. */
  546.  
  547. /**
  548. * Check if player has a specific Item.
  549. * Set amount = '0' to don't check amount.
  550. * Set name = 'null' to don't check name.
  551. *
  552. * @param player
  553. * @param material
  554. * @param amount
  555. * @param name
  556. * @return if a player contains {@link material} on his inventory.
  557. */
  558. public static boolean contains(Player player, Material material, int amount, String name) {
  559. int count = 0;
  560.  
  561. for (ItemStack items : player.getInventory().all(material).values()) {
  562. /*
  563. * Not necessary.
  564. *
  565. * if ((items == null) || (items.getType() == Material.AIR)) {
  566. * continue;
  567. * }
  568. *
  569. */
  570. if (name != null) {
  571. if (items.hasItemMeta()) {
  572. if (items.getItemMeta().hasDisplayName()) {
  573. if (items.getItemMeta().getDisplayName().equals(name)) {
  574. if (amount <= 0) {
  575. return true;
  576. } else {
  577. count+=items.getAmount();
  578.  
  579. if (count >= amount) {
  580. return true;
  581. }
  582. }
  583. }
  584.  
  585. continue;
  586. }
  587. }
  588. }
  589.  
  590. if (amount <= 0) {
  591. return true;
  592. } else {
  593. count+=items.getAmount();
  594.  
  595. if (count >= amount) {
  596. return true;
  597. }
  598. }
  599. }
  600.  
  601. return false;
  602. }
  603.  
  604. /**
  605. * Check if player has a specific Item.
  606. * Set amount = '0' to don't check amount.
  607. * Set name = 'null' to don't check name.
  608. *
  609. * @param player
  610. * @param item
  611. * @param amount
  612. * @return if a player contains {@link item} on his inventory.
  613. */
  614. public static boolean contains(Player player, ItemStack item, int amount) {
  615. return contains(player, item.getType(), amount, (item.hasItemMeta() ? item.getItemMeta().getDisplayName() : null));
  616. }
  617. }
Advertisement
Add Comment
Please, Sign In to add comment