DecayTaskManager.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.taskmanager;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import java.util.Map.Entry;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javolution.util.FastMap;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.gameserver.ThreadPoolManager;
  28. import com.l2jserver.gameserver.model.actor.L2Attackable;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. /**
  31. * @author la2 Lets drink to code!
  32. */
  33. public class DecayTaskManager
  34. {
  35. protected static final Logger _log = Logger.getLogger(DecayTaskManager.class.getName());
  36. protected final Map<L2Character, Long> _decayTasks = new FastMap<L2Character, Long>().shared();
  37. protected DecayTaskManager()
  38. {
  39. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new DecayScheduler(), 10000, Config.DECAY_TIME_TASK);
  40. }
  41. public static DecayTaskManager getInstance()
  42. {
  43. return SingletonHolder._instance;
  44. }
  45. public void addDecayTask(L2Character actor)
  46. {
  47. addDecayTask(actor, 0);
  48. }
  49. public void addDecayTask(L2Character actor, int interval)
  50. {
  51. _decayTasks.put(actor, System.currentTimeMillis() + interval);
  52. }
  53. public void cancelDecayTask(L2Character actor)
  54. {
  55. _decayTasks.remove(actor);
  56. }
  57. protected class DecayScheduler implements Runnable
  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. {
  77. continue;
  78. }
  79. if (actor.isRaid() && !actor.isRaidMinion())
  80. {
  81. delay = Config.RAID_BOSS_DECAY_TIME;
  82. }
  83. else if ((actor instanceof L2Attackable) && (((L2Attackable) actor).isSpoil() || ((L2Attackable) actor).isSeeded()))
  84. {
  85. delay = Config.SPOILED_DECAY_TIME;
  86. }
  87. else
  88. {
  89. delay = Config.NPC_DECAY_TIME;
  90. }
  91. if ((current - next) > delay)
  92. {
  93. actor.onDecay();
  94. it.remove();
  95. }
  96. }
  97. }
  98. catch (Exception e)
  99. {
  100. // TODO: Find out the reason for exception. Unless caught here, mob decay would stop.
  101. _log.log(Level.WARNING, "Error in DecayScheduler: " + e.getMessage(), e);
  102. }
  103. }
  104. }
  105. @Override
  106. public String toString()
  107. {
  108. StringBuilder ret = new StringBuilder();
  109. ret.append("============= DecayTask Manager Report ============");
  110. ret.append(Config.EOL);
  111. ret.append("Tasks count: ");
  112. ret.append(_decayTasks.size());
  113. ret.append(Config.EOL);
  114. ret.append("Tasks dump:");
  115. ret.append(Config.EOL);
  116. Long current = System.currentTimeMillis();
  117. for (L2Character actor : _decayTasks.keySet())
  118. {
  119. ret.append("Class/Name: ");
  120. ret.append(actor.getClass().getSimpleName());
  121. ret.append('/');
  122. ret.append(actor.getName());
  123. ret.append(" decay timer: ");
  124. ret.append(current - _decayTasks.get(actor));
  125. ret.append(Config.EOL);
  126. }
  127. return ret.toString();
  128. }
  129. /**
  130. * <u><b><font color="FF0000">Read only</font></b></u>
  131. * @return
  132. */
  133. public Map<L2Character, Long> getTasks()
  134. {
  135. return _decayTasks;
  136. }
  137. private static class SingletonHolder
  138. {
  139. protected static final DecayTaskManager _instance = new DecayTaskManager();
  140. }
  141. }