BlockUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package com.gmail.nossr50.util;
  2. import java.util.HashSet;
  3. import org.bukkit.CropState;
  4. import org.bukkit.Material;
  5. import org.bukkit.NetherWartsState;
  6. import org.bukkit.block.BlockState;
  7. import org.bukkit.material.CocoaPlant;
  8. import org.bukkit.material.CocoaPlant.CocoaPlantSize;
  9. import org.bukkit.material.Crops;
  10. import org.bukkit.material.NetherWarts;
  11. import org.bukkit.material.SmoothBrick;
  12. import com.gmail.nossr50.mcMMO;
  13. import com.gmail.nossr50.skills.repair.Repair;
  14. import com.gmail.nossr50.skills.salvage.Salvage;
  15. public final class BlockUtils {
  16. private BlockUtils() {}
  17. /**
  18. * Checks to see if a given block awards XP.
  19. *
  20. * @param blockState The {@link BlockState} of the block to check
  21. * @return true if the block awards XP, false otherwise
  22. */
  23. public static boolean shouldBeWatched(BlockState blockState) {
  24. return affectedByGigaDrillBreaker(blockState) || affectedByGreenTerra(blockState) || affectedBySuperBreaker(blockState) || isLog(blockState);
  25. }
  26. /**
  27. * Check if a given block should allow for the activation of abilities
  28. *
  29. * @param blockState The {@link BlockState} of the block to check
  30. * @return true if the block should allow ability activation, false otherwise
  31. */
  32. public static boolean canActivateAbilities(BlockState blockState) {
  33. switch (blockState.getType()) {
  34. case BED_BLOCK:
  35. case BREWING_STAND:
  36. case BOOKSHELF:
  37. case BURNING_FURNACE:
  38. case CAKE_BLOCK:
  39. case CHEST:
  40. case DISPENSER:
  41. case ENCHANTMENT_TABLE:
  42. case ENDER_CHEST:
  43. case FENCE_GATE:
  44. case ACACIA_FENCE_GATE:
  45. case DARK_OAK_FENCE_GATE:
  46. case SPRUCE_FENCE_GATE:
  47. case BIRCH_FENCE_GATE:
  48. case JUNGLE_FENCE_GATE:
  49. case FURNACE:
  50. case IRON_DOOR_BLOCK:
  51. case JUKEBOX:
  52. case LEVER:
  53. case NOTE_BLOCK:
  54. case STONE_BUTTON:
  55. case WOOD_BUTTON:
  56. case TRAP_DOOR:
  57. case WALL_SIGN:
  58. case WOODEN_DOOR:
  59. case WORKBENCH:
  60. case BEACON:
  61. case ANVIL:
  62. case DROPPER:
  63. case HOPPER:
  64. case TRAPPED_CHEST:
  65. case IRON_DOOR:
  66. case IRON_TRAPDOOR:
  67. case ACACIA_DOOR:
  68. case SPRUCE_DOOR:
  69. case BIRCH_DOOR:
  70. case JUNGLE_DOOR:
  71. case DARK_OAK_DOOR:
  72. case FENCE:
  73. case ACACIA_FENCE:
  74. case DARK_OAK_FENCE:
  75. case BIRCH_FENCE:
  76. case JUNGLE_FENCE:
  77. case SPRUCE_FENCE:
  78. case ARMOR_STAND:
  79. return false;
  80. default:
  81. return !isMcMMOAnvil(blockState) && !mcMMO.getModManager().isCustomAbilityBlock(blockState);
  82. }
  83. }
  84. /**
  85. * Check if a given block is an ore
  86. *
  87. * @param blockState The {@link BlockState} of the block to check
  88. * @return true if the block is an ore, false otherwise
  89. */
  90. public static boolean isOre(BlockState blockState) {
  91. return MaterialUtils.isOre(blockState.getData());
  92. }
  93. /**
  94. * Determine if a given block can be made mossy
  95. *
  96. * @param blockState The {@link BlockState} of the block to check
  97. * @return true if the block can be made mossy, false otherwise
  98. */
  99. public static boolean canMakeMossy(BlockState blockState) {
  100. switch (blockState.getType()) {
  101. case COBBLESTONE:
  102. case DIRT:
  103. return true;
  104. case SMOOTH_BRICK:
  105. return ((SmoothBrick) blockState.getData()).getMaterial() == Material.STONE;
  106. case COBBLE_WALL:
  107. return blockState.getRawData() == (byte) 0x0;
  108. default:
  109. return false;
  110. }
  111. }
  112. /**
  113. * Determine if a given block should be affected by Green Terra
  114. *
  115. * @param blockState The {@link BlockState} of the block to check
  116. * @return true if the block should affected by Green Terra, false otherwise
  117. */
  118. public static boolean affectedByGreenTerra(BlockState blockState) {
  119. switch (blockState.getType()) {
  120. case BROWN_MUSHROOM:
  121. case CACTUS:
  122. case DOUBLE_PLANT:
  123. case MELON_BLOCK:
  124. case LONG_GRASS:
  125. case PUMPKIN:
  126. case RED_MUSHROOM:
  127. case RED_ROSE:
  128. case SUGAR_CANE_BLOCK:
  129. case VINE:
  130. case WATER_LILY:
  131. case YELLOW_FLOWER:
  132. return true;
  133. case CARROT:
  134. case POTATO:
  135. return blockState.getRawData() == CropState.RIPE.getData();
  136. case CROPS:
  137. return ((Crops) blockState.getData()).getState() == CropState.RIPE;
  138. case NETHER_WARTS:
  139. return ((NetherWarts) blockState.getData()).getState() == NetherWartsState.RIPE;
  140. case COCOA:
  141. return ((CocoaPlant) blockState.getData()).getSize() == CocoaPlantSize.LARGE;
  142. default:
  143. return mcMMO.getModManager().isCustomHerbalismBlock(blockState);
  144. }
  145. }
  146. /**
  147. * Determine if a given block should be affected by Super Breaker
  148. *
  149. * @param blockState The {@link BlockState} of the block to check
  150. * @return true if the block should affected by Super Breaker, false otherwise
  151. */
  152. public static Boolean affectedBySuperBreaker(BlockState blockState) {
  153. switch (blockState.getType()) {
  154. case ENDER_STONE:
  155. case GLOWSTONE:
  156. case HARD_CLAY:
  157. case MOSSY_COBBLESTONE:
  158. case NETHERRACK:
  159. case OBSIDIAN:
  160. case PACKED_ICE:
  161. case SANDSTONE:
  162. case STAINED_CLAY:
  163. case STONE:
  164. case PRISMARINE:
  165. case RED_SANDSTONE:
  166. return true;
  167. default:
  168. return isOre(blockState) || mcMMO.getModManager().isCustomMiningBlock(blockState);
  169. }
  170. }
  171. /**
  172. * Determine if a given block should be affected by Giga Drill Breaker
  173. *
  174. * @param blockState The {@link BlockState} of the block to check
  175. * @return true if the block should affected by Giga Drill Breaker, false otherwise
  176. */
  177. public static boolean affectedByGigaDrillBreaker(BlockState blockState) {
  178. switch (blockState.getType()) {
  179. case CLAY:
  180. case DIRT:
  181. case GRASS:
  182. case GRAVEL:
  183. case MYCEL:
  184. case SAND:
  185. case SNOW:
  186. case SNOW_BLOCK:
  187. case SOUL_SAND:
  188. return true;
  189. default:
  190. return mcMMO.getModManager().isCustomExcavationBlock(blockState);
  191. }
  192. }
  193. /**
  194. * Check if a given block is a log
  195. *
  196. * @param blockState The {@link BlockState} of the block to check
  197. * @return true if the block is a log, false otherwise
  198. */
  199. public static boolean isLog(BlockState blockState) {
  200. switch (blockState.getType()) {
  201. case LOG:
  202. case LOG_2:
  203. case HUGE_MUSHROOM_1:
  204. case HUGE_MUSHROOM_2:
  205. return true;
  206. default:
  207. return mcMMO.getModManager().isCustomLog(blockState);
  208. }
  209. }
  210. /**
  211. * Check if a given block is a leaf
  212. *
  213. * @param blockState The {@link BlockState} of the block to check
  214. * @return true if the block is a leaf, false otherwise
  215. */
  216. public static boolean isLeaves(BlockState blockState) {
  217. switch (blockState.getType()) {
  218. case LEAVES:
  219. case LEAVES_2:
  220. return true;
  221. default:
  222. return mcMMO.getModManager().isCustomLeaf(blockState);
  223. }
  224. }
  225. /**
  226. * Determine if a given block should be affected by Flux Mining
  227. *
  228. * @param blockState The {@link BlockState} of the block to check
  229. * @return true if the block should affected by Flux Mining, false otherwise
  230. */
  231. public static boolean affectedByFluxMining(BlockState blockState) {
  232. switch (blockState.getType()) {
  233. case IRON_ORE:
  234. case GOLD_ORE:
  235. return true;
  236. default:
  237. return false;
  238. }
  239. }
  240. /**
  241. * Determine if a given block can activate Herbalism abilities
  242. *
  243. * @param blockState The {@link BlockState} of the block to check
  244. * @return true if the block can be activate Herbalism abilities, false otherwise
  245. */
  246. public static boolean canActivateHerbalism(BlockState blockState) {
  247. switch (blockState.getType()) {
  248. case DIRT:
  249. case GRASS:
  250. case SOIL:
  251. return false;
  252. default:
  253. return true;
  254. }
  255. }
  256. /**
  257. * Determine if a given block should be affected by Block Cracker
  258. *
  259. * @param blockState The {@link BlockState} of the block to check
  260. * @return true if the block should affected by Block Cracker, false otherwise
  261. */
  262. public static boolean affectedByBlockCracker(BlockState blockState) {
  263. switch (blockState.getType()) {
  264. case SMOOTH_BRICK:
  265. return ((SmoothBrick) blockState.getData()).getMaterial() == Material.STONE;
  266. default:
  267. return false;
  268. }
  269. }
  270. /**
  271. * Determine if a given block can be made into Mycelium
  272. *
  273. * @param blockState The {@link BlockState} of the block to check
  274. * @return true if the block can be made into Mycelium, false otherwise
  275. */
  276. public static boolean canMakeShroomy(BlockState blockState) {
  277. switch (blockState.getType()) {
  278. case DIRT:
  279. case GRASS:
  280. return true;
  281. default:
  282. return false;
  283. }
  284. }
  285. /**
  286. * Determine if a given block is an mcMMO anvil
  287. *
  288. * @param blockState The {@link BlockState} of the block to check
  289. * @return true if the block is an mcMMO anvil, false otherwise
  290. */
  291. public static boolean isMcMMOAnvil(BlockState blockState) {
  292. Material type = blockState.getType();
  293. return type == Repair.anvilMaterial || type == Salvage.anvilMaterial;
  294. }
  295. public static boolean isPistonPiece(BlockState blockState) {
  296. Material type = blockState.getType();
  297. return type == Material.PISTON_MOVING_PIECE || type == Material.AIR;
  298. }
  299. /**
  300. * Get a HashSet containing every transparent block
  301. *
  302. * @return HashSet with the IDs of every transparent block
  303. */
  304. public static HashSet<Byte> getTransparentBlocks() {
  305. HashSet<Byte> transparentBlocks = new HashSet<Byte>();
  306. for (Material material : Material.values()) {
  307. if (material.isTransparent()) {
  308. transparentBlocks.add((byte) material.getId());
  309. }
  310. }
  311. return transparentBlocks;
  312. }
  313. }