Finish off FishingManager.

This commit is contained in:
GJ 2013-02-26 23:31:00 -05:00
parent 90368d4ece
commit 0bdd5b219f
5 changed files with 241 additions and 251 deletions

View File

@ -27,7 +27,6 @@ import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.chat.ChatManager; import com.gmail.nossr50.chat.ChatManager;
import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.McMMOPlayer; import com.gmail.nossr50.datatypes.McMMOPlayer;
import com.gmail.nossr50.datatypes.PlayerProfile; 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.Party;
import com.gmail.nossr50.party.ShareHandler; import com.gmail.nossr50.party.ShareHandler;
import com.gmail.nossr50.skills.SkillManagerStore; 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.fishing.FishingManager;
import com.gmail.nossr50.skills.herbalism.Herbalism; import com.gmail.nossr50.skills.herbalism.Herbalism;
import com.gmail.nossr50.skills.mining.BlastMining; import com.gmail.nossr50.skills.mining.BlastMining;
@ -166,8 +164,6 @@ public class PlayerListener implements Listener {
} }
FishingManager fishingManager = SkillManagerStore.getInstance().getFishingManager(player.getName()); FishingManager fishingManager = SkillManagerStore.getInstance().getFishingManager(player.getName());
McMMOPlayer mcMMOPlayer = Users.getPlayer(player);
int skillLevel = mcMMOPlayer.getProfile().getSkillLevel(SkillType.FISHING);
switch (event.getState()) { switch (event.getState()) {
case CAUGHT_FISH: case CAUGHT_FISH:
@ -181,8 +177,8 @@ public class PlayerListener implements Listener {
case CAUGHT_ENTITY: case CAUGHT_ENTITY:
Entity entity = event.getCaught(); Entity entity = event.getCaught();
if (entity instanceof LivingEntity && skillLevel >= AdvancedConfig.getInstance().getShakeUnlockLevel() && Permissions.shake(player)) { if (fishingManager.canShake(entity)) {
Fishing.beginShakeMob(player, (LivingEntity) entity, skillLevel); fishingManager.shakeCheck((LivingEntity) entity);
} }
break; break;

View File

@ -1,9 +1,17 @@
package com.gmail.nossr50.skills.fishing; 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.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.config.AdvancedConfig;
import com.gmail.nossr50.util.Misc;
public final class Fishing { public final class Fishing {
// The order of the values is extremely important, a few methods depend on it to work properly // 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() {} private Fishing() {}
/** /**
* Begins Shake Mob ability * Finds the possible drops of an entity
* *
* @param player Player using the ability * @param target Targeted entity
* @param mob Targeted mob * @param possibleDrops List of ItemStack that can be dropped
* @param skillLevel Fishing level of the player
*/ */
public static void beginShakeMob(Player player, LivingEntity mob, int skillLevel) { protected static void findPossibleDrops(LivingEntity target, Map<ItemStack, Integer> possibleDrops) {
ShakeMob.process(player, mob, skillLevel); 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<ItemStack, Integer> possibleDrops) {
int dropProbability = Misc.getRandom().nextInt(100);
int cumulatedProbability = 0;
for (Entry<ItemStack, Integer> entry : possibleDrops.entrySet()) {
cumulatedProbability += entry.getValue();
if (dropProbability < cumulatedProbability) {
return entry.getKey();
}
}
return null;
} }
} }

View File

@ -42,7 +42,7 @@ public class FishingCommand extends SkillCommand {
magicChanceLucky = treasureHunterStrings[1]; magicChanceLucky = treasureHunterStrings[1];
//SHAKE //SHAKE
String[] shakeStrings = calculateAbilityDisplayValues(ShakeMob.getShakeProbability((int) skillValue)); String[] shakeStrings = calculateAbilityDisplayValues(SkillManagerStore.getInstance().getFishingManager(player.getName()).getShakeProbability());
shakeChance = shakeStrings[0]; shakeChance = shakeStrings[0];
shakeChanceLucky = shakeStrings[1]; shakeChanceLucky = shakeStrings[1];

View File

@ -2,11 +2,19 @@ package com.gmail.nossr50.skills.fishing;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; 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.inventory.ItemStack;
import com.gmail.nossr50.config.AdvancedConfig; 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.locale.LocaleLoader;
import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.skills.SkillManager;
import com.gmail.nossr50.skills.fishing.Fishing.Tier; 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.SkillTools;
import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.skills.utilities.SkillType;
import com.gmail.nossr50.util.ItemChecks; import com.gmail.nossr50.util.ItemChecks;
@ -24,11 +33,15 @@ import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
public class FishingManager extends SkillManager { public class FishingManager extends SkillManager {
public FishingManager(McMMOPlayer mcMMOPlayer) { public FishingManager(McMMOPlayer mcMMOPlayer) {
super(mcMMOPlayer, SkillType.FISHING); 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 * Handle the Fisherman's Diet ability
* *
@ -82,6 +95,71 @@ public class FishingManager extends SkillManager {
return experience * getVanillaXpMultiplier(); 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<ItemStack, Integer> possibleDrops = new HashMap<ItemStack, Integer>();
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 * Process the Treasure Hunter ability for Fishing
* *
@ -190,6 +268,22 @@ public class FishingManager extends SkillManager {
return 0; 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 * Gets the vanilla XP multiplier

View File

@ -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<ItemStack, Integer> possibleDrops = new HashMap<ItemStack, Integer>();
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<ItemStack, Integer> 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<ItemStack, Integer> possibleDrops) {
int dropProbability = Misc.getRandom().nextInt(100);
int cumulatedProbability = 0;
for (Entry<ItemStack, Integer> 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;
}
}