FloodProtector.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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[]{ 4, 42, 42, 16, 100 };
  44. // protected actions
  45. public static final int PROTECTED_USEITEM = 0;
  46. public static final int PROTECTED_ROLLDICE = 1;
  47. public static final int PROTECTED_FIREWORK = 2;
  48. public static final int PROTECTED_ITEMPETSUMMON = 3;
  49. public static final int PROTECTED_HEROVOICE = 4;
  50. // =========================================================
  51. // Constructor
  52. private FloodProtector()
  53. {
  54. _log.info("Initializing FloodProtector");
  55. _floodClient = new FastMap<Integer, Integer[]>(Config.FLOODPROTECTOR_INITIALSIZE).setShared(true);
  56. }
  57. /**
  58. * Add a new player to the flood protector
  59. * (should be done for all players when they enter the world)
  60. * @param playerObjId
  61. */
  62. public void registerNewPlayer(int playerObjId)
  63. {
  64. // create a new array
  65. Integer[] array = new Integer[REUSEDELAY.length];
  66. for (int i=0; i<array.length; i++)
  67. array[i] = 0;
  68. // register the player with an empty array
  69. _floodClient.put(playerObjId, array);
  70. }
  71. /**
  72. * Remove a player from the flood protector
  73. * (should be done if player loggs off)
  74. * @param playerObjId
  75. */
  76. public void removePlayer(int playerObjId)
  77. {
  78. _floodClient.remove(playerObjId);
  79. }
  80. /**
  81. * Return the size of the flood protector
  82. * @return size
  83. */
  84. public int getSize()
  85. {
  86. return _floodClient.size();
  87. }
  88. /**
  89. * Try to perform the requested action
  90. *
  91. * @param playerObjId
  92. * @param action
  93. * @return true if the action may be performed
  94. */
  95. public boolean tryPerformAction(int playerObjId, int action)
  96. {
  97. Entry<Integer, Integer[]> entry = _floodClient.getEntry(playerObjId);
  98. if (entry == null) return false; // player just disconnected
  99. Integer[] value = entry.getValue();
  100. if (value[action] < GameTimeController.getGameTicks())
  101. {
  102. value[action] = GameTimeController.getGameTicks()+REUSEDELAY[action];
  103. entry.setValue(value);
  104. return true;
  105. }
  106. return false;
  107. }
  108. }