Mining.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. package com.gmail.nossr50.skills.gathering;
  2. import java.util.Random;
  3. import org.bukkit.CoalType;
  4. import org.bukkit.Location;
  5. import org.bukkit.Material;
  6. import org.bukkit.block.Block;
  7. import org.bukkit.enchantments.Enchantment;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.inventory.ItemStack;
  10. import org.bukkit.material.MaterialData;
  11. import org.getspout.spoutapi.sound.SoundEffect;
  12. import com.gmail.nossr50.mcMMO;
  13. import com.gmail.nossr50.config.AdvancedConfig;
  14. import com.gmail.nossr50.config.Config;
  15. import com.gmail.nossr50.datatypes.PlayerProfile;
  16. import com.gmail.nossr50.datatypes.SkillType;
  17. import com.gmail.nossr50.datatypes.mods.CustomBlock;
  18. import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent;
  19. import com.gmail.nossr50.spout.SpoutSounds;
  20. import com.gmail.nossr50.util.Misc;
  21. import com.gmail.nossr50.util.ModChecks;
  22. import com.gmail.nossr50.util.Permissions;
  23. import com.gmail.nossr50.util.Skills;
  24. import com.gmail.nossr50.util.Users;
  25. public class Mining {
  26. private static Random random = new Random();
  27. static AdvancedConfig advancedConfig = AdvancedConfig.getInstance();
  28. /**
  29. * Handle double drops when using Silk Touch.
  30. *
  31. * @param block The block to process drops for
  32. */
  33. private static void silkTouchDrops(Block block) {
  34. Location location = block.getLocation();
  35. Material type = block.getType();
  36. ItemStack item = new ItemStack(type);
  37. Config configInstance = Config.getInstance();
  38. switch (type) {
  39. case ENDER_STONE:
  40. case GOLD_ORE:
  41. case IRON_ORE:
  42. case MOSSY_COBBLESTONE:
  43. case NETHERRACK:
  44. case OBSIDIAN:
  45. case SANDSTONE:
  46. miningDrops(block);
  47. break;
  48. case COAL_ORE:
  49. if (configInstance.getCoalDoubleDropsEnabled()) {
  50. Misc.dropItem(location, item);
  51. }
  52. break;
  53. case DIAMOND_ORE:
  54. if (configInstance.getDiamondDoubleDropsEnabled()) {
  55. Misc.dropItem(location, item);
  56. }
  57. break;
  58. case GLOWING_REDSTONE_ORE:
  59. case REDSTONE_ORE:
  60. if (configInstance.getRedstoneDoubleDropsEnabled()) {
  61. Misc.dropItem(location, item);
  62. }
  63. break;
  64. case GLOWSTONE:
  65. if (configInstance.getGlowstoneDoubleDropsEnabled()) {
  66. Misc.dropItem(location, item);
  67. }
  68. break;
  69. case LAPIS_ORE:
  70. if (configInstance.getLapisDoubleDropsEnabled()) {
  71. Misc.dropItem(location, item);
  72. }
  73. break;
  74. case STONE:
  75. if (configInstance.getStoneDoubleDropsEnabled()) {
  76. Misc.dropItem(location, item);
  77. }
  78. break;
  79. case EMERALD_ORE:
  80. if (configInstance.getEmeraldDoubleDropsEnabled()) {
  81. Misc.dropItem(location, item);
  82. }
  83. break;
  84. default:
  85. if (ModChecks.isCustomMiningBlock(block)) {
  86. ItemStack dropItem = (new MaterialData(block.getTypeId(), block.getData())).toItemStack(1);
  87. Misc.dropItem(location, dropItem);
  88. }
  89. break;
  90. }
  91. }
  92. /**
  93. * Drop items from Mining & Blast Mining skills.
  94. *
  95. * @param block The block to process drops for
  96. */
  97. public static void miningDrops(Block block) {
  98. Location location = block.getLocation();
  99. Material type = block.getType();
  100. ItemStack item = new ItemStack(type);
  101. Config configInstance = Config.getInstance();
  102. switch (type) {
  103. case COAL_ORE:
  104. if (configInstance.getCoalDoubleDropsEnabled()) {
  105. item = (new MaterialData(Material.COAL, CoalType.COAL.getData())).toItemStack(1);
  106. Misc.dropItem(location, item);
  107. }
  108. break;
  109. case DIAMOND_ORE:
  110. if (configInstance.getDiamondDoubleDropsEnabled()) {
  111. item = new ItemStack(Material.DIAMOND);
  112. Misc.dropItem(location, item);
  113. }
  114. break;
  115. case ENDER_STONE:
  116. if (configInstance.getEndStoneDoubleDropsEnabled()) {
  117. Misc.dropItem(location, item);
  118. }
  119. break;
  120. case GLOWING_REDSTONE_ORE:
  121. case REDSTONE_ORE:
  122. if (configInstance.getRedstoneDoubleDropsEnabled()) {
  123. item = new ItemStack(Material.REDSTONE);
  124. Misc.dropItems(location, item, 4);
  125. Misc.randomDropItem(location, item, 50);
  126. }
  127. break;
  128. case GLOWSTONE:
  129. if (configInstance.getGlowstoneDoubleDropsEnabled()) {
  130. item = new ItemStack(Material.GLOWSTONE_DUST);
  131. Misc.dropItems(location, item, 2);
  132. Misc.randomDropItems(location, item, 50, 2);
  133. }
  134. break;
  135. case GOLD_ORE:
  136. if (configInstance.getGoldDoubleDropsEnabled()) {
  137. Misc.dropItem(location, item);
  138. }
  139. break;
  140. case IRON_ORE:
  141. if (configInstance.getIronDoubleDropsEnabled()) {
  142. Misc.dropItem(location, item);
  143. }
  144. break;
  145. case LAPIS_ORE:
  146. if (configInstance.getLapisDoubleDropsEnabled()) {
  147. item = (new MaterialData(Material.INK_SACK, (byte) 0x4)).toItemStack(1);
  148. Misc.dropItems(location, item, 4);
  149. Misc.randomDropItems(location, item, 50, 4);
  150. }
  151. break;
  152. case MOSSY_COBBLESTONE:
  153. if (configInstance.getMossyCobblestoneDoubleDropsEnabled()) {
  154. Misc.dropItem(location, item);
  155. }
  156. break;
  157. case NETHERRACK:
  158. if (configInstance.getNetherrackDoubleDropsEnabled()) {
  159. Misc.dropItem(location, item);
  160. }
  161. break;
  162. case OBSIDIAN:
  163. if (configInstance.getObsidianDoubleDropsEnabled()) {
  164. Misc.dropItem(location, item);
  165. }
  166. break;
  167. case SANDSTONE:
  168. if (configInstance.getSandstoneDoubleDropsEnabled()) {
  169. Misc.dropItem(location, item);
  170. }
  171. break;
  172. case STONE:
  173. if (configInstance.getStoneDoubleDropsEnabled()) {
  174. item = new ItemStack(Material.COBBLESTONE);
  175. Misc.dropItem(location, item);
  176. }
  177. break;
  178. case EMERALD_ORE:
  179. if (configInstance.getEmeraldDoubleDropsEnabled()) {
  180. item = new ItemStack(Material.EMERALD);
  181. Misc.dropItem(location, item);
  182. }
  183. break;
  184. default:
  185. if (ModChecks.isCustomMiningBlock(block)) {
  186. CustomBlock customBlock = ModChecks.getCustomBlock(block);
  187. int minimumDropAmount = customBlock.getMinimumDropAmount();
  188. int maximumDropAmount = customBlock.getMaximumDropAmount();
  189. item = ModChecks.getCustomBlock(block).getItemDrop();
  190. if (minimumDropAmount != maximumDropAmount) {
  191. Misc.dropItems(location, item, minimumDropAmount);
  192. Misc.randomDropItems(location, item, 50, maximumDropAmount - minimumDropAmount);
  193. }
  194. else {
  195. Misc.dropItems(location, item, minimumDropAmount);
  196. }
  197. }
  198. break;
  199. }
  200. }
  201. /**
  202. * Award XP for Mining blocks.
  203. *
  204. * @param player The player to award XP to
  205. * @param block The block to award XP for
  206. */
  207. public static void miningXP(Player player, Block block) {
  208. PlayerProfile profile = Users.getProfile(player);
  209. Material type = block.getType();
  210. int xp = 0;
  211. switch (type) {
  212. case COAL_ORE:
  213. xp += Config.getInstance().getMiningXPCoalOre();
  214. break;
  215. case DIAMOND_ORE:
  216. xp += Config.getInstance().getMiningXPDiamondOre();
  217. break;
  218. case ENDER_STONE:
  219. xp += Config.getInstance().getMiningXPEndStone();
  220. break;
  221. case GLOWING_REDSTONE_ORE:
  222. case REDSTONE_ORE:
  223. xp += Config.getInstance().getMiningXPRedstoneOre();
  224. break;
  225. case GLOWSTONE:
  226. xp += Config.getInstance().getMiningXPGlowstone();
  227. break;
  228. case GOLD_ORE:
  229. xp += Config.getInstance().getMiningXPGoldOre();
  230. break;
  231. case IRON_ORE:
  232. xp += Config.getInstance().getMiningXPIronOre();
  233. break;
  234. case LAPIS_ORE:
  235. xp += Config.getInstance().getMiningXPLapisOre();
  236. break;
  237. case MOSSY_COBBLESTONE:
  238. xp += Config.getInstance().getMiningXPMossyStone();
  239. break;
  240. case NETHERRACK:
  241. xp += Config.getInstance().getMiningXPNetherrack();
  242. break;
  243. case OBSIDIAN:
  244. xp += Config.getInstance().getMiningXPObsidian();
  245. break;
  246. case SANDSTONE:
  247. xp += Config.getInstance().getMiningXPSandstone();
  248. break;
  249. case STONE:
  250. xp += Config.getInstance().getMiningXPStone();
  251. break;
  252. case EMERALD_ORE:
  253. xp += Config.getInstance().getMiningXPEmeraldOre();
  254. break;
  255. default:
  256. if (ModChecks.isCustomMiningBlock(block)) {
  257. xp += ModChecks.getCustomBlock(block).getXpGain();
  258. }
  259. break;
  260. }
  261. Skills.xpProcessing(player, profile, SkillType.MINING, xp);
  262. }
  263. /**
  264. * Process Mining block drops.
  265. *
  266. * @param player The player mining the block
  267. * @param block The block being broken
  268. */
  269. public static void miningBlockCheck(Player player, Block block) {
  270. if (mcMMO.placeStore.isTrue(block)) {
  271. return;
  272. }
  273. miningXP(player, block);
  274. final int MAX_BONUS_LEVEL = advancedConfig.getMiningDoubleDropMaxLevel();
  275. int MAX_CHANCE = advancedConfig.getMiningDoubleDropChance();
  276. int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.MINING);
  277. int skillCheck = Misc.skillCheck(skillLevel, MAX_BONUS_LEVEL);
  278. int randomChance = 100;
  279. int chance = (int) (((double) MAX_CHANCE / (double) MAX_BONUS_LEVEL) * skillCheck);
  280. if (Permissions.luckyMining(player)) {
  281. randomChance = (int) (randomChance * 0.75);
  282. }
  283. if (chance > random.nextInt(randomChance) && Permissions.miningDoubleDrops(player)) {
  284. if (player.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) {
  285. silkTouchDrops(block);
  286. }
  287. else {
  288. miningDrops(block);
  289. }
  290. }
  291. }
  292. /**
  293. * Handle the Super Breaker ability.
  294. *
  295. * @param player The player using the ability
  296. * @param block The block being affected
  297. */
  298. public static void superBreakerBlockCheck(Player player, Block block) {
  299. Material type = block.getType();
  300. int tier = Misc.getTier(player.getItemInHand());
  301. int durabilityLoss = Config.getInstance().getAbilityToolDamage();
  302. FakePlayerAnimationEvent armswing = new FakePlayerAnimationEvent(player);
  303. if (ModChecks.isCustomMiningBlock(block)) {
  304. if (ModChecks.getCustomBlock(block).getTier() < tier) {
  305. return;
  306. }
  307. if (mcMMO.placeStore.isTrue(block) || Misc.blockBreakSimulate(block, player, true)) {
  308. return;
  309. }
  310. mcMMO.p.getServer().getPluginManager().callEvent(armswing);
  311. Skills.abilityDurabilityLoss(player.getItemInHand(), durabilityLoss);
  312. miningBlockCheck(player, block);
  313. if (mcMMO.spoutEnabled) {
  314. SpoutSounds.playSoundForPlayer(SoundEffect.POP, player, block.getLocation());
  315. }
  316. }
  317. else {
  318. switch (type) {
  319. case OBSIDIAN:
  320. if (tier < 4) {
  321. return;
  322. }
  323. durabilityLoss = durabilityLoss * 5; //Obsidian needs to do more damage than normal
  324. /* FALL THROUGH */
  325. case DIAMOND_ORE:
  326. case GLOWING_REDSTONE_ORE:
  327. case GOLD_ORE:
  328. case LAPIS_ORE:
  329. case REDSTONE_ORE:
  330. case EMERALD_ORE:
  331. if (tier < 3) {
  332. return;
  333. }
  334. /* FALL THROUGH */
  335. case IRON_ORE:
  336. if (tier < 2) {
  337. return;
  338. }
  339. /* FALL THROUGH */
  340. case COAL_ORE:
  341. case ENDER_STONE:
  342. case GLOWSTONE:
  343. case MOSSY_COBBLESTONE:
  344. case NETHERRACK:
  345. case SANDSTONE:
  346. case STONE:
  347. if (mcMMO.placeStore.isTrue(block) || Misc.blockBreakSimulate(block, player, true)) {
  348. return;
  349. }
  350. mcMMO.p.getServer().getPluginManager().callEvent(armswing);
  351. Skills.abilityDurabilityLoss(player.getItemInHand(), durabilityLoss);
  352. miningBlockCheck(player, block);
  353. if (mcMMO.spoutEnabled) {
  354. SpoutSounds.playSoundForPlayer(SoundEffect.POP, player, block.getLocation());
  355. }
  356. default:
  357. return;
  358. }
  359. }
  360. }
  361. }