L2GameClientPacket.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.network.clientpackets;
  20. import java.nio.BufferUnderflowException;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.network.L2GameClient;
  26. import com.l2jserver.gameserver.network.SystemMessageId;
  27. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  28. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  29. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  30. import com.l2jserver.mmocore.ReceivablePacket;
  31. /**
  32. * Packets received by the game server from clients
  33. * @author KenM
  34. */
  35. public abstract class L2GameClientPacket extends ReceivablePacket<L2GameClient>
  36. {
  37. protected static final Logger _log = Logger.getLogger(L2GameClientPacket.class.getName());
  38. @Override
  39. public boolean read()
  40. {
  41. try
  42. {
  43. readImpl();
  44. return true;
  45. }
  46. catch (Exception e)
  47. {
  48. _log.log(Level.SEVERE, "Client: " + getClient().toString() + " - Failed reading: " + getType() + " ; " + e.getMessage(), e);
  49. if (e instanceof BufferUnderflowException)
  50. {
  51. getClient().onBufferUnderflow();
  52. }
  53. }
  54. return false;
  55. }
  56. protected abstract void readImpl();
  57. @Override
  58. public void run()
  59. {
  60. try
  61. {
  62. runImpl();
  63. /*
  64. * Removes onspawn protection - player has faster computer than average Since GE: True for all packets except RequestItemList and UseItem (in case the item is a Scroll of Escape (736)
  65. */
  66. if (triggersOnActionRequest())
  67. {
  68. final L2PcInstance actor = getClient().getActiveChar();
  69. if ((actor != null) && (actor.isSpawnProtected() || actor.isInvul()))
  70. {
  71. actor.onActionRequest();
  72. if (Config.DEBUG)
  73. {
  74. _log.info("Spawn protection for player " + actor.getName() + " removed by packet: " + getType());
  75. }
  76. }
  77. }
  78. }
  79. catch (Throwable t)
  80. {
  81. _log.log(Level.SEVERE, "Client: " + getClient().toString() + " - Failed running: " + getType() + " ; " + t.getMessage(), t);
  82. // in case of EnterWorld error kick player from game
  83. if (this instanceof EnterWorld)
  84. {
  85. getClient().closeNow();
  86. }
  87. }
  88. }
  89. protected abstract void runImpl();
  90. /**
  91. * Sends a game server packet to the client.
  92. * @param gsp the game server packet
  93. */
  94. protected final void sendPacket(L2GameServerPacket gsp)
  95. {
  96. getClient().sendPacket(gsp);
  97. }
  98. /**
  99. * Sends a system message to the client.
  100. * @param id the system message Id
  101. */
  102. public void sendPacket(SystemMessageId id)
  103. {
  104. sendPacket(SystemMessage.getSystemMessage(id));
  105. }
  106. /**
  107. * @return A String with this packet name for debugging purposes
  108. */
  109. public abstract String getType();
  110. /**
  111. * Overridden with true value on some packets that should disable spawn protection (RequestItemList and UseItem only)
  112. * @return
  113. */
  114. protected boolean triggersOnActionRequest()
  115. {
  116. return true;
  117. }
  118. /**
  119. * @return the active player if exist, otherwise null.
  120. */
  121. protected final L2PcInstance getActiveChar()
  122. {
  123. return getClient().getActiveChar();
  124. }
  125. protected final void sendActionFailed()
  126. {
  127. if (getClient() != null)
  128. {
  129. getClient().sendPacket(ActionFailed.STATIC_PACKET);
  130. }
  131. }
  132. }