Fishing.java 7.0 KB

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