FloodProtector.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.util;
  16. import java.util.logging.Logger;
  17. import javolution.util.FastMap;
  18. import javolution.util.FastMap.Entry;
  19. import net.sf.l2j.Config;
  20. import net.sf.l2j.gameserver.GameTimeController;
  21. /**
  22. * Flood protector
  23. *
  24. * @author durgus
  25. */
  26. public class FloodProtector
  27. {
  28. private static final Logger _log = Logger.getLogger(FloodProtector.class.getName());
  29. private static FloodProtector _instance;
  30. public static final FloodProtector getInstance()
  31. {
  32. if (_instance == null)
  33. {
  34. _instance = new FloodProtector();
  35. }
  36. return _instance;
  37. }
  38. // =========================================================
  39. // Data Field
  40. private FastMap<Integer, Integer[]> _floodClient;
  41. // =========================================================
  42. // reuse delays for protected actions (in game ticks 1 tick = 100ms)
  43. private static final int[] REUSEDELAY = new int[]
  44. {
  45. 4, 42, 42, 16, 100
  46. };
  47. // protected actions
  48. public static final int PROTECTED_USEITEM = 0;
  49. public static final int PROTECTED_ROLLDICE = 1;
  50. public static final int PROTECTED_FIREWORK = 2;
  51. public static final int PROTECTED_ITEMPETSUMMON = 3;
  52. public static final int PROTECTED_HEROVOICE = 4;
  53. // =========================================================
  54. // Constructor
  55. private FloodProtector()
  56. {
  57. _log.info("Initializing FloodProtector");
  58. _floodClient = new FastMap<Integer, Integer[]>(Config.FLOODPROTECTOR_INITIALSIZE).setShared(true);
  59. }
  60. /**
  61. * Add a new player to the flood protector (should be done for all players
  62. * when they enter the world)
  63. *
  64. * @param playerObjId
  65. */
  66. public void registerNewPlayer(int playerObjId)
  67. {
  68. // create a new array
  69. Integer[] array = new Integer[REUSEDELAY.length];
  70. for (int i = 0; i < array.length; i++)
  71. array[i] = 0;
  72. // register the player with an empty array
  73. _floodClient.put(playerObjId, array);
  74. }
  75. /**
  76. * Remove a player from the flood protector (should be done if player loggs
  77. * off)
  78. *
  79. * @param playerObjId
  80. */
  81. public void removePlayer(int playerObjId)
  82. {
  83. _floodClient.remove(playerObjId);
  84. }
  85. /**
  86. * Return the size of the flood protector
  87. *
  88. * @return size
  89. */
  90. public int getSize()
  91. {
  92. return _floodClient.size();
  93. }
  94. /**
  95. * Try to perform the requested action
  96. *
  97. * @param playerObjId
  98. * @param action
  99. * @return true if the action may be performed
  100. */
  101. public boolean tryPerformAction(int playerObjId, int action)
  102. {
  103. Entry<Integer, Integer[]> entry = _floodClient.getEntry(playerObjId);
  104. if (entry == null)
  105. return false; // player just disconnected
  106. Integer[] value = entry.getValue();
  107. if (value[action] < GameTimeController.getGameTicks())
  108. {
  109. value[action] = GameTimeController.getGameTicks() + REUSEDELAY[action];
  110. entry.setValue(value);
  111. return true;
  112. }
  113. return false;
  114. }
  115. }