L2NpcAction.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2004-2015 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.actionhandlers;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.ai.CtrlIntention;
  22. import com.l2jserver.gameserver.enums.InstanceType;
  23. import com.l2jserver.gameserver.handler.IActionHandler;
  24. import com.l2jserver.gameserver.model.L2Object;
  25. import com.l2jserver.gameserver.model.actor.L2Character;
  26. import com.l2jserver.gameserver.model.actor.L2Npc;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.entity.L2Event;
  29. import com.l2jserver.gameserver.model.events.EventDispatcher;
  30. import com.l2jserver.gameserver.model.events.EventType;
  31. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcFirstTalk;
  32. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  33. import com.l2jserver.gameserver.network.serverpackets.MoveToPawn;
  34. import com.l2jserver.util.Rnd;
  35. public class L2NpcAction implements IActionHandler
  36. {
  37. /**
  38. * Manage actions when a player click on the L2Npc.<BR>
  39. * <BR>
  40. * <B><U> Actions on first click on the L2Npc (Select it)</U> :</B><BR>
  41. * <BR>
  42. * <li>Set the L2Npc as target of the L2PcInstance player (if necessary)</li> <li>Send a Server->Client packet MyTargetSelected to the L2PcInstance player (display the select window)</li> <li>If L2Npc is autoAttackable, send a Server->Client packet StatusUpdate to the L2PcInstance in order to
  43. * update L2Npc HP bar</li> <li>Send a Server->Client packet ValidateLocation to correct the L2Npc position and heading on the client</li><BR>
  44. * <BR>
  45. * <B><U> Actions on second click on the L2Npc (Attack it/Intercat with it)</U> :</B><BR>
  46. * <BR>
  47. * <li>Send a Server->Client packet MyTargetSelected to the L2PcInstance player (display the select window)</li> <li>If L2Npc is autoAttackable, notify the L2PcInstance AI with AI_INTENTION_ATTACK (after a height verification)</li> <li>If L2Npc is NOT autoAttackable, notify the L2PcInstance AI
  48. * with AI_INTENTION_INTERACT (after a distance verification) and show message</li><BR>
  49. * <BR>
  50. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Each group of Server->Client packet must be terminated by a ActionFailed packet in order to avoid that client wait an other packet</B></FONT><BR>
  51. * <BR>
  52. * <B><U> Example of use </U> :</B><BR>
  53. * <BR>
  54. * <li>Client packet : Action, AttackRequest</li><BR>
  55. * <BR>
  56. * @param activeChar The L2PcInstance that start an action on the L2Npc
  57. */
  58. @Override
  59. public boolean action(L2PcInstance activeChar, L2Object target, boolean interact)
  60. {
  61. if (!((L2Npc) target).canTarget(activeChar))
  62. {
  63. return false;
  64. }
  65. activeChar.setLastFolkNPC((L2Npc) target);
  66. // Check if the L2PcInstance already target the L2Npc
  67. if (target != activeChar.getTarget())
  68. {
  69. // Set the target of the L2PcInstance activeChar
  70. activeChar.setTarget(target);
  71. // Check if the activeChar is attackable (without a forced attack)
  72. if (target.isAutoAttackable(activeChar))
  73. {
  74. ((L2Npc) target).getAI(); // wake up ai
  75. }
  76. }
  77. else if (interact)
  78. {
  79. // Check if the activeChar is attackable (without a forced attack) and isn't dead
  80. if (target.isAutoAttackable(activeChar) && !((L2Character) target).isAlikeDead())
  81. {
  82. // Check the height difference
  83. if (Math.abs(activeChar.getZ() - target.getZ()) < 400) // this max heigth difference might need some tweaking
  84. {
  85. // Set the L2PcInstance Intention to AI_INTENTION_ATTACK
  86. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  87. // activeChar.startAttack(this);
  88. }
  89. else
  90. {
  91. // Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
  92. activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  93. }
  94. }
  95. else if (!target.isAutoAttackable(activeChar))
  96. {
  97. // Calculate the distance between the L2PcInstance and the L2Npc
  98. if (!((L2Npc) target).canInteract(activeChar))
  99. {
  100. // Notify the L2PcInstance AI with AI_INTENTION_INTERACT
  101. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_INTERACT, target);
  102. }
  103. else
  104. {
  105. final L2Npc npc = (L2Npc) target;
  106. // Turn NPC to the player.
  107. activeChar.sendPacket(new MoveToPawn(activeChar, npc, 100));
  108. if (npc.hasRandomAnimation())
  109. {
  110. npc.onRandomAnimation(Rnd.get(8));
  111. }
  112. // Open a chat window on client with the text of the L2Npc
  113. if (npc.isEventMob())
  114. {
  115. L2Event.showEventHtml(activeChar, String.valueOf(target.getObjectId()));
  116. }
  117. else
  118. {
  119. if (npc.hasListener(EventType.ON_NPC_QUEST_START))
  120. {
  121. activeChar.setLastQuestNpcObject(target.getObjectId());
  122. }
  123. if (npc.hasListener(EventType.ON_NPC_FIRST_TALK))
  124. {
  125. EventDispatcher.getInstance().notifyEventAsync(new OnNpcFirstTalk(npc, activeChar), npc);
  126. }
  127. else
  128. {
  129. npc.showChatWindow(activeChar);
  130. }
  131. }
  132. if ((Config.PLAYER_MOVEMENT_BLOCK_TIME > 0) && !activeChar.isGM())
  133. {
  134. activeChar.updateNotMoveUntil();
  135. }
  136. }
  137. }
  138. }
  139. return true;
  140. }
  141. @Override
  142. public InstanceType getInstanceType()
  143. {
  144. return InstanceType.L2Npc;
  145. }
  146. }