DecayTaskManager.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.taskmanager;
  16. import java.util.Map;
  17. import java.util.NoSuchElementException;
  18. import java.util.logging.Logger;
  19. import com.l2jserver.gameserver.ThreadPoolManager;
  20. import com.l2jserver.gameserver.model.actor.L2Character;
  21. import javolution.util.FastMap;
  22. /**
  23. * @author la2 Lets drink to code!
  24. */
  25. public class DecayTaskManager
  26. {
  27. protected static final Logger _log = Logger.getLogger(DecayTaskManager.class.getName());
  28. protected Map<L2Character, Long> _decayTasks = new FastMap<L2Character, Long>().shared();
  29. public static final int RAID_BOSS_DECAY_TIME = 30000;
  30. public static final int ATTACKABLE_DECAY_TIME = 8500;
  31. private DecayTaskManager()
  32. {
  33. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new DecayScheduler(), 10000, 5000);
  34. }
  35. public static DecayTaskManager getInstance()
  36. {
  37. return SingletonHolder._instance;
  38. }
  39. public void addDecayTask(L2Character actor)
  40. {
  41. _decayTasks.put(actor, System.currentTimeMillis());
  42. }
  43. public void addDecayTask(L2Character actor, int interval)
  44. {
  45. _decayTasks.put(actor, System.currentTimeMillis() + interval);
  46. }
  47. public void cancelDecayTask(L2Character actor)
  48. {
  49. try
  50. {
  51. _decayTasks.remove(actor);
  52. }
  53. catch (NoSuchElementException e)
  54. {
  55. }
  56. }
  57. private class DecayScheduler implements Runnable
  58. {
  59. protected DecayScheduler()
  60. {
  61. // Do nothing
  62. }
  63. public void run()
  64. {
  65. Long current = System.currentTimeMillis();
  66. int delay;
  67. try
  68. {
  69. if (_decayTasks != null)
  70. for (L2Character actor : _decayTasks.keySet())
  71. {
  72. if (actor.isRaid() && !actor.isRaidMinion())
  73. delay = RAID_BOSS_DECAY_TIME;
  74. else
  75. delay = ATTACKABLE_DECAY_TIME;
  76. if ((current - _decayTasks.get(actor)) > delay)
  77. {
  78. actor.onDecay();
  79. _decayTasks.remove(actor);
  80. }
  81. }
  82. }
  83. catch (Exception e)
  84. {
  85. // TODO: Find out the reason for exception. Unless caught here,
  86. // mob decay would stop.
  87. _log.warning(e.toString());
  88. }
  89. }
  90. }
  91. @Override
  92. public String toString()
  93. {
  94. String ret = "============= DecayTask Manager Report ============\r\n";
  95. ret += "Tasks count: " + _decayTasks.size() + "\r\n";
  96. ret += "Tasks dump:\r\n";
  97. Long current = System.currentTimeMillis();
  98. for (L2Character actor : _decayTasks.keySet())
  99. {
  100. ret += "Class/Name: " + actor.getClass().getSimpleName() + "/" + actor.getName() + " decay timer: "
  101. + (current - _decayTasks.get(actor)) + "\r\n";
  102. }
  103. return ret;
  104. }
  105. /**
  106. * <u><b><font color="FF0000">Read only</font></b></u>
  107. */
  108. public Map<L2Character, Long> getTasks()
  109. {
  110. return _decayTasks;
  111. }
  112. @SuppressWarnings("synthetic-access")
  113. private static class SingletonHolder
  114. {
  115. protected static final DecayTaskManager _instance = new DecayTaskManager();
  116. }
  117. }