MiningManager.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package com.gmail.nossr50.skills.mining;
  2. import com.gmail.nossr50.config.AdvancedConfig;
  3. import com.gmail.nossr50.config.Config;
  4. import com.gmail.nossr50.datatypes.experience.XPGainReason;
  5. import com.gmail.nossr50.datatypes.interactions.NotificationType;
  6. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  7. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  8. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  9. import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
  10. import com.gmail.nossr50.mcMMO;
  11. import com.gmail.nossr50.runnables.skills.AbilityCooldownTask;
  12. import com.gmail.nossr50.skills.SkillManager;
  13. import com.gmail.nossr50.util.*;
  14. import com.gmail.nossr50.util.player.NotificationManager;
  15. import com.gmail.nossr50.util.random.RandomChanceUtil;
  16. import com.gmail.nossr50.util.skills.RankUtils;
  17. import com.gmail.nossr50.util.skills.SkillUtils;
  18. import org.bukkit.Material;
  19. import org.bukkit.block.Block;
  20. import org.bukkit.block.BlockState;
  21. import org.bukkit.enchantments.Enchantment;
  22. import org.bukkit.entity.Player;
  23. import org.bukkit.entity.TNTPrimed;
  24. import org.bukkit.inventory.ItemStack;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. public class MiningManager extends SkillManager {
  28. public MiningManager(McMMOPlayer mcMMOPlayer) {
  29. super(mcMMOPlayer, PrimarySkillType.MINING);
  30. }
  31. public boolean canUseDemolitionsExpertise() {
  32. if(!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.MINING_DEMOLITIONS_EXPERTISE))
  33. return false;
  34. return getSkillLevel() >= BlastMining.getDemolitionExpertUnlockLevel() && Permissions.demolitionsExpertise(getPlayer());
  35. }
  36. public boolean canDetonate() {
  37. Player player = getPlayer();
  38. return canUseBlastMining() && player.isSneaking()
  39. && ItemUtils.isPickaxe(getPlayer().getInventory().getItemInMainHand())
  40. && Permissions.remoteDetonation(player);
  41. }
  42. public boolean canUseBlastMining() {
  43. //Not checking permissions?
  44. return RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.MINING_BLAST_MINING);
  45. }
  46. public boolean canUseBiggerBombs() {
  47. if(!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.MINING_BIGGER_BOMBS))
  48. return false;
  49. return getSkillLevel() >= BlastMining.getBiggerBombsUnlockLevel() && Permissions.biggerBombs(getPlayer());
  50. }
  51. /**
  52. * Process double drops & XP gain for Mining.
  53. *
  54. * @param blockState The {@link BlockState} to check ability activation for
  55. */
  56. public void miningBlockCheck(BlockState blockState) {
  57. Player player = getPlayer();
  58. applyXpGain(Mining.getBlockXp(blockState), XPGainReason.PVE);
  59. if (!Permissions.isSubSkillEnabled(player, SubSkillType.MINING_DOUBLE_DROPS)) {
  60. return;
  61. }
  62. Material material = blockState.getType();
  63. if (mcMMOPlayer.getAbilityMode(skill.getAbility())) {
  64. SkillUtils.handleDurabilityChange(getPlayer().getInventory().getItemInMainHand(), Config.getInstance().getAbilityToolDamage());
  65. }
  66. if ((mcMMO.getModManager().isCustomMiningBlock(blockState) && !mcMMO.getModManager().getBlock(blockState).isDoubleDropEnabled()) || !Config.getInstance().getDoubleDropsEnabled(skill, material)) {
  67. return;
  68. }
  69. boolean silkTouch = player.getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH);
  70. if(silkTouch && !AdvancedConfig.getInstance().getDoubleDropSilkTouchEnabled())
  71. return;
  72. //TODO: Make this readable
  73. if (RandomChanceUtil.checkRandomChanceExecutionSuccess(getPlayer(), SubSkillType.MINING_DOUBLE_DROPS, true)) {
  74. BlockUtils.markDropsAsBonus(blockState, mcMMOPlayer.getAbilityMode(skill.getAbility()));
  75. }
  76. }
  77. /**
  78. * Detonate TNT for Blast Mining
  79. */
  80. public void remoteDetonation() {
  81. Player player = getPlayer();
  82. Block targetBlock = player.getTargetBlock(BlockUtils.getTransparentBlocks(), BlastMining.MAXIMUM_REMOTE_DETONATION_DISTANCE);
  83. //Blast mining cooldown check needs to be first so the player can be messaged
  84. if (!blastMiningCooldownOver() || targetBlock.getType() != Material.TNT || !EventUtils.simulateBlockBreak(targetBlock, player, true)) {
  85. return;
  86. }
  87. TNTPrimed tnt = player.getWorld().spawn(targetBlock.getLocation(), TNTPrimed.class);
  88. //SkillUtils.sendSkillMessage(player, SuperAbilityType.BLAST_MINING.getAbilityPlayer(player));
  89. NotificationManager.sendPlayerInformation(player, NotificationType.SUPER_ABILITY, "Mining.Blast.Boom");
  90. //player.sendMessage(LocaleLoader.getString("Mining.Blast.Boom"));
  91. tnt.setMetadata(mcMMO.tntMetadataKey, mcMMOPlayer.getPlayerMetadata());
  92. tnt.setFuseTicks(0);
  93. targetBlock.setType(Material.AIR);
  94. mcMMOPlayer.setAbilityDATS(SuperAbilityType.BLAST_MINING, System.currentTimeMillis());
  95. mcMMOPlayer.setAbilityInformed(SuperAbilityType.BLAST_MINING, false);
  96. new AbilityCooldownTask(mcMMOPlayer, SuperAbilityType.BLAST_MINING).runTaskLaterAsynchronously(mcMMO.p, SuperAbilityType.BLAST_MINING.getCooldown() * Misc.TICK_CONVERSION_FACTOR);
  97. }
  98. /**
  99. * Handler for explosion drops and XP gain.
  100. *
  101. * @param yield The % of blocks to drop
  102. * @param blockList The list of blocks to drop
  103. */
  104. public void blastMiningDropProcessing(float yield, List<Block> blockList) {
  105. List<BlockState> ores = new ArrayList<BlockState>();
  106. List<BlockState> debris = new ArrayList<BlockState>();
  107. int xp = 0;
  108. float oreBonus = (float) (getOreBonus() / 100);
  109. float debrisReduction = (float) (getDebrisReduction() / 100);
  110. int dropMultiplier = getDropMultiplier();
  111. float debrisYield = yield - debrisReduction;
  112. for (Block block : blockList) {
  113. BlockState blockState = block.getState();
  114. if (BlockUtils.isOre(blockState)) {
  115. ores.add(blockState);
  116. }
  117. else {
  118. debris.add(blockState);
  119. }
  120. }
  121. for (BlockState blockState : ores) {
  122. if (Misc.getRandom().nextFloat() < (yield + oreBonus)) {
  123. if (!mcMMO.getPlaceStore().isTrue(blockState)) {
  124. xp += Mining.getBlockXp(blockState);
  125. }
  126. Misc.dropItem(Misc.getBlockCenter(blockState), new ItemStack(blockState.getType())); // Initial block that would have been dropped
  127. if (!mcMMO.getPlaceStore().isTrue(blockState)) {
  128. for (int i = 1; i < dropMultiplier; i++) {
  129. Mining.handleSilkTouchDrops(blockState); // Bonus drops - should drop the block & not the items
  130. }
  131. }
  132. }
  133. }
  134. if (debrisYield > 0) {
  135. for (BlockState blockState : debris) {
  136. if (Misc.getRandom().nextFloat() < debrisYield) {
  137. Misc.dropItems(Misc.getBlockCenter(blockState), blockState.getBlock().getDrops());
  138. }
  139. }
  140. }
  141. applyXpGain(xp, XPGainReason.PVE);
  142. }
  143. /**
  144. * Increases the blast radius of the explosion.
  145. *
  146. * @param radius to modify
  147. * @return modified radius
  148. */
  149. public float biggerBombs(float radius) {
  150. return (float) (radius + getBlastRadiusModifier());
  151. }
  152. public double processDemolitionsExpertise(double damage) {
  153. return damage * ((100.0D - getBlastDamageModifier()) / 100.0D);
  154. }
  155. /**
  156. * Gets the Blast Mining tier
  157. *
  158. * @return the Blast Mining tier
  159. */
  160. public int getBlastMiningTier() {
  161. return RankUtils.getRank(getPlayer(), SubSkillType.MINING_BLAST_MINING);
  162. }
  163. /**
  164. * Gets the Blast Mining tier
  165. *
  166. * @return the Blast Mining tier
  167. */
  168. public double getOreBonus() {
  169. return getOreBonus(getBlastMiningTier());
  170. }
  171. public static double getOreBonus(int rank) {
  172. return AdvancedConfig.getInstance().getOreBonus(rank);
  173. }
  174. public static double getDebrisReduction(int rank) {
  175. return AdvancedConfig.getInstance().getDebrisReduction(rank);
  176. }
  177. /**
  178. * Gets the Blast Mining tier
  179. *
  180. * @return the Blast Mining tier
  181. */
  182. public double getDebrisReduction() {
  183. return getDebrisReduction(getBlastMiningTier());
  184. }
  185. public static int getDropMultiplier(int rank) {
  186. return AdvancedConfig.getInstance().getDropMultiplier(rank);
  187. }
  188. /**
  189. * Gets the Blast Mining tier
  190. *
  191. * @return the Blast Mining tier
  192. */
  193. public int getDropMultiplier() {
  194. return getDropMultiplier(getBlastMiningTier());
  195. }
  196. /**
  197. * Gets the Blast Mining tier
  198. *
  199. * @return the Blast Mining tier
  200. */
  201. public double getBlastRadiusModifier() {
  202. return BlastMining.getBlastRadiusModifier(getBlastMiningTier());
  203. }
  204. /**
  205. * Gets the Blast Mining tier
  206. *
  207. * @return the Blast Mining tier
  208. */
  209. public double getBlastDamageModifier() {
  210. return BlastMining.getBlastDamageDecrease(getBlastMiningTier());
  211. }
  212. private boolean blastMiningCooldownOver() {
  213. int timeRemaining = mcMMOPlayer.calculateTimeRemaining(SuperAbilityType.BLAST_MINING);
  214. if (timeRemaining > 0) {
  215. //getPlayer().sendMessage(LocaleLoader.getString("Skills.TooTired", timeRemaining));
  216. NotificationManager.sendPlayerInformation(getPlayer(), NotificationType.ABILITY_COOLDOWN, "Skills.TooTired", String.valueOf("timeRemaining"));
  217. return false;
  218. }
  219. return true;
  220. }
  221. }