mcMMO/src/main/java/com/gmail/nossr50/skills/gathering/Fishing.java

483 lines
18 KiB
Java
Raw Normal View History

2012-05-01 19:58:47 +02:00
package com.gmail.nossr50.skills.gathering;
import java.util.ArrayList;
import java.util.List;
2013-01-08 17:31:07 +01:00
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.Material;
2011-12-01 18:46:46 +01:00
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
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.event.player.PlayerFishEvent;
import org.bukkit.inventory.ItemStack;
2012-12-25 07:01:10 +01:00
import org.bukkit.material.MaterialData;
2011-12-01 18:46:46 +01:00
import org.bukkit.material.Wool;
2013-01-09 00:45:44 +01:00
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionType;
2012-11-21 21:49:54 +01:00
import com.gmail.nossr50.config.AdvancedConfig;
2012-04-26 16:27:57 +02:00
import com.gmail.nossr50.config.Config;
2012-05-17 16:26:21 +02:00
import com.gmail.nossr50.config.TreasuresConfig;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.datatypes.treasure.FishingTreasure;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.Combat;
import com.gmail.nossr50.util.ItemChecks;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;
2012-05-01 19:58:47 +02:00
import com.gmail.nossr50.util.Skills;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.util.Users;
public class Fishing {
2012-11-21 21:49:54 +01:00
static AdvancedConfig advancedConfig = AdvancedConfig.getInstance();
2012-03-26 17:04:17 +02:00
/**
* Get the player's current fishing loot tier.
2012-12-24 22:56:25 +01:00
*
* @param profile
* The profile of the player
* @return the player's current fishing rank
*/
2012-07-03 16:04:04 +02:00
public static int getFishingLootTier(PlayerProfile profile) {
int level = profile.getSkillLevel(SkillType.FISHING);
int fishingTier;
if (level >= advancedConfig.getFishingTierLevelsTier5()) {
fishingTier = 5;
} else if (level >= advancedConfig.getFishingTierLevelsTier4()) {
fishingTier = 4;
} else if (level >= advancedConfig.getFishingTierLevelsTier3()) {
2012-12-24 22:56:25 +01:00
fishingTier = 3;
} else if (level >= advancedConfig.getFishingTierLevelsTier2()) {
2012-12-24 22:56:25 +01:00
fishingTier = 2;
} else {
fishingTier = 1;
}
return fishingTier;
}
/**
* Get item results from Fishing.
2012-12-24 22:56:25 +01:00
*
* @param player
* The player that was fishing
* @param event
* The event to modify
*/
private static void getFishingResults(Player player, PlayerFishEvent event) {
2012-12-24 22:56:25 +01:00
if (player == null)
return;
2012-07-03 16:04:04 +02:00
PlayerProfile profile = Users.getProfile(player);
Item theCatch = (Item) event.getCaught();
if (Config.getInstance().getFishingDropsEnabled() && Permissions.fishingTreasures(player)) {
int skillLevel = profile.getSkillLevel(SkillType.FISHING);
List<FishingTreasure> rewards = new ArrayList<FishingTreasure>();
for (FishingTreasure treasure : TreasuresConfig.getInstance().fishingRewards) {
if (treasure.getDropLevel() <= skillLevel && treasure.getMaxLevel() >= skillLevel) {
rewards.add(treasure);
}
}
if (rewards.size() <= 0) {
return;
}
FishingTreasure foundTreasure = rewards.get(Misc.getRandom().nextInt(rewards.size()));
2012-04-24 15:21:21 +02:00
int randomChance = 100;
2013-01-07 02:52:31 +01:00
if (Permissions.luckyFishing(player)) {
randomChance = (int) (randomChance * 0.75);
}
if (Misc.getRandom().nextDouble() * randomChance <= foundTreasure.getDropChance()) {
Users.getPlayer(player).addXP(SkillType.FISHING, foundTreasure.getXp());
theCatch.setItemStack(foundTreasure.getDrop());
}
}
else {
theCatch.setItemStack(new ItemStack(Material.RAW_FISH));
}
2013-01-03 17:40:22 +01:00
short maxDurability = theCatch.getItemStack().getType().getMaxDurability();
if (maxDurability > 0) {
theCatch.getItemStack().setDurability((short) (Misc.getRandom().nextInt(maxDurability))); // Change durability to random value
}
2013-01-03 17:40:22 +01:00
Skills.xpProcessing(player, profile, SkillType.FISHING, Config.getInstance().getFishingBaseXP());
}
/**
* Process results from Fishing.
2012-12-24 22:56:25 +01:00
*
* @param event
* The event to modify
*/
public static void processResults(PlayerFishEvent event) {
Player player = event.getPlayer();
2012-12-24 22:56:25 +01:00
if (player == null)
return;
2012-07-03 16:04:04 +02:00
PlayerProfile profile = Users.getProfile(player);
getFishingResults(player, event);
Item theCatch = (Item) event.getCaught();
if (theCatch.getItemStack().getType() != Material.RAW_FISH) {
int lootTier = Fishing.getFishingLootTier(profile);
int magicHunterMultiplier = advancedConfig.getFishingMagicMultiplier();
int specificChance = 1;
boolean enchanted = false;
ItemStack fishingResults = theCatch.getItemStack();
2012-04-27 11:47:11 +02:00
player.sendMessage(LocaleLoader.getString("Fishing.ItemFound"));
2012-10-31 20:39:04 +01:00
if (ItemChecks.isEnchantable(fishingResults)) {
int randomChance = 100;
2013-01-07 02:52:31 +01:00
if (Permissions.luckyFishing(player)) {
2012-11-07 13:32:30 +01:00
randomChance = (int) (randomChance * 0.75);
}
if (player.getWorld().hasStorm()) {
randomChance = (int) (randomChance * 0.909);
}
/* CHANCE OF ITEM BEING ENCHANTED
2013-01-10 21:28:02 +01:00
* 5% - Tier 1
* 10% - Tier 2
* 15% - Tier 3
* 20% - Tier 4
* 25% - Tier 5
*/
if (Misc.getRandom().nextInt(randomChance) <= (lootTier * magicHunterMultiplier) && Permissions.fishingMagic(player)) {
for (Enchantment newEnchant : Enchantment.values()) {
if (newEnchant.canEnchantItem(fishingResults)) {
specificChance++;
for (Enchantment oldEnchant : fishingResults.getEnchantments().keySet()) {
if (oldEnchant.conflictsWith(newEnchant))
specificChance--;
continue;
}
/* CHANCE OF GETTING EACH ENCHANTMENT
* 50% - 1st Enchantment
* 33% - 2nd Enchantment
* 25% - 3rd Enchantment
* 20% - 4th Enchantment
* 16.66% - 5th Enchantment
* 14.29% - 6th Enchantment
* 12.5% - 7th Enchantment
* 11.11% - 8th Enchantment
2012-12-24 22:56:25 +01:00
*/
if (Misc.getRandom().nextInt(specificChance) < 1) {
enchanted = true;
int randomEnchantLevel = Misc.getRandom().nextInt(newEnchant.getMaxLevel()) + 1;
2013-01-03 17:40:22 +01:00
if (randomEnchantLevel < newEnchant.getStartLevel()) {
randomEnchantLevel = newEnchant.getStartLevel();
}
fishingResults.addEnchantment(newEnchant, randomEnchantLevel);
}
}
}
}
}
if (enchanted) {
2012-04-27 11:47:11 +02:00
player.sendMessage(LocaleLoader.getString("Fishing.MagicFound"));
}
}
}
/**
* Shake a mob, have them drop an item.
2012-12-24 22:56:25 +01:00
*
* @param event
* The event to modify
*/
public static void shakeMob(PlayerFishEvent event) {
int randomChance = 100;
2013-01-07 02:52:31 +01:00
if (Permissions.luckyFishing(event.getPlayer())) {
2012-11-07 13:32:30 +01:00
randomChance = (int) (randomChance * 1.25);
}
final Player player = event.getPlayer();
final PlayerProfile profile = Users.getProfile(player);
int lootTier = getFishingLootTier(profile);
2012-06-05 15:57:10 +02:00
int dropChance = getShakeChance(lootTier);
2013-01-09 02:28:09 +01:00
2013-01-07 02:52:31 +01:00
if (Permissions.luckyFishing(player)) {
2013-01-09 02:28:09 +01:00
// With lucky perk on max level tier, its 100%
dropChance = (int) (dropChance * 1.25);
}
final int DROP_CHANCE = Misc.getRandom().nextInt(100);
final int DROP_NUMBER = Misc.getRandom().nextInt(randomChance) + 1;
LivingEntity le = (LivingEntity) event.getCaught();
EntityType type = le.getType();
Location location = le.getLocation();
if (DROP_CHANCE < dropChance) {
switch (type) {
case BLAZE:
Misc.dropItem(location, new ItemStack(Material.BLAZE_ROD));
break;
case CAVE_SPIDER:
if (DROP_NUMBER > 50) {
Misc.dropItem(location, new ItemStack(Material.SPIDER_EYE));
} else {
Misc.dropItem(location, new ItemStack(Material.STRING));
}
break;
case CHICKEN:
if (DROP_NUMBER > 66) {
Misc.dropItem(location, new ItemStack(Material.FEATHER));
} else if (DROP_NUMBER > 33) {
Misc.dropItem(location, new ItemStack(Material.RAW_CHICKEN));
} else {
Misc.dropItem(location, new ItemStack(Material.EGG));
}
break;
case COW:
if (DROP_NUMBER > 95) {
Misc.dropItem(location, new ItemStack(Material.MILK_BUCKET));
} else if (DROP_NUMBER > 50) {
Misc.dropItem(location, new ItemStack(Material.LEATHER));
} else {
Misc.dropItem(location, new ItemStack(Material.RAW_BEEF));
}
break;
case CREEPER:
2012-12-23 00:00:29 +01:00
if (DROP_NUMBER > 97) {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.SKULL_ITEM, 1, (short) 4));
} else {
Misc.dropItem(location, new ItemStack(Material.SULPHUR));
}
break;
case ENDERMAN:
Misc.dropItem(location, new ItemStack(Material.ENDER_PEARL));
break;
case GHAST:
if (DROP_NUMBER > 50) {
Misc.dropItem(location, new ItemStack(Material.SULPHUR));
} else {
Misc.dropItem(location, new ItemStack(Material.GHAST_TEAR));
}
break;
case IRON_GOLEM:
if (DROP_NUMBER > 97) {
Misc.dropItem(location, new ItemStack(Material.PUMPKIN));
} else if (DROP_NUMBER > 85) {
Misc.dropItem(location, new ItemStack(Material.IRON_INGOT));
} else {
Misc.dropItem(location, new ItemStack(Material.RED_ROSE));
}
break;
case MAGMA_CUBE:
Misc.dropItem(location, new ItemStack(Material.MAGMA_CREAM));
break;
case MUSHROOM_COW:
if (DROP_NUMBER > 95) {
Misc.dropItem(location, new ItemStack(Material.MILK_BUCKET));
} else if (DROP_NUMBER > 90) {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.MUSHROOM_SOUP));
} else if (DROP_NUMBER > 60) {
Misc.dropItem(location, new ItemStack(Material.LEATHER));
} else if (DROP_NUMBER > 30) {
Misc.dropItem(location, new ItemStack(Material.RAW_BEEF));
} else {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.RED_MUSHROOM));
Misc.randomDropItems(location, new ItemStack(Material.RED_MUSHROOM), 50, 2);
}
break;
case PIG:
Misc.dropItem(location, new ItemStack(Material.PORK));
break;
case PIG_ZOMBIE:
if (DROP_NUMBER > 50) {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.ROTTEN_FLESH));
} else {
Misc.dropItem(location, new ItemStack(Material.GOLD_NUGGET));
}
break;
case SHEEP:
final Sheep sheep = (Sheep) le;
if (!sheep.isSheared()) {
final Wool wool = new Wool();
wool.setColor(sheep.getColor());
final ItemStack theWool = wool.toItemStack();
theWool.setAmount(1 + Misc.getRandom().nextInt(6));
Misc.dropItem(location, theWool);
sheep.setSheared(true);
}
break;
case SKELETON:
2012-12-24 22:56:25 +01:00
if (((Skeleton) le).getSkeletonType() == SkeletonType.WITHER) {
2012-12-23 00:00:29 +01:00
if (DROP_NUMBER > 97) {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.SKULL_ITEM, 1, (short) 1));
} else if (DROP_NUMBER > 50) {
Misc.dropItem(location, new ItemStack(Material.BONE));
} else {
Misc.dropItem(location, new ItemStack(Material.COAL));
2012-12-25 07:01:10 +01:00
Misc.randomDropItems(location, new ItemStack(Material.COAL), 50, 2);
}
} else {
2012-12-23 00:00:29 +01:00
if (DROP_NUMBER > 97) {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.SKULL_ITEM));
} else if (DROP_NUMBER > 50) {
Misc.dropItem(location, new ItemStack(Material.BONE));
} else {
Misc.dropItem(location, new ItemStack(Material.ARROW));
2012-12-25 07:01:10 +01:00
Misc.randomDropItems(location, new ItemStack(Material.ARROW), 50, 2);
}
}
break;
case SLIME:
Misc.dropItem(location, new ItemStack(Material.SLIME_BALL));
break;
case SNOWMAN:
if (DROP_NUMBER > 97) {
Misc.dropItem(location, new ItemStack(Material.PUMPKIN));
} else {
Misc.dropItem(location, new ItemStack(Material.SNOW_BALL));
2012-12-25 07:01:10 +01:00
Misc.randomDropItems(location, new ItemStack(Material.SNOW_BALL), 50, 4);
}
break;
case SPIDER:
if (DROP_NUMBER > 50) {
Misc.dropItem(location, new ItemStack(Material.SPIDER_EYE));
} else {
Misc.dropItem(location, new ItemStack(Material.STRING));
}
break;
2013-01-09 00:27:59 +01:00
case SQUID:
ItemStack item;
try {
2013-01-14 03:23:11 +01:00
item = (new MaterialData(Material.INK_SACK, DyeColor.BLACK.getDyeData())).toItemStack(1);
}
2013-01-09 00:52:50 +01:00
catch(Exception e) {
item = (new MaterialData(Material.INK_SACK, (byte) 0)).toItemStack(1);
}
catch(NoSuchMethodError e) {
item = (new MaterialData(Material.INK_SACK, (byte) 0)).toItemStack(1);
2013-01-09 00:27:59 +01:00
}
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, item);
break;
case WITCH:
final int DROP_NUMBER_2 = Misc.getRandom().nextInt(randomChance) + 1;
if (DROP_NUMBER > 95) {
if (DROP_NUMBER_2 > 66) {
2013-01-09 00:45:44 +01:00
Misc.dropItem(location, new Potion(PotionType.INSTANT_HEAL).toItemStack(1));
} else if (DROP_NUMBER_2 > 33) {
2013-01-09 00:45:44 +01:00
Misc.dropItem(location, new Potion(PotionType.FIRE_RESISTANCE).toItemStack(1));
} else {
2013-01-09 00:45:44 +01:00
Misc.dropItem(location, new Potion(PotionType.SPEED).toItemStack(1));
}
} else {
if (DROP_NUMBER_2 > 88) {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.GLASS_BOTTLE));
} else if (DROP_NUMBER_2 > 75) {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.GLOWSTONE_DUST));
} else if (DROP_NUMBER_2 > 63) {
Misc.dropItem(location, new ItemStack(Material.SULPHUR));
} else if (DROP_NUMBER_2 > 50) {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.REDSTONE));
} else if (DROP_NUMBER_2 > 38) {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.SPIDER_EYE));
} else if (DROP_NUMBER_2 > 25) {
Misc.dropItem(location, new ItemStack(Material.STICK));
} else if (DROP_NUMBER_2 > 13) {
Misc.dropItem(location, new ItemStack(Material.SUGAR));
} else {
Misc.dropItem(location, new ItemStack(Material.POTION));
}
}
break;
case ZOMBIE:
2012-12-23 00:00:29 +01:00
if (DROP_NUMBER > 97) {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.SKULL_ITEM, 1, (short) 2));
} else {
2012-12-25 07:01:10 +01:00
Misc.dropItem(location, new ItemStack(Material.ROTTEN_FLESH));
}
break;
default:
break;
}
}
Combat.dealDamage(le, 1);
}
2012-12-24 22:56:25 +01:00
/**
* Gets chance of shake success.
2012-12-24 22:56:25 +01:00
*
* @param rank
* Treasure hunter rank
* @return The chance of a successful shake
*/
public static int getShakeChance(int lootTier) {
switch (lootTier) {
case 1:
return advancedConfig.getShakeChanceRank1();
case 2:
return advancedConfig.getShakeChanceRank2();
case 3:
return advancedConfig.getShakeChanceRank3();
case 4:
return advancedConfig.getShakeChanceRank4();
case 5:
return advancedConfig.getShakeChanceRank5();
default:
return 10;
}
2012-12-24 22:56:25 +01:00
}
}