DecayTaskManager.java 4.2 KB

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