Fishing.java 18 KB

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