BlockUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package com.gmail.nossr50.util;
  2. import com.gmail.nossr50.config.experience.ExperienceConfig;
  3. import com.gmail.nossr50.datatypes.meta.BonusDropMeta;
  4. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  5. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  6. import com.gmail.nossr50.mcMMO;
  7. import com.gmail.nossr50.skills.repair.Repair;
  8. import com.gmail.nossr50.skills.salvage.Salvage;
  9. import com.gmail.nossr50.util.compat.layers.world.WorldCompatibilityLayer;
  10. import com.gmail.nossr50.util.random.RandomChanceSkill;
  11. import com.gmail.nossr50.util.random.RandomChanceUtil;
  12. import org.bukkit.Material;
  13. import org.bukkit.World;
  14. import org.bukkit.block.Block;
  15. import org.bukkit.block.BlockState;
  16. import org.bukkit.block.data.Ageable;
  17. import org.bukkit.block.data.BlockData;
  18. import org.bukkit.entity.Player;
  19. import org.jetbrains.annotations.NotNull;
  20. import java.util.HashSet;
  21. public final class BlockUtils {
  22. private BlockUtils() {
  23. }
  24. /**
  25. * Mark a block for giving bonus drops, double drops are used if triple is false
  26. *
  27. * @param blockState target blockstate
  28. * @param triple marks the block to give triple drops
  29. */
  30. public static void markDropsAsBonus(BlockState blockState, boolean triple) {
  31. if (triple)
  32. blockState.setMetadata(mcMMO.BONUS_DROPS_METAKEY, new BonusDropMeta(2, mcMMO.p));
  33. else
  34. blockState.setMetadata(mcMMO.BONUS_DROPS_METAKEY, new BonusDropMeta(1, mcMMO.p));
  35. }
  36. /**
  37. * Marks a block to drop extra copies of items
  38. * @param blockState target blockstate
  39. * @param amount amount of extra items to drop
  40. */
  41. public static void markDropsAsBonus(BlockState blockState, int amount) {
  42. blockState.setMetadata(mcMMO.BONUS_DROPS_METAKEY, new BonusDropMeta(amount, mcMMO.p));
  43. }
  44. /**
  45. * Checks if a player successfully passed the double drop check
  46. *
  47. * @param blockState the blockstate
  48. * @return true if the player succeeded in the check
  49. */
  50. public static boolean checkDoubleDrops(Player player, BlockState blockState, PrimarySkillType skillType, SubSkillType subSkillType) {
  51. if (mcMMO.p.getGeneralConfig().getDoubleDropsEnabled(skillType, blockState.getType()) && Permissions.isSubSkillEnabled(player, subSkillType)) {
  52. return RandomChanceUtil.checkRandomChanceExecutionSuccess(new RandomChanceSkill(player, subSkillType, true));
  53. }
  54. return false;
  55. }
  56. /**
  57. * Checks to see if a given block awards XP.
  58. *
  59. * @param blockState The {@link BlockState} of the block to check
  60. * @return true if the block awards XP, false otherwise
  61. */
  62. public static boolean shouldBeWatched(BlockState blockState) {
  63. return affectedByGigaDrillBreaker(blockState) || affectedByGreenTerra(blockState) || affectedBySuperBreaker(blockState) || hasWoodcuttingXP(blockState)
  64. || mcMMO.p.getGeneralConfig().getDoubleDropsEnabled(PrimarySkillType.MINING, blockState.getType())
  65. || mcMMO.p.getGeneralConfig().getDoubleDropsEnabled(PrimarySkillType.EXCAVATION, blockState.getType())
  66. || mcMMO.p.getGeneralConfig().getDoubleDropsEnabled(PrimarySkillType.WOODCUTTING, blockState.getType())
  67. || mcMMO.p.getGeneralConfig().getDoubleDropsEnabled(PrimarySkillType.SMELTING, blockState.getType());
  68. }
  69. /**
  70. * Check if a given block should allow for the activation of abilities
  71. *
  72. * @param blockState The {@link BlockState} of the block to check
  73. * @return true if the block should allow ability activation, false
  74. * otherwise
  75. */
  76. public static boolean canActivateAbilities(BlockState blockState) {
  77. return !mcMMO.getMaterialMapStore().isAbilityActivationBlackListed(blockState.getType());
  78. }
  79. /**
  80. * Check if a given block should allow for the activation of tools
  81. * Activating a tool is step 1 of a 2 step process for super ability activation
  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 canActivateTools(BlockState blockState) {
  88. return !mcMMO.getMaterialMapStore().isToolActivationBlackListed(blockState.getType())
  89. && blockState.getType() != Repair.anvilMaterial
  90. && blockState.getType() != Salvage.anvilMaterial;
  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. if (ExperienceConfig.getInstance().doesBlockGiveSkillXP(PrimarySkillType.HERBALISM, blockState.getBlockData())) {
  118. return true;
  119. }
  120. return mcMMO.getModManager().isCustomHerbalismBlock(blockState);
  121. }
  122. /**
  123. * Determine if a given block should be affected by Super Breaker
  124. *
  125. * @param blockState The {@link BlockState} of the block to check
  126. * @return true if the block should affected by Super Breaker, false
  127. * otherwise
  128. */
  129. public static boolean affectedBySuperBreaker(BlockState blockState) {
  130. if(mcMMO.getMaterialMapStore().isIntendedToolPickaxe(blockState.getType()))
  131. return true;
  132. if (ExperienceConfig.getInstance().doesBlockGiveSkillXP(PrimarySkillType.MINING, blockState.getBlockData()))
  133. return true;
  134. return mcMMO.getModManager().isCustomMiningBlock(blockState);
  135. }
  136. /**
  137. * Determine if a given block should be affected by Giga Drill Breaker
  138. *
  139. * @param blockState The {@link BlockState} of the block to check
  140. * @return true if the block should affected by Giga Drill Breaker, false
  141. * otherwise
  142. */
  143. public static boolean affectedByGigaDrillBreaker(@NotNull BlockState blockState) {
  144. if (ExperienceConfig.getInstance().doesBlockGiveSkillXP(PrimarySkillType.EXCAVATION, blockState.getBlockData()))
  145. return true;
  146. return mcMMO.getModManager().isCustomExcavationBlock(blockState);
  147. }
  148. /**
  149. * Check if a given block is a log
  150. *
  151. * @param blockState The {@link BlockState} of the block to check
  152. * @return true if the block is a log, false otherwise
  153. */
  154. public static boolean hasWoodcuttingXP(@NotNull BlockState blockState) {
  155. return ExperienceConfig.getInstance().doesBlockGiveSkillXP(PrimarySkillType.WOODCUTTING, blockState.getBlockData());
  156. }
  157. /**
  158. * Check if a given block is a leaf
  159. *
  160. * @param blockState The {@link BlockState} of the block to check
  161. * @return true if the block is a leaf, false otherwise
  162. */
  163. public static boolean isNonWoodPartOfTree(@NotNull BlockState blockState) {
  164. return mcMMO.getMaterialMapStore().isTreeFellerDestructible(blockState.getType());
  165. }
  166. public static boolean isNonWoodPartOfTree(@NotNull Material material) {
  167. return mcMMO.getMaterialMapStore().isTreeFellerDestructible(material);
  168. }
  169. /**
  170. * Determine if a given block should be affected by Flux Mining
  171. *
  172. * @param blockState The {@link BlockState} of the block to check
  173. * @return true if the block should affected by Flux Mining, false otherwise
  174. */
  175. public static boolean affectedByFluxMining(BlockState blockState) {
  176. switch (blockState.getType()) {
  177. case IRON_ORE:
  178. case GOLD_ORE:
  179. return true;
  180. default:
  181. return false;
  182. }
  183. }
  184. /**
  185. * Determine if a given block can activate Herbalism abilities
  186. *
  187. * @param blockState The {@link BlockState} of the block to check
  188. * @return true if the block can be activate Herbalism abilities, false
  189. * otherwise
  190. */
  191. public static boolean canActivateHerbalism(BlockState blockState) {
  192. return mcMMO.getMaterialMapStore().isHerbalismAbilityWhiteListed(blockState.getType());
  193. }
  194. /**
  195. * Determine if a given block should be affected by Block Cracker
  196. *
  197. * @param blockState The {@link BlockState} of the block to check
  198. * @return true if the block should affected by Block Cracker, false
  199. * otherwise
  200. */
  201. public static boolean affectedByBlockCracker(BlockState blockState) {
  202. return mcMMO.getMaterialMapStore().isBlockCrackerWhiteListed(blockState.getType());
  203. }
  204. /**
  205. * Determine if a given block can be made into Mycelium
  206. *
  207. * @param blockState The {@link BlockState} of the block to check
  208. * @return true if the block can be made into Mycelium, false otherwise
  209. */
  210. public static boolean canMakeShroomy(BlockState blockState) {
  211. return mcMMO.getMaterialMapStore().isShroomyWhiteListed(blockState.getType());
  212. }
  213. /**
  214. * Determine if a given block is an mcMMO anvil
  215. *
  216. * @param blockState The {@link BlockState} of the block to check
  217. * @return true if the block is an mcMMO anvil, false otherwise
  218. */
  219. public static boolean isMcMMOAnvil(BlockState blockState) {
  220. Material type = blockState.getType();
  221. return type == Repair.anvilMaterial || type == Salvage.anvilMaterial;
  222. }
  223. public static boolean isPistonPiece(BlockState blockState) {
  224. Material type = blockState.getType();
  225. return type == Material.MOVING_PISTON || type == Material.AIR;
  226. }
  227. /**
  228. * Get a HashSet containing every transparent block
  229. *
  230. * @return HashSet with the IDs of every transparent block
  231. */
  232. public static HashSet<Material> getTransparentBlocks() {
  233. HashSet<Material> transparentBlocks = new HashSet<>();
  234. for (Material material : Material.values()) {
  235. if (material.isTransparent()) {
  236. transparentBlocks.add(material);
  237. }
  238. }
  239. return transparentBlocks;
  240. }
  241. public static boolean isFullyGrown(BlockState blockState) {
  242. BlockData data = blockState.getBlockData();
  243. if (data.getMaterial() == Material.CACTUS || data.getMaterial() == Material.SUGAR_CANE) {
  244. return true;
  245. }
  246. if (data instanceof Ageable) {
  247. Ageable ageable = (Ageable) data;
  248. return ageable.getAge() == ageable.getMaximumAge();
  249. }
  250. return true;
  251. }
  252. public static boolean isPartOfTree(Block rayCast) {
  253. return hasWoodcuttingXP(rayCast.getState()) || isNonWoodPartOfTree(rayCast.getType());
  254. }
  255. public static boolean isWithinWorldBounds(@NotNull WorldCompatibilityLayer worldCompatibilityLayer, @NotNull Block block) {
  256. World world = block.getWorld();
  257. //World min height = inclusive | World max height = exclusive
  258. return block.getY() >= worldCompatibilityLayer.getMinWorldHeight(world) && block.getY() < worldCompatibilityLayer.getMaxWorldHeight(world);
  259. }
  260. }