From b8d346d890f3256cc8d961e125209418ae961998 Mon Sep 17 00:00:00 2001 From: gmcferrin Date: Mon, 7 Jan 2013 12:51:39 -0500 Subject: [PATCH] More work on Mining restructuring. --- .../nossr50/listeners/BlockListener.java | 6 +- .../gmail/nossr50/skills/mining/Mining.java | 372 ++++++++---------- .../mining/MiningDropsBlockHandler.java | 39 ++ .../nossr50/skills/mining/MiningManager.java | 66 ++++ 4 files changed, 281 insertions(+), 202 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/skills/mining/MiningDropsBlockHandler.java create mode 100644 src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java diff --git a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java index 4f41f2b1a..ac126bc7e 100644 --- a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java @@ -31,6 +31,7 @@ import com.gmail.nossr50.runnables.StickyPistonTracker; import com.gmail.nossr50.skills.gathering.Excavation; import com.gmail.nossr50.skills.gathering.Herbalism; import com.gmail.nossr50.skills.mining.Mining; +import com.gmail.nossr50.skills.mining.MiningManager; import com.gmail.nossr50.skills.gathering.WoodCutting; import com.gmail.nossr50.skills.repair.Repair; import com.gmail.nossr50.skills.repair.Salvage; @@ -204,13 +205,14 @@ public class BlockListener implements Listener { /* MINING */ else if (BlockChecks.canBeSuperBroken(block) && Permissions.mining(player)) { + MiningManager manager = new MiningManager(player); if (configInstance.getMiningRequiresTool()) { if (ItemChecks.isPickaxe(inHand)) { - Mining.miningBlockCheck(player, block); + manager.miningBlockCheck(block); } } else { - Mining.miningBlockCheck(player, block); + manager.miningBlockCheck(block); } } diff --git a/src/main/java/com/gmail/nossr50/skills/mining/Mining.java b/src/main/java/com/gmail/nossr50/skills/mining/Mining.java index 32a4eafdd..2d3ecae48 100644 --- a/src/main/java/com/gmail/nossr50/skills/mining/Mining.java +++ b/src/main/java/com/gmail/nossr50/skills/mining/Mining.java @@ -6,7 +6,6 @@ import org.bukkit.CoalType; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.material.MaterialData; @@ -22,20 +21,186 @@ import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent; import com.gmail.nossr50.spout.SpoutSounds; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.ModChecks; -import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Skills; import com.gmail.nossr50.util.Users; public class Mining { - private static Random random = new Random(); static AdvancedConfig advancedConfig = AdvancedConfig.getInstance(); + private static Random random = new Random(); + + public static final int DOUBLE_DROPS_MAX_BONUS_LEVEL = advancedConfig.getMiningDoubleDropMaxLevel(); + public static final int DOUBLE_DROPS_MAX_CHANCE = advancedConfig.getMiningDoubleDropChance(); + + /** + * Award XP for Mining blocks. + * + * @param player The player to award XP to + * @param block The block to award XP for + */ + public static void miningXP(Player player, Block block) { + PlayerProfile profile = Users.getProfile(player); + Material type = block.getType(); + int xp = 0; + + switch (type) { + case COAL_ORE: + xp += Config.getInstance().getMiningXPCoalOre(); + break; + + case DIAMOND_ORE: + xp += Config.getInstance().getMiningXPDiamondOre(); + break; + + case ENDER_STONE: + xp += Config.getInstance().getMiningXPEndStone(); + break; + + case GLOWING_REDSTONE_ORE: + case REDSTONE_ORE: + xp += Config.getInstance().getMiningXPRedstoneOre(); + break; + + case GLOWSTONE: + xp += Config.getInstance().getMiningXPGlowstone(); + break; + + case GOLD_ORE: + xp += Config.getInstance().getMiningXPGoldOre(); + break; + + case IRON_ORE: + xp += Config.getInstance().getMiningXPIronOre(); + break; + + case LAPIS_ORE: + xp += Config.getInstance().getMiningXPLapisOre(); + break; + + case MOSSY_COBBLESTONE: + xp += Config.getInstance().getMiningXPMossyStone(); + break; + + case NETHERRACK: + xp += Config.getInstance().getMiningXPNetherrack(); + break; + + case OBSIDIAN: + xp += Config.getInstance().getMiningXPObsidian(); + break; + + case SANDSTONE: + xp += Config.getInstance().getMiningXPSandstone(); + break; + + case STONE: + xp += Config.getInstance().getMiningXPStone(); + break; + + case EMERALD_ORE: + xp += Config.getInstance().getMiningXPEmeraldOre(); + break; + + default: + if (ModChecks.isCustomMiningBlock(block)) { + xp += ModChecks.getCustomBlock(block).getXpGain(); + } + break; + } + + Skills.xpProcessing(player, profile, SkillType.MINING, xp); + } + + /** + * Handle the Super Breaker ability. + * + * @param player The player using the ability + * @param block The block being affected + */ + public static void superBreakerBlockCheck(Player player, Block block) { + Material type = block.getType(); + int tier = Misc.getTier(player.getItemInHand()); + int durabilityLoss = Config.getInstance().getAbilityToolDamage(); + FakePlayerAnimationEvent armswing = new FakePlayerAnimationEvent(player); + + if (ModChecks.isCustomMiningBlock(block)) { + if (ModChecks.getCustomBlock(block).getTier() < tier) { + return; + } + + if (mcMMO.placeStore.isTrue(block) || Misc.blockBreakSimulate(block, player, true)) { + return; + } + + mcMMO.p.getServer().getPluginManager().callEvent(armswing); + Skills.abilityDurabilityLoss(player.getItemInHand(), durabilityLoss); + + MiningManager manager = new MiningManager(player); + manager.miningBlockCheck(block); + + if (mcMMO.spoutEnabled) { + SpoutSounds.playSoundForPlayer(SoundEffect.POP, player, block.getLocation()); + } + } + else { + switch (type) { + case OBSIDIAN: + if (tier < 4) { + return; + } + durabilityLoss = durabilityLoss * 5; //Obsidian needs to do more damage than normal + /* FALL THROUGH */ + + case DIAMOND_ORE: + case GLOWING_REDSTONE_ORE: + case GOLD_ORE: + case LAPIS_ORE: + case REDSTONE_ORE: + case EMERALD_ORE: + if (tier < 3) { + return; + } + /* FALL THROUGH */ + + case IRON_ORE: + if (tier < 2) { + return; + } + /* FALL THROUGH */ + + case COAL_ORE: + case ENDER_STONE: + case GLOWSTONE: + case MOSSY_COBBLESTONE: + case NETHERRACK: + case SANDSTONE: + case STONE: + if (mcMMO.placeStore.isTrue(block) || Misc.blockBreakSimulate(block, player, true)) { + return; + } + + mcMMO.p.getServer().getPluginManager().callEvent(armswing); + Skills.abilityDurabilityLoss(player.getItemInHand(), durabilityLoss); + + MiningManager manager = new MiningManager(player); + manager.miningBlockCheck(block); + + if (mcMMO.spoutEnabled) { + SpoutSounds.playSoundForPlayer(SoundEffect.POP, player, block.getLocation()); + } + + default: + return; + } + } + } + /** * Handle double drops when using Silk Touch. * * @param block The block to process drops for */ - private static void silkTouchDrops(Block block) { + protected static void silkTouchDrops(Block block) { Location location = block.getLocation(); Material type = block.getType(); ItemStack item = new ItemStack(type); @@ -111,7 +276,7 @@ public class Mining { * * @param block The block to process drops for */ - public static void miningDrops(Block block) { + protected static void miningDrops(Block block) { Location location = block.getLocation(); Material type = block.getType(); ItemStack item = new ItemStack(type); @@ -235,200 +400,7 @@ public class Mining { } } - /** - * Award XP for Mining blocks. - * - * @param player The player to award XP to - * @param block The block to award XP for - */ - public static void miningXP(Player player, Block block) { - PlayerProfile profile = Users.getProfile(player); - Material type = block.getType(); - int xp = 0; - - switch (type) { - case COAL_ORE: - xp += Config.getInstance().getMiningXPCoalOre(); - break; - - case DIAMOND_ORE: - xp += Config.getInstance().getMiningXPDiamondOre(); - break; - - case ENDER_STONE: - xp += Config.getInstance().getMiningXPEndStone(); - break; - - case GLOWING_REDSTONE_ORE: - case REDSTONE_ORE: - xp += Config.getInstance().getMiningXPRedstoneOre(); - break; - - case GLOWSTONE: - xp += Config.getInstance().getMiningXPGlowstone(); - break; - - case GOLD_ORE: - xp += Config.getInstance().getMiningXPGoldOre(); - break; - - case IRON_ORE: - xp += Config.getInstance().getMiningXPIronOre(); - break; - - case LAPIS_ORE: - xp += Config.getInstance().getMiningXPLapisOre(); - break; - - case MOSSY_COBBLESTONE: - xp += Config.getInstance().getMiningXPMossyStone(); - break; - - case NETHERRACK: - xp += Config.getInstance().getMiningXPNetherrack(); - break; - - case OBSIDIAN: - xp += Config.getInstance().getMiningXPObsidian(); - break; - - case SANDSTONE: - xp += Config.getInstance().getMiningXPSandstone(); - break; - - case STONE: - xp += Config.getInstance().getMiningXPStone(); - break; - - case EMERALD_ORE: - xp += Config.getInstance().getMiningXPEmeraldOre(); - break; - - default: - if (ModChecks.isCustomMiningBlock(block)) { - xp += ModChecks.getCustomBlock(block).getXpGain(); - } - break; - } - - Skills.xpProcessing(player, profile, SkillType.MINING, xp); - } - - /** - * Process Mining block drops. - * - * @param player The player mining the block - * @param block The block being broken - */ - public static void miningBlockCheck(Player player, Block block) { - if (mcMMO.placeStore.isTrue(block)) { - return; - } - - miningXP(player, block); - - final int MAX_BONUS_LEVEL = advancedConfig.getMiningDoubleDropMaxLevel(); - int MAX_CHANCE = advancedConfig.getMiningDoubleDropChance(); - - int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.MINING); - int skillCheck = Misc.skillCheck(skillLevel, MAX_BONUS_LEVEL); - - int randomChance = 100; - int chance = (int) (((double) MAX_CHANCE / (double) MAX_BONUS_LEVEL) * skillCheck); - - if (Permissions.luckyMining(player)) { - randomChance = (int) (randomChance * 0.75); - } - - if (chance > random.nextInt(randomChance) && Permissions.miningDoubleDrops(player)) { - if (player.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) { - silkTouchDrops(block); - } - else { - miningDrops(block); - } - } - } - - /** - * Handle the Super Breaker ability. - * - * @param player The player using the ability - * @param block The block being affected - */ - public static void superBreakerBlockCheck(Player player, Block block) { - Material type = block.getType(); - int tier = Misc.getTier(player.getItemInHand()); - int durabilityLoss = Config.getInstance().getAbilityToolDamage(); - FakePlayerAnimationEvent armswing = new FakePlayerAnimationEvent(player); - - if (ModChecks.isCustomMiningBlock(block)) { - if (ModChecks.getCustomBlock(block).getTier() < tier) { - return; - } - - if (mcMMO.placeStore.isTrue(block) || Misc.blockBreakSimulate(block, player, true)) { - return; - } - - mcMMO.p.getServer().getPluginManager().callEvent(armswing); - Skills.abilityDurabilityLoss(player.getItemInHand(), durabilityLoss); - - miningBlockCheck(player, block); - - if (mcMMO.spoutEnabled) { - SpoutSounds.playSoundForPlayer(SoundEffect.POP, player, block.getLocation()); - } - } - else { - switch (type) { - case OBSIDIAN: - if (tier < 4) { - return; - } - durabilityLoss = durabilityLoss * 5; //Obsidian needs to do more damage than normal - /* FALL THROUGH */ - - case DIAMOND_ORE: - case GLOWING_REDSTONE_ORE: - case GOLD_ORE: - case LAPIS_ORE: - case REDSTONE_ORE: - case EMERALD_ORE: - if (tier < 3) { - return; - } - /* FALL THROUGH */ - - case IRON_ORE: - if (tier < 2) { - return; - } - /* FALL THROUGH */ - - case COAL_ORE: - case ENDER_STONE: - case GLOWSTONE: - case MOSSY_COBBLESTONE: - case NETHERRACK: - case SANDSTONE: - case STONE: - if (mcMMO.placeStore.isTrue(block) || Misc.blockBreakSimulate(block, player, true)) { - return; - } - - mcMMO.p.getServer().getPluginManager().callEvent(armswing); - Skills.abilityDurabilityLoss(player.getItemInHand(), durabilityLoss); - - miningBlockCheck(player, block); - - if (mcMMO.spoutEnabled) { - SpoutSounds.playSoundForPlayer(SoundEffect.POP, player, block.getLocation()); - } - - default: - return; - } - } + protected static Random getRandom() { + return random; } } diff --git a/src/main/java/com/gmail/nossr50/skills/mining/MiningDropsBlockHandler.java b/src/main/java/com/gmail/nossr50/skills/mining/MiningDropsBlockHandler.java new file mode 100644 index 000000000..7c0ce5500 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/skills/mining/MiningDropsBlockHandler.java @@ -0,0 +1,39 @@ +package com.gmail.nossr50.skills.mining; + +import org.bukkit.block.Block; +import org.bukkit.enchantments.Enchantment; + +import com.gmail.nossr50.util.Misc; + +public class MiningDropsBlockHandler { + private MiningManager manager; + private Block block; + + protected int skillModifier; + + protected MiningDropsBlockHandler(MiningManager manager, Block block) { + this.manager = manager; + this.block = block; + + calculateSkillModifier(); + } + + protected void calculateSkillModifier() { + this.skillModifier = Misc.skillCheck(manager.getSkillLevel(), Mining.DOUBLE_DROPS_MAX_BONUS_LEVEL); + } + + /** + * Process Mining block drops. + * + * @param player The player mining the block + * @param block The block being broken + */ + protected void processDrops() { + if (manager.getPlayer().getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) { + Mining.silkTouchDrops(block); + } + else { + Mining.miningDrops(block); + } + } +} diff --git a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java new file mode 100644 index 000000000..06fbdbbfd --- /dev/null +++ b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java @@ -0,0 +1,66 @@ +package com.gmail.nossr50.skills.mining; + +import org.bukkit.block.Block; +import org.bukkit.entity.Player; + +import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.datatypes.PlayerProfile; +import com.gmail.nossr50.datatypes.SkillType; +import com.gmail.nossr50.util.Permissions; +import com.gmail.nossr50.util.Users; + +public class MiningManager { + private Player player; + private PlayerProfile profile; + private int skillLevel; + private Permissions permissionsInstance; + + public MiningManager (Player player) { + this.player = player; + this.profile = Users.getProfile(player); + + this.skillLevel = profile.getSkillLevel(SkillType.MINING); + } + + /** + * Process Mining block drops. + * + * @param block The block being broken + */ + public void miningBlockCheck(Block block) { + if (mcMMO.placeStore.isTrue(block)) { + return; + } + + Mining.miningXP(player, block); + + if (!Permissions.miningDoubleDrops(player)) { + return; + } + + MiningDropsBlockHandler blockHandler = new MiningDropsBlockHandler(this, block); + + int randomChance = 100; + if (Permissions.luckyMining(player)) { + randomChance = (int) (randomChance * 0.75); + } + + float chance = (float) (((double) Mining.DOUBLE_DROPS_MAX_CHANCE / (double) Mining.DOUBLE_DROPS_MAX_BONUS_LEVEL) * blockHandler.skillModifier); + + if (chance > Mining.getRandom().nextInt(randomChance)) { + blockHandler.processDrops(); + } + } + + protected int getSkillLevel() { + return skillLevel; + } + + protected Permissions getPermissionsInstance() { + return permissionsInstance; + } + + protected Player getPlayer() { + return player; + } +}