Fishing.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.L2Skill.SkillType;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  27. import net.sf.l2j.gameserver.model.zone.type.L2FishingZone;
  28. import net.sf.l2j.gameserver.model.zone.type.L2WaterZone;
  29. import net.sf.l2j.gameserver.network.SystemMessageId;
  30. import net.sf.l2j.gameserver.network.serverpackets.InventoryUpdate;
  31. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  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 Logger _log = Logger.getLogger(SiegeFlag.class.getName());
  39. // protected SkillType[] _skillIds = {SkillType.FISHING};
  40. private static final SkillType[] SKILL_IDS = { SkillType.FISHING };
  41. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  42. {
  43. if (!(activeChar instanceof L2PcInstance))
  44. return;
  45. L2PcInstance player = (L2PcInstance) activeChar;
  46. /*
  47. * If fishing is disabled, there isn't much point in doing anything
  48. * else, unless you are GM. so this got moved up here, before anything
  49. * else.
  50. */
  51. if (!Config.ALLOWFISHING && !player.isGM())
  52. {
  53. player.sendMessage("Fishing server is currently offline");
  54. return;
  55. }
  56. if (player.isFishing())
  57. {
  58. if (player.getFishCombat() != null)
  59. player.getFishCombat().doDie(false);
  60. else
  61. player.endFishing(false);
  62. // Cancels fishing
  63. player.sendPacket(new SystemMessage(SystemMessageId.FISHING_ATTEMPT_CANCELLED));
  64. return;
  65. }
  66. L2Weapon weaponItem = player.getActiveWeaponItem();
  67. if ((weaponItem == null || weaponItem.getItemType() != L2WeaponType.ROD))
  68. {
  69. // Fishing poles are not installed
  70. player.sendPacket(new SystemMessage(SystemMessageId.FISHING_POLE_NOT_EQUIPPED));
  71. return;
  72. }
  73. L2ItemInstance lure = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LHAND);
  74. if (lure == null)
  75. {
  76. // Bait not equiped.
  77. player.sendPacket(new SystemMessage(SystemMessageId.BAIT_ON_HOOK_BEFORE_FISHING));
  78. return;
  79. }
  80. player.setLure(lure);
  81. L2ItemInstance lure2 = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LHAND);
  82. if (lure2 == null || lure2.getCount() < 1) // Not enough bait.
  83. {
  84. player.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_BAIT));
  85. return;
  86. }
  87. if (player.isInBoat())
  88. {
  89. // You can't fish while you are on boat
  90. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_FISH_ON_BOAT));
  91. if (!player.isGM())
  92. return;
  93. }
  94. if (player.isInCraftMode() || player.isInStoreMode())
  95. {
  96. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_FISH_WHILE_USING_RECIPE_BOOK));
  97. if (!player.isGM())
  98. return;
  99. }
  100. /*
  101. * If fishing is enabled, here is the code that was striped from
  102. * startFishing() in L2PcInstance. Decide now where will the hook be
  103. * cast...
  104. */
  105. int rnd = Rnd.get(200) + 200;
  106. double angle = Util.convertHeadingToDegree(player.getHeading());
  107. double radian = Math.toRadians(angle);
  108. double sin = Math.sin(radian);
  109. double cos = Math.cos(radian);
  110. int x1 = (int) (cos * rnd);
  111. int y1 = (int) (sin * rnd);
  112. int x = player.getX() + x1;
  113. int y = player.getY() + y1;
  114. int z = player.getZ() - 30;
  115. /*
  116. * ...and if the spot is in a fishing zone. If it is, it will then
  117. * position the hook on the water surface. If not, you have to be GM to
  118. * proceed past here... in that case, the hook will be positioned using
  119. * the old Z lookup method.
  120. */
  121. L2FishingZone aimingTo = FishingZoneManager.getInstance().isInsideFishingZone(x, y, z);
  122. L2WaterZone water = FishingZoneManager.getInstance().isInsideWaterZone(x, y, z);
  123. if (aimingTo != null && water != null
  124. && (GeoData.getInstance().canSeeTarget(player.getX(), player.getY(), player.getZ()+50, x, y, water.getWaterZ()-50)))
  125. {
  126. z = water.getWaterZ() + 10;
  127. // player.sendMessage("Hook x,y: " + x + "," + y + " - Water Z,
  128. // Player Z:" + z + ", " + player.getZ()); //debug line, shows hook
  129. // landing related coordinates. Uncoment if needed.
  130. }
  131. else if (aimingTo != null && GeoData.getInstance().canSeeTarget(player.getX(), player.getY(), player.getZ()+50, x, y, aimingTo.getWaterZ()-50))
  132. z = aimingTo.getWaterZ() +10;
  133. else
  134. {
  135. // You can't fish here
  136. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_FISH_HERE));
  137. if (!player.isGM())
  138. {
  139. return;
  140. }
  141. }
  142. /*
  143. * Of course since you can define fishing water volumes of any height,
  144. * the function needs to be changed to cope with that. Still, this is
  145. * assuming that fishing zones water surfaces, are always above "sea
  146. * level".
  147. */
  148. if (player.getZ() <= -3800 || player.getZ() < (z - 32))
  149. {
  150. // You can't fish in water
  151. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_FISH_UNDER_WATER));
  152. if (!player.isGM())
  153. return;
  154. }
  155. // Has enough bait, consume 1 and update inventory. Start fishing
  156. // follows.
  157. lure2 = player.getInventory().destroyItem("Consume", player.getInventory().getPaperdollObjectId(Inventory.PAPERDOLL_LHAND), 1, player, null);
  158. InventoryUpdate iu = new InventoryUpdate();
  159. iu.addModifiedItem(lure2);
  160. player.sendPacket(iu);
  161. // If everything else checks out, actually cast the hook and start
  162. // fishing... :P
  163. player.startFishing(x, y, z);
  164. }
  165. public SkillType[] getSkillIds()
  166. {
  167. return SKILL_IDS;
  168. }
  169. }