FishingManager.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. package com.gmail.nossr50.skills.fishing;
  2. import com.gmail.nossr50.config.AdvancedConfig;
  3. import com.gmail.nossr50.config.Config;
  4. import com.gmail.nossr50.config.experience.ExperienceConfig;
  5. import com.gmail.nossr50.config.treasure.TreasureConfig;
  6. import com.gmail.nossr50.datatypes.experience.XPGainReason;
  7. import com.gmail.nossr50.datatypes.interactions.NotificationType;
  8. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  9. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  10. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  11. import com.gmail.nossr50.datatypes.treasure.EnchantmentTreasure;
  12. import com.gmail.nossr50.datatypes.treasure.FishingTreasure;
  13. import com.gmail.nossr50.datatypes.treasure.Rarity;
  14. import com.gmail.nossr50.datatypes.treasure.ShakeTreasure;
  15. import com.gmail.nossr50.events.skills.fishing.McMMOPlayerFishingTreasureEvent;
  16. import com.gmail.nossr50.events.skills.fishing.McMMOPlayerShakeEvent;
  17. import com.gmail.nossr50.locale.LocaleLoader;
  18. import com.gmail.nossr50.mcMMO;
  19. import com.gmail.nossr50.skills.SkillManager;
  20. import com.gmail.nossr50.util.*;
  21. import com.gmail.nossr50.util.player.NotificationManager;
  22. import com.gmail.nossr50.util.random.RandomChanceSkillStatic;
  23. import com.gmail.nossr50.util.random.RandomChanceUtil;
  24. import com.gmail.nossr50.util.skills.CombatUtils;
  25. import com.gmail.nossr50.util.skills.RankUtils;
  26. import com.gmail.nossr50.util.skills.SkillUtils;
  27. import com.gmail.nossr50.util.sounds.SoundManager;
  28. import com.gmail.nossr50.util.sounds.SoundType;
  29. import org.bukkit.Bukkit;
  30. import org.bukkit.Location;
  31. import org.bukkit.Material;
  32. import org.bukkit.block.Block;
  33. import org.bukkit.block.BlockFace;
  34. import org.bukkit.enchantments.Enchantment;
  35. import org.bukkit.entity.*;
  36. import org.bukkit.event.entity.EntityDamageEvent;
  37. import org.bukkit.inventory.ItemStack;
  38. import org.bukkit.inventory.PlayerInventory;
  39. import org.bukkit.inventory.meta.SkullMeta;
  40. import org.bukkit.util.BoundingBox;
  41. import org.bukkit.util.Vector;
  42. import java.util.*;
  43. public class FishingManager extends SkillManager {
  44. public static final int FISHING_ROD_CAST_CD_MILLISECONDS = 200;
  45. public static final int OVERFISH_LIMIT = 4;
  46. private final long FISHING_COOLDOWN_SECONDS = 1000L;
  47. private long fishingRodCastTimestamp = 0L;
  48. private long fishHookSpawnTimestamp = 0L;
  49. private long lastWarned = 0L;
  50. private long lastWarnedExhaust = 0L;
  51. private FishHook fishHookReference;
  52. private BoundingBox lastFishingBoundingBox;
  53. private Item fishingCatch;
  54. private Location hookLocation;
  55. private int fishCaughtCounter = 1;
  56. public FishingManager(McMMOPlayer mcMMOPlayer) {
  57. super(mcMMOPlayer, PrimarySkillType.FISHING);
  58. }
  59. public boolean canShake(Entity target) {
  60. return target instanceof LivingEntity && RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.FISHING_SHAKE) && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.FISHING_SHAKE);
  61. }
  62. public boolean canMasterAngler() {
  63. return getSkillLevel() >= RankUtils.getUnlockLevel(SubSkillType.FISHING_MASTER_ANGLER) && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.FISHING_MASTER_ANGLER);
  64. }
  65. public void setFishingRodCastTimestamp()
  66. {
  67. long currentTime = System.currentTimeMillis();
  68. //Only track spam casting if the fishing hook is fresh
  69. if(currentTime > fishHookSpawnTimestamp + 500)
  70. return;
  71. if(currentTime < fishingRodCastTimestamp + FISHING_ROD_CAST_CD_MILLISECONDS)
  72. {
  73. getPlayer().setFoodLevel(Math.max(getPlayer().getFoodLevel() - 1, 0));
  74. getPlayer().getInventory().getItemInMainHand().setDurability((short) (getPlayer().getInventory().getItemInMainHand().getDurability() + 5));
  75. getPlayer().updateInventory();
  76. if(lastWarnedExhaust + (1000 * 1) < currentTime)
  77. {
  78. getPlayer().sendMessage(LocaleLoader.getString("Fishing.Exhausting"));
  79. lastWarnedExhaust = currentTime;
  80. SoundManager.sendSound(getPlayer(), getPlayer().getLocation(), SoundType.TIRED);
  81. }
  82. }
  83. fishingRodCastTimestamp = System.currentTimeMillis();
  84. }
  85. public void setFishHookReference(FishHook fishHook)
  86. {
  87. if(fishHook.getMetadata(mcMMO.FISH_HOOK_REF_METAKEY).size() > 0)
  88. return;
  89. fishHook.setMetadata(mcMMO.FISH_HOOK_REF_METAKEY, mcMMO.metadataValue);
  90. this.fishHookReference = fishHook;
  91. fishHookSpawnTimestamp = System.currentTimeMillis();
  92. fishingRodCastTimestamp = System.currentTimeMillis();
  93. }
  94. public boolean isFishingTooOften()
  95. {
  96. long currentTime = System.currentTimeMillis();
  97. long fishHookSpawnCD = fishHookSpawnTimestamp + 1000;
  98. boolean hasFished = (currentTime < fishHookSpawnCD);
  99. if(hasFished && (lastWarned + (1000 * 1) < currentTime))
  100. {
  101. getPlayer().sendMessage(LocaleLoader.getString("Fishing.Scared"));
  102. lastWarned = System.currentTimeMillis();
  103. }
  104. return hasFished;
  105. }
  106. public boolean isExploitingFishing(Vector centerOfCastVector) {
  107. /*Block targetBlock = getPlayer().getTargetBlock(BlockUtils.getTransparentBlocks(), 100);
  108. if (!targetBlock.isLiquid()) {
  109. return false;
  110. }*/
  111. BoundingBox newCastBoundingBox = makeBoundingBox(centerOfCastVector);
  112. boolean sameTarget = lastFishingBoundingBox != null && lastFishingBoundingBox.overlaps(newCastBoundingBox);
  113. if(sameTarget)
  114. fishCaughtCounter++;
  115. else
  116. fishCaughtCounter = 1;
  117. if(fishCaughtCounter + 1 == OVERFISH_LIMIT)
  118. {
  119. getPlayer().sendMessage(LocaleLoader.getString("Fishing.LowResources"));
  120. }
  121. //If the new bounding box does not intersect with the old one, then update our bounding box reference
  122. if(!sameTarget)
  123. lastFishingBoundingBox = newCastBoundingBox;
  124. return sameTarget && fishCaughtCounter >= OVERFISH_LIMIT;
  125. }
  126. public static BoundingBox makeBoundingBox(Vector centerOfCastVector) {
  127. return BoundingBox.of(centerOfCastVector, 1, 1, 1);
  128. }
  129. public void setFishingTarget() {
  130. getPlayer().getTargetBlock(BlockUtils.getTransparentBlocks(), 100);
  131. }
  132. public boolean canIceFish(Block block) {
  133. if (getSkillLevel() < RankUtils.getUnlockLevel(SubSkillType.FISHING_ICE_FISHING)) {
  134. return false;
  135. }
  136. if (block.getType() != Material.ICE) {
  137. return false;
  138. }
  139. // Make sure this is a body of water, not just a block of ice.
  140. if (!Fishing.iceFishingBiomes.contains(block.getBiome()) && (block.getRelative(BlockFace.DOWN, 3).getType() != Material.WATER)) {
  141. return false;
  142. }
  143. Player player = getPlayer();
  144. if (!Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.FISHING_ICE_FISHING)) {
  145. return false;
  146. }
  147. return EventUtils.simulateBlockBreak(block, player, false);
  148. }
  149. /**
  150. * Gets the loot tier
  151. *
  152. * @return the loot tier
  153. */
  154. public int getLootTier() {
  155. return RankUtils.getRank(getPlayer(), SubSkillType.FISHING_TREASURE_HUNTER);
  156. }
  157. public double getShakeChance() {
  158. return AdvancedConfig.getInstance().getShakeChance(getLootTier());
  159. }
  160. protected int getVanillaXPBoostModifier() {
  161. return AdvancedConfig.getInstance().getFishingVanillaXPModifier(getLootTier());
  162. }
  163. /**
  164. * Gets the Shake Mob probability
  165. *
  166. * @return Shake Mob probability
  167. */
  168. public double getShakeProbability() {
  169. return getShakeChance();
  170. }
  171. /**
  172. * Handle the Fisherman's Diet ability
  173. *
  174. * @param eventFoodLevel The initial change in hunger from the event
  175. *
  176. * @return the modified change in hunger for the event
  177. */
  178. public int handleFishermanDiet(int eventFoodLevel) {
  179. return SkillUtils.handleFoodSkills(getPlayer(), eventFoodLevel, SubSkillType.FISHING_FISHERMANS_DIET);
  180. }
  181. public void iceFishing(FishHook hook, Block block) {
  182. // Make a hole
  183. block.setType(Material.WATER);
  184. for (int x = -1; x <= 1; x++) {
  185. for (int z = -1; z <= 1; z++) {
  186. Block relative = block.getRelative(x, 0, z);
  187. if (relative.getType() == Material.ICE) {
  188. relative.setType(Material.WATER);
  189. }
  190. }
  191. }
  192. // Recast in the new spot
  193. EventUtils.callFakeFishEvent(getPlayer(), hook);
  194. }
  195. public void masterAngler(FishHook hook) {
  196. Player player = getPlayer();
  197. Location location = hook.getLocation();
  198. double biteChance = hook.getBiteChance();
  199. hookLocation = location;
  200. if (Fishing.masterAnglerBiomes.contains(location.getBlock().getBiome())) {
  201. biteChance = biteChance * AdvancedConfig.getInstance().getMasterAnglerBiomeModifier();
  202. }
  203. if (player.isInsideVehicle() && player.getVehicle().getType() == EntityType.BOAT) {
  204. biteChance = biteChance * AdvancedConfig.getInstance().getMasterAnglerBoatModifier();
  205. }
  206. hook.setBiteChance(Math.min(biteChance, 1.0));
  207. }
  208. public boolean isMagicHunterEnabled()
  209. {
  210. return RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.FISHING_MAGIC_HUNTER)
  211. && RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.FISHING_TREASURE_HUNTER)
  212. && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.FISHING_TREASURE_HUNTER);
  213. }
  214. /**
  215. * Process the results from a successful fishing trip
  216. *
  217. * @param fishingCatch The {@link Item} initially caught
  218. */
  219. public void handleFishing(Item fishingCatch) {
  220. this.fishingCatch = fishingCatch;
  221. int fishXp = ExperienceConfig.getInstance().getXp(PrimarySkillType.FISHING, fishingCatch.getItemStack().getType());
  222. int treasureXp = 0;
  223. Player player = getPlayer();
  224. FishingTreasure treasure = null;
  225. if (Config.getInstance().getFishingDropsEnabled() && Permissions.isSubSkillEnabled(player, SubSkillType.FISHING_TREASURE_HUNTER)) {
  226. treasure = getFishingTreasure();
  227. this.fishingCatch = null;
  228. }
  229. if (treasure != null) {
  230. ItemStack treasureDrop = treasure.getDrop().clone(); // Not cloning is bad, m'kay?
  231. Map<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>();
  232. if (isMagicHunterEnabled()
  233. && ItemUtils.isEnchantable(treasureDrop)) {
  234. enchants = handleMagicHunter(treasureDrop);
  235. }
  236. McMMOPlayerFishingTreasureEvent event = EventUtils.callFishingTreasureEvent(player, treasureDrop, treasure.getXp(), enchants);
  237. if (!event.isCancelled()) {
  238. treasureDrop = event.getTreasure();
  239. treasureXp = event.getXp();
  240. }
  241. else {
  242. treasureDrop = null;
  243. treasureXp = 0;
  244. }
  245. // Drop the original catch at the feet of the player and set the treasure as the real catch
  246. if (treasureDrop != null) {
  247. boolean enchanted = false;
  248. if (!enchants.isEmpty()) {
  249. treasureDrop.addUnsafeEnchantments(enchants);
  250. enchanted = true;
  251. }
  252. if (enchanted) {
  253. NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Fishing.Ability.TH.MagicFound");
  254. }
  255. if (Config.getInstance().getFishingExtraFish()) {
  256. Misc.dropItem(player.getEyeLocation(), fishingCatch.getItemStack());
  257. }
  258. fishingCatch.setItemStack(treasureDrop);
  259. }
  260. }
  261. applyXpGain(fishXp + treasureXp, XPGainReason.PVE);
  262. }
  263. /**
  264. * Handle the vanilla XP boost for Fishing
  265. *
  266. * @param experience The amount of experience initially awarded by the event
  267. *
  268. * @return the modified event damage
  269. */
  270. public int handleVanillaXpBoost(int experience) {
  271. return experience * getVanillaXpMultiplier();
  272. }
  273. public Location getHookLocation() {
  274. return hookLocation;
  275. }
  276. /**
  277. * Handle the Shake ability
  278. *
  279. * @param target The {@link LivingEntity} affected by the ability
  280. */
  281. public void shakeCheck(LivingEntity target) {
  282. if (RandomChanceUtil.checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(getShakeChance(), getPlayer(), SubSkillType.FISHING_SHAKE))) {
  283. List<ShakeTreasure> possibleDrops = Fishing.findPossibleDrops(target);
  284. if (possibleDrops == null || possibleDrops.isEmpty()) {
  285. return;
  286. }
  287. ItemStack drop = Fishing.chooseDrop(possibleDrops);
  288. // It's possible that chooseDrop returns null if the sum of probability in possibleDrops is inferior than 100
  289. if (drop == null) {
  290. return;
  291. }
  292. // Extra processing depending on the mob and drop type
  293. switch (target.getType()) {
  294. case PLAYER:
  295. Player targetPlayer = (Player) target;
  296. switch (drop.getType()) {
  297. case PLAYER_HEAD:
  298. drop.setDurability((short) 3);
  299. SkullMeta skullMeta = (SkullMeta) drop.getItemMeta();
  300. skullMeta.setOwningPlayer(targetPlayer);
  301. drop.setItemMeta(skullMeta);
  302. break;
  303. case BEDROCK:
  304. if (TreasureConfig.getInstance().getInventoryStealEnabled()) {
  305. PlayerInventory inventory = targetPlayer.getInventory();
  306. int length = inventory.getContents().length;
  307. int slot = Misc.getRandom().nextInt(length);
  308. drop = inventory.getItem(slot);
  309. if (drop == null) {
  310. break;
  311. }
  312. if (TreasureConfig.getInstance().getInventoryStealStacks()) {
  313. inventory.setItem(slot, null);
  314. }
  315. else {
  316. inventory.setItem(slot, (drop.getAmount() > 1) ? new ItemStack(drop.getType(), drop.getAmount() - 1) : null);
  317. drop.setAmount(1);
  318. }
  319. targetPlayer.updateInventory();
  320. }
  321. break;
  322. default:
  323. break;
  324. }
  325. break;
  326. case SHEEP:
  327. Sheep sheep = (Sheep) target;
  328. if (drop.getType().name().endsWith("WOOL")) {
  329. if (sheep.isSheared()) {
  330. return;
  331. }
  332. sheep.setSheared(true);
  333. }
  334. break;
  335. default:
  336. break;
  337. }
  338. McMMOPlayerShakeEvent shakeEvent = new McMMOPlayerShakeEvent(getPlayer(), drop);
  339. drop = shakeEvent.getDrop();
  340. if (shakeEvent.isCancelled() || drop == null) {
  341. return;
  342. }
  343. Misc.dropItem(target.getLocation(), drop);
  344. 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.
  345. applyXpGain(ExperienceConfig.getInstance().getFishingShakeXP(), XPGainReason.PVE);
  346. }
  347. }
  348. /**
  349. * Process the Treasure Hunter ability for Fishing
  350. *
  351. * @return The {@link FishingTreasure} found, or null if no treasure was found.
  352. */
  353. private FishingTreasure getFishingTreasure() {
  354. double diceRoll = Misc.getRandom().nextDouble() * 100;
  355. int luck;
  356. if (getPlayer().getInventory().getItemInMainHand().getType() == Material.FISHING_ROD) {
  357. luck = getPlayer().getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.LUCK);
  358. }
  359. else {
  360. // We know something was caught, so if the rod wasn't in the main hand it must be in the offhand
  361. luck = getPlayer().getInventory().getItemInOffHand().getEnchantmentLevel(Enchantment.LUCK);
  362. }
  363. // Rather than subtracting luck (and causing a minimum 3% chance for every drop), scale by luck.
  364. diceRoll *= (1.0 - luck * Config.getInstance().getFishingLureModifier() / 100);
  365. FishingTreasure treasure = null;
  366. for (Rarity rarity : Rarity.values()) {
  367. double dropRate = TreasureConfig.getInstance().getItemDropRate(getLootTier(), rarity);
  368. if (diceRoll <= dropRate) {
  369. /*if (rarity == Rarity.TRAP) {
  370. handleTraps();
  371. break;
  372. }*/
  373. List<FishingTreasure> fishingTreasures = TreasureConfig.getInstance().fishingRewards.get(rarity);
  374. if (fishingTreasures.isEmpty()) {
  375. return null;
  376. }
  377. treasure = fishingTreasures.get(Misc.getRandom().nextInt(fishingTreasures.size()));
  378. break;
  379. }
  380. diceRoll -= dropRate;
  381. }
  382. if (treasure == null) {
  383. return null;
  384. }
  385. ItemStack treasureDrop = treasure.getDrop().clone();
  386. short maxDurability = treasureDrop.getType().getMaxDurability();
  387. if (maxDurability > 0) {
  388. treasureDrop.setDurability((short) (Misc.getRandom().nextInt(maxDurability)));
  389. }
  390. if (treasureDrop.getAmount() > 1) {
  391. treasureDrop.setAmount(Misc.getRandom().nextInt(treasureDrop.getAmount()) + 1);
  392. }
  393. treasure.setDrop(treasureDrop);
  394. return treasure;
  395. }
  396. /**
  397. * Process the Magic Hunter ability
  398. *
  399. * @param treasureDrop The {@link ItemStack} to enchant
  400. *
  401. * @return true if the item has been enchanted
  402. */
  403. private Map<Enchantment, Integer> handleMagicHunter(ItemStack treasureDrop) {
  404. Map<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>();
  405. List<EnchantmentTreasure> fishingEnchantments = null;
  406. double diceRoll = Misc.getRandom().nextDouble() * 100;
  407. for (Rarity rarity : Rarity.values()) {
  408. if (rarity == Rarity.RECORD) {
  409. continue;
  410. }
  411. double dropRate = TreasureConfig.getInstance().getEnchantmentDropRate(getLootTier(), rarity);
  412. if (diceRoll <= dropRate) {
  413. // Make sure enchanted books always get some kind of enchantment. --hoorigan
  414. if (treasureDrop.getType() == Material.ENCHANTED_BOOK) {
  415. diceRoll = dropRate + 1;
  416. continue;
  417. }
  418. fishingEnchantments = TreasureConfig.getInstance().fishingEnchantments.get(rarity);
  419. break;
  420. }
  421. diceRoll -= dropRate;
  422. }
  423. if (fishingEnchantments == null) {
  424. return enchants;
  425. }
  426. List<Enchantment> validEnchantments = getPossibleEnchantments(treasureDrop);
  427. List<EnchantmentTreasure> possibleEnchants = new ArrayList<EnchantmentTreasure>();
  428. for (EnchantmentTreasure enchantmentTreasure : fishingEnchantments) {
  429. if (validEnchantments.contains(enchantmentTreasure.getEnchantment())) {
  430. possibleEnchants.add(enchantmentTreasure);
  431. }
  432. }
  433. if (possibleEnchants.isEmpty()) {
  434. return enchants;
  435. }
  436. // 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
  437. Collections.shuffle(possibleEnchants, Misc.getRandom());
  438. int specificChance = 1;
  439. for (EnchantmentTreasure enchantmentTreasure : possibleEnchants) {
  440. Enchantment possibleEnchantment = enchantmentTreasure.getEnchantment();
  441. if (treasureDrop.getItemMeta().hasConflictingEnchant(possibleEnchantment) || Misc.getRandom().nextInt(specificChance) != 0) {
  442. continue;
  443. }
  444. enchants.put(possibleEnchantment, enchantmentTreasure.getLevel());
  445. specificChance *= 2;
  446. }
  447. return enchants;
  448. }
  449. private List<Enchantment> getPossibleEnchantments(ItemStack treasureDrop) {
  450. Material dropType = treasureDrop.getType();
  451. if (Fishing.ENCHANTABLE_CACHE.containsKey(dropType)) {
  452. return Fishing.ENCHANTABLE_CACHE.get(dropType);
  453. }
  454. List<Enchantment> possibleEnchantments = new ArrayList<Enchantment>();
  455. for (Enchantment enchantment : Enchantment.values()) {
  456. if (enchantment.canEnchantItem(treasureDrop)) {
  457. possibleEnchantments.add(enchantment);
  458. }
  459. }
  460. Fishing.ENCHANTABLE_CACHE.put(dropType, possibleEnchantments);
  461. return possibleEnchantments;
  462. }
  463. /**
  464. * Gets the vanilla XP multiplier
  465. *
  466. * @return the vanilla XP multiplier
  467. */
  468. private int getVanillaXpMultiplier() {
  469. return getVanillaXPBoostModifier();
  470. }
  471. }