RequestDuelStart.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 com.l2jserver.gameserver.network.clientpackets;
  16. import java.util.logging.Logger;
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.model.L2World;
  19. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  20. import com.l2jserver.gameserver.network.SystemMessageId;
  21. import com.l2jserver.gameserver.network.serverpackets.ExDuelAskStart;
  22. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  23. /**
  24. * Format:(ch) Sd
  25. * @author -Wooden-
  26. */
  27. public final class RequestDuelStart extends L2GameClientPacket
  28. {
  29. private static final String _C__D0_27_REQUESTDUELSTART = "[C] D0:27 RequestDuelStart";
  30. private static Logger _log = Logger.getLogger(RequestDuelStart.class.getName());
  31. private String _player;
  32. private int _partyDuel;
  33. @Override
  34. protected void readImpl()
  35. {
  36. _player = readS();
  37. _partyDuel = readD();
  38. }
  39. /**
  40. * @see com.l2jserver.util.network.BaseRecievePacket.ClientBasePacket#runImpl()
  41. */
  42. @Override
  43. protected void runImpl()
  44. {
  45. L2PcInstance activeChar = getClient().getActiveChar();
  46. L2PcInstance targetChar = L2World.getInstance().getPlayer(_player);
  47. if (activeChar == null)
  48. return;
  49. if (targetChar == null)
  50. {
  51. activeChar.sendPacket(new SystemMessage(SystemMessageId.THERE_IS_NO_OPPONENT_TO_RECEIVE_YOUR_CHALLENGE_FOR_A_DUEL));
  52. return;
  53. }
  54. if (activeChar == targetChar)
  55. {
  56. activeChar.sendPacket(new SystemMessage(SystemMessageId.THERE_IS_NO_OPPONENT_TO_RECEIVE_YOUR_CHALLENGE_FOR_A_DUEL));
  57. return;
  58. }
  59. // Check if duel is possible
  60. if (!activeChar.canDuel())
  61. {
  62. activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_UNABLE_TO_REQUEST_A_DUEL_AT_THIS_TIME));
  63. return;
  64. }
  65. else if (!targetChar.canDuel())
  66. {
  67. activeChar.sendPacket(targetChar.getNoDuelReason());
  68. return;
  69. }
  70. // Players may not be too far apart
  71. else if (!activeChar.isInsideRadius(targetChar, 250, false, false))
  72. {
  73. SystemMessage msg = new SystemMessage(SystemMessageId.C1_CANNOT_RECEIVE_A_DUEL_CHALLENGE_BECAUSE_C1_IS_TOO_FAR_AWAY);
  74. msg.addString(targetChar.getName());
  75. activeChar.sendPacket(msg);
  76. return;
  77. }
  78. // Duel is a party duel
  79. if (_partyDuel == 1)
  80. {
  81. // Player must be in a party & the party leader
  82. if (!activeChar.isInParty() || !(activeChar.isInParty() && activeChar.getParty().isLeader(activeChar)))
  83. {
  84. activeChar.sendMessage("You have to be the leader of a party in order to request a party duel.");
  85. return;
  86. }
  87. // Target must be in a party
  88. else if (!targetChar.isInParty())
  89. {
  90. activeChar.sendPacket(new SystemMessage(SystemMessageId.SINCE_THE_PERSON_YOU_CHALLENGED_IS_NOT_CURRENTLY_IN_A_PARTY_THEY_CANNOT_DUEL_AGAINST_YOUR_PARTY));
  91. return;
  92. }
  93. // Target may not be of the same party
  94. else if (activeChar.getParty().getPartyMembers().contains(targetChar))
  95. {
  96. activeChar.sendMessage("This player is a member of your own party.");
  97. return;
  98. }
  99. // Check if every player is ready for a duel
  100. for (L2PcInstance temp : activeChar.getParty().getPartyMembers())
  101. {
  102. if (!temp.canDuel())
  103. {
  104. activeChar.sendMessage("Not all the members of your party are ready for a duel.");
  105. return;
  106. }
  107. }
  108. L2PcInstance partyLeader = null; // snatch party leader of targetChar's party
  109. for (L2PcInstance temp : targetChar.getParty().getPartyMembers())
  110. {
  111. if (partyLeader == null) partyLeader = temp;
  112. if (!temp.canDuel())
  113. {
  114. activeChar.sendPacket(new SystemMessage(SystemMessageId.THE_OPPOSING_PARTY_IS_CURRENTLY_UNABLE_TO_ACCEPT_A_CHALLENGE_TO_A_DUEL));
  115. return;
  116. }
  117. }
  118. // Send request to targetChar's party leader
  119. if (!partyLeader.isProcessingRequest())
  120. {
  121. activeChar.onTransactionRequest(partyLeader);
  122. partyLeader.sendPacket(new ExDuelAskStart(activeChar.getName(), _partyDuel));
  123. if (Config.DEBUG)
  124. _log.fine(activeChar.getName() + " requested a duel with " + partyLeader.getName());
  125. SystemMessage msg = new SystemMessage(SystemMessageId.C1_PARTY_HAS_BEEN_CHALLENGED_TO_A_DUEL);
  126. msg.addString(partyLeader.getName());
  127. activeChar.sendPacket(msg);
  128. msg = new SystemMessage(SystemMessageId.C1_PARTY_HAS_CHALLENGED_YOUR_PARTY_TO_A_DUEL);
  129. msg.addString(activeChar.getName());
  130. targetChar.sendPacket(msg);
  131. }
  132. else
  133. {
  134. SystemMessage msg = new SystemMessage(SystemMessageId.C1_IS_BUSY_TRY_LATER);
  135. msg.addString(partyLeader.getName());
  136. activeChar.sendPacket(msg);
  137. }
  138. }
  139. else // 1vs1 duel
  140. {
  141. if (!targetChar.isProcessingRequest())
  142. {
  143. activeChar.onTransactionRequest(targetChar);
  144. targetChar.sendPacket(new ExDuelAskStart(activeChar.getName(), _partyDuel));
  145. if (Config.DEBUG)
  146. _log.fine(activeChar.getName() + " requested a duel with " + targetChar.getName());
  147. SystemMessage msg = new SystemMessage(SystemMessageId.C1_HAS_BEEN_CHALLENGED_TO_A_DUEL);
  148. msg.addString(targetChar.getName());
  149. activeChar.sendPacket(msg);
  150. msg = new SystemMessage(SystemMessageId.C1_HAS_CHALLENGED_YOU_TO_A_DUEL);
  151. msg.addString(activeChar.getName());
  152. targetChar.sendPacket(msg);
  153. }
  154. else
  155. {
  156. SystemMessage msg = new SystemMessage(SystemMessageId.C1_IS_BUSY_TRY_LATER);
  157. msg.addString(targetChar.getName());
  158. activeChar.sendPacket(msg);
  159. }
  160. }
  161. }
  162. /**
  163. * @see com.l2jserver.gameserver.BasePacket#getType()
  164. */
  165. @Override
  166. public String getType()
  167. {
  168. return _C__D0_27_REQUESTDUELSTART;
  169. }
  170. }