DecayTaskManager.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2004-2015 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.Map;
  21. import java.util.Map.Entry;
  22. import java.util.concurrent.ConcurrentHashMap;
  23. import java.util.concurrent.Executors;
  24. import java.util.concurrent.ScheduledExecutorService;
  25. import java.util.concurrent.ScheduledFuture;
  26. import java.util.concurrent.TimeUnit;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.gameserver.model.actor.L2Attackable;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  31. /**
  32. * @author NosBit
  33. */
  34. public final class DecayTaskManager
  35. {
  36. private final ScheduledExecutorService _decayExecutor = Executors.newSingleThreadScheduledExecutor();
  37. protected final Map<L2Character, ScheduledFuture<?>> _decayTasks = new ConcurrentHashMap<>();
  38. /**
  39. * Adds a decay task for the specified character.<br>
  40. * <br>
  41. * If the decay task already exists it cancels it and re-adds it.
  42. * @param character the character
  43. */
  44. public void add(L2Character character)
  45. {
  46. if (character == null)
  47. {
  48. return;
  49. }
  50. long delay;
  51. if (character.getTemplate() instanceof L2NpcTemplate)
  52. {
  53. delay = ((L2NpcTemplate) character.getTemplate()).getCorpseTime();
  54. }
  55. else
  56. {
  57. delay = Config.DEFAULT_CORPSE_TIME;
  58. }
  59. if ((character instanceof L2Attackable) && (((L2Attackable) character).isSpoiled() || ((L2Attackable) character).isSeeded()))
  60. {
  61. delay += Config.SPOILED_CORPSE_EXTEND_TIME;
  62. }
  63. add(character, delay, TimeUnit.SECONDS);
  64. }
  65. /**
  66. * Adds a decay task for the specified character.<br>
  67. * <br>
  68. * If the decay task already exists it cancels it and re-adds it.
  69. * @param character the character
  70. * @param delay the delay
  71. * @param timeUnit the time unit of the delay parameter
  72. */
  73. public void add(L2Character character, long delay, TimeUnit timeUnit)
  74. {
  75. ScheduledFuture<?> decayTask = _decayExecutor.schedule(new DecayTask(character), delay, TimeUnit.SECONDS);
  76. decayTask = _decayTasks.put(character, decayTask);
  77. // if decay task already existed cancel it so we use the new time
  78. if (decayTask != null)
  79. {
  80. if (!decayTask.cancel(false))
  81. {
  82. // old decay task was completed while canceling it remove and cancel the new one
  83. decayTask = _decayTasks.remove(character);
  84. if (decayTask != null)
  85. {
  86. decayTask.cancel(false);
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * Cancels the decay task of the specified character.
  93. * @param character the character
  94. */
  95. public void cancel(L2Character character)
  96. {
  97. final ScheduledFuture<?> decayTask = _decayTasks.remove(character);
  98. if (decayTask != null)
  99. {
  100. decayTask.cancel(false);
  101. }
  102. }
  103. /**
  104. * Gets the remaining time of the specified character's decay task.
  105. * @param character the character
  106. * @return if a decay task exists the remaining time, {@code Long.MAX_VALUE} otherwise
  107. */
  108. public long getRemainingTime(L2Character character)
  109. {
  110. final ScheduledFuture<?> decayTask = _decayTasks.get(character);
  111. if (decayTask != null)
  112. {
  113. return decayTask.getDelay(TimeUnit.MILLISECONDS);
  114. }
  115. return Long.MAX_VALUE;
  116. }
  117. private class DecayTask implements Runnable
  118. {
  119. private final L2Character _character;
  120. protected DecayTask(L2Character character)
  121. {
  122. _character = character;
  123. }
  124. @Override
  125. public void run()
  126. {
  127. _decayTasks.remove(_character);
  128. _character.onDecay();
  129. }
  130. }
  131. @Override
  132. public String toString()
  133. {
  134. StringBuilder ret = new StringBuilder();
  135. ret.append("============= DecayTask Manager Report ============");
  136. ret.append(Config.EOL);
  137. ret.append("Tasks count: ");
  138. ret.append(_decayTasks.size());
  139. ret.append(Config.EOL);
  140. ret.append("Tasks dump:");
  141. ret.append(Config.EOL);
  142. for (Entry<L2Character, ScheduledFuture<?>> entry : _decayTasks.entrySet())
  143. {
  144. ret.append("Class/Name: ");
  145. ret.append(entry.getKey().getClass().getSimpleName());
  146. ret.append('/');
  147. ret.append(entry.getKey().getName());
  148. ret.append(" decay timer: ");
  149. ret.append(entry.getValue().getDelay(TimeUnit.MILLISECONDS));
  150. ret.append(Config.EOL);
  151. }
  152. return ret.toString();
  153. }
  154. public static DecayTaskManager getInstance()
  155. {
  156. return SingletonHolder._instance;
  157. }
  158. private static class SingletonHolder
  159. {
  160. protected static final DecayTaskManager _instance = new DecayTaskManager();
  161. }
  162. }