AntiFeedManager.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 com.l2jserver.gameserver.instancemanager;
  16. import java.util.Map;
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.model.actor.L2Character;
  19. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  20. import com.l2jserver.gameserver.network.L2GameClient;
  21. import javolution.util.FastMap;
  22. public class AntiFeedManager
  23. {
  24. private Map<Integer,Long> _lastDeathTimes;
  25. public static final AntiFeedManager getInstance()
  26. {
  27. return SingletonHolder._instance;
  28. }
  29. private AntiFeedManager()
  30. {
  31. _lastDeathTimes = new FastMap<Integer,Long>().shared();
  32. }
  33. /**
  34. * Set time of the last player's death to current
  35. * @param objectId Player's objectId
  36. */
  37. public final void setLastDeathTime(int objectId)
  38. {
  39. _lastDeathTimes.put(objectId, System.currentTimeMillis());
  40. }
  41. /**
  42. * Check if current kill should be counted as non-feeded.
  43. * @param attacker Attacker character
  44. * @param target Target character
  45. * @return True if kill is non-feeded.
  46. */
  47. public final boolean check(L2Character attacker, L2Character target)
  48. {
  49. if (!Config.L2JMOD_ANTIFEED_ENABLE)
  50. return true;
  51. if (target == null)
  52. return false;
  53. final L2PcInstance targetPlayer = target.getActingPlayer();
  54. if (targetPlayer == null)
  55. return false;
  56. if (Config.L2JMOD_ANTIFEED_INTERVAL > 0
  57. && _lastDeathTimes.containsKey(targetPlayer.getObjectId()))
  58. return (System.currentTimeMillis() - _lastDeathTimes.get(targetPlayer.getObjectId())) > Config.L2JMOD_ANTIFEED_INTERVAL;
  59. if (Config.L2JMOD_ANTIFEED_DUALBOX && attacker != null)
  60. {
  61. final L2PcInstance attackerPlayer = attacker.getActingPlayer();
  62. if (attackerPlayer == null)
  63. return false;
  64. final L2GameClient targetClient = targetPlayer.getClient();
  65. final L2GameClient attackerClient = attackerPlayer.getClient();
  66. if (targetClient == null
  67. || attackerClient == null
  68. || targetClient.isDetached()
  69. || attackerClient.isDetached())
  70. // unable to check ip address
  71. return !Config.L2JMOD_ANTIFEED_DISCONNECTED_AS_DUALBOX;
  72. return !targetClient.getConnection().getInetAddress().equals(attackerClient.getConnection().getInetAddress());
  73. }
  74. return true;
  75. }
  76. /**
  77. * Clears all timestamps
  78. */
  79. public final void clear()
  80. {
  81. _lastDeathTimes.clear();
  82. }
  83. @SuppressWarnings("synthetic-access")
  84. private static class SingletonHolder
  85. {
  86. protected static final AntiFeedManager _instance = new AntiFeedManager();
  87. }
  88. }