BlockUtils.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package com.gmail.nossr50.util;
  2. import com.gmail.nossr50.config.experience.ExperienceConfig;
  3. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  4. import com.gmail.nossr50.mcMMO;
  5. import com.gmail.nossr50.skills.repair.Repair;
  6. import com.gmail.nossr50.skills.salvage.Salvage;
  7. import org.bukkit.Material;
  8. import org.bukkit.block.BlockState;
  9. import org.bukkit.block.data.Ageable;
  10. import org.bukkit.block.data.BlockData;
  11. import java.util.HashSet;
  12. public final class BlockUtils {
  13. private BlockUtils() {}
  14. /**
  15. * Checks to see if a given block awards XP.
  16. *
  17. * @param blockState
  18. * The {@link BlockState} of the block to check
  19. * @return true if the block awards XP, false otherwise
  20. */
  21. public static boolean shouldBeWatched(BlockState blockState) {
  22. return affectedByGigaDrillBreaker(blockState) || affectedByGreenTerra(blockState) || affectedBySuperBreaker(blockState) || isLog(blockState);
  23. }
  24. /**
  25. * Check if a given block should allow for the activation of abilities
  26. *
  27. * @param blockState
  28. * The {@link BlockState} of the block to check
  29. * @return true if the block should allow ability activation, false
  30. * otherwise
  31. */
  32. public static boolean canActivateAbilities(BlockState blockState) {
  33. switch (blockState.getType()) {
  34. case BLACK_BED:
  35. case BLUE_BED:
  36. case BROWN_BED:
  37. case CYAN_BED:
  38. case GRAY_BED:
  39. case GREEN_BED:
  40. case LIGHT_BLUE_BED:
  41. case LIGHT_GRAY_BED:
  42. case LIME_BED:
  43. case MAGENTA_BED:
  44. case ORANGE_BED:
  45. case PINK_BED:
  46. case PURPLE_BED:
  47. case RED_BED:
  48. case WHITE_BED:
  49. case YELLOW_BED:
  50. case BREWING_STAND :
  51. case BOOKSHELF :
  52. case CAKE:
  53. case CHEST :
  54. case DISPENSER :
  55. case ENCHANTING_TABLE:
  56. case ENDER_CHEST :
  57. case OAK_FENCE_GATE:
  58. case ACACIA_FENCE_GATE :
  59. case DARK_OAK_FENCE_GATE :
  60. case SPRUCE_FENCE_GATE :
  61. case BIRCH_FENCE_GATE :
  62. case JUNGLE_FENCE_GATE :
  63. case FURNACE :
  64. case JUKEBOX :
  65. case LEVER :
  66. case NOTE_BLOCK :
  67. case STONE_BUTTON :
  68. case OAK_BUTTON:
  69. case BIRCH_BUTTON:
  70. case ACACIA_BUTTON:
  71. case DARK_OAK_BUTTON:
  72. case JUNGLE_BUTTON:
  73. case SPRUCE_BUTTON:
  74. case ACACIA_TRAPDOOR:
  75. case BIRCH_TRAPDOOR:
  76. case DARK_OAK_TRAPDOOR:
  77. case JUNGLE_TRAPDOOR:
  78. case OAK_TRAPDOOR:
  79. case SPRUCE_TRAPDOOR:
  80. case WALL_SIGN :
  81. case CRAFTING_TABLE:
  82. case BEACON :
  83. case ANVIL :
  84. case DROPPER :
  85. case HOPPER :
  86. case TRAPPED_CHEST :
  87. case IRON_DOOR :
  88. case IRON_TRAPDOOR :
  89. case OAK_DOOR:
  90. case ACACIA_DOOR :
  91. case SPRUCE_DOOR :
  92. case BIRCH_DOOR :
  93. case JUNGLE_DOOR :
  94. case DARK_OAK_DOOR :
  95. case OAK_FENCE:
  96. case ACACIA_FENCE :
  97. case DARK_OAK_FENCE :
  98. case BIRCH_FENCE :
  99. case JUNGLE_FENCE :
  100. case SPRUCE_FENCE :
  101. case ARMOR_STAND :
  102. case BLACK_SHULKER_BOX :
  103. case BLUE_SHULKER_BOX :
  104. case BROWN_SHULKER_BOX :
  105. case CYAN_SHULKER_BOX :
  106. case GRAY_SHULKER_BOX :
  107. case GREEN_SHULKER_BOX :
  108. case LIGHT_BLUE_SHULKER_BOX :
  109. case LIME_SHULKER_BOX :
  110. case MAGENTA_SHULKER_BOX :
  111. case ORANGE_SHULKER_BOX :
  112. case PINK_SHULKER_BOX :
  113. case PURPLE_SHULKER_BOX :
  114. case RED_SHULKER_BOX :
  115. case LIGHT_GRAY_SHULKER_BOX:
  116. case WHITE_SHULKER_BOX :
  117. case YELLOW_SHULKER_BOX :
  118. return false;
  119. default :
  120. return !isMcMMOAnvil(blockState) && !mcMMO.getModManager().isCustomAbilityBlock(blockState);
  121. }
  122. }
  123. /**
  124. * Check if a given block should allow for the activation of tools
  125. * Activating a tool is step 1 of a 2 step process for super ability activation
  126. *
  127. * @param blockState
  128. * The {@link BlockState} of the block to check
  129. * @return true if the block should allow ability activation, false
  130. * otherwise
  131. */
  132. public static boolean canActivateTools(BlockState blockState) {
  133. switch (blockState.getType()) {
  134. case BLACK_BED:
  135. case BLUE_BED:
  136. case BROWN_BED:
  137. case CYAN_BED:
  138. case GRAY_BED:
  139. case GREEN_BED:
  140. case LIGHT_BLUE_BED:
  141. case LIGHT_GRAY_BED:
  142. case LIME_BED:
  143. case MAGENTA_BED:
  144. case ORANGE_BED:
  145. case PINK_BED:
  146. case PURPLE_BED:
  147. case RED_BED:
  148. case WHITE_BED:
  149. case YELLOW_BED:
  150. case BREWING_STAND :
  151. case BOOKSHELF :
  152. case CAKE:
  153. case CHEST :
  154. case DISPENSER :
  155. case ENCHANTING_TABLE:
  156. case ENDER_CHEST :
  157. case OAK_FENCE_GATE:
  158. case ACACIA_FENCE_GATE :
  159. case DARK_OAK_FENCE_GATE :
  160. case SPRUCE_FENCE_GATE :
  161. case BIRCH_FENCE_GATE :
  162. case JUNGLE_FENCE_GATE :
  163. case FURNACE :
  164. case JUKEBOX :
  165. case LEVER :
  166. case NOTE_BLOCK :
  167. case STONE_BUTTON :
  168. case OAK_BUTTON:
  169. case BIRCH_BUTTON:
  170. case ACACIA_BUTTON:
  171. case DARK_OAK_BUTTON:
  172. case JUNGLE_BUTTON:
  173. case SPRUCE_BUTTON:
  174. case ACACIA_TRAPDOOR:
  175. case BIRCH_TRAPDOOR:
  176. case DARK_OAK_TRAPDOOR:
  177. case JUNGLE_TRAPDOOR:
  178. case OAK_TRAPDOOR:
  179. case SPRUCE_TRAPDOOR:
  180. case WALL_SIGN :
  181. case CRAFTING_TABLE:
  182. case BEACON :
  183. case ANVIL :
  184. case DROPPER :
  185. case HOPPER :
  186. case TRAPPED_CHEST :
  187. case IRON_DOOR :
  188. case IRON_TRAPDOOR :
  189. case OAK_DOOR:
  190. case ACACIA_DOOR :
  191. case SPRUCE_DOOR :
  192. case BIRCH_DOOR :
  193. case JUNGLE_DOOR :
  194. case DARK_OAK_DOOR :
  195. case OAK_FENCE:
  196. case ACACIA_FENCE :
  197. case DARK_OAK_FENCE :
  198. case BIRCH_FENCE :
  199. case JUNGLE_FENCE :
  200. case SPRUCE_FENCE :
  201. case ARMOR_STAND :
  202. case BLACK_SHULKER_BOX :
  203. case BLUE_SHULKER_BOX :
  204. case BROWN_SHULKER_BOX :
  205. case CYAN_SHULKER_BOX :
  206. case GRAY_SHULKER_BOX :
  207. case GREEN_SHULKER_BOX :
  208. case LIGHT_BLUE_SHULKER_BOX :
  209. case LIME_SHULKER_BOX :
  210. case MAGENTA_SHULKER_BOX :
  211. case ORANGE_SHULKER_BOX :
  212. case PINK_SHULKER_BOX :
  213. case PURPLE_SHULKER_BOX :
  214. case RED_SHULKER_BOX :
  215. case LIGHT_GRAY_SHULKER_BOX:
  216. case WHITE_SHULKER_BOX :
  217. case YELLOW_SHULKER_BOX :
  218. case STRIPPED_ACACIA_LOG:
  219. case STRIPPED_ACACIA_WOOD:
  220. case STRIPPED_BIRCH_LOG:
  221. case STRIPPED_BIRCH_WOOD:
  222. case STRIPPED_DARK_OAK_LOG:
  223. case STRIPPED_DARK_OAK_WOOD:
  224. case STRIPPED_JUNGLE_LOG:
  225. case STRIPPED_JUNGLE_WOOD:
  226. case STRIPPED_OAK_LOG:
  227. case STRIPPED_OAK_WOOD:
  228. case STRIPPED_SPRUCE_LOG:
  229. case STRIPPED_SPRUCE_WOOD:
  230. case ACACIA_LOG:
  231. case ACACIA_WOOD:
  232. case BIRCH_LOG:
  233. case BIRCH_WOOD:
  234. case DARK_OAK_LOG:
  235. case DARK_OAK_WOOD:
  236. case JUNGLE_LOG:
  237. case JUNGLE_WOOD:
  238. case OAK_LOG:
  239. case OAK_WOOD:
  240. case SPRUCE_LOG:
  241. case SPRUCE_WOOD:
  242. return false;
  243. default :
  244. return !isMcMMOAnvil(blockState) && !mcMMO.getModManager().isCustomAbilityBlock(blockState);
  245. }
  246. }
  247. /**
  248. * Check if a given block is an ore
  249. *
  250. * @param blockState
  251. * The {@link BlockState} of the block to check
  252. * @return true if the block is an ore, false otherwise
  253. */
  254. public static boolean isOre(BlockState blockState) {
  255. return MaterialUtils.isOre(blockState.getType());
  256. }
  257. /**
  258. * Determine if a given block can be made mossy
  259. *
  260. * @param blockState
  261. * The {@link BlockState} of the block to check
  262. * @return true if the block can be made mossy, false otherwise
  263. */
  264. public static boolean canMakeMossy(BlockState blockState) {
  265. switch (blockState.getType()) {
  266. case COBBLESTONE :
  267. case DIRT :
  268. case GRASS_PATH :
  269. return true;
  270. case STONE_BRICKS:
  271. return true;
  272. case COBBLESTONE_WALL:
  273. return true;
  274. default :
  275. return false;
  276. }
  277. }
  278. /**
  279. * Determine if a given block should be affected by Green Terra
  280. *
  281. * @param blockState
  282. * The {@link BlockState} of the block to check
  283. * @return true if the block should affected by Green Terra, false otherwise
  284. */
  285. public static boolean affectedByGreenTerra(BlockState blockState) {
  286. if (ExperienceConfig.getInstance().doesBlockGiveSkillXP(PrimarySkillType.HERBALISM, blockState.getBlockData())) {
  287. return true;
  288. }
  289. return mcMMO.getModManager().isCustomHerbalismBlock(blockState);
  290. }
  291. /**
  292. * Determine if a given block should be affected by Super Breaker
  293. *
  294. * @param blockState
  295. * The {@link BlockState} of the block to check
  296. * @return true if the block should affected by Super Breaker, false
  297. * otherwise
  298. */
  299. public static Boolean affectedBySuperBreaker(BlockState blockState) {
  300. if (ExperienceConfig.getInstance().doesBlockGiveSkillXP(PrimarySkillType.MINING, blockState.getBlockData()))
  301. return true;
  302. return isOre(blockState) || mcMMO.getModManager().isCustomMiningBlock(blockState);
  303. }
  304. /**
  305. * Determine if a given block should be affected by Giga Drill Breaker
  306. *
  307. * @param blockState
  308. * The {@link BlockState} of the block to check
  309. * @return true if the block should affected by Giga Drill Breaker, false
  310. * otherwise
  311. */
  312. public static boolean affectedByGigaDrillBreaker(BlockState blockState) {
  313. if (ExperienceConfig.getInstance().doesBlockGiveSkillXP(PrimarySkillType.EXCAVATION, blockState.getBlockData()))
  314. return true;
  315. return mcMMO.getModManager().isCustomExcavationBlock(blockState);
  316. }
  317. /**
  318. * Check if a given block is a log
  319. *
  320. * @param blockState
  321. * The {@link BlockState} of the block to check
  322. * @return true if the block is a log, false otherwise
  323. */
  324. public static boolean isLog(BlockState blockState) {
  325. if (ExperienceConfig.getInstance().doesBlockGiveSkillXP(PrimarySkillType.WOODCUTTING, blockState.getBlockData()))
  326. return true;
  327. return mcMMO.getModManager().isCustomLog(blockState);
  328. }
  329. /**
  330. * Check if a given block is a leaf
  331. *
  332. * @param blockState
  333. * The {@link BlockState} of the block to check
  334. * @return true if the block is a leaf, false otherwise
  335. */
  336. public static boolean isLeaves(BlockState blockState) {
  337. switch (blockState.getType()) {
  338. case OAK_LEAVES:
  339. case ACACIA_LEAVES:
  340. case BIRCH_LEAVES:
  341. case DARK_OAK_LEAVES:
  342. case JUNGLE_LEAVES:
  343. case SPRUCE_LEAVES:
  344. return true;
  345. default :
  346. return mcMMO.getModManager().isCustomLeaf(blockState);
  347. }
  348. }
  349. /**
  350. * Determine if a given block should be affected by Flux Mining
  351. *
  352. * @param blockState
  353. * The {@link BlockState} of the block to check
  354. * @return true if the block should affected by Flux Mining, false otherwise
  355. */
  356. public static boolean affectedByFluxMining(BlockState blockState) {
  357. switch (blockState.getType()) {
  358. case IRON_ORE :
  359. case GOLD_ORE :
  360. return true;
  361. default :
  362. return false;
  363. }
  364. }
  365. /**
  366. * Determine if a given block can activate Herbalism abilities
  367. *
  368. * @param blockState
  369. * The {@link BlockState} of the block to check
  370. * @return true if the block can be activate Herbalism abilities, false
  371. * otherwise
  372. */
  373. public static boolean canActivateHerbalism(BlockState blockState) {
  374. switch (blockState.getType()) {
  375. case DIRT :
  376. case GRASS :
  377. case GRASS_PATH :
  378. case FARMLAND:
  379. return false;
  380. default :
  381. return true;
  382. }
  383. }
  384. /**
  385. * Determine if a given block should be affected by Block Cracker
  386. *
  387. * @param blockState
  388. * The {@link BlockState} of the block to check
  389. * @return true if the block should affected by Block Cracker, false
  390. * otherwise
  391. */
  392. public static boolean affectedByBlockCracker(BlockState blockState) {
  393. switch (blockState.getType()) {
  394. case STONE_BRICKS:
  395. return true;
  396. default :
  397. return false;
  398. }
  399. }
  400. /**
  401. * Determine if a given block can be made into Mycelium
  402. *
  403. * @param blockState
  404. * The {@link BlockState} of the block to check
  405. * @return true if the block can be made into Mycelium, false otherwise
  406. */
  407. public static boolean canMakeShroomy(BlockState blockState) {
  408. switch (blockState.getType()) {
  409. case DIRT :
  410. case GRASS :
  411. case GRASS_PATH :
  412. return true;
  413. default :
  414. return false;
  415. }
  416. }
  417. /**
  418. * Determine if a given block is an mcMMO anvil
  419. *
  420. * @param blockState
  421. * The {@link BlockState} of the block to check
  422. * @return true if the block is an mcMMO anvil, false otherwise
  423. */
  424. public static boolean isMcMMOAnvil(BlockState blockState) {
  425. Material type = blockState.getType();
  426. return type == Repair.anvilMaterial || type == Salvage.anvilMaterial;
  427. }
  428. public static boolean isPistonPiece(BlockState blockState) {
  429. Material type = blockState.getType();
  430. return type == Material.MOVING_PISTON || type == Material.AIR;
  431. }
  432. /**
  433. * Get a HashSet containing every transparent block
  434. *
  435. * @return HashSet with the IDs of every transparent block
  436. */
  437. public static HashSet<Material> getTransparentBlocks() {
  438. HashSet<Material> transparentBlocks = new HashSet<Material>();
  439. for (Material material : Material.values()) {
  440. if (material.isTransparent()) {
  441. transparentBlocks.add(material);
  442. }
  443. }
  444. return transparentBlocks;
  445. }
  446. public static boolean isFullyGrown(BlockState blockState) {
  447. BlockData data = blockState.getBlockData();
  448. if (data.getMaterial() == Material.CACTUS || data.getMaterial() == Material.SUGAR_CANE)
  449. return true;
  450. if (data instanceof Ageable)
  451. {
  452. Ageable ageable = (Ageable) data;
  453. return ageable.getAge() == ageable.getMaximumAge();
  454. }
  455. return true;
  456. }
  457. }