123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- package com.gmail.nossr50.skills.fishing;
- import com.gmail.nossr50.config.AdvancedConfig;
- import com.gmail.nossr50.config.Config;
- import com.gmail.nossr50.config.experience.ExperienceConfig;
- import com.gmail.nossr50.config.treasure.TreasureConfig;
- import com.gmail.nossr50.datatypes.experience.XPGainReason;
- import com.gmail.nossr50.datatypes.interactions.NotificationType;
- import com.gmail.nossr50.datatypes.player.McMMOPlayer;
- import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
- import com.gmail.nossr50.datatypes.skills.SubSkillType;
- import com.gmail.nossr50.datatypes.treasure.EnchantmentTreasure;
- import com.gmail.nossr50.datatypes.treasure.FishingTreasure;
- import com.gmail.nossr50.datatypes.treasure.Rarity;
- import com.gmail.nossr50.datatypes.treasure.ShakeTreasure;
- import com.gmail.nossr50.events.skills.fishing.McMMOPlayerFishingTreasureEvent;
- import com.gmail.nossr50.events.skills.fishing.McMMOPlayerShakeEvent;
- import com.gmail.nossr50.skills.SkillManager;
- import com.gmail.nossr50.util.*;
- import com.gmail.nossr50.util.player.NotificationManager;
- import com.gmail.nossr50.util.random.RandomChanceUtil;
- import com.gmail.nossr50.util.skills.CombatUtils;
- import com.gmail.nossr50.util.skills.RankUtils;
- import com.gmail.nossr50.util.skills.SkillUtils;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.block.BlockFace;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.entity.*;
- import org.bukkit.event.entity.EntityDamageEvent;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.PlayerInventory;
- import org.bukkit.inventory.meta.SkullMeta;
- import java.util.*;
- public class FishingManager extends SkillManager {
- private final long FISHING_COOLDOWN_SECONDS = 1000L;
- private int fishingTries = 0;
- private long fishingTimestamp = 0L;
- private Location fishingTarget;
- private Item fishingCatch;
- private Location hookLocation;
- public FishingManager(McMMOPlayer mcMMOPlayer) {
- super(mcMMOPlayer, PrimarySkillType.FISHING);
- }
- public boolean canShake(Entity target) {
- return target instanceof LivingEntity && RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.FISHING_SHAKE) && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.FISHING_SHAKE);
- }
- public boolean canMasterAngler() {
- return getSkillLevel() >= RankUtils.getUnlockLevel(SubSkillType.FISHING_MASTER_ANGLER) && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.FISHING_MASTER_ANGLER);
- }
- public boolean exploitPrevention() {
- Block targetBlock = getPlayer().getTargetBlock(BlockUtils.getTransparentBlocks(), 100);
- if (!targetBlock.isLiquid()) {
- return false;
- }
- long currentTime = System.currentTimeMillis();
- boolean hasFished = (currentTime < fishingTimestamp + (FISHING_COOLDOWN_SECONDS * 10));
- if(hasFished == true)
- fishingTimestamp = currentTime;
- Location targetLocation = targetBlock.getLocation();
- boolean sameTarget = (fishingTarget != null && fishingTarget.equals(targetLocation));
- return hasFished || sameTarget;
- }
- public void setFishingTarget() {
- getPlayer().getTargetBlock(BlockUtils.getTransparentBlocks(), 100);
- }
- public boolean canIceFish(Block block) {
- if (getSkillLevel() < RankUtils.getUnlockLevel(SubSkillType.FISHING_ICE_FISHING)) {
- return false;
- }
- if (block.getType() != Material.ICE) {
- return false;
- }
- // Make sure this is a body of water, not just a block of ice.
- if (!Fishing.iceFishingBiomes.contains(block.getBiome()) && (block.getRelative(BlockFace.DOWN, 3).getType() != Material.WATER)) {
- return false;
- }
- Player player = getPlayer();
- if (!Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.FISHING_ICE_FISHING)) {
- return false;
- }
- return EventUtils.simulateBlockBreak(block, player, false);
- }
- /**
- * Gets the loot tier
- *
- * @return the loot tier
- */
- public int getLootTier() {
- return RankUtils.getRank(getPlayer(), SubSkillType.FISHING_TREASURE_HUNTER);
- }
- protected double getShakeChance() {
- return AdvancedConfig.getInstance().getShakeChance(getLootTier());
- }
- protected int getVanillaXPBoostModifier() {
- return AdvancedConfig.getInstance().getFishingVanillaXPModifier(getLootTier());
- }
- /**
- * Gets the Shake Mob probability
- *
- * @return Shake Mob probability
- */
- public double getShakeProbability() {
- return getShakeChance();
- }
- /**
- * Handle the Fisherman's Diet ability
- *
- * @param rankChange The # of levels to change rank for the food
- * @param eventFoodLevel The initial change in hunger from the event
- *
- * @return the modified change in hunger for the event
- */
- public int handleFishermanDiet(int rankChange, int eventFoodLevel) {
- return SkillUtils.handleFoodSkills(getPlayer(), eventFoodLevel, SubSkillType.FISHING_FISHERMANS_DIET);
- }
- public void iceFishing(FishHook hook, Block block) {
- // Make a hole
- block.setType(Material.WATER);
- for (int x = -1; x <= 1; x++) {
- for (int z = -1; z <= 1; z++) {
- Block relative = block.getRelative(x, 0, z);
- if (relative.getType() == Material.ICE) {
- relative.setType(Material.WATER);
- }
- }
- }
- // Recast in the new spot
- EventUtils.callFakeFishEvent(getPlayer(), hook);
- }
- public void masterAngler(FishHook hook) {
- Player player = getPlayer();
- Location location = hook.getLocation();
- double biteChance = hook.getBiteChance();
- hookLocation = location;
- if (Fishing.masterAnglerBiomes.contains(location.getBlock().getBiome())) {
- biteChance = biteChance * AdvancedConfig.getInstance().getMasterAnglerBiomeModifier();
- }
- if (player.isInsideVehicle() && player.getVehicle().getType() == EntityType.BOAT) {
- biteChance = biteChance * AdvancedConfig.getInstance().getMasterAnglerBoatModifier();
- }
- hook.setBiteChance(Math.min(biteChance, 1.0));
- }
- /**
- * Process the results from a successful fishing trip
- *
- * @param fishingCatch The {@link Item} initially caught
- */
- public void handleFishing(Item fishingCatch) {
- this.fishingCatch = fishingCatch;
- int fishXp = ExperienceConfig.getInstance().getXp(PrimarySkillType.FISHING, fishingCatch.getItemStack().getType());
- int treasureXp = 0;
- Player player = getPlayer();
- FishingTreasure treasure = null;
- if (Config.getInstance().getFishingDropsEnabled() && Permissions.isSubSkillEnabled(player, SubSkillType.FISHING_TREASURE_HUNTER)) {
- treasure = getFishingTreasure();
- this.fishingCatch = null;
- }
- if (treasure != null) {
- ItemStack treasureDrop = treasure.getDrop().clone(); // Not cloning is bad, m'kay?
- Map<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>();
- if (Permissions.isSubSkillEnabled(player, SubSkillType.FISHING_MAGIC_HUNTER) && ItemUtils.isEnchantable(treasureDrop)) {
- enchants = handleMagicHunter(treasureDrop);
- }
- McMMOPlayerFishingTreasureEvent event = EventUtils.callFishingTreasureEvent(player, treasureDrop, treasure.getXp(), enchants);
- if (!event.isCancelled()) {
- treasureDrop = event.getTreasure();
- treasureXp = event.getXp();
- }
- else {
- treasureDrop = null;
- treasureXp = 0;
- }
- // Drop the original catch at the feet of the player and set the treasure as the real catch
- if (treasureDrop != null) {
- boolean enchanted = false;
- if (!enchants.isEmpty()) {
- treasureDrop.addUnsafeEnchantments(enchants);
- enchanted = true;
- }
- if (enchanted) {
- NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Fishing.Ability.TH.MagicFound");
- }
- if (Config.getInstance().getFishingExtraFish()) {
- Misc.dropItem(player.getEyeLocation(), fishingCatch.getItemStack());
- }
- fishingCatch.setItemStack(treasureDrop);
- }
- }
- applyXpGain(fishXp + treasureXp, XPGainReason.PVE);
- }
- /**
- * Handle the vanilla XP boost for Fishing
- *
- * @param experience The amount of experience initially awarded by the event
- *
- * @return the modified event damage
- */
- public int handleVanillaXpBoost(int experience) {
- return experience * getVanillaXpMultiplier();
- }
- public Location getHookLocation() {
- return hookLocation;
- }
- /**
- * Handle the Shake ability
- *
- * @param target The {@link LivingEntity} affected by the ability
- */
- public void shakeCheck(LivingEntity target) {
- fishingTries--; // Because autoclicking to shake is OK.
- if (RandomChanceUtil.checkRandomChanceExecutionSuccess(getPlayer(), SubSkillType.FISHING_SHAKE, true)) {
- List<ShakeTreasure> possibleDrops = Fishing.findPossibleDrops(target);
- if (possibleDrops == null || 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 PLAYER:
- Player targetPlayer = (Player) target;
- switch (drop.getType()) {
- case PLAYER_HEAD:
- drop.setDurability((short) 3);
- SkullMeta skullMeta = (SkullMeta) drop.getItemMeta();
- skullMeta.setOwningPlayer(targetPlayer);
- drop.setItemMeta(skullMeta);
- break;
- case BEDROCK:
- if (TreasureConfig.getInstance().getInventoryStealEnabled()) {
- PlayerInventory inventory = targetPlayer.getInventory();
- int length = inventory.getContents().length;
- int slot = Misc.getRandom().nextInt(length);
- drop = inventory.getItem(slot);
- if (drop == null) {
- break;
- }
- if (TreasureConfig.getInstance().getInventoryStealStacks()) {
- inventory.setItem(slot, null);
- }
- else {
- inventory.setItem(slot, (drop.getAmount() > 1) ? new ItemStack(drop.getType(), drop.getAmount() - 1) : null);
- drop.setAmount(1);
- }
- targetPlayer.updateInventory();
- }
- break;
- default:
- break;
- }
- break;
- case SHEEP:
- Sheep sheep = (Sheep) target;
- if (drop.getType().name().endsWith("WOOL")) {
- if (sheep.isSheared()) {
- return;
- }
- sheep.setSheared(true);
- }
- break;
- default:
- break;
- }
- McMMOPlayerShakeEvent shakeEvent = new McMMOPlayerShakeEvent(getPlayer(), drop);
- drop = shakeEvent.getDrop();
- if (shakeEvent.isCancelled() || drop == null) {
- return;
- }
- Misc.dropItem(target.getLocation(), drop);
- CombatUtils.dealDamage(target, Math.max(target.getMaxHealth() / 4, 1), EntityDamageEvent.DamageCause.CUSTOM, getPlayer()); // Make it so you can shake a mob no more than 4 times.
- applyXpGain(ExperienceConfig.getInstance().getFishingShakeXP(), XPGainReason.PVE);
- }
- }
- /**
- * Process the Treasure Hunter ability for Fishing
- *
- * @return The {@link FishingTreasure} found, or null if no treasure was found.
- */
- private FishingTreasure getFishingTreasure() {
- double diceRoll = Misc.getRandom().nextDouble() * 100;
- int luck;
- if (getPlayer().getInventory().getItemInMainHand().getType() == Material.FISHING_ROD) {
- luck = getPlayer().getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.LUCK);
- }
- else {
- // We know something was caught, so if the rod wasn't in the main hand it must be in the offhand
- luck = getPlayer().getInventory().getItemInOffHand().getEnchantmentLevel(Enchantment.LUCK);
- }
- // Rather than subtracting luck (and causing a minimum 3% chance for every drop), scale by luck.
- diceRoll *= (1.0 - luck * Config.getInstance().getFishingLureModifier() / 100);
- FishingTreasure treasure = null;
- for (Rarity rarity : Rarity.values()) {
- double dropRate = TreasureConfig.getInstance().getItemDropRate(getLootTier(), rarity);
- if (diceRoll <= dropRate) {
- /*if (rarity == Rarity.TRAP) {
- handleTraps();
- break;
- }*/
- List<FishingTreasure> fishingTreasures = TreasureConfig.getInstance().fishingRewards.get(rarity);
- if (fishingTreasures.isEmpty()) {
- return null;
- }
- treasure = fishingTreasures.get(Misc.getRandom().nextInt(fishingTreasures.size()));
- break;
- }
- diceRoll -= dropRate;
- }
- if (treasure == null) {
- return null;
- }
- ItemStack treasureDrop = treasure.getDrop().clone();
- short maxDurability = treasureDrop.getType().getMaxDurability();
- if (maxDurability > 0) {
- treasureDrop.setDurability((short) (Misc.getRandom().nextInt(maxDurability)));
- }
- if (treasureDrop.getAmount() > 1) {
- treasureDrop.setAmount(Misc.getRandom().nextInt(treasureDrop.getAmount()) + 1);
- }
- treasure.setDrop(treasureDrop);
- return treasure;
- }
- /**
- * Process the Magic Hunter ability
- *
- * @param treasureDrop The {@link ItemStack} to enchant
- *
- * @return true if the item has been enchanted
- */
- private Map<Enchantment, Integer> handleMagicHunter(ItemStack treasureDrop) {
- Map<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>();
- List<EnchantmentTreasure> fishingEnchantments = null;
- double diceRoll = Misc.getRandom().nextDouble() * 100;
- for (Rarity rarity : Rarity.values()) {
- if (rarity == Rarity.TRAP || rarity == Rarity.RECORD) {
- continue;
- }
- double dropRate = TreasureConfig.getInstance().getEnchantmentDropRate(getLootTier(), rarity);
- if (diceRoll <= dropRate) {
- // Make sure enchanted books always get some kind of enchantment. --hoorigan
- if (treasureDrop.getType() == Material.ENCHANTED_BOOK) {
- diceRoll = dropRate + 1;
- continue;
- }
- fishingEnchantments = TreasureConfig.getInstance().fishingEnchantments.get(rarity);
- break;
- }
- diceRoll -= dropRate;
- }
- if (fishingEnchantments == null) {
- return enchants;
- }
- List<Enchantment> validEnchantments = getPossibleEnchantments(treasureDrop);
- List<EnchantmentTreasure> possibleEnchants = new ArrayList<EnchantmentTreasure>();
- for (EnchantmentTreasure enchantmentTreasure : fishingEnchantments) {
- if (validEnchantments.contains(enchantmentTreasure.getEnchantment())) {
- possibleEnchants.add(enchantmentTreasure);
- }
- }
- if (possibleEnchants.isEmpty()) {
- return enchants;
- }
- // This make sure that the order isn't always the same, for example previously Unbreaking had a lot more chance to be used than any other enchant
- Collections.shuffle(possibleEnchants, Misc.getRandom());
- int specificChance = 1;
- for (EnchantmentTreasure enchantmentTreasure : possibleEnchants) {
- Enchantment possibleEnchantment = enchantmentTreasure.getEnchantment();
- if (treasureDrop.getItemMeta().hasConflictingEnchant(possibleEnchantment) || Misc.getRandom().nextInt(specificChance) != 0) {
- continue;
- }
- enchants.put(possibleEnchantment, enchantmentTreasure.getLevel());
- specificChance *= 2;
- }
- return enchants;
- }
- private List<Enchantment> getPossibleEnchantments(ItemStack treasureDrop) {
- Material dropType = treasureDrop.getType();
- if (Fishing.ENCHANTABLE_CACHE.containsKey(dropType)) {
- return Fishing.ENCHANTABLE_CACHE.get(dropType);
- }
- List<Enchantment> possibleEnchantments = new ArrayList<Enchantment>();
- for (Enchantment enchantment : Enchantment.values()) {
- if (enchantment.canEnchantItem(treasureDrop)) {
- possibleEnchantments.add(enchantment);
- }
- }
- Fishing.ENCHANTABLE_CACHE.put(dropType, possibleEnchantments);
- return possibleEnchantments;
- }
- /**
- * Gets the vanilla XP multiplier
- *
- * @return the vanilla XP multiplier
- */
- private int getVanillaXpMultiplier() {
- return getVanillaXPBoostModifier();
- }
- }
|