Fishing.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 handlers.skillhandlers;
  16. import com.l2jserver.Config;
  17. import com.l2jserver.gameserver.GeoData;
  18. import com.l2jserver.gameserver.handler.ISkillHandler;
  19. import com.l2jserver.gameserver.instancemanager.ZoneManager;
  20. import com.l2jserver.gameserver.model.L2Object;
  21. import com.l2jserver.gameserver.model.L2Skill;
  22. import com.l2jserver.gameserver.model.actor.L2Character;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.model.item.L2Weapon;
  25. import com.l2jserver.gameserver.model.item.instance.L2ItemInstance;
  26. import com.l2jserver.gameserver.model.item.type.L2WeaponType;
  27. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  28. import com.l2jserver.gameserver.model.zone.L2ZoneType;
  29. import com.l2jserver.gameserver.model.zone.type.L2FishingZone;
  30. import com.l2jserver.gameserver.model.zone.type.L2WaterZone;
  31. import com.l2jserver.gameserver.network.SystemMessageId;
  32. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  33. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  34. import com.l2jserver.gameserver.util.Util;
  35. import com.l2jserver.util.Rnd;
  36. public class Fishing implements ISkillHandler
  37. {
  38. private static final L2SkillType[] SKILL_IDS =
  39. {
  40. L2SkillType.FISHING
  41. };
  42. @Override
  43. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  44. {
  45. if (!(activeChar instanceof L2PcInstance))
  46. return;
  47. final L2PcInstance player = activeChar.getActingPlayer();
  48. /*
  49. * If fishing is disabled, there isn't much point in doing anything
  50. * else, unless you are GM. so this got moved up here, before anything else.
  51. */
  52. if (!Config.ALLOWFISHING && !player.isGM())
  53. {
  54. player.sendMessage("Fishing server is currently offline");
  55. return;
  56. }
  57. if (player.isFishing())
  58. {
  59. if (player.getFishCombat() != null)
  60. player.getFishCombat().doDie(false);
  61. else
  62. player.endFishing(false);
  63. // Cancels fishing
  64. player.sendPacket(SystemMessageId.FISHING_ATTEMPT_CANCELLED);
  65. return;
  66. }
  67. L2Weapon weaponItem = player.getActiveWeaponItem();
  68. if ((weaponItem == null || weaponItem.getItemType() != L2WeaponType.FISHINGROD))
  69. {
  70. // Fishing poles are not installed
  71. player.sendPacket(SystemMessageId.FISHING_POLE_NOT_EQUIPPED);
  72. return;
  73. }
  74. L2ItemInstance lure = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LHAND);
  75. if (lure == null)
  76. {
  77. // Bait not equiped.
  78. player.sendPacket(SystemMessageId.BAIT_ON_HOOK_BEFORE_FISHING);
  79. return;
  80. }
  81. player.setLure(lure);
  82. L2ItemInstance lure2 = player.getInventory().getPaperdollItem(Inventory.PAPERDOLL_LHAND);
  83. if (lure2 == null || lure2.getCount() < 1) // Not enough bait.
  84. {
  85. player.sendPacket(SystemMessageId.NOT_ENOUGH_BAIT);
  86. return;
  87. }
  88. if (!player.isGM())
  89. {
  90. if (player.isInBoat())
  91. {
  92. // You can't fish while you are on boat
  93. player.sendPacket(SystemMessageId.CANNOT_FISH_ON_BOAT);
  94. return;
  95. }
  96. if (player.isInCraftMode() || player.isInStoreMode())
  97. {
  98. player.sendPacket(SystemMessageId.CANNOT_FISH_WHILE_USING_RECIPE_BOOK);
  99. return;
  100. }
  101. if (player.isInsideZone(L2Character.ZONE_WATER))
  102. {
  103. // You can't fish in water
  104. player.sendPacket(SystemMessageId.CANNOT_FISH_UNDER_WATER);
  105. return;
  106. }
  107. if (player.isInsideZone(L2Character.ZONE_PEACE))
  108. {
  109. // You can't fish here.
  110. player.sendPacket(SystemMessageId.CANNOT_FISH_HERE);
  111. return;
  112. }
  113. }
  114. /*
  115. * If fishing is enabled, here is the code that was striped from
  116. * startFishing() in L2PcInstance. Decide now where will the hook be cast...
  117. */
  118. int rnd = Rnd.get(150) + 50;
  119. double angle = Util.convertHeadingToDegree(player.getHeading());
  120. double radian = Math.toRadians(angle);
  121. double sin = Math.sin(radian);
  122. double cos = Math.cos(radian);
  123. int x = player.getX() + (int) (cos * rnd);
  124. int y = player.getY() + (int) (sin * rnd);
  125. int z = player.getZ() + 50;
  126. /*
  127. * ...and if the spot is in a fishing zone. If it is, it will then
  128. * position the hook on the water surface. If not, you have to be GM to
  129. * proceed past here... in that case, the hook will be positioned using
  130. * the old Z lookup method.
  131. */
  132. L2FishingZone aimingTo = null;
  133. L2WaterZone water = null;
  134. boolean canFish = false;
  135. for (L2ZoneType zone : ZoneManager.getInstance().getZones(x, y))
  136. {
  137. if (zone instanceof L2FishingZone)
  138. {
  139. aimingTo = (L2FishingZone)zone;
  140. continue;
  141. }
  142. if (zone instanceof L2WaterZone)
  143. {
  144. water = (L2WaterZone)zone;
  145. }
  146. }
  147. if (aimingTo != null)
  148. {
  149. // fishing zone found, we can fish here
  150. if (Config.GEODATA > 0)
  151. {
  152. // geodata enabled, checking if we can see end of the pole
  153. if (GeoData.getInstance().canSeeTarget(player.getX(), player.getY(), z, x, y, z))
  154. {
  155. // finding z level for hook
  156. if (water != null)
  157. {
  158. // water zone exist
  159. if (GeoData.getInstance().getHeight(x, y, z) < water.getWaterZ())
  160. {
  161. // water Z is higher than geo Z
  162. z = water.getWaterZ() + 10;
  163. canFish = true;
  164. }
  165. }
  166. else
  167. {
  168. // no water zone, using fishing zone
  169. if (GeoData.getInstance().getHeight(x, y, z) < aimingTo.getWaterZ())
  170. {
  171. // fishing Z is higher than geo Z
  172. z = aimingTo.getWaterZ() + 10;
  173. canFish = true;
  174. }
  175. }
  176. }
  177. }
  178. else
  179. {
  180. // geodata disabled
  181. // if water zone exist using it, if not - using fishing zone
  182. if (water != null)
  183. z = water.getWaterZ() + 10;
  184. else
  185. z = aimingTo.getWaterZ() + 10;
  186. canFish = true;
  187. }
  188. }
  189. if (!canFish)
  190. {
  191. // You can't fish here
  192. player.sendPacket(SystemMessageId.CANNOT_FISH_HERE);
  193. if (!player.isGM())
  194. return;
  195. }
  196. // Has enough bait, consume 1 and update inventory. Start fishing
  197. // follows.
  198. lure2 = player.getInventory().destroyItem("Consume", player.getInventory().getPaperdollObjectId(Inventory.PAPERDOLL_LHAND), 1, player, null);
  199. InventoryUpdate iu = new InventoryUpdate();
  200. iu.addModifiedItem(lure2);
  201. player.sendPacket(iu);
  202. // If everything else checks out, actually cast the hook and start
  203. // fishing... :P
  204. player.startFishing(x, y, z);
  205. }
  206. @Override
  207. public L2SkillType[] getSkillIds()
  208. {
  209. return SKILL_IDS;
  210. }
  211. }