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

338 lines
11 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;
import java.util.Map;
2012-03-26 17:04:17 +02:00
import java.util.Random;
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.event.player.PlayerFishEvent;
import org.bukkit.inventory.ItemStack;
2011-12-01 18:46:46 +01:00
import org.bukkit.material.Wool;
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-03-26 17:04:17 +02:00
private static Random random = new Random();
/**
* Get the player's current fishing loot tier.
*
* @param PP The profile of the player
* @return the player's current fishing rank
*/
public static int getFishingLootTier(PlayerProfile PP) {
int level = PP.getSkillLevel(SkillType.FISHING);
int fishingTier;
if (level >= Config.getInstance().getFishingTierLevelsTier5()) {
fishingTier = 5;
}
else if (level >= Config.getInstance().getFishingTierLevelsTier4()) {
fishingTier = 4;
}
else if (level >= Config.getInstance().getFishingTierLevelsTier3()) {
fishingTier = 3;
}
else if (level >= Config.getInstance().getFishingTierLevelsTier2()) {
fishingTier = 2;
}
else {
fishingTier = 1;
}
return fishingTier;
}
/**
* Get item results from Fishing.
*
* @param player The player that was fishing
* @param event The event to modify
*/
private static void getFishingResults(Player player, PlayerFishEvent event) {
PlayerProfile PP = Users.getProfile(player);
List<FishingTreasure> rewards = new ArrayList<FishingTreasure>();
Item theCatch = (Item) event.getCaught();
switch (getFishingLootTier(PP)) {
case 1:
2012-05-17 16:26:21 +02:00
rewards = TreasuresConfig.getInstance().fishingRewardsTier1;
break;
case 2:
2012-05-17 16:26:21 +02:00
rewards = TreasuresConfig.getInstance().fishingRewardsTier2;
break;
case 3:
2012-05-17 16:26:21 +02:00
rewards = TreasuresConfig.getInstance().fishingRewardsTier3;
break;
case 4:
2012-05-17 16:26:21 +02:00
rewards = TreasuresConfig.getInstance().fishingRewardsTier4;
break;
case 5:
2012-05-17 16:26:21 +02:00
rewards = TreasuresConfig.getInstance().fishingRewardsTier5;
break;
default:
break;
}
if (Config.getInstance().getFishingDropsEnabled() && rewards.size() > 0 && Permissions.getInstance().fishingTreasures(player)) {
2012-04-24 15:21:21 +02:00
FishingTreasure treasure = rewards.get(random.nextInt(rewards.size()));
if (random.nextDouble() * 100 <= treasure.getDropChance()) {
Users.getProfile(player).addXP(SkillType.FISHING, treasure.getXp());
2012-04-24 15:21:21 +02:00
theCatch.setItemStack(treasure.getDrop());
}
}
else {
theCatch.setItemStack(new ItemStack(Material.RAW_FISH));
}
short maxDurability = theCatch.getItemStack().getType().getMaxDurability();
if (maxDurability > 0) {
theCatch.getItemStack().setDurability((short) (random.nextInt(maxDurability))); //Change durability to random value
}
Skills.xpProcessing(player, PP, SkillType.FISHING, Config.getInstance().getFishingBaseXP());
}
/**
* Process results from Fishing.
*
* @param event The event to modify
*/
public static void processResults(PlayerFishEvent event) {
Player player = event.getPlayer();
PlayerProfile PP = Users.getProfile(player);
getFishingResults(player, event);
Item theCatch = (Item) event.getCaught();
if (theCatch.getItemStack().getType() != Material.RAW_FISH) {
final int ENCHANTMENT_CHANCE = 10;
boolean enchanted = false;
ItemStack fishingResults = theCatch.getItemStack();
2012-04-27 11:47:11 +02:00
player.sendMessage(LocaleLoader.getString("Fishing.ItemFound"));
if (ItemChecks.isArmor(fishingResults) || ItemChecks.isTool(fishingResults)) {
if (random.nextInt(100) <= ENCHANTMENT_CHANCE && Permissions.getInstance().fishingMagic(player)) {
for (Enchantment newEnchant : Enchantment.values()) {
if (newEnchant.canEnchantItem(fishingResults)) {
Map<Enchantment, Integer> resultEnchantments = fishingResults.getEnchantments();
for (Enchantment oldEnchant : resultEnchantments.keySet()) {
if (oldEnchant.conflictsWith(newEnchant)) {
return;
}
}
/* Actual chance to have an enchantment is related to your fishing skill */
2012-03-26 17:04:17 +02:00
if (random.nextInt(15) < Fishing.getFishingLootTier(PP)) {
enchanted = true;
2012-03-26 17:04:17 +02:00
int randomEnchantLevel = random.nextInt(newEnchant.getMaxLevel()) + 1;
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.
*
* @param event The event to modify
*/
public static void shakeMob(PlayerFishEvent event) {
2012-03-26 17:04:17 +02:00
final int DROP_NUMBER = random.nextInt(100);
LivingEntity le = (LivingEntity) event.getCaught();
EntityType type = le.getType();
Location loc = le.getLocation();
switch (type) {
case BLAZE:
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.BLAZE_ROD));
break;
case CAVE_SPIDER:
if (DROP_NUMBER > 50) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.SPIDER_EYE));
}
else {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.STRING));
}
break;
case CHICKEN:
if (DROP_NUMBER > 66) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.FEATHER));
}
else if (DROP_NUMBER > 33) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.RAW_CHICKEN));
}
else {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.EGG));
}
break;
case COW:
if (DROP_NUMBER > 99) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.MILK_BUCKET));
}
else if (DROP_NUMBER > 50) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.LEATHER));
}
else {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.RAW_BEEF));
}
break;
case CREEPER:
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.SULPHUR));
break;
case ENDERMAN:
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.ENDER_PEARL));
break;
case GHAST:
if (DROP_NUMBER > 50) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.SULPHUR));
}
else {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.GHAST_TEAR));
}
break;
case MAGMA_CUBE:
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.MAGMA_CREAM));
break;
case MUSHROOM_COW:
if (DROP_NUMBER > 99) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.MILK_BUCKET));
}
else if (DROP_NUMBER > 98) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.MUSHROOM_SOUP));
}
else if (DROP_NUMBER > 66) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.LEATHER));
}
else if (DROP_NUMBER > 33) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.RAW_BEEF));
}
else {
2012-05-18 19:40:21 +02:00
Misc.dropItems(loc, new ItemStack(Material.RED_MUSHROOM), 3);
}
break;
case PIG:
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.PORK));
break;
case PIG_ZOMBIE:
if (DROP_NUMBER > 50) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.ROTTEN_FLESH));
}
else {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.GOLD_NUGGET));
}
break;
case SHEEP:
Sheep sheep = (Sheep) le;
2012-06-05 15:57:10 +02:00
if (!sheep.isSheared()) {
Wool wool = new Wool();
wool.setColor(sheep.getColor());
ItemStack theWool = wool.toItemStack();
2012-03-26 17:04:17 +02:00
theWool.setAmount(1 + random.nextInt(6));
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, theWool);
sheep.setSheared(true);
}
break;
case SKELETON:
if (DROP_NUMBER > 50) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.BONE));
}
else {
2012-05-18 19:40:21 +02:00
Misc.dropItems(loc, new ItemStack(Material.ARROW), 3);
}
break;
case SLIME:
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.SLIME_BALL));
break;
case SNOWMAN:
if (DROP_NUMBER > 99) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.PUMPKIN));
}
else {
2012-05-18 19:40:21 +02:00
Misc.dropItems(loc, new ItemStack(Material.SNOW_BALL), 5);
}
break;
case SPIDER:
if (DROP_NUMBER > 50) {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.SPIDER_EYE));
}
else {
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.STRING));
}
break;
case SQUID:
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.INK_SACK, 1, (short) 0, (byte) 0x0));
break;
case ZOMBIE:
2012-05-18 19:40:21 +02:00
Misc.dropItem(loc, new ItemStack(Material.ROTTEN_FLESH));
break;
default:
break;
}
Combat.dealDamage(le, 1);
}
}