Fishing.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package com.gmail.nossr50.skills.gathering;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Random;
  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.event.player.PlayerFishEvent;
  15. import org.bukkit.inventory.ItemStack;
  16. import org.bukkit.material.Wool;
  17. import com.gmail.nossr50.config.Config;
  18. import com.gmail.nossr50.config.TreasuresConfig;
  19. import com.gmail.nossr50.datatypes.PlayerProfile;
  20. import com.gmail.nossr50.datatypes.SkillType;
  21. import com.gmail.nossr50.datatypes.treasure.FishingTreasure;
  22. import com.gmail.nossr50.locale.LocaleLoader;
  23. import com.gmail.nossr50.util.Combat;
  24. import com.gmail.nossr50.util.ItemChecks;
  25. import com.gmail.nossr50.util.Misc;
  26. import com.gmail.nossr50.util.Permissions;
  27. import com.gmail.nossr50.util.Skills;
  28. import com.gmail.nossr50.util.Users;
  29. public class Fishing {
  30. private static Random random = new Random();
  31. /**
  32. * Get the player's current fishing loot tier.
  33. *
  34. * @param profile The profile of the player
  35. * @return the player's current fishing rank
  36. */
  37. public static int getFishingLootTier(PlayerProfile profile) {
  38. int level = profile.getSkillLevel(SkillType.FISHING);
  39. int fishingTier;
  40. if (level >= Config.getInstance().getFishingTierLevelsTier5()) {
  41. fishingTier = 5;
  42. }
  43. else if (level >= Config.getInstance().getFishingTierLevelsTier4()) {
  44. fishingTier = 4;
  45. }
  46. else if (level >= Config.getInstance().getFishingTierLevelsTier3()) {
  47. fishingTier = 3;
  48. }
  49. else if (level >= Config.getInstance().getFishingTierLevelsTier2()) {
  50. fishingTier = 2;
  51. }
  52. else {
  53. fishingTier = 1;
  54. }
  55. return fishingTier;
  56. }
  57. /**
  58. * Get item results from Fishing.
  59. *
  60. * @param player The player that was fishing
  61. * @param event The event to modify
  62. */
  63. private static void getFishingResults(Player player, PlayerFishEvent event) {
  64. PlayerProfile profile = Users.getProfile(player);
  65. List<FishingTreasure> rewards = new ArrayList<FishingTreasure>();
  66. Item theCatch = (Item) event.getCaught();
  67. switch (getFishingLootTier(profile)) {
  68. case 1:
  69. rewards = TreasuresConfig.getInstance().fishingRewardsTier1;
  70. break;
  71. case 2:
  72. rewards = TreasuresConfig.getInstance().fishingRewardsTier2;
  73. break;
  74. case 3:
  75. rewards = TreasuresConfig.getInstance().fishingRewardsTier3;
  76. break;
  77. case 4:
  78. rewards = TreasuresConfig.getInstance().fishingRewardsTier4;
  79. break;
  80. case 5:
  81. rewards = TreasuresConfig.getInstance().fishingRewardsTier5;
  82. break;
  83. default:
  84. break;
  85. }
  86. if (Config.getInstance().getFishingDropsEnabled() && rewards.size() > 0 && Permissions.getInstance().fishingTreasures(player)) {
  87. FishingTreasure treasure = rewards.get(random.nextInt(rewards.size()));
  88. int randomChance = 100;
  89. if (player.hasPermission("mcmmo.perks.lucky.fishing")) {
  90. randomChance = (int) (randomChance * 0.75);
  91. }
  92. if (random.nextDouble() * randomChance <= treasure.getDropChance()) {
  93. Users.getProfile(player).addXP(SkillType.FISHING, treasure.getXp());
  94. theCatch.setItemStack(treasure.getDrop());
  95. }
  96. }
  97. else {
  98. theCatch.setItemStack(new ItemStack(Material.RAW_FISH));
  99. }
  100. short maxDurability = theCatch.getItemStack().getType().getMaxDurability();
  101. if (maxDurability > 0) {
  102. theCatch.getItemStack().setDurability((short) (random.nextInt(maxDurability))); //Change durability to random value
  103. }
  104. Skills.xpProcessing(player, profile, SkillType.FISHING, Config.getInstance().getFishingBaseXP());
  105. }
  106. /**
  107. * Process results from Fishing.
  108. *
  109. * @param event The event to modify
  110. */
  111. public static void processResults(PlayerFishEvent event) {
  112. Player player = event.getPlayer();
  113. PlayerProfile profile = Users.getProfile(player);
  114. getFishingResults(player, event);
  115. Item theCatch = (Item) event.getCaught();
  116. if (theCatch.getItemStack().getType() != Material.RAW_FISH) {
  117. final int ENCHANTMENT_CHANCE = 10;
  118. boolean enchanted = false;
  119. ItemStack fishingResults = theCatch.getItemStack();
  120. player.sendMessage(LocaleLoader.getString("Fishing.ItemFound"));
  121. if (ItemChecks.isArmor(fishingResults) || ItemChecks.isTool(fishingResults)) {
  122. int randomChance = 100;
  123. if (player.hasPermission("mcmmo.perks.lucky.fishing")) {
  124. randomChance = (int) (randomChance * 0.75);
  125. }
  126. if (random.nextInt(randomChance) <= ENCHANTMENT_CHANCE && Permissions.getInstance().fishingMagic(player)) {
  127. for (Enchantment newEnchant : Enchantment.values()) {
  128. if (newEnchant.canEnchantItem(fishingResults)) {
  129. Map<Enchantment, Integer> resultEnchantments = fishingResults.getEnchantments();
  130. for (Enchantment oldEnchant : resultEnchantments.keySet()) {
  131. if (oldEnchant.conflictsWith(newEnchant)) {
  132. return;
  133. }
  134. }
  135. /* Actual chance to have an enchantment is related to your fishing skill */
  136. if (random.nextInt(15) < Fishing.getFishingLootTier(profile)) {
  137. enchanted = true;
  138. int randomEnchantLevel = random.nextInt(newEnchant.getMaxLevel()) + 1;
  139. if (randomEnchantLevel < newEnchant.getStartLevel()) {
  140. randomEnchantLevel = newEnchant.getStartLevel();
  141. }
  142. fishingResults.addEnchantment(newEnchant, randomEnchantLevel);
  143. }
  144. }
  145. }
  146. }
  147. }
  148. if (enchanted) {
  149. player.sendMessage(LocaleLoader.getString("Fishing.MagicFound"));
  150. }
  151. }
  152. }
  153. /**
  154. * Shake a mob, have them drop an item.
  155. *
  156. * @param event The event to modify
  157. */
  158. public static void shakeMob(PlayerFishEvent event) {
  159. int randomChance = 100;
  160. if (event.getPlayer().hasPermission("mcmmo.perks.lucky.fishing")) {
  161. randomChance = (int) (randomChance * 0.75);
  162. }
  163. final int DROP_NUMBER = random.nextInt(randomChance);
  164. LivingEntity le = (LivingEntity) event.getCaught();
  165. EntityType type = le.getType();
  166. Location location = le.getLocation();
  167. switch (type) {
  168. case BLAZE:
  169. Misc.dropItem(location, new ItemStack(Material.BLAZE_ROD));
  170. break;
  171. case CAVE_SPIDER:
  172. if (DROP_NUMBER > 50) {
  173. Misc.dropItem(location, new ItemStack(Material.SPIDER_EYE));
  174. }
  175. else {
  176. Misc.dropItem(location, new ItemStack(Material.STRING));
  177. }
  178. break;
  179. case CHICKEN:
  180. if (DROP_NUMBER > 66) {
  181. Misc.dropItem(location, new ItemStack(Material.FEATHER));
  182. }
  183. else if (DROP_NUMBER > 33) {
  184. Misc.dropItem(location, new ItemStack(Material.RAW_CHICKEN));
  185. }
  186. else {
  187. Misc.dropItem(location, new ItemStack(Material.EGG));
  188. }
  189. break;
  190. case COW:
  191. if (DROP_NUMBER > 99) {
  192. Misc.dropItem(location, new ItemStack(Material.MILK_BUCKET));
  193. }
  194. else if (DROP_NUMBER > 50) {
  195. Misc.dropItem(location, new ItemStack(Material.LEATHER));
  196. }
  197. else {
  198. Misc.dropItem(location, new ItemStack(Material.RAW_BEEF));
  199. }
  200. break;
  201. case CREEPER:
  202. Misc.dropItem(location, new ItemStack(Material.SULPHUR));
  203. break;
  204. case ENDERMAN:
  205. Misc.dropItem(location, new ItemStack(Material.ENDER_PEARL));
  206. break;
  207. case GHAST:
  208. if (DROP_NUMBER > 50) {
  209. Misc.dropItem(location, new ItemStack(Material.SULPHUR));
  210. }
  211. else {
  212. Misc.dropItem(location, new ItemStack(Material.GHAST_TEAR));
  213. }
  214. break;
  215. case MAGMA_CUBE:
  216. Misc.dropItem(location, new ItemStack(Material.MAGMA_CREAM));
  217. break;
  218. case MUSHROOM_COW:
  219. if (DROP_NUMBER > 99) {
  220. Misc.dropItem(location, new ItemStack(Material.MILK_BUCKET));
  221. }
  222. else if (DROP_NUMBER > 98) {
  223. Misc.dropItem(location, new ItemStack(Material.MUSHROOM_SOUP));
  224. }
  225. else if (DROP_NUMBER > 66) {
  226. Misc.dropItem(location, new ItemStack(Material.LEATHER));
  227. }
  228. else if (DROP_NUMBER > 33) {
  229. Misc.dropItem(location, new ItemStack(Material.RAW_BEEF));
  230. }
  231. else {
  232. Misc.dropItems(location, new ItemStack(Material.RED_MUSHROOM), 3);
  233. }
  234. break;
  235. case PIG:
  236. Misc.dropItem(location, new ItemStack(Material.PORK));
  237. break;
  238. case PIG_ZOMBIE:
  239. if (DROP_NUMBER > 50) {
  240. Misc.dropItem(location, new ItemStack(Material.ROTTEN_FLESH));
  241. }
  242. else {
  243. Misc.dropItem(location, new ItemStack(Material.GOLD_NUGGET));
  244. }
  245. break;
  246. case SHEEP:
  247. Sheep sheep = (Sheep) le;
  248. if (!sheep.isSheared()) {
  249. Wool wool = new Wool();
  250. wool.setColor(sheep.getColor());
  251. ItemStack theWool = wool.toItemStack();
  252. theWool.setAmount(1 + random.nextInt(6));
  253. Misc.dropItem(location, theWool);
  254. sheep.setSheared(true);
  255. }
  256. break;
  257. case SKELETON:
  258. if (DROP_NUMBER > 50) {
  259. Misc.dropItem(location, new ItemStack(Material.BONE));
  260. }
  261. else {
  262. Misc.dropItems(location, new ItemStack(Material.ARROW), 3);
  263. }
  264. break;
  265. case SLIME:
  266. Misc.dropItem(location, new ItemStack(Material.SLIME_BALL));
  267. break;
  268. case SNOWMAN:
  269. if (DROP_NUMBER > 99) {
  270. Misc.dropItem(location, new ItemStack(Material.PUMPKIN));
  271. }
  272. else {
  273. Misc.dropItems(location, new ItemStack(Material.SNOW_BALL), 5);
  274. }
  275. break;
  276. case SPIDER:
  277. if (DROP_NUMBER > 50) {
  278. Misc.dropItem(location, new ItemStack(Material.SPIDER_EYE));
  279. }
  280. else {
  281. Misc.dropItem(location, new ItemStack(Material.STRING));
  282. }
  283. break;
  284. case SQUID:
  285. Misc.dropItem(location, new ItemStack(Material.INK_SACK, 1, (short) 0, (byte) 0x0));
  286. break;
  287. case ZOMBIE:
  288. Misc.dropItem(location, new ItemStack(Material.ROTTEN_FLESH));
  289. break;
  290. default:
  291. break;
  292. }
  293. Combat.dealDamage(le, 1);
  294. }
  295. }