Mining.java 13 KB

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