FishingManager.java 18 KB

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