BlockUtils.java 16 KB

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