ItemUtils.java 20 KB

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