Fishing.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. package com.gmail.nossr50.skills.gathering;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.bukkit.DyeColor;
  6. import org.bukkit.Location;
  7. import org.bukkit.Material;
  8. import org.bukkit.enchantments.Enchantment;
  9. import org.bukkit.entity.EntityType;
  10. import org.bukkit.entity.Item;
  11. import org.bukkit.entity.LivingEntity;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.entity.Sheep;
  14. import org.bukkit.entity.Skeleton;
  15. import org.bukkit.entity.Skeleton.SkeletonType;
  16. import org.bukkit.event.player.PlayerFishEvent;
  17. import org.bukkit.inventory.ItemStack;
  18. import org.bukkit.material.MaterialData;
  19. import org.bukkit.material.Wool;
  20. import org.bukkit.potion.Potion;
  21. import org.bukkit.potion.PotionType;
  22. import com.gmail.nossr50.config.AdvancedConfig;
  23. import com.gmail.nossr50.config.Config;
  24. import com.gmail.nossr50.config.TreasuresConfig;
  25. import com.gmail.nossr50.datatypes.PlayerProfile;
  26. import com.gmail.nossr50.datatypes.SkillType;
  27. import com.gmail.nossr50.datatypes.treasure.FishingTreasure;
  28. import com.gmail.nossr50.locale.LocaleLoader;
  29. import com.gmail.nossr50.util.Combat;
  30. import com.gmail.nossr50.util.ItemChecks;
  31. import com.gmail.nossr50.util.Misc;
  32. import com.gmail.nossr50.util.Permissions;
  33. import com.gmail.nossr50.util.Skills;
  34. import com.gmail.nossr50.util.Users;
  35. public class Fishing {
  36. static AdvancedConfig advancedConfig = AdvancedConfig.getInstance();
  37. /**
  38. * Get the player's current fishing loot tier.
  39. *
  40. * @param profile
  41. * The profile of the player
  42. * @return the player's current fishing rank
  43. */
  44. public static int getFishingLootTier(PlayerProfile profile) {
  45. int level = profile.getSkillLevel(SkillType.FISHING);
  46. int fishingTier;
  47. if (level >= Config.getInstance().getFishingTierLevelsTier5()) {
  48. fishingTier = 5;
  49. } else if (level >= Config.getInstance().getFishingTierLevelsTier4()) {
  50. fishingTier = 4;
  51. } else if (level >= Config.getInstance().getFishingTierLevelsTier3()) {
  52. fishingTier = 3;
  53. } else if (level >= Config.getInstance().getFishingTierLevelsTier2()) {
  54. fishingTier = 2;
  55. } else {
  56. fishingTier = 1;
  57. }
  58. return fishingTier;
  59. }
  60. /**
  61. * Get item results from Fishing.
  62. *
  63. * @param player
  64. * The player that was fishing
  65. * @param event
  66. * The event to modify
  67. */
  68. private static void getFishingResults(Player player, PlayerFishEvent event) {
  69. if (player == null)
  70. return;
  71. PlayerProfile profile = Users.getProfile(player);
  72. List<FishingTreasure> rewards = new ArrayList<FishingTreasure>();
  73. Item theCatch = (Item) event.getCaught();
  74. switch (getFishingLootTier(profile)) {
  75. case 1:
  76. rewards = TreasuresConfig.getInstance().fishingRewardsTier1;
  77. break;
  78. case 2:
  79. rewards = TreasuresConfig.getInstance().fishingRewardsTier2;
  80. break;
  81. case 3:
  82. rewards = TreasuresConfig.getInstance().fishingRewardsTier3;
  83. break;
  84. case 4:
  85. rewards = TreasuresConfig.getInstance().fishingRewardsTier4;
  86. break;
  87. case 5:
  88. rewards = TreasuresConfig.getInstance().fishingRewardsTier5;
  89. break;
  90. default:
  91. break;
  92. }
  93. if (Config.getInstance().getFishingDropsEnabled() && rewards.size() > 0
  94. && Permissions.fishingTreasures(player)) {
  95. FishingTreasure treasure;
  96. treasure = rewards.get(Misc.getRandom().nextInt(rewards.size()));
  97. int randomChance = 100;
  98. if (Permissions.luckyFishing(player)) {
  99. randomChance = (int) (randomChance * 0.75);
  100. }
  101. if (Misc.getRandom().nextDouble() * randomChance <= treasure.getDropChance()) {
  102. Users.getPlayer(player).addXP(SkillType.FISHING,treasure.getXp());
  103. theCatch.setItemStack(treasure.getDrop());
  104. }
  105. } else {
  106. theCatch.setItemStack(new ItemStack(Material.RAW_FISH));
  107. }
  108. short maxDurability = theCatch.getItemStack().getType().getMaxDurability();
  109. if (maxDurability > 0) {
  110. theCatch.getItemStack().setDurability((short) (Misc.getRandom().nextInt(maxDurability))); // Change durability to random value
  111. }
  112. Skills.xpProcessing(player, profile, SkillType.FISHING, Config.getInstance().getFishingBaseXP());
  113. }
  114. /**
  115. * Process results from Fishing.
  116. *
  117. * @param event
  118. * The event to modify
  119. */
  120. public static void processResults(PlayerFishEvent event) {
  121. Player player = event.getPlayer();
  122. if (player == null)
  123. return;
  124. PlayerProfile profile = Users.getProfile(player);
  125. getFishingResults(player, event);
  126. Item theCatch = (Item) event.getCaught();
  127. if (theCatch.getItemStack().getType() != Material.RAW_FISH) {
  128. final int ENCHANTMENT_CHANCE = advancedConfig.getFishingEnchantmentChance();
  129. boolean enchanted = false;
  130. ItemStack fishingResults = theCatch.getItemStack();
  131. player.sendMessage(LocaleLoader.getString("Fishing.ItemFound"));
  132. if (ItemChecks.isEnchantable(fishingResults)) {
  133. int randomChance = 100;
  134. if (Permissions.luckyFishing(player)) {
  135. randomChance = (int) (randomChance * 0.75);
  136. }
  137. if (Misc.getRandom().nextInt(randomChance) <= ENCHANTMENT_CHANCE
  138. && Permissions.fishingMagic(player)) {
  139. for (Enchantment newEnchant : Enchantment.values()) {
  140. if (newEnchant.canEnchantItem(fishingResults)) {
  141. Map<Enchantment, Integer> resultEnchantments = fishingResults.getEnchantments();
  142. for (Enchantment oldEnchant : resultEnchantments.keySet()) {
  143. if (oldEnchant.conflictsWith(newEnchant))
  144. continue;
  145. }
  146. /*
  147. * Actual chance to have an enchantment is related
  148. * to your fishing skill
  149. */
  150. if (Misc.getRandom().nextInt(15) < Fishing.getFishingLootTier(profile)) {
  151. enchanted = true;
  152. int randomEnchantLevel = Misc.getRandom().nextInt(newEnchant.getMaxLevel()) + 1;
  153. if (randomEnchantLevel < newEnchant.getStartLevel()) {
  154. randomEnchantLevel = newEnchant.getStartLevel();
  155. }
  156. if (randomEnchantLevel >= 1000)
  157. continue;
  158. fishingResults.addEnchantment(newEnchant,randomEnchantLevel);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. if (enchanted) {
  165. player.sendMessage(LocaleLoader.getString("Fishing.MagicFound"));
  166. }
  167. }
  168. }
  169. /**
  170. * Shake a mob, have them drop an item.
  171. *
  172. * @param event
  173. * The event to modify
  174. */
  175. public static void shakeMob(PlayerFishEvent event) {
  176. int randomChance = 100;
  177. if (Permissions.luckyFishing(event.getPlayer())) {
  178. randomChance = (int) (randomChance * 1.25);
  179. }
  180. final Player player = event.getPlayer();
  181. final PlayerProfile profile = Users.getProfile(player);
  182. int lootTier = getFishingLootTier(profile);
  183. int dropChance = getShakeChance(lootTier);
  184. if (Permissions.luckyFishing(player)) {
  185. // With lucky perk on max level tier, its 100%
  186. dropChance = (int) (dropChance * 1.25);
  187. }
  188. final int DROP_CHANCE = Misc.getRandom().nextInt(100);
  189. final int DROP_NUMBER = Misc.getRandom().nextInt(randomChance) + 1;
  190. LivingEntity le = (LivingEntity) event.getCaught();
  191. EntityType type = le.getType();
  192. Location location = le.getLocation();
  193. if (DROP_CHANCE < dropChance) {
  194. switch (type) {
  195. case BLAZE:
  196. Misc.dropItem(location, new ItemStack(Material.BLAZE_ROD));
  197. break;
  198. case CAVE_SPIDER:
  199. if (DROP_NUMBER > 50) {
  200. Misc.dropItem(location, new ItemStack(Material.SPIDER_EYE));
  201. } else {
  202. Misc.dropItem(location, new ItemStack(Material.STRING));
  203. }
  204. break;
  205. case CHICKEN:
  206. if (DROP_NUMBER > 66) {
  207. Misc.dropItem(location, new ItemStack(Material.FEATHER));
  208. } else if (DROP_NUMBER > 33) {
  209. Misc.dropItem(location, new ItemStack(Material.RAW_CHICKEN));
  210. } else {
  211. Misc.dropItem(location, new ItemStack(Material.EGG));
  212. }
  213. break;
  214. case COW:
  215. if (DROP_NUMBER > 95) {
  216. Misc.dropItem(location, new ItemStack(Material.MILK_BUCKET));
  217. } else if (DROP_NUMBER > 50) {
  218. Misc.dropItem(location, new ItemStack(Material.LEATHER));
  219. } else {
  220. Misc.dropItem(location, new ItemStack(Material.RAW_BEEF));
  221. }
  222. break;
  223. case CREEPER:
  224. if (DROP_NUMBER > 97) {
  225. Misc.dropItem(location, new ItemStack(Material.SKULL_ITEM, 1, (short) 4));
  226. } else {
  227. Misc.dropItem(location, new ItemStack(Material.SULPHUR));
  228. }
  229. break;
  230. case ENDERMAN:
  231. Misc.dropItem(location, new ItemStack(Material.ENDER_PEARL));
  232. break;
  233. case GHAST:
  234. if (DROP_NUMBER > 50) {
  235. Misc.dropItem(location, new ItemStack(Material.SULPHUR));
  236. } else {
  237. Misc.dropItem(location, new ItemStack(Material.GHAST_TEAR));
  238. }
  239. break;
  240. case IRON_GOLEM:
  241. if (DROP_NUMBER > 97) {
  242. Misc.dropItem(location, new ItemStack(Material.PUMPKIN));
  243. } else if (DROP_NUMBER > 85) {
  244. Misc.dropItem(location, new ItemStack(Material.IRON_INGOT));
  245. } else {
  246. Misc.dropItem(location, new ItemStack(Material.RED_ROSE));
  247. }
  248. break;
  249. case MAGMA_CUBE:
  250. Misc.dropItem(location, new ItemStack(Material.MAGMA_CREAM));
  251. break;
  252. case MUSHROOM_COW:
  253. if (DROP_NUMBER > 95) {
  254. Misc.dropItem(location, new ItemStack(Material.MILK_BUCKET));
  255. } else if (DROP_NUMBER > 90) {
  256. Misc.dropItem(location, new ItemStack(Material.MUSHROOM_SOUP));
  257. } else if (DROP_NUMBER > 60) {
  258. Misc.dropItem(location, new ItemStack(Material.LEATHER));
  259. } else if (DROP_NUMBER > 30) {
  260. Misc.dropItem(location, new ItemStack(Material.RAW_BEEF));
  261. } else {
  262. Misc.dropItem(location, new ItemStack(Material.RED_MUSHROOM));
  263. Misc.randomDropItems(location, new ItemStack(Material.RED_MUSHROOM), 50, 2);
  264. }
  265. break;
  266. case PIG:
  267. Misc.dropItem(location, new ItemStack(Material.PORK));
  268. break;
  269. case PIG_ZOMBIE:
  270. if (DROP_NUMBER > 50) {
  271. Misc.dropItem(location, new ItemStack(Material.ROTTEN_FLESH));
  272. } else {
  273. Misc.dropItem(location, new ItemStack(Material.GOLD_NUGGET));
  274. }
  275. break;
  276. case SHEEP:
  277. final Sheep sheep = (Sheep) le;
  278. if (!sheep.isSheared()) {
  279. final Wool wool = new Wool();
  280. wool.setColor(sheep.getColor());
  281. final ItemStack theWool = wool.toItemStack();
  282. theWool.setAmount(1 + Misc.getRandom().nextInt(6));
  283. Misc.dropItem(location, theWool);
  284. sheep.setSheared(true);
  285. }
  286. break;
  287. case SKELETON:
  288. if (((Skeleton) le).getSkeletonType() == SkeletonType.WITHER) {
  289. if (DROP_NUMBER > 97) {
  290. Misc.dropItem(location, new ItemStack(Material.SKULL_ITEM, 1, (short) 1));
  291. } else if (DROP_NUMBER > 50) {
  292. Misc.dropItem(location, new ItemStack(Material.BONE));
  293. } else {
  294. Misc.dropItem(location, new ItemStack(Material.COAL));
  295. Misc.randomDropItems(location, new ItemStack(Material.COAL), 50, 2);
  296. }
  297. } else {
  298. if (DROP_NUMBER > 97) {
  299. Misc.dropItem(location, new ItemStack(Material.SKULL_ITEM));
  300. } else if (DROP_NUMBER > 50) {
  301. Misc.dropItem(location, new ItemStack(Material.BONE));
  302. } else {
  303. Misc.dropItem(location, new ItemStack(Material.ARROW));
  304. Misc.randomDropItems(location, new ItemStack(Material.ARROW), 50, 2);
  305. }
  306. }
  307. break;
  308. case SLIME:
  309. Misc.dropItem(location, new ItemStack(Material.SLIME_BALL));
  310. break;
  311. case SNOWMAN:
  312. if (DROP_NUMBER > 97) {
  313. Misc.dropItem(location, new ItemStack(Material.PUMPKIN));
  314. } else {
  315. Misc.dropItem(location, new ItemStack(Material.SNOW_BALL));
  316. Misc.randomDropItems(location, new ItemStack(Material.SNOW_BALL), 50, 4);
  317. }
  318. break;
  319. case SPIDER:
  320. if (DROP_NUMBER > 50) {
  321. Misc.dropItem(location, new ItemStack(Material.SPIDER_EYE));
  322. } else {
  323. Misc.dropItem(location, new ItemStack(Material.STRING));
  324. }
  325. break;
  326. case SQUID:
  327. ItemStack item;
  328. try {
  329. item = (new MaterialData(Material.INK_SACK, DyeColor.BLACK.getDyeData())).toItemStack(1);
  330. }
  331. catch(Exception e) {
  332. item = (new MaterialData(Material.INK_SACK, (byte) 0)).toItemStack(1);
  333. }
  334. Misc.dropItem(location, item);
  335. break;
  336. case WITCH:
  337. final int DROP_NUMBER_2 = Misc.getRandom().nextInt(randomChance) + 1;
  338. if (DROP_NUMBER > 95) {
  339. if (DROP_NUMBER_2 > 66) {
  340. Misc.dropItem(location, new Potion(PotionType.INSTANT_HEAL).toItemStack(1));
  341. } else if (DROP_NUMBER_2 > 33) {
  342. Misc.dropItem(location, new Potion(PotionType.FIRE_RESISTANCE).toItemStack(1));
  343. } else {
  344. Misc.dropItem(location, new Potion(PotionType.SPEED).toItemStack(1));
  345. }
  346. } else {
  347. if (DROP_NUMBER_2 > 88) {
  348. Misc.dropItem(location, new ItemStack(Material.GLASS_BOTTLE));
  349. } else if (DROP_NUMBER_2 > 75) {
  350. Misc.dropItem(location, new ItemStack(Material.GLOWSTONE_DUST));
  351. } else if (DROP_NUMBER_2 > 63) {
  352. Misc.dropItem(location, new ItemStack(Material.SULPHUR));
  353. } else if (DROP_NUMBER_2 > 50) {
  354. Misc.dropItem(location, new ItemStack(Material.REDSTONE));
  355. } else if (DROP_NUMBER_2 > 38) {
  356. Misc.dropItem(location, new ItemStack(Material.SPIDER_EYE));
  357. } else if (DROP_NUMBER_2 > 25) {
  358. Misc.dropItem(location, new ItemStack(Material.STICK));
  359. } else if (DROP_NUMBER_2 > 13) {
  360. Misc.dropItem(location, new ItemStack(Material.SUGAR));
  361. } else {
  362. Misc.dropItem(location, new ItemStack(Material.POTION));
  363. }
  364. }
  365. break;
  366. case ZOMBIE:
  367. if (DROP_NUMBER > 97) {
  368. Misc.dropItem(location, new ItemStack(Material.SKULL_ITEM, 1, (short) 2));
  369. } else {
  370. Misc.dropItem(location, new ItemStack(Material.ROTTEN_FLESH));
  371. }
  372. break;
  373. default:
  374. break;
  375. }
  376. }
  377. Combat.dealDamage(le, 1);
  378. }
  379. /**
  380. * Gets chance of shake success.
  381. *
  382. * @param rank
  383. * Treasure hunter rank
  384. * @return The chance of a successful shake
  385. */
  386. public static int getShakeChance(int lootTier) {
  387. switch (lootTier) {
  388. case 1:
  389. return Config.getInstance().getShakeChanceRank1();
  390. case 2:
  391. return Config.getInstance().getShakeChanceRank2();
  392. case 3:
  393. return Config.getInstance().getShakeChanceRank3();
  394. case 4:
  395. return Config.getInstance().getShakeChanceRank4();
  396. case 5:
  397. return Config.getInstance().getShakeChanceRank5();
  398. default:
  399. return 10;
  400. }
  401. }
  402. }