FishingManager.java 19 KB

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