BlockUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package com.gmail.nossr50.util;
  2. import com.gmail.nossr50.core.MetadataConstants;
  3. import com.gmail.nossr50.datatypes.meta.BonusDropMeta;
  4. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  5. import com.gmail.nossr50.mcMMO;
  6. import com.gmail.nossr50.skills.repair.Repair;
  7. import com.gmail.nossr50.skills.salvage.Salvage;
  8. import com.gmail.nossr50.util.random.RandomChanceSkill;
  9. import com.gmail.nossr50.util.random.RandomChanceUtil;
  10. import org.bukkit.Material;
  11. import org.bukkit.block.Block;
  12. import org.bukkit.block.BlockState;
  13. import org.bukkit.block.data.Ageable;
  14. import org.bukkit.block.data.BlockData;
  15. import org.bukkit.entity.Player;
  16. import java.util.HashSet;
  17. public final class BlockUtils {
  18. private BlockUtils() {
  19. }
  20. /**
  21. * Mark a block for giving bonus drops, double drops are used if triple is false
  22. *
  23. * @param blockState target blockstate
  24. * @param triple marks the block to give triple drops
  25. */
  26. public static void markDropsAsBonus(BlockState blockState, boolean triple) {
  27. if (triple)
  28. blockState.setMetadata(MetadataConstants.BONUS_DROPS_METAKEY, new BonusDropMeta(2, mcMMO.p));
  29. else
  30. blockState.setMetadata(MetadataConstants.BONUS_DROPS_METAKEY, new BonusDropMeta(1, mcMMO.p));
  31. }
  32. /**
  33. * Checks if a player successfully passed the double drop check
  34. *
  35. * @param blockState the blockstate
  36. * @return true if the player succeeded in the check
  37. */
  38. public static boolean checkDoubleDrops(Player player, BlockState blockState, SubSkillType subSkillType) {
  39. if (mcMMO.getConfigManager().isBonusDropsEnabled(blockState.getType()) && Permissions.isSubSkillEnabled(player, subSkillType)) {
  40. return RandomChanceUtil.checkRandomChanceExecutionSuccess(new RandomChanceSkill(player, subSkillType, true));
  41. }
  42. return false;
  43. }
  44. /**
  45. * Checks to see if a given block awards XP.
  46. *
  47. * @param block The {@link Block} of the block to check
  48. * @return true if the block awards XP, false otherwise
  49. */
  50. public static boolean shouldBeWatched(Block block) {
  51. return affectedByGigaDrillBreaker(block.getType()) || affectedByGreenTerra(block.getType()) || affectedBySuperBreaker(block.getType()) || isLog(block.getType());
  52. }
  53. /**
  54. * Checks to see if a given block awards XP.
  55. *
  56. * @param blockState The {@link BlockState} of the block to check
  57. * @return true if the block awards XP, false otherwise
  58. */
  59. public static boolean shouldBeWatched(BlockState blockState) {
  60. return affectedByGigaDrillBreaker(blockState) || affectedByGreenTerra(blockState) || affectedBySuperBreaker(blockState) || isLog(blockState);
  61. }
  62. /**
  63. * Checks to see if a given block awards XP.
  64. *
  65. * @param material The {@link Material} of the block to check
  66. * @return true if the block awards XP, false otherwise
  67. */
  68. public static boolean shouldBeWatched(Material material) {
  69. return affectedByGigaDrillBreaker(material) || affectedByGreenTerra(material) || affectedBySuperBreaker(material) || isLog(material);
  70. }
  71. /**
  72. * Check if a given block should allow for the activation of abilities
  73. *
  74. * @param blockState The {@link BlockState} of the block to check
  75. * @return true if the block should allow ability activation, false
  76. * otherwise
  77. */
  78. public static boolean canActivateAbilities(BlockState blockState) {
  79. return !mcMMO.getMaterialMapStore().isAbilityActivationBlackListed(blockState.getType());
  80. }
  81. /**
  82. * Check if a given block should allow for the activation of tools
  83. * Activating a tool is step 1 of a 2 step process for super ability activation
  84. *
  85. * @param blockState The {@link BlockState} of the block to check
  86. * @return true if the block should allow ability activation, false
  87. * otherwise
  88. */
  89. public static boolean canActivateTools(BlockState blockState) {
  90. return !mcMMO.getMaterialMapStore().isToolActivationBlackListed(blockState.getType());
  91. }
  92. /**
  93. * Check if a given block is an ore
  94. *
  95. * @param blockState The {@link BlockState} of the block to check
  96. * @return true if the block is an ore, false otherwise
  97. */
  98. public static boolean isOre(BlockState blockState) {
  99. return MaterialUtils.isOre(blockState.getType());
  100. }
  101. /**
  102. * Determine if a given block can be made mossy
  103. *
  104. * @param blockState The {@link BlockState} of the block to check
  105. * @return true if the block can be made mossy, false otherwise
  106. */
  107. public static boolean canMakeMossy(BlockState blockState) {
  108. return mcMMO.getMaterialMapStore().isMossyWhiteListed(blockState.getType());
  109. }
  110. /**
  111. * Determine if a given block should be affected by Green Terra
  112. *
  113. * @param blockState The {@link BlockState} of the block to check
  114. * @return true if the block should affected by Green Terra, false otherwise
  115. */
  116. public static boolean affectedByGreenTerra(BlockState blockState) {
  117. return mcMMO.getConfigManager().getExperienceMapManager().hasHerbalismXp(blockState.getType());
  118. }
  119. /**
  120. * Determine if a given block should be affected by Green Terra
  121. *
  122. * @param material The {@link Material} of the block to check
  123. * @return true if the block should affected by Green Terra, false otherwise
  124. */
  125. public static boolean affectedByGreenTerra(Material material) {
  126. return mcMMO.getConfigManager().getExperienceMapManager().hasHerbalismXp(material);
  127. }
  128. /**
  129. * Determine if a given block should be affected by Super Breaker
  130. *
  131. * @param blockState The {@link BlockState} of the block to check
  132. * @return true if the block should affected by Super Breaker, false
  133. * otherwise
  134. */
  135. public static Boolean affectedBySuperBreaker(BlockState blockState) {
  136. if (mcMMO.getConfigManager().getExperienceMapManager().hasMiningXp(blockState.getType()))
  137. return true;
  138. return isMineable(blockState);
  139. }
  140. /**
  141. * Determine if a given block should be affected by Super Breaker
  142. *
  143. * @param material The {@link Material} of the block to check
  144. * @return true if the block should affected by Super Breaker, false
  145. * otherwise
  146. */
  147. public static Boolean affectedBySuperBreaker(Material material) {
  148. if (mcMMO.getConfigManager().getExperienceMapManager().hasMiningXp(material))
  149. return true;
  150. return isMineable(material);
  151. }
  152. /**
  153. * Whether or not a block is gathered via Pickaxes
  154. *
  155. * @param material target blocks material
  156. * @return
  157. */
  158. public static boolean isMineable(Material material) {
  159. switch (material) {
  160. case COAL_ORE:
  161. case DIAMOND_ORE:
  162. case EMERALD_ORE:
  163. case END_STONE:
  164. case GOLD_ORE:
  165. case IRON_ORE:
  166. case LAPIS_ORE:
  167. case NETHER_QUARTZ_ORE:
  168. case REDSTONE_ORE:
  169. case ANDESITE:
  170. case DIORITE:
  171. case GRANITE:
  172. case STONE:
  173. case PRISMARINE:
  174. case DARK_PRISMARINE:
  175. case SANDSTONE:
  176. case NETHERRACK:
  177. case ICE:
  178. case PACKED_ICE:
  179. return true;
  180. default:
  181. return false;
  182. }
  183. }
  184. public static boolean isMineable(BlockState blockState) {
  185. return isMineable(blockState.getType());
  186. }
  187. /**
  188. * Determine if a given block should be affected by Giga Drill Breaker
  189. *
  190. * @param material The {@link Material} of the block to check
  191. * @return true if the block should affected by Giga Drill Breaker, false
  192. * otherwise
  193. */
  194. public static boolean affectedByGigaDrillBreaker(Material material) {
  195. if (mcMMO.getConfigManager().getExperienceMapManager().hasExcavationXp(material))
  196. return true;
  197. return isDiggable(material);
  198. }
  199. /**
  200. * Determine if a given block should be affected by Giga Drill Breaker
  201. *
  202. * @param blockState The {@link BlockState} of the block to check
  203. * @return true if the block should affected by Giga Drill Breaker, false
  204. * otherwise
  205. */
  206. public static boolean affectedByGigaDrillBreaker(BlockState blockState) {
  207. if (mcMMO.getConfigManager().getExperienceMapManager().hasExcavationXp(blockState.getType()))
  208. return true;
  209. return isDiggable(blockState);
  210. }
  211. /**
  212. * Returns true if a shovel is used for digging this block
  213. *
  214. * @param blockState target blockstate
  215. * @return true if a shovel is typically used for digging this block
  216. */
  217. @Deprecated
  218. public static boolean isDiggable(BlockState blockState) {
  219. return isDiggable(blockState.getType());
  220. }
  221. /**
  222. * Returns true if a shovel is used for digging this block
  223. *
  224. * @param material target blocks material
  225. * @return true if a shovel is typically used for digging this block
  226. */
  227. public static boolean isDiggable(Material material) {
  228. switch (material) {
  229. case CLAY:
  230. case FARMLAND:
  231. case GRASS_BLOCK:
  232. case GRASS_PATH:
  233. case GRAVEL:
  234. case MYCELIUM:
  235. case PODZOL:
  236. case COARSE_DIRT:
  237. case DIRT:
  238. case RED_SAND:
  239. case SAND:
  240. case SOUL_SAND:
  241. case SNOW:
  242. case SNOW_BLOCK:
  243. return true;
  244. default:
  245. return false;
  246. }
  247. }
  248. /**
  249. * Check if a given block is a log
  250. *
  251. * @param blockState The {@link BlockState} of the block to check
  252. * @return true if the block is a log, false otherwise
  253. */
  254. public static boolean isLog(BlockState blockState) {
  255. if (mcMMO.getConfigManager().getExperienceMapManager().hasWoodcuttingXp(blockState.getType()))
  256. return true;
  257. return isLoggingRelated(blockState);
  258. //return mcMMO.getModManager().isCustomLog(blockState);
  259. }
  260. /**
  261. * Check if a given block is a log
  262. *
  263. * @param material The {@link Material} of the block to check
  264. * @return true if the block is a log, false otherwise
  265. */
  266. public static boolean isLog(Material material) {
  267. if (mcMMO.getConfigManager().getExperienceMapManager().hasWoodcuttingXp(material))
  268. return true;
  269. return isLoggingRelated(material);
  270. //return mcMMO.getModManager().isCustomLog(blockState);
  271. }
  272. /**
  273. * Determines if this particular block is typically gathered using an Axe
  274. *
  275. * @param material target material
  276. * @return true if the block is gathered via axe
  277. */
  278. public static boolean isLoggingRelated(Material material) {
  279. switch (material) {
  280. case ACACIA_LOG:
  281. case BIRCH_LOG:
  282. case DARK_OAK_LOG:
  283. case JUNGLE_LOG:
  284. case OAK_LOG:
  285. case SPRUCE_LOG:
  286. case STRIPPED_ACACIA_LOG:
  287. case STRIPPED_ACACIA_WOOD:
  288. case STRIPPED_BIRCH_LOG:
  289. case STRIPPED_BIRCH_WOOD:
  290. case STRIPPED_DARK_OAK_LOG:
  291. case STRIPPED_DARK_OAK_WOOD:
  292. case STRIPPED_JUNGLE_LOG:
  293. case STRIPPED_JUNGLE_WOOD:
  294. case STRIPPED_OAK_LOG:
  295. case STRIPPED_OAK_WOOD:
  296. case STRIPPED_SPRUCE_LOG:
  297. case STRIPPED_SPRUCE_WOOD:
  298. case ACACIA_WOOD:
  299. case BIRCH_WOOD:
  300. case DARK_OAK_WOOD:
  301. case JUNGLE_WOOD:
  302. case OAK_WOOD:
  303. case SPRUCE_WOOD:
  304. return true;
  305. default:
  306. return false;
  307. }
  308. }
  309. /**
  310. * Determines if this particular block is typically gathered using an Axe
  311. *
  312. * @param blockState target blockstate
  313. * @return true if the block is gathered via axe
  314. */
  315. public static boolean isLoggingRelated(BlockState blockState) {
  316. return isLoggingRelated(blockState.getType());
  317. }
  318. /**
  319. * Check if a given block is a leaf
  320. *
  321. * @param blockState The {@link BlockState} of the block to check
  322. * @return true if the block is a leaf, false otherwise
  323. */
  324. public static boolean isLeaves(BlockState blockState) {
  325. return mcMMO.getMaterialMapStore().isLeavesWhiteListed(blockState.getType());
  326. }
  327. /**
  328. * Determine if a given block should be affected by Flux Mining
  329. *
  330. * @param blockState The {@link BlockState} of the block to check
  331. * @return true if the block should affected by Flux Mining, false otherwise
  332. */
  333. public static boolean affectedByFluxMining(BlockState blockState) {
  334. switch (blockState.getType()) {
  335. case IRON_ORE:
  336. case GOLD_ORE:
  337. return true;
  338. default:
  339. return false;
  340. //return mcMMO.getModManager().isCustomLeaf(blockState);
  341. }
  342. }
  343. /**
  344. * Determine if a given block can activate Herbalism abilities
  345. *
  346. * @param blockState The {@link BlockState} of the block to check
  347. * @return true if the block can be activate Herbalism abilities, false
  348. * otherwise
  349. */
  350. public static boolean canActivateHerbalism(BlockState blockState) {
  351. return mcMMO.getMaterialMapStore().isHerbalismAbilityWhiteListed(blockState.getType());
  352. }
  353. /**
  354. * Determine if a given block should be affected by Block Cracker
  355. *
  356. * @param blockState The {@link BlockState} of the block to check
  357. * @return true if the block should affected by Block Cracker, false
  358. * otherwise
  359. */
  360. public static boolean affectedByBlockCracker(BlockState blockState) {
  361. return mcMMO.getMaterialMapStore().isBlockCrackerWhiteListed(blockState.getType());
  362. }
  363. /**
  364. * Determine if a given block can be made into Mycelium
  365. *
  366. * @param blockState The {@link BlockState} of the block to check
  367. * @return true if the block can be made into Mycelium, false otherwise
  368. */
  369. public static boolean canMakeShroomy(BlockState blockState) {
  370. return mcMMO.getMaterialMapStore().isShroomyWhiteListed(blockState.getType());
  371. }
  372. /**
  373. * Determine if a given block is an mcMMO anvil
  374. *
  375. * @param blockState The {@link BlockState} of the block to check
  376. * @return true if the block is an mcMMO anvil, false otherwise
  377. */
  378. public static boolean isMcMMOAnvil(BlockState blockState) {
  379. Material type = blockState.getType();
  380. return type == Repair.getInstance().getAnvilMaterial() || type == Salvage.anvilMaterial;
  381. }
  382. public static boolean isPistonPiece(BlockState blockState) {
  383. Material type = blockState.getType();
  384. return type == Material.MOVING_PISTON || type == Material.AIR;
  385. }
  386. /**
  387. * Get a HashSet containing every transparent block
  388. *
  389. * @return HashSet with the IDs of every transparent block
  390. */
  391. public static HashSet<Material> getTransparentBlocks() {
  392. HashSet<Material> transparentBlocks = new HashSet<>();
  393. for (Material material : Material.values()) {
  394. if (material.isTransparent()) {
  395. transparentBlocks.add(material);
  396. }
  397. }
  398. return transparentBlocks;
  399. }
  400. public static boolean isFullyGrown(BlockState blockState) {
  401. BlockData data = blockState.getBlockData();
  402. if (data.getMaterial() == Material.CACTUS || data.getMaterial() == Material.SUGAR_CANE)
  403. return true;
  404. if (data instanceof Ageable) {
  405. Ageable ageable = (Ageable) data;
  406. return ageable.getAge() == ageable.getMaximumAge();
  407. }
  408. return true;
  409. }
  410. }