MiningManager.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package com.gmail.nossr50.skills.mining;
  2. import com.gmail.nossr50.api.ItemSpawnReason;
  3. import com.gmail.nossr50.config.experience.ExperienceConfig;
  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.apache.commons.lang.math.RandomUtils;
  19. import org.bukkit.Material;
  20. import org.bukkit.block.Block;
  21. import org.bukkit.block.BlockState;
  22. import org.bukkit.block.Container;
  23. import org.bukkit.enchantments.Enchantment;
  24. import org.bukkit.entity.Player;
  25. import org.bukkit.entity.TNTPrimed;
  26. import org.bukkit.event.entity.EntityExplodeEvent;
  27. import org.bukkit.inventory.ItemStack;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. public class MiningManager extends SkillManager {
  31. public MiningManager(McMMOPlayer mcMMOPlayer) {
  32. super(mcMMOPlayer, PrimarySkillType.MINING);
  33. }
  34. public boolean canUseDemolitionsExpertise() {
  35. if(!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.MINING_DEMOLITIONS_EXPERTISE))
  36. return false;
  37. return getSkillLevel() >= BlastMining.getDemolitionExpertUnlockLevel() && Permissions.demolitionsExpertise(getPlayer());
  38. }
  39. public boolean canDetonate() {
  40. Player player = getPlayer();
  41. return canUseBlastMining() && player.isSneaking()
  42. && (ItemUtils.isPickaxe(getPlayer().getInventory().getItemInMainHand()) || player.getInventory().getItemInMainHand().getType() == mcMMO.p.getGeneralConfig().getDetonatorItem())
  43. && Permissions.remoteDetonation(player);
  44. }
  45. public boolean canUseBlastMining() {
  46. //Not checking permissions?
  47. return RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.MINING_BLAST_MINING);
  48. }
  49. public boolean canUseBiggerBombs() {
  50. if(!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.MINING_BIGGER_BOMBS))
  51. return false;
  52. return getSkillLevel() >= BlastMining.getBiggerBombsUnlockLevel() && Permissions.biggerBombs(getPlayer());
  53. }
  54. public boolean canDoubleDrop() {
  55. return RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.MINING_DOUBLE_DROPS) && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.MINING_DOUBLE_DROPS);
  56. }
  57. /**
  58. * Process double drops & XP gain for Mining.
  59. *
  60. * @param blockState The {@link BlockState} to check ability activation for
  61. */
  62. public void miningBlockCheck(BlockState blockState) {
  63. Player player = getPlayer();
  64. applyXpGain(Mining.getBlockXp(blockState), XPGainReason.PVE);
  65. if (!Permissions.isSubSkillEnabled(player, SubSkillType.MINING_DOUBLE_DROPS)) {
  66. return;
  67. }
  68. if (mmoPlayer.getAbilityMode(mcMMO.p.getSkillTools().getSuperAbility(skill))) {
  69. SkillUtils.handleDurabilityChange(getPlayer().getInventory().getItemInMainHand(), mcMMO.p.getGeneralConfig().getAbilityToolDamage());
  70. }
  71. if(!mcMMO.p.getGeneralConfig().getDoubleDropsEnabled(PrimarySkillType.MINING, blockState.getType()) || !canDoubleDrop())
  72. return;
  73. boolean silkTouch = player.getInventory().getItemInMainHand().containsEnchantment(Enchantment.SILK_TOUCH);
  74. if(silkTouch && !mcMMO.p.getAdvancedConfig().getDoubleDropSilkTouchEnabled())
  75. return;
  76. //TODO: Make this readable
  77. if (RandomChanceUtil.checkRandomChanceExecutionSuccess(getPlayer(), SubSkillType.MINING_DOUBLE_DROPS, true)) {
  78. boolean useTriple = mmoPlayer.getAbilityMode(mcMMO.p.getSkillTools().getSuperAbility(skill)) && mcMMO.p.getAdvancedConfig().getAllowMiningTripleDrops();
  79. BlockUtils.markDropsAsBonus(blockState, useTriple);
  80. }
  81. }
  82. /**
  83. * Detonate TNT for Blast Mining
  84. */
  85. public void remoteDetonation() {
  86. Player player = getPlayer();
  87. Block targetBlock = player.getTargetBlock(BlockUtils.getTransparentBlocks(), BlastMining.MAXIMUM_REMOTE_DETONATION_DISTANCE);
  88. //Blast mining cooldown check needs to be first so the player can be messaged
  89. if (!blastMiningCooldownOver() || targetBlock.getType() != Material.TNT || !EventUtils.simulateBlockBreak(targetBlock, player, true)) {
  90. return;
  91. }
  92. TNTPrimed tnt = player.getWorld().spawn(targetBlock.getLocation(), TNTPrimed.class);
  93. //SkillUtils.sendSkillMessage(player, SuperAbilityType.BLAST_MINING.getAbilityPlayer(player));
  94. NotificationManager.sendPlayerInformation(player, NotificationType.SUPER_ABILITY, "Mining.Blast.Boom");
  95. //player.sendMessage(LocaleLoader.getString("Mining.Blast.Boom"));
  96. tnt.setMetadata(mcMMO.tntMetadataKey, mmoPlayer.getPlayerMetadata());
  97. tnt.setFuseTicks(0);
  98. targetBlock.setType(Material.AIR);
  99. mmoPlayer.setAbilityDATS(SuperAbilityType.BLAST_MINING, System.currentTimeMillis());
  100. mmoPlayer.setAbilityInformed(SuperAbilityType.BLAST_MINING, false);
  101. new AbilityCooldownTask(mmoPlayer, SuperAbilityType.BLAST_MINING).runTaskLater(mcMMO.p, SuperAbilityType.BLAST_MINING.getCooldown() * Misc.TICK_CONVERSION_FACTOR);
  102. }
  103. /**
  104. * Handler for explosion drops and XP gain.
  105. *
  106. * @param yield The % of blocks to drop
  107. * @param event The {@link EntityExplodeEvent}
  108. */
  109. //TODO: Rewrite this garbage
  110. //TODO: Rewrite this garbage
  111. //TODO: Rewrite this garbage
  112. //TODO: Rewrite this garbage
  113. //TODO: Rewrite this garbage
  114. //TODO: Rewrite this garbage
  115. //TODO: Rewrite this garbage
  116. //TODO: Rewrite this garbage
  117. //TODO: Rewrite this garbage
  118. //TODO: Rewrite this garbage
  119. //TODO: Rewrite this garbage
  120. //TODO: Rewrite this garbage
  121. //TODO: Rewrite this garbage
  122. //TODO: Rewrite this garbage
  123. //TODO: Rewrite this garbage
  124. //TODO: Rewrite this garbage
  125. //TODO: Rewrite this garbage
  126. //TODO: Rewrite this garbage
  127. //TODO: Rewrite this garbage
  128. //TODO: Rewrite this garbage
  129. //TODO: Rewrite this garbage
  130. //TODO: Rewrite this garbage
  131. //TODO: Rewrite this garbage
  132. //TODO: Rewrite this garbage
  133. public void blastMiningDropProcessing(float yield, EntityExplodeEvent event) {
  134. //Strip out only stuff that gives mining XP
  135. List<BlockState> ores = new ArrayList<>();
  136. List<BlockState> notOres = new ArrayList<>();
  137. for (Block targetBlock : event.blockList()) {
  138. BlockState blockState = targetBlock.getState();
  139. //Containers usually have 0 XP unless someone edited their config in a very strange way
  140. if (ExperienceConfig.getInstance().getXp(PrimarySkillType.MINING, targetBlock) != 0
  141. && !(targetBlock instanceof Container)
  142. && !mcMMO.getPlaceStore().isTrue(targetBlock)) {
  143. if(BlockUtils.isOre(blockState)) {
  144. ores.add(blockState);
  145. } else {
  146. notOres.add(blockState);
  147. }
  148. }
  149. }
  150. int xp = 0;
  151. float oreBonus = (float) (getOreBonus() / 100);
  152. //TODO: Pretty sure something is fucked with debrisReduction stuff
  153. float debrisReduction = (float) (getDebrisReduction() / 100);
  154. int dropMultiplier = getDropMultiplier();
  155. float debrisYield = yield - debrisReduction;
  156. //Drop "debris" based on skill modifiers
  157. for(BlockState blockState : notOres) {
  158. if(RandomUtils.nextFloat() < debrisYield) {
  159. Misc.spawnItem(Misc.getBlockCenter(blockState), new ItemStack(blockState.getType()), ItemSpawnReason.BLAST_MINING_DEBRIS_NON_ORES); // Initial block that would have been dropped
  160. }
  161. }
  162. for (BlockState blockState : ores) {
  163. if (RandomUtils.nextFloat() < (yield + oreBonus)) {
  164. xp += Mining.getBlockXp(blockState);
  165. Misc.spawnItem(Misc.getBlockCenter(blockState), new ItemStack(blockState.getType()), ItemSpawnReason.BLAST_MINING_ORES); // Initial block that would have been dropped
  166. if (!mcMMO.getPlaceStore().isTrue(blockState)) {
  167. for (int i = 1; i < dropMultiplier; i++) {
  168. // Bukkit.broadcastMessage("Bonus Drop on Ore: "+blockState.getType().toString());
  169. Misc.spawnItem(Misc.getBlockCenter(blockState), new ItemStack(blockState.getType()), ItemSpawnReason.BLAST_MINING_ORES_BONUS_DROP); // Initial block that would have been dropped
  170. }
  171. }
  172. }
  173. }
  174. //Replace the event blocklist with the newYield list
  175. event.setYield(0F);
  176. // event.blockList().clear();
  177. // event.blockList().addAll(notOres);
  178. applyXpGain(xp, XPGainReason.PVE);
  179. }
  180. /**
  181. * Increases the blast radius of the explosion.
  182. *
  183. * @param radius to modify
  184. * @return modified radius
  185. */
  186. public float biggerBombs(float radius) {
  187. return (float) (radius + getBlastRadiusModifier());
  188. }
  189. public double processDemolitionsExpertise(double damage) {
  190. return damage * ((100.0D - getBlastDamageModifier()) / 100.0D);
  191. }
  192. /**
  193. * Gets the Blast Mining tier
  194. *
  195. * @return the Blast Mining tier
  196. */
  197. public int getBlastMiningTier() {
  198. return RankUtils.getRank(getPlayer(), SubSkillType.MINING_BLAST_MINING);
  199. }
  200. /**
  201. * Gets the Blast Mining tier
  202. *
  203. * @return the Blast Mining tier
  204. */
  205. public double getOreBonus() {
  206. return getOreBonus(getBlastMiningTier());
  207. }
  208. public static double getOreBonus(int rank) {
  209. return mcMMO.p.getAdvancedConfig().getOreBonus(rank);
  210. }
  211. public static double getDebrisReduction(int rank) {
  212. return mcMMO.p.getAdvancedConfig().getDebrisReduction(rank);
  213. }
  214. /**
  215. * Gets the Blast Mining tier
  216. *
  217. * @return the Blast Mining tier
  218. */
  219. public double getDebrisReduction() {
  220. return getDebrisReduction(getBlastMiningTier());
  221. }
  222. public static int getDropMultiplier(int rank) {
  223. return mcMMO.p.getAdvancedConfig().getDropMultiplier(rank);
  224. }
  225. /**
  226. * Gets the Blast Mining tier
  227. *
  228. * @return the Blast Mining tier
  229. */
  230. public int getDropMultiplier() {
  231. switch(getBlastMiningTier()) {
  232. case 8:
  233. case 7:
  234. return 3;
  235. case 6:
  236. case 5:
  237. case 4:
  238. case 3:
  239. return 2;
  240. case 2:
  241. case 1:
  242. return 1;
  243. default:
  244. return 0;
  245. }
  246. }
  247. /**
  248. * Gets the Blast Mining tier
  249. *
  250. * @return the Blast Mining tier
  251. */
  252. public double getBlastRadiusModifier() {
  253. return BlastMining.getBlastRadiusModifier(getBlastMiningTier());
  254. }
  255. /**
  256. * Gets the Blast Mining tier
  257. *
  258. * @return the Blast Mining tier
  259. */
  260. public double getBlastDamageModifier() {
  261. return BlastMining.getBlastDamageDecrease(getBlastMiningTier());
  262. }
  263. private boolean blastMiningCooldownOver() {
  264. int timeRemaining = mmoPlayer.calculateTimeRemaining(SuperAbilityType.BLAST_MINING);
  265. if (timeRemaining > 0) {
  266. //getPlayer().sendMessage(LocaleLoader.getString("Skills.TooTired", timeRemaining));
  267. NotificationManager.sendPlayerInformation(getPlayer(), NotificationType.ABILITY_COOLDOWN, "Skills.TooTired", String.valueOf(timeRemaining));
  268. return false;
  269. }
  270. return true;
  271. }
  272. }