2
0

FloodProtector.java 3.1 KB

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