ItemUtils.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. package com.gmail.nossr50.util;
  2. import java.util.List;
  3. import org.bukkit.ChatColor;
  4. import org.bukkit.DyeColor;
  5. import org.bukkit.Material;
  6. import org.bukkit.inventory.ItemStack;
  7. import org.bukkit.inventory.meta.ItemMeta;
  8. import com.gmail.nossr50.mcMMO;
  9. import com.gmail.nossr50.api.SpoutToolsAPI;
  10. import com.gmail.nossr50.config.Config;
  11. import com.gmail.nossr50.config.mods.CustomArmorConfig;
  12. import com.gmail.nossr50.config.mods.CustomToolConfig;
  13. import com.gmail.nossr50.config.party.ItemWeightConfig;
  14. import com.gmail.nossr50.locale.LocaleLoader;
  15. public class ItemUtils {
  16. private static Config configInstance = Config.getInstance();
  17. private static boolean customToolsEnabled = configInstance.getToolModsEnabled();
  18. private static boolean customArmorEnabled = configInstance.getArmorModsEnabled();
  19. /**
  20. * Checks if the item is a sword.
  21. *
  22. * @param is Item to check
  23. * @return true if the item is a sword, false otherwise
  24. */
  25. public static boolean isSword(ItemStack is) {
  26. switch (is.getType()) {
  27. case DIAMOND_SWORD:
  28. case GOLD_SWORD:
  29. case IRON_SWORD:
  30. case STONE_SWORD:
  31. case WOOD_SWORD:
  32. return true;
  33. default:
  34. if (customToolsEnabled && CustomToolConfig.getInstance().customSwordIDs.contains(is.getTypeId())) {
  35. return true;
  36. }
  37. else if (mcMMO.isSpoutEnabled() && SpoutToolsAPI.spoutSwords.contains(is)) {
  38. return true;
  39. }
  40. else {
  41. return false;
  42. }
  43. }
  44. }
  45. /**
  46. * Checks if the item is a hoe.
  47. *
  48. * @param is Item to check
  49. * @return true if the item is a hoe, false otherwise
  50. */
  51. public static boolean isHoe(ItemStack is) {
  52. switch (is.getType()) {
  53. case DIAMOND_HOE:
  54. case GOLD_HOE:
  55. case IRON_HOE:
  56. case STONE_HOE:
  57. case WOOD_HOE:
  58. return true;
  59. default:
  60. if (customToolsEnabled && CustomToolConfig.getInstance().customHoeIDs.contains(is.getTypeId())) {
  61. return true;
  62. }
  63. else if (mcMMO.isSpoutEnabled() && SpoutToolsAPI.spoutHoes.contains(is)) {
  64. return true;
  65. }
  66. else {
  67. return false;
  68. }
  69. }
  70. }
  71. /**
  72. * Checks if the item is a shovel.
  73. *
  74. * @param is Item to check
  75. * @return true if the item is a shovel, false otherwise
  76. */
  77. public static boolean isShovel(ItemStack is) {
  78. switch (is.getType()) {
  79. case DIAMOND_SPADE:
  80. case GOLD_SPADE:
  81. case IRON_SPADE:
  82. case STONE_SPADE:
  83. case WOOD_SPADE:
  84. return true;
  85. default:
  86. if (customToolsEnabled && CustomToolConfig.getInstance().customShovelIDs.contains(is.getTypeId())) {
  87. return true;
  88. }
  89. else if (mcMMO.isSpoutEnabled() && SpoutToolsAPI.spoutShovels.contains(is)) {
  90. return true;
  91. }
  92. else {
  93. return false;
  94. }
  95. }
  96. }
  97. /**
  98. * Checks if the item is an axe.
  99. *
  100. * @param is Item to check
  101. * @return true if the item is an axe, false otherwise
  102. */
  103. public static boolean isAxe(ItemStack is) {
  104. switch (is.getType()) {
  105. case DIAMOND_AXE:
  106. case GOLD_AXE:
  107. case IRON_AXE:
  108. case STONE_AXE:
  109. case WOOD_AXE:
  110. return true;
  111. default:
  112. if (customToolsEnabled && CustomToolConfig.getInstance().customAxeIDs.contains(is.getTypeId())) {
  113. return true;
  114. }
  115. else if (mcMMO.isSpoutEnabled() && SpoutToolsAPI.spoutAxes.contains(is)) {
  116. return true;
  117. }
  118. else {
  119. return false;
  120. }
  121. }
  122. }
  123. /**
  124. * Checks if the item is a pickaxe.
  125. *
  126. * @param is Item to check
  127. * @return true if the item is a pickaxe, false otherwise
  128. */
  129. public static boolean isPickaxe(ItemStack is) {
  130. switch (is.getType()) {
  131. case DIAMOND_PICKAXE:
  132. case GOLD_PICKAXE:
  133. case IRON_PICKAXE:
  134. case STONE_PICKAXE:
  135. case WOOD_PICKAXE:
  136. return true;
  137. default:
  138. if (customToolsEnabled && CustomToolConfig.getInstance().customPickaxeIDs.contains(is.getTypeId())) {
  139. return true;
  140. }
  141. else if (mcMMO.isSpoutEnabled() && SpoutToolsAPI.spoutPickaxes.contains(is)) {
  142. return true;
  143. }
  144. else {
  145. return false;
  146. }
  147. }
  148. }
  149. /**
  150. * Checks if the item is a helmet.
  151. *
  152. * @param is Item to check
  153. * @return true if the item is a helmet, false otherwise
  154. */
  155. public static boolean isHelmet(ItemStack is) {
  156. switch (is.getType()) {
  157. case DIAMOND_HELMET:
  158. case GOLD_HELMET:
  159. case IRON_HELMET:
  160. case LEATHER_HELMET:
  161. return true;
  162. default:
  163. if (customArmorEnabled && CustomArmorConfig.getInstance().customHelmetIDs.contains(is.getTypeId())) {
  164. return true;
  165. }
  166. return false;
  167. }
  168. }
  169. /**
  170. * Checks if the item is a chestplate.
  171. *
  172. * @param is Item to check
  173. * @return true if the item is a chestplate, false otherwise
  174. */
  175. public static boolean isChestplate(ItemStack is) {
  176. switch (is.getType()) {
  177. case DIAMOND_CHESTPLATE:
  178. case GOLD_CHESTPLATE:
  179. case IRON_CHESTPLATE:
  180. case LEATHER_CHESTPLATE:
  181. return true;
  182. default:
  183. if (customArmorEnabled && CustomArmorConfig.getInstance().customChestplateIDs.contains(is.getTypeId())) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. }
  189. /**
  190. * Checks if the item is a pair of pants.
  191. *
  192. * @param is Item to check
  193. * @return true if the item is a pair of pants, false otherwise
  194. */
  195. public static boolean isLeggings(ItemStack is) {
  196. switch (is.getType()) {
  197. case DIAMOND_LEGGINGS:
  198. case GOLD_LEGGINGS:
  199. case IRON_LEGGINGS:
  200. case LEATHER_LEGGINGS:
  201. return true;
  202. default:
  203. if (customArmorEnabled && CustomArmorConfig.getInstance().customLeggingIDs.contains(is.getTypeId())) {
  204. return true;
  205. }
  206. return false;
  207. }
  208. }
  209. /**
  210. * Checks if the item is a pair of boots.
  211. *
  212. * @param is Item to check
  213. * @return true if the item is a pair of boots, false otherwise
  214. */
  215. public static boolean isBoots(ItemStack is) {
  216. switch (is.getType()) {
  217. case DIAMOND_BOOTS:
  218. case GOLD_BOOTS:
  219. case IRON_BOOTS:
  220. case LEATHER_BOOTS:
  221. return true;
  222. default:
  223. if (customArmorEnabled && CustomArmorConfig.getInstance().customBootIDs.contains(is.getTypeId())) {
  224. return true;
  225. }
  226. return false;
  227. }
  228. }
  229. /**
  230. * Checks to see if an item is a wearable armor piece.
  231. *
  232. * @param is Item to check
  233. * @return true if the item is armor, false otherwise
  234. */
  235. public static boolean isArmor(ItemStack is) {
  236. return isHelmet(is) || isChestplate(is) || isLeggings(is) || isBoots(is);
  237. }
  238. /**
  239. * Checks to see if an item is a wearable armor piece.
  240. *
  241. * @param is Item to check
  242. * @return true if the item is armor, false otherwise
  243. */
  244. public static boolean isMinecraftArmor(ItemStack is) {
  245. return isLeatherArmor(is) || isGoldArmor(is) || isIronArmor(is) || isDiamondArmor(is);
  246. }
  247. /**
  248. * Checks to see if an item is a leather armor piece.
  249. *
  250. * @param is Item to check
  251. * @return true if the item is leather armor, false otherwise
  252. */
  253. public static boolean isLeatherArmor(ItemStack is) {
  254. switch (is.getType()) {
  255. case LEATHER_BOOTS:
  256. case LEATHER_CHESTPLATE:
  257. case LEATHER_HELMET:
  258. case LEATHER_LEGGINGS:
  259. return true;
  260. default:
  261. return false;
  262. }
  263. }
  264. /**
  265. * Checks to see if an item is a gold armor piece.
  266. *
  267. * @param is Item to check
  268. * @return true if the item is gold armor, false otherwise
  269. */
  270. public static boolean isGoldArmor(ItemStack is) {
  271. switch (is.getType()) {
  272. case GOLD_BOOTS:
  273. case GOLD_CHESTPLATE:
  274. case GOLD_HELMET:
  275. case GOLD_LEGGINGS:
  276. return true;
  277. default:
  278. return false;
  279. }
  280. }
  281. /**
  282. * Checks to see if an item is an iron armor piece.
  283. *
  284. * @param is Item to check
  285. * @return true if the item is iron armor, false otherwise
  286. */
  287. public static boolean isIronArmor(ItemStack is) {
  288. switch (is.getType()) {
  289. case IRON_BOOTS:
  290. case IRON_CHESTPLATE:
  291. case IRON_HELMET:
  292. case IRON_LEGGINGS:
  293. return true;
  294. default:
  295. return false;
  296. }
  297. }
  298. /**
  299. * Checks to see if an item is a diamond armor piece.
  300. *
  301. * @param is Item to check
  302. * @return true if the item is diamond armor, false otherwise
  303. */
  304. public static boolean isDiamondArmor(ItemStack is) {
  305. switch (is.getType()) {
  306. case DIAMOND_BOOTS:
  307. case DIAMOND_CHESTPLATE:
  308. case DIAMOND_HELMET:
  309. case DIAMOND_LEGGINGS:
  310. return true;
  311. default:
  312. return false;
  313. }
  314. }
  315. /**
  316. * Checks to see if an item is a tool.
  317. *
  318. * @param is Item to check
  319. * @return true if the item is a tool, false otherwise
  320. */
  321. public static boolean isMinecraftTool(ItemStack is) {
  322. return isStoneTool(is) || isWoodTool(is) || isGoldTool(is) || isIronTool(is) || isDiamondTool(is) || isStringTool(is);
  323. }
  324. /**
  325. * Checks to see if an item is a stone tool.
  326. *
  327. * @param is Item to check
  328. * @return true if the item is a stone tool, false otherwise
  329. */
  330. public static boolean isStoneTool(ItemStack is) {
  331. switch (is.getType()) {
  332. case STONE_AXE:
  333. case STONE_HOE:
  334. case STONE_PICKAXE:
  335. case STONE_SPADE:
  336. case STONE_SWORD:
  337. return true;
  338. default:
  339. return false;
  340. }
  341. }
  342. /**
  343. * Checks to see if an item is a wooden tool.
  344. *
  345. * @param is Item to check
  346. * @return true if the item is a wooden tool, false otherwise
  347. */
  348. public static boolean isWoodTool(ItemStack is) {
  349. switch (is.getType()) {
  350. case WOOD_AXE:
  351. case WOOD_HOE:
  352. case WOOD_PICKAXE:
  353. case WOOD_SPADE:
  354. case WOOD_SWORD:
  355. return true;
  356. default:
  357. return false;
  358. }
  359. }
  360. /**
  361. * Checks to see if an item is a string tool.
  362. *
  363. * @param is Item to check
  364. * @return true if the item is a string tool, false otherwise
  365. */
  366. public static boolean isStringTool(ItemStack is) {
  367. switch (is.getType()) {
  368. case BOW:
  369. case CARROT_STICK:
  370. case FISHING_ROD:
  371. return true;
  372. default:
  373. return false;
  374. }
  375. }
  376. /**
  377. * Checks to see if an item is a gold tool.
  378. *
  379. * @param is Item to check
  380. * @return true if the item is a stone tool, false otherwise
  381. */
  382. public static boolean isGoldTool(ItemStack is) {
  383. switch (is.getType()) {
  384. case GOLD_AXE:
  385. case GOLD_HOE:
  386. case GOLD_PICKAXE:
  387. case GOLD_SPADE:
  388. case GOLD_SWORD:
  389. return true;
  390. default:
  391. return false;
  392. }
  393. }
  394. /**
  395. * Checks to see if an item is an iron tool.
  396. *
  397. * @param is Item to check
  398. * @return true if the item is an iron tool, false otherwise
  399. */
  400. public static boolean isIronTool(ItemStack is) {
  401. switch (is.getType()) {
  402. case BUCKET:
  403. case FLINT_AND_STEEL:
  404. case IRON_AXE:
  405. case IRON_HOE:
  406. case IRON_PICKAXE:
  407. case IRON_SPADE:
  408. case IRON_SWORD:
  409. case SHEARS:
  410. return true;
  411. default:
  412. return false;
  413. }
  414. }
  415. /**
  416. * Checks to see if an item is a diamond tool.
  417. *
  418. * @param is Item to check
  419. * @return true if the item is a diamond tool, false otherwise
  420. */
  421. public static boolean isDiamondTool(ItemStack is) {
  422. switch (is.getType()) {
  423. case DIAMOND_AXE:
  424. case DIAMOND_HOE:
  425. case DIAMOND_PICKAXE:
  426. case DIAMOND_SPADE:
  427. case DIAMOND_SWORD:
  428. return true;
  429. default:
  430. return false;
  431. }
  432. }
  433. /**
  434. * Checks to see if an item is enchantable.
  435. *
  436. * @param is Item to check
  437. * @return true if the item is enchantable, false otherwise
  438. */
  439. public static boolean isEnchantable(ItemStack is) {
  440. Material type = is.getType();
  441. return isArmor(is) || isSword(is) || isAxe(is) || isShovel(is) || isPickaxe(is) || type == Material.SHEARS || type == Material.FISHING_ROD || type == Material.CARROT_STICK || type == Material.FLINT_AND_STEEL || type == Material.BOW;
  442. }
  443. public static boolean isSmeltable(ItemStack itemStack) {
  444. switch (itemStack.getType()) {
  445. case COAL_ORE:
  446. case DIAMOND_ORE:
  447. case GLOWING_REDSTONE_ORE:
  448. case GOLD_ORE:
  449. case IRON_ORE:
  450. case LAPIS_ORE:
  451. case REDSTONE_ORE:
  452. case EMERALD_ORE:
  453. return true;
  454. default:
  455. return false;
  456. }
  457. }
  458. public static boolean isSmelted(ItemStack itemStack) {
  459. switch (itemStack.getType()) {
  460. case COAL:
  461. case DIAMOND:
  462. case REDSTONE:
  463. case GOLD_INGOT:
  464. case IRON_INGOT:
  465. case EMERALD:
  466. return true;
  467. case INK_SACK:
  468. if (itemStack.getData().getData() == DyeColor.BLUE.getDyeData()) {
  469. return true;
  470. }
  471. return false;
  472. default:
  473. return false;
  474. }
  475. }
  476. /**
  477. * Check if an item is sharable.
  478. *
  479. * @param item Item that will get shared
  480. * @return True if the item can be shared.
  481. */
  482. public static boolean isShareable(ItemStack is) {
  483. return isMiningDrop(is) || isWoodcuttingDrop(is) || isMobDrop(is) || isHerbalismDrop(is) || isMiscDrop(is);
  484. }
  485. /**
  486. * Checks to see if an item is a mining drop.
  487. *
  488. * @param is Item to check
  489. * @return true if the item is a mining drop, false otherwise
  490. */
  491. public static boolean isMiningDrop(ItemStack is) {
  492. switch (is.getType()) {
  493. case COAL:
  494. case COAL_ORE:
  495. case DIAMOND:
  496. case DIAMOND_ORE:
  497. case EMERALD:
  498. case EMERALD_ORE:
  499. case GOLD_ORE:
  500. case IRON_ORE:
  501. case LAPIS_ORE:
  502. case REDSTONE_ORE:
  503. case REDSTONE:
  504. case GLOWSTONE_DUST:
  505. return true;
  506. case INK_SACK:
  507. if (is.getData().getData() == DyeColor.BLUE.getDyeData()) {
  508. return true;
  509. }
  510. return false;
  511. default:
  512. return false;
  513. }
  514. }
  515. /**
  516. * Checks to see if an item is a herbalism drop.
  517. *
  518. * @param is Item to check
  519. * @return true if the item is a herbalism drop, false otherwise
  520. */
  521. public static boolean isHerbalismDrop(ItemStack is) {
  522. switch (is.getType()) {
  523. case WHEAT:
  524. case SEEDS:
  525. case CARROT_ITEM:
  526. case POTATO_ITEM:
  527. case NETHER_WARTS:
  528. case BROWN_MUSHROOM:
  529. case RED_MUSHROOM:
  530. case RED_ROSE:
  531. case YELLOW_FLOWER:
  532. case CACTUS:
  533. case SUGAR_CANE:
  534. case MELON:
  535. case MELON_SEEDS:
  536. case PUMPKIN:
  537. case PUMPKIN_SEEDS:
  538. case WATER_LILY:
  539. case VINE:
  540. return true;
  541. case INK_SACK:
  542. return is.getData().getData() == DyeColor.BROWN.getDyeData();
  543. default:
  544. return false;
  545. }
  546. }
  547. /**
  548. * Checks to see if an item is a mob drop.
  549. *
  550. * @param is Item to check
  551. * @return true if the item is a mob drop, false otherwise
  552. */
  553. public static boolean isMobDrop(ItemStack is) {
  554. switch (is.getType()) {
  555. case STRING:
  556. case FEATHER:
  557. case RAW_CHICKEN:
  558. case COOKED_CHICKEN:
  559. case LEATHER:
  560. case RAW_BEEF:
  561. case COOKED_BEEF:
  562. case PORK:
  563. case GRILLED_PORK:
  564. case WOOL:
  565. case RED_ROSE:
  566. case IRON_INGOT:
  567. case SNOW_BALL:
  568. case BLAZE_ROD:
  569. case SPIDER_EYE:
  570. case SULPHUR:
  571. case ENDER_PEARL:
  572. case GHAST_TEAR:
  573. case MAGMA_CREAM:
  574. case BONE:
  575. case ARROW:
  576. case SLIME_BALL:
  577. case NETHER_STAR:
  578. case COAL:
  579. case ROTTEN_FLESH:
  580. case GOLD_NUGGET:
  581. case EGG:
  582. return true;
  583. default:
  584. return false;
  585. }
  586. }
  587. /**
  588. * Checks to see if an item is a woodcutting drop.
  589. *
  590. * @param is Item to check
  591. * @return true if the item is a woodcutting drop, false otherwise
  592. */
  593. public static boolean isWoodcuttingDrop(ItemStack is) {
  594. switch (is.getType()) {
  595. case LOG:
  596. case LEAVES:
  597. case SAPLING:
  598. case APPLE:
  599. return true;
  600. default:
  601. return false;
  602. }
  603. }
  604. /**
  605. * Checks to see if an item is a miscellaneous drop. These items are read from the config file
  606. *
  607. * @param is Item to check
  608. * @return true if the item is a miscellaneous drop, false otherwise
  609. */
  610. public static boolean isMiscDrop(ItemStack is) {
  611. return ItemWeightConfig.getInstance().getMiscItems().contains(is.getType());
  612. }
  613. public static boolean isMcMMOItem(ItemStack is) {
  614. if (!is.hasItemMeta()) {
  615. return false;
  616. }
  617. ItemMeta itemMeta = is.getItemMeta();
  618. if (itemMeta.hasLore()) {
  619. List<String> itemLore = itemMeta.getLore();
  620. if (itemLore.contains("mcMMO Item")) {
  621. return true;
  622. }
  623. }
  624. return false;
  625. }
  626. public static boolean isChimaeraWing(ItemStack is) {
  627. if (!isMcMMOItem(is)) {
  628. return false;
  629. }
  630. ItemMeta itemMeta = is.getItemMeta();
  631. if (itemMeta.hasDisplayName() && itemMeta.getDisplayName().equals(ChatColor.GOLD + LocaleLoader.getString("Item.ChimaeraWing.Name"))) {
  632. return true;
  633. }
  634. return false;
  635. }
  636. }