DecayTaskManager.java 4.0 KB

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