MiningManager.java 9.4 KB

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