From 0bdd5b219fa8e120db213caa3e5e9757e74a2b5b Mon Sep 17 00:00:00 2001 From: GJ Date: Tue, 26 Feb 2013 23:31:00 -0500 Subject: [PATCH] Finish off FishingManager. --- .../nossr50/listeners/PlayerListener.java | 8 +- .../gmail/nossr50/skills/fishing/Fishing.java | 150 ++++++++++- .../skills/fishing/FishingCommand.java | 2 +- .../skills/fishing/FishingManager.java | 96 ++++++- .../nossr50/skills/fishing/ShakeMob.java | 236 ------------------ 5 files changed, 241 insertions(+), 251 deletions(-) delete mode 100644 src/main/java/com/gmail/nossr50/skills/fishing/ShakeMob.java diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index a4fc61d91..110f54c5e 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -27,7 +27,6 @@ import org.bukkit.inventory.ItemStack; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.chat.ChatManager; -import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.McMMOPlayer; import com.gmail.nossr50.datatypes.PlayerProfile; @@ -35,7 +34,6 @@ import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.party.Party; import com.gmail.nossr50.party.ShareHandler; import com.gmail.nossr50.skills.SkillManagerStore; -import com.gmail.nossr50.skills.fishing.Fishing; import com.gmail.nossr50.skills.fishing.FishingManager; import com.gmail.nossr50.skills.herbalism.Herbalism; import com.gmail.nossr50.skills.mining.BlastMining; @@ -166,8 +164,6 @@ public class PlayerListener implements Listener { } FishingManager fishingManager = SkillManagerStore.getInstance().getFishingManager(player.getName()); - McMMOPlayer mcMMOPlayer = Users.getPlayer(player); - int skillLevel = mcMMOPlayer.getProfile().getSkillLevel(SkillType.FISHING); switch (event.getState()) { case CAUGHT_FISH: @@ -181,8 +177,8 @@ public class PlayerListener implements Listener { case CAUGHT_ENTITY: Entity entity = event.getCaught(); - if (entity instanceof LivingEntity && skillLevel >= AdvancedConfig.getInstance().getShakeUnlockLevel() && Permissions.shake(player)) { - Fishing.beginShakeMob(player, (LivingEntity) entity, skillLevel); + if (fishingManager.canShake(entity)) { + fishingManager.shakeCheck((LivingEntity) entity); } break; diff --git a/src/main/java/com/gmail/nossr50/skills/fishing/Fishing.java b/src/main/java/com/gmail/nossr50/skills/fishing/Fishing.java index da9294989..bb1e03533 100644 --- a/src/main/java/com/gmail/nossr50/skills/fishing/Fishing.java +++ b/src/main/java/com/gmail/nossr50/skills/fishing/Fishing.java @@ -1,9 +1,17 @@ package com.gmail.nossr50.skills.fishing; +import java.util.Map; +import java.util.Map.Entry; + +import org.bukkit.DyeColor; +import org.bukkit.Material; import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.Potion; +import org.bukkit.potion.PotionType; import com.gmail.nossr50.config.AdvancedConfig; +import com.gmail.nossr50.util.Misc; public final class Fishing { // The order of the values is extremely important, a few methods depend on it to work properly @@ -53,13 +61,141 @@ public final class Fishing { private Fishing() {} /** - * Begins Shake Mob ability + * Finds the possible drops of an entity * - * @param player Player using the ability - * @param mob Targeted mob - * @param skillLevel Fishing level of the player + * @param target Targeted entity + * @param possibleDrops List of ItemStack that can be dropped */ - public static void beginShakeMob(Player player, LivingEntity mob, int skillLevel) { - ShakeMob.process(player, mob, skillLevel); + protected static void findPossibleDrops(LivingEntity target, Map possibleDrops) { + switch (target.getType()) { + case BLAZE: + possibleDrops.put(new ItemStack(Material.BLAZE_ROD), 100); + break; + + case CAVE_SPIDER: + case SPIDER: + possibleDrops.put(new ItemStack(Material.SPIDER_EYE), 50); + possibleDrops.put(new ItemStack(Material.STRING), 50); + break; + + case CHICKEN: + possibleDrops.put(new ItemStack(Material.FEATHER), 34); + possibleDrops.put(new ItemStack(Material.RAW_CHICKEN), 33); + possibleDrops.put(new ItemStack(Material.EGG), 33); + break; + + case COW: + possibleDrops.put(new ItemStack(Material.MILK_BUCKET), 2); + possibleDrops.put(new ItemStack(Material.LEATHER), 49); + possibleDrops.put(new ItemStack(Material.RAW_BEEF), 49); + break; + + case CREEPER: + possibleDrops.put(new ItemStack(Material.SKULL_ITEM, 1, (short) 4), 1); + possibleDrops.put(new ItemStack(Material.SULPHUR), 99); + break; + + case ENDERMAN: + possibleDrops.put(new ItemStack(Material.ENDER_PEARL), 100); + break; + + case GHAST: + possibleDrops.put(new ItemStack(Material.SULPHUR), 50); + possibleDrops.put(new ItemStack(Material.GHAST_TEAR), 50); + break; + + case IRON_GOLEM: + possibleDrops.put(new ItemStack(Material.PUMPKIN), 3); + possibleDrops.put(new ItemStack(Material.IRON_INGOT), 12); + possibleDrops.put(new ItemStack(Material.RED_ROSE), 85); + break; + + case MAGMA_CUBE: + possibleDrops.put(new ItemStack(Material.MAGMA_CREAM), 100); + break; + + case MUSHROOM_COW: + possibleDrops.put(new ItemStack(Material.MILK_BUCKET), 5); + possibleDrops.put(new ItemStack(Material.MUSHROOM_SOUP), 5); + possibleDrops.put(new ItemStack(Material.LEATHER), 30); + possibleDrops.put(new ItemStack(Material.RAW_BEEF), 30); + possibleDrops.put(new ItemStack(Material.RED_MUSHROOM, Misc.getRandom().nextInt(3) + 1), 30); + break; + + case PIG: + possibleDrops.put(new ItemStack(Material.PORK), 100); + break; + + case PIG_ZOMBIE: + possibleDrops.put(new ItemStack(Material.ROTTEN_FLESH), 50); + possibleDrops.put(new ItemStack(Material.GOLD_NUGGET), 50); + break; + + case SHEEP: + possibleDrops.put(new ItemStack(Material.WOOL, Misc.getRandom().nextInt(6) + 1), 100); + break; + + case SKELETON: + possibleDrops.put(new ItemStack(Material.SKULL_ITEM, 1, (short) 0), 2); + possibleDrops.put(new ItemStack(Material.BONE), 49); + possibleDrops.put(new ItemStack(Material.ARROW, Misc.getRandom().nextInt(3) + 1), 49); + break; + + case SLIME: + possibleDrops.put(new ItemStack(Material.SLIME_BALL), 100); + break; + + case SNOWMAN: + possibleDrops.put(new ItemStack(Material.PUMPKIN), 3); + possibleDrops.put(new ItemStack(Material.SNOW_BALL, Misc.getRandom().nextInt(4) + 1), 97); + break; + + case SQUID: + possibleDrops.put(new ItemStack(Material.INK_SACK, 1, DyeColor.BLACK.getDyeData()), 100); + break; + + case WITCH: + possibleDrops.put(new Potion(PotionType.INSTANT_HEAL).toItemStack(1), 1); + possibleDrops.put(new Potion(PotionType.FIRE_RESISTANCE).toItemStack(1), 1); + possibleDrops.put(new Potion(PotionType.SPEED).toItemStack(1), 1); + possibleDrops.put(new ItemStack(Material.GLASS_BOTTLE), 9); + possibleDrops.put(new ItemStack(Material.GLOWSTONE_DUST), 13); + possibleDrops.put(new ItemStack(Material.SULPHUR), 12); + possibleDrops.put(new ItemStack(Material.REDSTONE), 13); + possibleDrops.put(new ItemStack(Material.SPIDER_EYE), 12); + possibleDrops.put(new ItemStack(Material.STICK), 13); + possibleDrops.put(new ItemStack(Material.SUGAR), 12); + possibleDrops.put(new ItemStack(Material.POTION), 13); + break; + + case ZOMBIE: + possibleDrops.put(new ItemStack(Material.SKULL_ITEM, 1, (short) 2), 2); + possibleDrops.put(new ItemStack(Material.ROTTEN_FLESH), 98); + break; + + default: + return; + } + } + + /** + * Randomly chooses a drop among the list + * + * @param possibleDrops List of ItemStack that can be dropped + * @return Chosen ItemStack + */ + protected static ItemStack chooseDrop(Map possibleDrops) { + int dropProbability = Misc.getRandom().nextInt(100); + int cumulatedProbability = 0; + + for (Entry entry : possibleDrops.entrySet()) { + cumulatedProbability += entry.getValue(); + + if (dropProbability < cumulatedProbability) { + return entry.getKey(); + } + } + + return null; } } diff --git a/src/main/java/com/gmail/nossr50/skills/fishing/FishingCommand.java b/src/main/java/com/gmail/nossr50/skills/fishing/FishingCommand.java index 3b0731670..eb7db1053 100644 --- a/src/main/java/com/gmail/nossr50/skills/fishing/FishingCommand.java +++ b/src/main/java/com/gmail/nossr50/skills/fishing/FishingCommand.java @@ -42,7 +42,7 @@ public class FishingCommand extends SkillCommand { magicChanceLucky = treasureHunterStrings[1]; //SHAKE - String[] shakeStrings = calculateAbilityDisplayValues(ShakeMob.getShakeProbability((int) skillValue)); + String[] shakeStrings = calculateAbilityDisplayValues(SkillManagerStore.getInstance().getFishingManager(player.getName()).getShakeProbability()); shakeChance = shakeStrings[0]; shakeChanceLucky = shakeStrings[1]; diff --git a/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java b/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java index caba1abcb..01a378a7b 100644 --- a/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java +++ b/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java @@ -2,11 +2,19 @@ package com.gmail.nossr50.skills.fishing; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Entity; import org.bukkit.entity.Item; +import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.entity.Sheep; +import org.bukkit.entity.Skeleton; +import org.bukkit.entity.Skeleton.SkeletonType; import org.bukkit.inventory.ItemStack; import com.gmail.nossr50.config.AdvancedConfig; @@ -17,6 +25,7 @@ import com.gmail.nossr50.datatypes.treasure.FishingTreasure; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.skills.fishing.Fishing.Tier; +import com.gmail.nossr50.skills.utilities.CombatTools; import com.gmail.nossr50.skills.utilities.SkillTools; import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.util.ItemChecks; @@ -24,11 +33,15 @@ import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; public class FishingManager extends SkillManager { - public FishingManager(McMMOPlayer mcMMOPlayer) { super(mcMMOPlayer, SkillType.FISHING); } + public boolean canShake(Entity target) { + Player player = getPlayer(); + + return target instanceof LivingEntity && SkillTools.unlockLevelReached(player, skill, AdvancedConfig.getInstance().getShakeUnlockLevel()) && Permissions.shake(player); + } /** * Handle the Fisherman's Diet ability * @@ -82,6 +95,71 @@ public class FishingManager extends SkillManager { return experience * getVanillaXpMultiplier(); } + /** + * Handle the Shake ability + * + * @param mob The {@link LivingEntity} affected by the ability + */ + public void shakeCheck(LivingEntity target) { + if (SkillTools.activationSuccessful(getPlayer(), skill, getShakeProbability())) { + Map possibleDrops = new HashMap(); + + Fishing.findPossibleDrops(target, possibleDrops); + + if (possibleDrops.isEmpty()) { + return; + } + + ItemStack drop = Fishing.chooseDrop(possibleDrops); + + // It's possible that chooseDrop returns null if the sum of probability in possibleDrops is inferior than 100 + if (drop == null) { + return; + } + + // Extra processing depending on the mob and drop type + switch (target.getType()) { + case SHEEP: + Sheep sheep = (Sheep) target; + + if (drop.getType() == Material.WOOL) { + if (sheep.isSheared()) { + return; + } + + drop.setDurability(sheep.getColor().getWoolData()); + sheep.setSheared(true); + } + break; + + case SKELETON: + Skeleton skeleton = (Skeleton) target; + + if (skeleton.getSkeletonType() == SkeletonType.WITHER) { + switch (drop.getType()) { + case SKULL_ITEM: + drop.setDurability((short) 1); + break; + + case ARROW: + drop.setType(Material.COAL); + break; + + default: + break; + } + } + break; + + default: + break; + } + + Misc.dropItem(target.getLocation(), drop); + CombatTools.dealDamage(target, target.getMaxHealth() / 4); // Make it so you can shake a mob no more than 4 times. + } + } + /** * Process the Treasure Hunter ability for Fishing * @@ -190,6 +268,22 @@ public class FishingManager extends SkillManager { return 0; } + /** + * Gets the Shake Mob probability + * + * @return Shake Mob probability + */ + public int getShakeProbability() { + int skillLevel = getSkillLevel(); + + for (Tier tier : Tier.values()) { + if (skillLevel >= tier.getLevel()) { + return tier.getShakeChance(); + } + } + + return 0; + } /** * Gets the vanilla XP multiplier diff --git a/src/main/java/com/gmail/nossr50/skills/fishing/ShakeMob.java b/src/main/java/com/gmail/nossr50/skills/fishing/ShakeMob.java deleted file mode 100644 index 4a746c8d5..000000000 --- a/src/main/java/com/gmail/nossr50/skills/fishing/ShakeMob.java +++ /dev/null @@ -1,236 +0,0 @@ -package com.gmail.nossr50.skills.fishing; - -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - -import org.bukkit.DyeColor; -import org.bukkit.Material; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; -import org.bukkit.entity.Sheep; -import org.bukkit.entity.Skeleton; -import org.bukkit.entity.Skeleton.SkeletonType; -import org.bukkit.inventory.ItemStack; -import org.bukkit.material.Wool; -import org.bukkit.potion.Potion; -import org.bukkit.potion.PotionType; - -import com.gmail.nossr50.skills.fishing.Fishing.Tier; -import com.gmail.nossr50.skills.utilities.CombatTools; -import com.gmail.nossr50.skills.utilities.PerksUtils; -import com.gmail.nossr50.skills.utilities.SkillType; -import com.gmail.nossr50.util.Misc; - -public final class ShakeMob { - private ShakeMob() {} - - /** - * Begins Tree Feller - * - * @param player Player using Shake Mob - * @param mob Targeted entity - * @param skillLevel Fishing level of the player - */ - public static void process(Player player, LivingEntity mob, int skillLevel) { - int activationChance = PerksUtils.handleLuckyPerks(player, SkillType.FISHING); - - if (getShakeProbability(skillLevel) <= Misc.getRandom().nextInt(activationChance)) { - return; - } - - Map possibleDrops = new HashMap(); - - findPossibleDrops(mob, possibleDrops); - - if (possibleDrops.isEmpty()) { - return; - } - - ItemStack drop = chooseDrop(possibleDrops); - - // It's possible that chooseDrop returns null if the sum of probability in possibleDrops is inferior than 100 - if (drop == null) { - return; - } - - // Extra processing depending on the mob and drop type - switch (mob.getType()) { - case SHEEP: - Sheep sheep = (Sheep) mob; - - if (drop.getType() == Material.WOOL) { - if (sheep.isSheared()) { - return; - } - - // TODO: Find a cleaner way to do this, maybe by using Sheep.getColor().getWoolData() (available since 1.4.7-R0.1) - Wool wool = (Wool) drop.getData(); - - wool.setColor(sheep.getColor()); - drop.setDurability(wool.getData()); - sheep.setSheared(true); - } - break; - - case SKELETON: - Skeleton skeleton = (Skeleton) mob; - - if (skeleton.getSkeletonType() == SkeletonType.WITHER) { - switch (drop.getType()) { - case SKULL_ITEM: - drop.setDurability((short) 1); - break; - case ARROW: - drop.setType(Material.COAL); - break; - default: - break; - } - } - break; - - default: - break; - } - - Misc.dropItem(mob.getLocation(), drop); - CombatTools.dealDamage(mob, 1); // We may want to base the damage on the entity max health - } - - /** - * Finds the possible drops of an entity - * - * @param mob Targeted entity - * @param possibleDrops List of ItemStack that can be dropped - */ - private static void findPossibleDrops(LivingEntity mob, Map possibleDrops) { - switch (mob.getType()) { - case BLAZE: - possibleDrops.put(new ItemStack(Material.BLAZE_ROD), 100); - break; - case CAVE_SPIDER: - case SPIDER: - possibleDrops.put(new ItemStack(Material.SPIDER_EYE), 50); - possibleDrops.put(new ItemStack(Material.STRING), 50); - break; - case CHICKEN: - possibleDrops.put(new ItemStack(Material.FEATHER), 34); - possibleDrops.put(new ItemStack(Material.RAW_CHICKEN), 33); - possibleDrops.put(new ItemStack(Material.EGG), 33); - break; - case COW: - possibleDrops.put(new ItemStack(Material.MILK_BUCKET), 2); - possibleDrops.put(new ItemStack(Material.LEATHER), 49); - possibleDrops.put(new ItemStack(Material.RAW_BEEF), 49); - break; - case CREEPER: - possibleDrops.put(new ItemStack(Material.SKULL_ITEM, 1, (short) 4), 1); - possibleDrops.put(new ItemStack(Material.SULPHUR), 99); - break; - case ENDERMAN: - possibleDrops.put(new ItemStack(Material.ENDER_PEARL), 100); - break; - case GHAST: - possibleDrops.put(new ItemStack(Material.SULPHUR), 50); - possibleDrops.put(new ItemStack(Material.GHAST_TEAR), 50); - break; - case IRON_GOLEM: - possibleDrops.put(new ItemStack(Material.PUMPKIN), 3); - possibleDrops.put(new ItemStack(Material.IRON_INGOT), 12); - possibleDrops.put(new ItemStack(Material.RED_ROSE), 85); - break; - case MAGMA_CUBE: - possibleDrops.put(new ItemStack(Material.MAGMA_CREAM), 100); - break; - case MUSHROOM_COW: - possibleDrops.put(new ItemStack(Material.MILK_BUCKET), 5); - possibleDrops.put(new ItemStack(Material.MUSHROOM_SOUP), 5); - possibleDrops.put(new ItemStack(Material.LEATHER), 30); - possibleDrops.put(new ItemStack(Material.RAW_BEEF), 30); - possibleDrops.put(new ItemStack(Material.RED_MUSHROOM, Misc.getRandom().nextInt(3) + 1), 30); - break; - case PIG: - possibleDrops.put(new ItemStack(Material.PORK), 100); - break; - case PIG_ZOMBIE: - possibleDrops.put(new ItemStack(Material.ROTTEN_FLESH), 50); - possibleDrops.put(new ItemStack(Material.GOLD_NUGGET), 50); - break; - case SHEEP: - possibleDrops.put(new ItemStack(Material.WOOL, Misc.getRandom().nextInt(6) + 1), 100); - break; - case SKELETON: - possibleDrops.put(new ItemStack(Material.SKULL_ITEM, 1, (short) 0), 2); - possibleDrops.put(new ItemStack(Material.BONE), 49); - possibleDrops.put(new ItemStack(Material.ARROW, Misc.getRandom().nextInt(3) + 1), 49); - break; - case SLIME: - possibleDrops.put(new ItemStack(Material.SLIME_BALL), 100); - break; - case SNOWMAN: - possibleDrops.put(new ItemStack(Material.PUMPKIN), 3); - possibleDrops.put(new ItemStack(Material.SNOW_BALL, Misc.getRandom().nextInt(4) + 1), 97); - break; - case SQUID: - possibleDrops.put(new ItemStack(Material.INK_SACK, 1, DyeColor.BLACK.getDyeData()), 100); - break; - case WITCH: - possibleDrops.put(new Potion(PotionType.INSTANT_HEAL).toItemStack(1), 1); - possibleDrops.put(new Potion(PotionType.FIRE_RESISTANCE).toItemStack(1), 1); - possibleDrops.put(new Potion(PotionType.SPEED).toItemStack(1), 1); - possibleDrops.put(new ItemStack(Material.GLASS_BOTTLE), 9); - possibleDrops.put(new ItemStack(Material.GLOWSTONE_DUST), 13); - possibleDrops.put(new ItemStack(Material.SULPHUR), 12); - possibleDrops.put(new ItemStack(Material.REDSTONE), 13); - possibleDrops.put(new ItemStack(Material.SPIDER_EYE), 12); - possibleDrops.put(new ItemStack(Material.STICK), 13); - possibleDrops.put(new ItemStack(Material.SUGAR), 12); - possibleDrops.put(new ItemStack(Material.POTION), 13); - break; - case ZOMBIE: - possibleDrops.put(new ItemStack(Material.SKULL_ITEM, 1, (short) 2), 2); - possibleDrops.put(new ItemStack(Material.ROTTEN_FLESH), 98); - break; - default: - return; - } - } - - /** - * Randomly chooses a drop among the list - * - * @param possibleDrops List of ItemStack that can be dropped - * @return Chosen ItemStack - */ - private static ItemStack chooseDrop(Map possibleDrops) { - int dropProbability = Misc.getRandom().nextInt(100); - int cumulatedProbability = 0; - - for (Entry entry : possibleDrops.entrySet()) { - cumulatedProbability += entry.getValue(); - - if (dropProbability < cumulatedProbability) { - return entry.getKey(); - } - } - - return null; - } - - /** - * Gets the Shake Mob probability for a given skill level - * - * @param skillLevel Fishing skill level - * @return Shake Mob probability - */ - public static int getShakeProbability(int skillLevel) { - for (Tier tier : Tier.values()) { - if (skillLevel >= tier.getLevel()) { - return tier.getShakeChance(); - } - } - - return 0; - } -}