Fishing.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. This file is part of mcMMO.
  3. mcMMO is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. mcMMO is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with mcMMO. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package com.gmail.nossr50.skills;
  15. import java.util.List;
  16. import org.bukkit.Location;
  17. import org.bukkit.Material;
  18. import org.bukkit.enchantments.Enchantment;
  19. import org.bukkit.entity.*;
  20. import org.bukkit.event.player.PlayerFishEvent;
  21. import org.bukkit.inventory.ItemStack;
  22. import org.bukkit.material.Wool;
  23. import com.gmail.nossr50.Combat;
  24. import com.gmail.nossr50.Users;
  25. import com.gmail.nossr50.m;
  26. import com.gmail.nossr50.config.LoadProperties;
  27. import com.gmail.nossr50.datatypes.PlayerProfile;
  28. import com.gmail.nossr50.datatypes.SkillType;
  29. import com.gmail.nossr50.datatypes.treasure.FishingTreasure;
  30. import com.gmail.nossr50.locale.mcLocale;
  31. public class Fishing {
  32. //Return the fishing tier for the player
  33. public static int getFishingLootTier(PlayerProfile PP)
  34. {
  35. int lvl = PP.getSkillLevel(SkillType.FISHING);
  36. if(lvl >= LoadProperties.fishingTier1 && lvl < LoadProperties.fishingTier2)
  37. {
  38. return 1;
  39. }
  40. else if (lvl >= LoadProperties.fishingTier2 && lvl < LoadProperties.fishingTier3)
  41. {
  42. return 2;
  43. }
  44. else if (lvl >= LoadProperties.fishingTier3 && lvl < LoadProperties.fishingTier4)
  45. {
  46. return 3;
  47. }
  48. else if (lvl >= LoadProperties.fishingTier4 && lvl < LoadProperties.fishingTier5)
  49. {
  50. return 4;
  51. }
  52. else
  53. {
  54. return 5;
  55. }
  56. }
  57. public static void getFishingResults(Player player, PlayerFishEvent event)
  58. {
  59. switch(getFishingLootTier(Users.getProfile(player)))
  60. {
  61. case 1:
  62. getFishingResultsTier1(player, event);
  63. break;
  64. case 2:
  65. getFishingResultsTier2(player, event);
  66. break;
  67. case 3:
  68. getFishingResultsTier3(player, event);
  69. break;
  70. case 4:
  71. getFishingResultsTier4(player, event);
  72. break;
  73. case 5:
  74. getFishingResultsTier5(player, event);
  75. break;
  76. }
  77. m.mcDropItem(player.getLocation(), new ItemStack(Material.RAW_FISH, 1));
  78. Users.getProfile(player).addXP(SkillType.FISHING, LoadProperties.mfishing, player);
  79. Skills.XpCheckSkill(SkillType.FISHING, player);
  80. }
  81. private static void getFishingResultsTier1(Player player, PlayerFishEvent event)
  82. {
  83. Item theCatch = (Item)event.getCaught();
  84. if(LoadProperties.fishingDrops)
  85. {
  86. List<FishingTreasure> rewards = LoadProperties.fishingRewardsTier1;
  87. FishingTreasure treasure = rewards.get((int)(Math.random() * rewards.size()));
  88. if(Math.random() * 100 > (100.00 - treasure.getDropChance()))
  89. {
  90. Users.getProfile(player).addXP(SkillType.FISHING, treasure.getXp(), player);
  91. theCatch.setItemStack(treasure.getDrop());
  92. }
  93. }
  94. else
  95. {
  96. theCatch.setItemStack(new ItemStack(Material.RAW_FISH));
  97. }
  98. //Change durability to random value
  99. theCatch.getItemStack().setDurability((short) (Math.random() * theCatch.getItemStack().getType().getMaxDurability())); //Change the damage value
  100. }
  101. private static void getFishingResultsTier2(Player player, PlayerFishEvent event)
  102. {
  103. Item theCatch = (Item)event.getCaught();
  104. if(LoadProperties.fishingDrops)
  105. {
  106. List<FishingTreasure> rewards = LoadProperties.fishingRewardsTier2;
  107. FishingTreasure treasure = rewards.get((int)(Math.random() * rewards.size()));
  108. if(Math.random() * 100 > (100.00 - treasure.getDropChance()))
  109. {
  110. Users.getProfile(player).addXP(SkillType.FISHING, treasure.getXp(), player);
  111. theCatch.setItemStack(treasure.getDrop());
  112. }
  113. }
  114. else
  115. {
  116. theCatch.setItemStack(new ItemStack(Material.RAW_FISH));
  117. }
  118. //Change durability to random value
  119. theCatch.getItemStack().setDurability((short) (Math.random() * theCatch.getItemStack().getType().getMaxDurability())); //Change the damage value
  120. }
  121. private static void getFishingResultsTier3(Player player, PlayerFishEvent event)
  122. {
  123. Item theCatch = (Item)event.getCaught();
  124. if(LoadProperties.fishingDrops)
  125. {
  126. List<FishingTreasure> rewards = LoadProperties.fishingRewardsTier3;
  127. FishingTreasure treasure = rewards.get((int)(Math.random() * rewards.size()));
  128. if(Math.random() * 100 > (100.00 - treasure.getDropChance()))
  129. {
  130. Users.getProfile(player).addXP(SkillType.FISHING, treasure.getXp(), player);
  131. theCatch.setItemStack(treasure.getDrop());
  132. }
  133. }
  134. else
  135. {
  136. theCatch.setItemStack(new ItemStack(Material.RAW_FISH));
  137. }
  138. //Change durability to random value
  139. theCatch.getItemStack().setDurability((short) (Math.random() * theCatch.getItemStack().getType().getMaxDurability())); //Change the damage value
  140. }
  141. private static void getFishingResultsTier4(Player player, PlayerFishEvent event)
  142. {
  143. Item theCatch = (Item)event.getCaught();
  144. if(LoadProperties.fishingDrops)
  145. {
  146. List<FishingTreasure> rewards = LoadProperties.fishingRewardsTier4;
  147. FishingTreasure treasure = rewards.get((int)(Math.random() * rewards.size()));
  148. if(Math.random() * 100 > (100.00 - treasure.getDropChance()))
  149. {
  150. Users.getProfile(player).addXP(SkillType.FISHING, treasure.getXp(), player);
  151. theCatch.setItemStack(treasure.getDrop());
  152. }
  153. }
  154. else
  155. {
  156. theCatch.setItemStack(new ItemStack(Material.RAW_FISH));
  157. }
  158. //Change durability to random value
  159. theCatch.getItemStack().setDurability((short) (Math.random() * theCatch.getItemStack().getType().getMaxDurability())); //Change the damage value
  160. }
  161. private static void getFishingResultsTier5(Player player, PlayerFishEvent event)
  162. {
  163. Item theCatch = (Item)event.getCaught();
  164. if(LoadProperties.fishingDrops)
  165. {
  166. List<FishingTreasure> rewards = LoadProperties.fishingRewardsTier5;
  167. FishingTreasure treasure = rewards.get((int)(Math.random() * rewards.size()));
  168. if(Math.random() * 100 > (100.00 - treasure.getDropChance()))
  169. {
  170. Users.getProfile(player).addXP(SkillType.FISHING, treasure.getXp(), player);
  171. theCatch.setItemStack(treasure.getDrop());
  172. }
  173. }
  174. else
  175. {
  176. theCatch.setItemStack(new ItemStack(Material.RAW_FISH));
  177. }
  178. //Change durability to random value
  179. theCatch.getItemStack().setDurability((short) (Math.random() * theCatch.getItemStack().getType().getMaxDurability())); //Change the damage value
  180. }
  181. public static void processResults(PlayerFishEvent event)
  182. {
  183. Player player = event.getPlayer();
  184. PlayerProfile PP = Users.getProfile(player);
  185. Fishing.getFishingResults(player, event);
  186. Item theCatch = (Item)event.getCaught();
  187. if(theCatch.getItemStack().getType() != Material.RAW_FISH)
  188. {
  189. //Inform the player they retrieved a treasure...
  190. player.sendMessage(mcLocale.getString("Fishing.ItemFound"));
  191. //Keep track of whether or not the treasure is enchanted
  192. boolean enchanted = false;
  193. ItemStack fishingResults = theCatch.getItemStack();
  194. if(Repair.isArmor(fishingResults) || Repair.isTools(fishingResults))
  195. {
  196. //Fishing up items will have a 10% chance to enter them into random enchantment lottery
  197. if(Math.random() * 100 < 10)
  198. {
  199. for(Enchantment x : Enchantment.values())
  200. {
  201. if(x.canEnchantItem(fishingResults))
  202. {
  203. //Prevent impossible enchantment combinations
  204. if((fishingResults.containsEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL) || fishingResults.containsEnchantment(Enchantment.PROTECTION_EXPLOSIONS) ||
  205. fishingResults.containsEnchantment(Enchantment.PROTECTION_FIRE) || fishingResults.containsEnchantment(Enchantment.PROTECTION_PROJECTILE)) &&
  206. (x.equals(Enchantment.PROTECTION_EXPLOSIONS) || x.equals(Enchantment.PROTECTION_PROJECTILE) || x.equals(Enchantment.PROTECTION_FIRE) || x.equals(Enchantment.PROTECTION_ENVIRONMENTAL))){
  207. return;
  208. }
  209. //More impossible enchantment combinations
  210. else if((fishingResults.containsEnchantment(Enchantment.DAMAGE_ALL) || fishingResults.containsEnchantment(Enchantment.DAMAGE_ARTHROPODS) || fishingResults.containsEnchantment(Enchantment.DAMAGE_UNDEAD)) &&
  211. (x.equals(Enchantment.DAMAGE_ALL) || x.equals(Enchantment.DAMAGE_ARTHROPODS) || x.equals(Enchantment.DAMAGE_UNDEAD))){
  212. return;
  213. }
  214. //Even more impossible enchantment combinations
  215. else if((fishingResults.containsEnchantment(Enchantment.SILK_TOUCH) || fishingResults.containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)) &&
  216. (x.equals(Enchantment.SILK_TOUCH) || x.equals(Enchantment.LOOT_BONUS_BLOCKS))){
  217. return;
  218. }
  219. else{
  220. //Actual chance to have an enchantment is related to your fishing skill
  221. if(Math.random() * 15 < Fishing.getFishingLootTier(PP))
  222. {
  223. enchanted = true;
  224. int randomEnchantLevel = (int)(Math.random() * x.getMaxLevel()) + 1;
  225. if(randomEnchantLevel == 0)
  226. randomEnchantLevel = 1;
  227. fishingResults.addEnchantment(x, randomEnchantLevel);
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. //Inform the player of magical properties
  235. if(enchanted)
  236. {
  237. player.sendMessage(mcLocale.getString("Fishing.MagicFound"));
  238. }
  239. }
  240. }
  241. public static void shakeMob(PlayerFishEvent event)
  242. {
  243. LivingEntity le = (LivingEntity)event.getCaught();
  244. //Do nothing to players
  245. if(le instanceof Player)
  246. return;
  247. Combat.dealDamage(le, 1);
  248. Location loc = le.getLocation();
  249. /* Neutral Mobs */
  250. if(le instanceof Sheep)
  251. {
  252. Sheep sheep = (Sheep)le;
  253. if(!sheep.isSheared())
  254. {
  255. Wool wool = new Wool();
  256. wool.setColor(sheep.getColor());
  257. ItemStack theWool = wool.toItemStack();
  258. theWool.setAmount((int)(Math.random() * 6));
  259. m.mcDropItem(loc, theWool);
  260. sheep.setSheared(true);
  261. }
  262. }
  263. else if(le instanceof Pig)
  264. {
  265. m.mcDropItem(loc, new ItemStack(Material.PORK, 1));
  266. }
  267. else if(le instanceof Cow)
  268. {
  269. if(Math.random() * 100 >= 99){
  270. m.mcDropItem(loc, new ItemStack(Material.MILK_BUCKET, 1)); //rare chance to drop milk
  271. }
  272. else if(Math.random() * 10 < 5){
  273. m.mcDropItem(loc, new ItemStack(Material.LEATHER, 1));
  274. }
  275. else{
  276. m.mcDropItem(loc, new ItemStack(Material.RAW_BEEF, 1));
  277. }
  278. }
  279. else if(le instanceof Chicken)
  280. {
  281. if(Math.random() * 10 <= 7){
  282. if(Math.random() * 10 < 5){
  283. m.mcDropItem(loc, new ItemStack(Material.FEATHER, 1));
  284. }
  285. else{
  286. m.mcDropItem(loc, new ItemStack(Material.RAW_CHICKEN, 1));
  287. }
  288. }
  289. else{
  290. m.mcDropItem(loc, new ItemStack(Material.EGG, 1));
  291. }
  292. }
  293. //need to implement new shearing method
  294. else if(le instanceof MushroomCow)
  295. {
  296. if(Math.random() * 100 >= 99){
  297. if(Math.random() * 10 < 5){
  298. m.mcDropItem(loc, new ItemStack(Material.MILK_BUCKET, 1)); //rare chance to drop milk
  299. }
  300. else{
  301. m.mcDropItem(loc, new ItemStack(Material.MUSHROOM_SOUP, 1)); //rare chance to drop soup
  302. }
  303. }
  304. else if(Math.random() * 10 <= 7){
  305. if(Math.random() * 10 < 5){
  306. m.mcDropItem(loc, new ItemStack(Material.LEATHER, 1));
  307. }
  308. else{
  309. m.mcDropItem(loc, new ItemStack(Material.RAW_BEEF, 1));
  310. }
  311. }
  312. else{
  313. m.mcDropItem(loc, new ItemStack(Material.RED_MUSHROOM, 3));
  314. //need some way to remove MushroomCow & replace with regular cow when sheared
  315. }
  316. }
  317. else if(le instanceof Squid)
  318. {
  319. m.mcDropItem(loc, new ItemStack(Material.getMaterial(351), 1, (byte)0, (byte)0));
  320. }
  321. else if(le instanceof Snowman){
  322. if(Math.random() * 100 >= 99){
  323. m.mcDropItem(loc, new ItemStack(Material.PUMPKIN, 1)); //rare chance to drop pumpkin
  324. }
  325. else{
  326. m.mcDropItem(loc, new ItemStack(Material.SNOW_BALL, 5));
  327. }
  328. }
  329. /* Hostile Mobs */
  330. else if(le instanceof Skeleton)
  331. {
  332. if(Math.random() * 10 < 5)
  333. m.mcDropItem(loc, new ItemStack(Material.BONE, 1));
  334. else
  335. m.mcDropItem(loc, new ItemStack(Material.ARROW, 3));
  336. }
  337. else if(le instanceof Spider)
  338. {
  339. if(Math.random() * 10 < 5)
  340. m.mcDropItem(loc, new ItemStack(Material.SPIDER_EYE, 1));
  341. else
  342. m.mcDropItem(loc, new ItemStack(Material.STRING, 1));
  343. }
  344. else if(le instanceof Creeper)
  345. {
  346. m.mcDropItem(loc, new ItemStack(Material.SULPHUR, 1));
  347. }
  348. else if(le instanceof Enderman)
  349. {
  350. m.mcDropItem(loc, new ItemStack(Material.ENDER_PEARL, 1));
  351. }
  352. else if(le instanceof PigZombie)
  353. {
  354. if(Math.random() * 10 < 5)
  355. m.mcDropItem(loc, new ItemStack(Material.ROTTEN_FLESH, 1));
  356. else
  357. m.mcDropItem(loc, new ItemStack(Material.GOLD_NUGGET, 1));
  358. }
  359. else if(le instanceof Blaze)
  360. {
  361. m.mcDropItem(loc, new ItemStack(Material.BLAZE_ROD, 1));
  362. }
  363. else if(le instanceof CaveSpider)
  364. {
  365. if(Math.random() * 10 < 5)
  366. m.mcDropItem(loc, new ItemStack(Material.SPIDER_EYE, 1));
  367. else
  368. m.mcDropItem(loc, new ItemStack(Material.STRING, 1));
  369. }
  370. else if(le instanceof Ghast)
  371. {
  372. if(Math.random() * 10 < 5)
  373. m.mcDropItem(loc, new ItemStack(Material.SULPHUR, 1));
  374. else
  375. m.mcDropItem(loc, new ItemStack(Material.GHAST_TEAR, 1));
  376. }
  377. else if(le instanceof MagmaCube)
  378. {
  379. m.mcDropItem(loc, new ItemStack(Material.MAGMA_CREAM, 1));
  380. }
  381. else if(le instanceof Slime)
  382. {
  383. m.mcDropItem(loc, new ItemStack(Material.SLIME_BALL, 1));
  384. }
  385. else if(le instanceof Zombie)
  386. {
  387. m.mcDropItem(loc, new ItemStack(Material.ROTTEN_FLESH, 1));
  388. }
  389. }
  390. }