DecayTaskManager.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 net.sf.l2j.gameserver.taskmanager;
  16. import java.util.Map;
  17. import java.util.NoSuchElementException;
  18. import java.util.logging.Logger;
  19. import javolution.util.FastMap;
  20. import net.sf.l2j.gameserver.ThreadPoolManager;
  21. import net.sf.l2j.gameserver.model.L2Character;
  22. import net.sf.l2j.gameserver.model.actor.instance.L2RaidBossInstance;
  23. /**
  24. * @author la2
  25. * Lets drink to code!
  26. */
  27. public class DecayTaskManager
  28. {
  29. protected static final Logger _log = Logger.getLogger(DecayTaskManager.class.getName());
  30. protected Map<L2Character,Long> _decayTasks = new FastMap<L2Character,Long>().setShared(true);
  31. private static DecayTaskManager _instance;
  32. public DecayTaskManager()
  33. {
  34. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new DecayScheduler(),10000,5000);
  35. }
  36. public static DecayTaskManager getInstance()
  37. {
  38. if(_instance == null)
  39. _instance = new DecayTaskManager();
  40. return _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. private class DecayScheduler implements Runnable
  59. {
  60. protected DecayScheduler()
  61. {
  62. // Do nothing
  63. }
  64. public void run()
  65. {
  66. Long current = System.currentTimeMillis();
  67. int delay;
  68. try
  69. {
  70. if (_decayTasks != null)
  71. for(L2Character actor : _decayTasks.keySet())
  72. {
  73. if(actor instanceof L2RaidBossInstance) delay = 30000;
  74. else delay = 8500;
  75. if((current - _decayTasks.get(actor)) > delay)
  76. {
  77. actor.onDecay();
  78. _decayTasks.remove(actor);
  79. }
  80. }
  81. } catch (Throwable e) {
  82. // TODO: Find out the reason for exception. Unless caught here, mob decay would stop.
  83. _log.warning(e.toString());
  84. }
  85. }
  86. }
  87. @Override
  88. public String toString()
  89. {
  90. String ret = "============= DecayTask Manager Report ============\r\n";
  91. ret += "Tasks count: "+_decayTasks.size()+"\r\n";
  92. ret += "Tasks dump:\r\n";
  93. Long current = System.currentTimeMillis();
  94. for( L2Character actor : _decayTasks.keySet())
  95. {
  96. ret += "Class/Name: "+actor.getClass().getSimpleName()+"/"+actor.getName()
  97. +" decay timer: "+(current - _decayTasks.get(actor))+"\r\n";
  98. }
  99. return ret;
  100. }
  101. }