Fishing.java 7.2 KB

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