L2GameClientPacket.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 net.sf.l2j.gameserver.clientpackets;
  16. import java.util.logging.Logger;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.GameTimeController;
  19. import net.sf.l2j.gameserver.network.L2GameClient;
  20. import net.sf.l2j.gameserver.serverpackets.ActionFailed;
  21. import net.sf.l2j.gameserver.serverpackets.L2GameServerPacket;
  22. import org.mmocore.network.ReceivablePacket;
  23. /**
  24. * Packets received by the game server from clients
  25. * @author KenM
  26. */
  27. public abstract class L2GameClientPacket extends ReceivablePacket<L2GameClient>
  28. {
  29. private static final Logger _log = Logger.getLogger(L2GameClientPacket.class.getName());
  30. @Override
  31. protected boolean read()
  32. {
  33. //_log.info(this.getType());
  34. try
  35. {
  36. readImpl();
  37. return true;
  38. }
  39. catch (Throwable t)
  40. {
  41. _log.severe("Client: "+getClient().toString()+" - Failed reading: "+getType()+" - L2J Server Version: "+Config.SERVER_VERSION+" - DP Revision: "+Config.DATAPACK_VERSION);
  42. t.printStackTrace();
  43. }
  44. return false;
  45. }
  46. protected abstract void readImpl();
  47. @Override
  48. public void run()
  49. {
  50. try
  51. {
  52. // flood protection
  53. if (GameTimeController.getGameTicks() - getClient().packetsSentStartTick > 10)
  54. {
  55. getClient().packetsSentStartTick = GameTimeController.getGameTicks();
  56. getClient().packetsSentInSec = 0;
  57. }
  58. else
  59. {
  60. getClient().packetsSentInSec++;
  61. if (getClient().packetsSentInSec > 12)
  62. {
  63. if (getClient().packetsSentInSec < 100)
  64. sendPacket(new ActionFailed());
  65. return;
  66. }
  67. }
  68. runImpl();
  69. if (this instanceof MoveBackwardToLocation
  70. || this instanceof AttackRequest
  71. || this instanceof RequestMagicSkillUse)
  72. // could include pickup and talk too, but less is better
  73. {
  74. // Removes onspawn protection - player has faster computer than
  75. // average
  76. if (getClient().getActiveChar() != null)
  77. getClient().getActiveChar().onActionRequest();
  78. }
  79. }
  80. catch (Throwable t)
  81. {
  82. _log.severe("Client: "+getClient().toString()+" - Failed running: "+getType()+" - L2J Server Version: "+Config.SERVER_VERSION+" - DP Revision: "+Config.DATAPACK_VERSION);
  83. t.printStackTrace();
  84. }
  85. }
  86. protected abstract void runImpl();
  87. protected final void sendPacket(L2GameServerPacket gsp)
  88. {
  89. getClient().sendPacket(gsp);
  90. }
  91. /**
  92. * @return A String with this packet name for debuging purposes
  93. */
  94. public abstract String getType();
  95. }