Fishing.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.handler.skillhandlers;
  16. import net.sf.l2j.Config;
  17. import net.sf.l2j.gameserver.GeoData;
  18. import net.sf.l2j.gameserver.handler.ISkillHandler;
  19. import net.sf.l2j.gameserver.instancemanager.FishingZoneManager;
  20. import net.sf.l2j.gameserver.model.Inventory;
  21. import net.sf.l2j.gameserver.model.L2Character;
  22. import net.sf.l2j.gameserver.model.L2ItemInstance;
  23. import net.sf.l2j.gameserver.model.L2Object;
  24. import net.sf.l2j.gameserver.model.L2Skill;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  26. import net.sf.l2j.gameserver.model.zone.type.L2FishingZone;
  27. import net.sf.l2j.gameserver.model.zone.type.L2WaterZone;
  28. import net.sf.l2j.gameserver.network.SystemMessageId;
  29. import net.sf.l2j.gameserver.network.serverpackets.InventoryUpdate;
  30. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  31. import net.sf.l2j.gameserver.templates.L2SkillType;
  32. import net.sf.l2j.gameserver.templates.L2Weapon;
  33. import net.sf.l2j.gameserver.templates.L2WeaponType;
  34. import net.sf.l2j.gameserver.util.Util;
  35. import net.sf.l2j.util.Rnd;
  36. public class Fishing implements ISkillHandler
  37. {
  38. private static final L2SkillType[] SKILL_IDS =
  39. {
  40. L2SkillType.FISHING
  41. };
  42. /**
  43. *
  44. * @see net.sf.l2j.gameserver.handler.ISkillHandler#useSkill(net.sf.l2j.gameserver.model.L2Character, net.sf.l2j.gameserver.model.L2Skill, net.sf.l2j.gameserver.model.L2Object[])
  45. */
  46. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  47. {
  48. if (!(activeChar instanceof L2PcInstance))
  49. return;
  50. L2PcInstance player = (L2PcInstance) activeChar;
  51. /*
  52. * If fishing is disabled, there isn't much point in doing anything
  53. * else, unless you are GM. so this got moved up here, before anything
  54. * else.
  55. */
  56. if (!Config.ALLOWFISHING && !player.isGM())
  57. {
  58. player.sendMessage("Fishing server is currently offline");
  59. return;
  60. }
  61. if (player.isFishing())
  62. {
  63. if (player.getFishCombat() != null)
  64. player.getFishCombat().doDie(false);
  65. else
  66. player.endFishing(false);
  67. // Cancels fishing
  68. player.sendPacket(new SystemMessage(SystemMessageId.FISHING_ATTEMPT_CANCELLED));
  69. return;
  70. }
  71. L2Weapon weaponItem = player.getActiveWeaponItem();
  72. if ((weaponItem == null || weaponItem.getItemType() != L2WeaponType.ROD))
  73. {
  74. // Fishing poles are not installed
  75. player.sendPacket(new SystemMessage(SystemMessageId.FISHING_POLE_NOT_EQUIPPED));
  76. return;
  77. }
  78. L2ItemInstance lure = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LHAND);
  79. if (lure == null)
  80. {
  81. // Bait not equiped.
  82. player.sendPacket(new SystemMessage(SystemMessageId.BAIT_ON_HOOK_BEFORE_FISHING));
  83. return;
  84. }
  85. player.setLure(lure);
  86. L2ItemInstance lure2 = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LHAND);
  87. if (lure2 == null || lure2.getCount() < 1) // Not enough bait.
  88. {
  89. player.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_BAIT));
  90. return;
  91. }
  92. if (player.isInBoat())
  93. {
  94. // You can't fish while you are on boat
  95. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_FISH_ON_BOAT));
  96. if (!player.isGM())
  97. return;
  98. }
  99. if (player.isInCraftMode() || player.isInStoreMode())
  100. {
  101. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_FISH_WHILE_USING_RECIPE_BOOK));
  102. if (!player.isGM())
  103. return;
  104. }
  105. /*
  106. * If fishing is enabled, here is the code that was striped from
  107. * startFishing() in L2PcInstance. Decide now where will the hook be
  108. * cast...
  109. */
  110. int rnd = Rnd.get(200) + 200;
  111. double angle = Util.convertHeadingToDegree(player.getHeading());
  112. double radian = Math.toRadians(angle);
  113. double sin = Math.sin(radian);
  114. double cos = Math.cos(radian);
  115. int x1 = (int) (cos * rnd);
  116. int y1 = (int) (sin * rnd);
  117. int x = player.getX() + x1;
  118. int y = player.getY() + y1;
  119. int z = player.getZ() - 30;
  120. /*
  121. * ...and if the spot is in a fishing zone. If it is, it will then
  122. * position the hook on the water surface. If not, you have to be GM to
  123. * proceed past here... in that case, the hook will be positioned using
  124. * the old Z lookup method.
  125. */
  126. L2FishingZone aimingTo = FishingZoneManager.getInstance().isInsideFishingZone(x, y, z);
  127. L2WaterZone water = FishingZoneManager.getInstance().isInsideWaterZone(x, y, z);
  128. if (aimingTo != null && water != null && (GeoData.getInstance().canSeeTarget(player.getX(), player.getY(), player.getZ() + 50, x, y, water.getWaterZ() - 50)))
  129. {
  130. z = water.getWaterZ() + 10;
  131. // player.sendMessage("Hook x,y: " + x + "," + y + " - Water Z,
  132. // Player Z:" + z + ", " + player.getZ()); //debug line, shows hook
  133. // landing related coordinates. Uncoment if needed.
  134. }
  135. else if (aimingTo != null && GeoData.getInstance().canSeeTarget(player.getX(), player.getY(), player.getZ() + 50, x, y, aimingTo.getWaterZ() - 50))
  136. z = aimingTo.getWaterZ() + 10;
  137. else
  138. {
  139. // You can't fish here
  140. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_FISH_HERE));
  141. if (!player.isGM())
  142. {
  143. return;
  144. }
  145. }
  146. /*
  147. * Of course since you can define fishing water volumes of any height,
  148. * the function needs to be changed to cope with that. Still, this is
  149. * assuming that fishing zones water surfaces, are always above "sea
  150. * level".
  151. */
  152. if (player.getZ() <= -3800 || player.getZ() < (z - 32))
  153. {
  154. // You can't fish in water
  155. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_FISH_UNDER_WATER));
  156. if (!player.isGM())
  157. return;
  158. }
  159. // Has enough bait, consume 1 and update inventory. Start fishing
  160. // follows.
  161. lure2 = player.getInventory().destroyItem("Consume", player.getInventory().getPaperdollObjectId(Inventory.PAPERDOLL_LHAND), 1, player, null);
  162. InventoryUpdate iu = new InventoryUpdate();
  163. iu.addModifiedItem(lure2);
  164. player.sendPacket(iu);
  165. // If everything else checks out, actually cast the hook and start
  166. // fishing... :P
  167. player.startFishing(x, y, z);
  168. }
  169. /**
  170. *
  171. * @see net.sf.l2j.gameserver.handler.ISkillHandler#getSkillIds()
  172. */
  173. public L2SkillType[] getSkillIds()
  174. {
  175. return SKILL_IDS;
  176. }
  177. }