Fishing.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  43. {
  44. if (!(activeChar instanceof L2PcInstance))
  45. return;
  46. final L2PcInstance player = activeChar.getActingPlayer();
  47. /*
  48. * If fishing is disabled, there isn't much point in doing anything
  49. * else, unless you are GM. so this got moved up here, before anything 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(SystemMessageId.FISHING_ATTEMPT_CANCELLED);
  64. return;
  65. }
  66. L2Weapon weaponItem = player.getActiveWeaponItem();
  67. if ((weaponItem == null || weaponItem.getItemType() != L2WeaponType.FISHINGROD))
  68. {
  69. // Fishing poles are not installed
  70. player.sendPacket(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(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(SystemMessageId.NOT_ENOUGH_BAIT);
  85. return;
  86. }
  87. if (!player.isGM())
  88. {
  89. if (player.isInBoat())
  90. {
  91. // You can't fish while you are on boat
  92. player.sendPacket(SystemMessageId.CANNOT_FISH_ON_BOAT);
  93. return;
  94. }
  95. if (player.isInCraftMode() || player.isInStoreMode())
  96. {
  97. player.sendPacket(SystemMessageId.CANNOT_FISH_WHILE_USING_RECIPE_BOOK);
  98. return;
  99. }
  100. if (player.isInsideZone(L2Character.ZONE_WATER))
  101. {
  102. // You can't fish in water
  103. player.sendPacket(SystemMessageId.CANNOT_FISH_UNDER_WATER);
  104. return;
  105. }
  106. if (player.isInsideZone(L2Character.ZONE_PEACE))
  107. {
  108. // You can't fish here.
  109. player.sendPacket(SystemMessageId.CANNOT_FISH_HERE);
  110. return;
  111. }
  112. }
  113. /*
  114. * If fishing is enabled, here is the code that was striped from
  115. * startFishing() in L2PcInstance. Decide now where will the hook be cast...
  116. */
  117. int rnd = Rnd.get(150) + 50;
  118. double angle = Util.convertHeadingToDegree(player.getHeading());
  119. double radian = Math.toRadians(angle);
  120. double sin = Math.sin(radian);
  121. double cos = Math.cos(radian);
  122. int x = player.getX() + (int) (cos * rnd);
  123. int y = player.getY() + (int) (sin * rnd);
  124. int z = player.getZ() + 50;
  125. /*
  126. * ...and if the spot is in a fishing zone. If it is, it will then
  127. * position the hook on the water surface. If not, you have to be GM to
  128. * proceed past here... in that case, the hook will be positioned using
  129. * the old Z lookup method.
  130. */
  131. L2FishingZone aimingTo = null;
  132. L2WaterZone water = null;
  133. boolean canFish = false;
  134. for (L2ZoneType zone : ZoneManager.getInstance().getZones(x, y))
  135. {
  136. if (zone instanceof L2FishingZone)
  137. {
  138. aimingTo = (L2FishingZone)zone;
  139. continue;
  140. }
  141. if (zone instanceof L2WaterZone)
  142. {
  143. water = (L2WaterZone)zone;
  144. }
  145. }
  146. if (aimingTo != null)
  147. {
  148. // fishing zone found, we can fish here
  149. if (Config.GEODATA > 0)
  150. {
  151. // geodata enabled, checking if we can see end of the pole
  152. if (GeoData.getInstance().canSeeTarget(player.getX(), player.getY(), z, x, y, z))
  153. {
  154. // finding z level for hook
  155. if (water != null)
  156. {
  157. // water zone exist
  158. if (GeoData.getInstance().getHeight(x, y, z) < water.getWaterZ())
  159. {
  160. // water Z is higher than geo Z
  161. z = water.getWaterZ() + 10;
  162. canFish = true;
  163. }
  164. }
  165. else
  166. {
  167. // no water zone, using fishing zone
  168. if (GeoData.getInstance().getHeight(x, y, z) < aimingTo.getWaterZ())
  169. {
  170. // fishing Z is higher than geo Z
  171. z = aimingTo.getWaterZ() + 10;
  172. canFish = true;
  173. }
  174. }
  175. }
  176. }
  177. else
  178. {
  179. // geodata disabled
  180. // if water zone exist using it, if not - using fishing zone
  181. if (water != null)
  182. z = water.getWaterZ() + 10;
  183. else
  184. z = aimingTo.getWaterZ() + 10;
  185. canFish = true;
  186. }
  187. }
  188. if (!canFish)
  189. {
  190. // You can't fish here
  191. player.sendPacket(SystemMessageId.CANNOT_FISH_HERE);
  192. if (!player.isGM())
  193. return;
  194. }
  195. // Has enough bait, consume 1 and update inventory. Start fishing
  196. // follows.
  197. lure2 = player.getInventory().destroyItem("Consume", player.getInventory().getPaperdollObjectId(Inventory.PAPERDOLL_LHAND), 1, player, null);
  198. InventoryUpdate iu = new InventoryUpdate();
  199. iu.addModifiedItem(lure2);
  200. player.sendPacket(iu);
  201. // If everything else checks out, actually cast the hook and start
  202. // fishing... :P
  203. player.startFishing(x, y, z);
  204. }
  205. public L2SkillType[] getSkillIds()
  206. {
  207. return SKILL_IDS;
  208. }
  209. }