From ccfe1181be4baab0d8fc0e446a1bebbcf27af64a Mon Sep 17 00:00:00 2001 From: gmcferrin Date: Tue, 8 Jan 2013 12:52:16 -0500 Subject: [PATCH] More mining restructuring. --- src/main/java/com/gmail/nossr50/mcMMO.java | 1 - .../gmail/nossr50/skills/mining/Mining.java | 96 +------------- .../mining/MiningBlockEventHandler.java | 7 +- .../nossr50/skills/mining/MiningManager.java | 27 +++- .../mining/SuperBreakerEventHandler.java | 123 ++++++++++++++++++ .../java/com/gmail/nossr50/util/Misc.java | 3 +- 6 files changed, 161 insertions(+), 96 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/skills/mining/SuperBreakerEventHandler.java diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index a0b973e54..90bdf68c5 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -83,7 +83,6 @@ import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManagerFactory; public class mcMMO extends JavaPlugin { - private final PlayerListener playerListener = new PlayerListener(this); private final BlockListener blockListener = new BlockListener(this); private final EntityListener entityListener = new EntityListener(this); 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 81fd48ed8..4e2b2408c 100644 --- a/src/main/java/com/gmail/nossr50/skills/mining/Mining.java +++ b/src/main/java/com/gmail/nossr50/skills/mining/Mining.java @@ -10,16 +10,12 @@ import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.material.MaterialData; -import org.getspout.spoutapi.sound.SoundEffect; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.SkillType; import com.gmail.nossr50.datatypes.mods.CustomBlock; -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.Skills; @@ -32,13 +28,17 @@ public class Mining { public static final int DOUBLE_DROPS_MAX_BONUS_LEVEL = advancedConfig.getMiningDoubleDropMaxLevel(); public static final int DOUBLE_DROPS_MAX_CHANCE = advancedConfig.getMiningDoubleDropChance(); + public static final int DIAMOND_TOOL_TIER = 4; + public static final int IRON_TOOL_TIER = 3; + public static final int STONE_TOOL_TIER = 2; + /** * Award XP for Mining blocks. * * @param player The player to award XP to * @param block The block to award XP for */ - protected static void miningXP(Player player, PlayerProfile profile, Block block, Material type) { + protected static void miningXP(Player player, PlayerProfile profile, Block block, Material type) { int xp = 0; switch (type) { @@ -109,91 +109,6 @@ public class Mining { 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.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. * @@ -266,7 +181,6 @@ public class Mining { } break; } - } /** diff --git a/src/main/java/com/gmail/nossr50/skills/mining/MiningBlockEventHandler.java b/src/main/java/com/gmail/nossr50/skills/mining/MiningBlockEventHandler.java index dedd33da6..6abbd0a75 100644 --- a/src/main/java/com/gmail/nossr50/skills/mining/MiningBlockEventHandler.java +++ b/src/main/java/com/gmail/nossr50/skills/mining/MiningBlockEventHandler.java @@ -4,11 +4,13 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; import com.gmail.nossr50.util.Misc; public class MiningBlockEventHandler { private MiningManager manager; + private Player player; private Block block; private Location blockLocation; @@ -18,6 +20,7 @@ public class MiningBlockEventHandler { protected MiningBlockEventHandler(MiningManager manager, Block block) { this.manager = manager; + this.player = manager.getPlayer(); this.block = block; this.blockLocation = block.getLocation(); @@ -37,7 +40,7 @@ public class MiningBlockEventHandler { * @param block The block being broken */ protected void processDrops() { - if (manager.getPlayer().getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) { + if (player.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) { Mining.silkTouchDrops(block, blockLocation, blockType); } else { @@ -46,6 +49,6 @@ public class MiningBlockEventHandler { } protected void processXP() { - Mining.miningXP(manager.getPlayer(), manager.getProfile(), block, blockType); + Mining.miningXP(player, manager.getProfile(), block, blockType); } } diff --git a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java index 7cf0740ed..87cfaab48 100644 --- a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java +++ b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java @@ -6,6 +6,7 @@ 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.Misc; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Users; @@ -44,13 +45,37 @@ public class MiningManager { randomChance = (int) (randomChance * 0.75); } - float chance = (float) (((double) Mining.DOUBLE_DROPS_MAX_CHANCE / Mining.DOUBLE_DROPS_MAX_BONUS_LEVEL) * eventHandler.skillModifier); + float chance = ((float) Mining.DOUBLE_DROPS_MAX_CHANCE / Mining.DOUBLE_DROPS_MAX_BONUS_LEVEL) * eventHandler.skillModifier; if (chance > Mining.getRandom().nextInt(randomChance)) { eventHandler.processDrops(); } } + /** + * Handle the Super Breaker ability. + * + * @param player The player using the ability + * @param block The block being affected + */ + public void superBreakerBlockCheck(Block block) { + if (mcMMO.placeStore.isTrue(block) || Misc.blockBreakSimulate(block, player, true)) { + return; + } + + MiningManager manager = new MiningManager(player); + SuperBreakerEventHandler eventHandler = new SuperBreakerEventHandler(manager, block); + + if (eventHandler.tierCheck()) { + return; + } + + eventHandler.callFakeArmswing(); + eventHandler.processDurabilityLoss(); + eventHandler.processDropsAndXP(); + eventHandler.playSpoutSound(); + } + protected int getSkillLevel() { return skillLevel; } diff --git a/src/main/java/com/gmail/nossr50/skills/mining/SuperBreakerEventHandler.java b/src/main/java/com/gmail/nossr50/skills/mining/SuperBreakerEventHandler.java new file mode 100644 index 000000000..1bc4f3297 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/skills/mining/SuperBreakerEventHandler.java @@ -0,0 +1,123 @@ +package com.gmail.nossr50.skills.mining; + +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.getspout.spoutapi.sound.SoundEffect; + +import com.gmail.nossr50.mcMMO; +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.Skills; + +public class SuperBreakerEventHandler { + private MiningManager manager; + private Player player; + + private Block block; + private Material blockType; + private boolean customBlock; + + private ItemStack heldItem; + private int tier; + + private int durabilityLoss; + private FakePlayerAnimationEvent armswing; + + protected SuperBreakerEventHandler (MiningManager manager, Block block) { + this.manager = manager; + this.player = manager.getPlayer(); + + this.block = block; + this.blockType = block.getType(); + this.customBlock = ModChecks.isCustomMiningBlock(block); + + this.heldItem = player.getItemInHand(); + this.tier = Misc.getTier(heldItem); + + this.armswing = new FakePlayerAnimationEvent(player); + + calculateDurabilityLoss(); + } + + protected void callFakeArmswing() { + mcMMO.p.getServer().getPluginManager().callEvent(armswing); + } + + protected void processDurabilityLoss() { + Skills.abilityDurabilityLoss(heldItem, durabilityLoss); + } + + protected void processDropsAndXP() { + manager.miningBlockCheck(block); + } + + protected void playSpoutSound() { + SpoutSounds.playSoundForPlayer(SoundEffect.POP, player, block.getLocation()); + } + + /** + * Check for the proper tier of item for use with Super Breaker. + * + * @return True if the item is the required tier or higher, false otherwise + */ + protected boolean tierCheck() { + if (customBlock) { + if (ModChecks.getCustomBlock(block).getTier() < tier) { + return false; + } + else { + return true; + } + } + else { + switch (blockType) { + case OBSIDIAN: + if (tier < Mining.DIAMOND_TOOL_TIER) { + return false; + } + /* FALL THROUGH */ + + case DIAMOND_ORE: + case GLOWING_REDSTONE_ORE: + case GOLD_ORE: + case LAPIS_ORE: + case REDSTONE_ORE: + case EMERALD_ORE: + if (tier < Mining.IRON_TOOL_TIER) { + return false; + } + /* FALL THROUGH */ + + case IRON_ORE: + if (tier < Mining.STONE_TOOL_TIER) { + return false; + } + /* FALL THROUGH */ + + case COAL_ORE: + case ENDER_STONE: + case GLOWSTONE: + case MOSSY_COBBLESTONE: + case NETHERRACK: + case SANDSTONE: + case STONE: + return true; + + default: + return false; + } + } + } + + private void calculateDurabilityLoss() { + this.durabilityLoss = Misc.TOOL_DURABILITY_LOSS; + + if (blockType.equals(Material.OBSIDIAN)) { + durabilityLoss = durabilityLoss * 5; + } + } +} diff --git a/src/main/java/com/gmail/nossr50/util/Misc.java b/src/main/java/com/gmail/nossr50/util/Misc.java index dc160cba5..596286518 100644 --- a/src/main/java/com/gmail/nossr50/util/Misc.java +++ b/src/main/java/com/gmail/nossr50/util/Misc.java @@ -20,9 +20,10 @@ import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent; import com.gmail.nossr50.events.items.McMMOItemSpawnEvent; public class Misc { - private static Random random = new Random(); + public static final int TOOL_DURABILITY_LOSS = Config.getInstance().getAbilityToolDamage(); + /** * Gets a capitalized version of the target string. *