Advertisement
Guest User

Untitled

a guest
Dec 28th, 2021
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.40 KB | None | 0 0
  1.  * Copyright (C) 2004-2015 L2J Server
  2.  *
  3.  * This file is part of L2J Server.
  4.  *
  5.  * L2J Server is free software: you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation, either version 3 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * L2J Server is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13.  * General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18. package l2r.gameserver.taskmanager;
  19.  
  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.  
  28. import l2r.Config;
  29. import l2r.gameserver.model.actor.L2Attackable;
  30. import l2r.gameserver.model.actor.L2Character;
  31. import l2r.gameserver.model.actor.templates.L2NpcTemplate;
  32.  
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35.  
  36. /**
  37.  * @author NosBit
  38.  */
  39. public final class DecayTaskManager
  40. {
  41.     protected static final Logger _log = LoggerFactory.getLogger(DecayTaskManager.class);
  42.    
  43.     private final ScheduledExecutorService _decayExecutor = Executors.newSingleThreadScheduledExecutor();
  44.    
  45.     protected final Map<L2Character, ScheduledFuture<?>> _decayTasks = new ConcurrentHashMap<>();
  46.    
  47.     /**
  48.      * Adds a decay task for the specified character.<br>
  49.      * <br>
  50.      * If the decay task already exists it cancels it and re-adds it.
  51.      * @param character the character
  52.      */
  53.     public void add(L2Character character)
  54.     {
  55.         try
  56.         {
  57.             if (character == null)
  58.             {
  59.                 return;
  60.             }
  61.            
  62.             long delay;
  63.             if (character.getTemplate() instanceof L2NpcTemplate)
  64.             {
  65.                 delay = ((L2NpcTemplate) character.getTemplate()).getCorpseTime();
  66.             }
  67.             else
  68.             {
  69.                 delay = Config.DEFAULT_CORPSE_TIME;
  70.             }
  71.            
  72.             if ((character instanceof L2Attackable) && (((L2Attackable) character).isSpoiled() || ((L2Attackable) character).isSeeded()))
  73.             {
  74.                 delay += Config.SPOILED_CORPSE_EXTEND_TIME;
  75.             }
  76.            
  77.             add(character, delay, TimeUnit.SECONDS);
  78.         }
  79.         catch (Exception e)
  80.         {
  81.             // TODO: Find out the reason for exception. Unless caught here, mob decay would stop.
  82.             _log.warn("Error in DecayScheduler: " + e.getMessage(), e);
  83.         }
  84.     }
  85.    
  86.     /**
  87.      * Adds a decay task for the specified character.<br>
  88.      * <br>
  89.      * If the decay task already exists it cancels it and re-adds it.
  90.      * @param character the character
  91.      * @param delay the delay
  92.      * @param timeUnit the time unit of the delay parameter
  93.      */
  94.     public void add(L2Character character, long delay, TimeUnit timeUnit)
  95.     {
  96.         try
  97.         {
  98.             ScheduledFuture<?> decayTask = _decayExecutor.schedule(new DecayTask(character), delay, TimeUnit.SECONDS);
  99.            
  100.             decayTask = _decayTasks.put(character, decayTask);
  101.             // if decay task already existed cancel it so we use the new time
  102.             if (decayTask != null)
  103.             {
  104.                 if (!decayTask.cancel(false))
  105.                 {
  106.                     // old decay task was completed while canceling it remove and cancel the new one
  107.                     decayTask = _decayTasks.remove(character);
  108.                     if (decayTask != null)
  109.                     {
  110.                         decayTask.cancel(false);
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.         catch (Exception e)
  116.         {
  117.             // TODO: Find out the reason for exception. Unless caught here, mob decay would stop.
  118.             _log.warn("Error in DecayScheduler: " + e.getMessage(), e);
  119.         }
  120.     }
  121.    
  122.     /**
  123.      * Cancels the decay task of the specified character.
  124.      * @param character the character
  125.      */
  126.     public void cancel(L2Character character)
  127.     {
  128.         try
  129.         {
  130.             final ScheduledFuture<?> decayTask = _decayTasks.remove(character);
  131.             if (decayTask != null)
  132.             {
  133.                 decayTask.cancel(false);
  134.             }
  135.         }
  136.         catch (Exception e)
  137.         {
  138.             // TODO: Find out the reason for exception. Unless caught here, mob decay would stop.
  139.             _log.warn("Error in DecayScheduler: " + e.getMessage(), e);
  140.         }
  141.     }
  142.    
  143.     /**
  144.      * Gets the remaining time of the specified character's decay task.
  145.      * @param character the character
  146.      * @return if a decay task exists the remaining time, {@code Long.MAX_VALUE} otherwise
  147.      */
  148.     public long getRemainingTime(L2Character character)
  149.     {
  150.         try
  151.         {
  152.             final ScheduledFuture<?> decayTask = _decayTasks.get(character);
  153.             if (decayTask != null)
  154.             {
  155.                 return decayTask.getDelay(TimeUnit.MILLISECONDS);
  156.             }
  157.         }
  158.         catch (Exception e)
  159.         {
  160.             // TODO: Find out the reason for exception. Unless caught here, mob decay would stop.
  161.             _log.warn("Error in DecayScheduler: " + e.getMessage(), e);
  162.         }
  163.        
  164.         return Long.MAX_VALUE;
  165.     }
  166.    
  167.     private class DecayTask implements Runnable
  168.     {
  169.         private final L2Character _character;
  170.        
  171.         protected DecayTask(L2Character character)
  172.         {
  173.             _character = character;
  174.         }
  175.        
  176.         @Override
  177.         public void run()
  178.         {
  179.             try
  180.             {
  181.                 _decayTasks.remove(_character);
  182.                 _character.onDecay();
  183.             }
  184.             catch (Exception e)
  185.             {
  186.                 // TODO: Find out the reason for exception. Unless caught here, mob decay would stop.
  187.                 _log.warn("Error in DecayScheduler: " + e.getMessage(), e);
  188.             }
  189.         }
  190.     }
  191.    
  192.     @Override
  193.     public String toString()
  194.     {
  195.         StringBuilder ret = new StringBuilder();
  196.         ret.append("============= DecayTask Manager Report ============");
  197.         ret.append(Config.EOL);
  198.         ret.append("Tasks count: ");
  199.         ret.append(_decayTasks.size());
  200.         ret.append(Config.EOL);
  201.         ret.append("Tasks dump:");
  202.         ret.append(Config.EOL);
  203.        
  204.         for (Entry<L2Character, ScheduledFuture<?>> entry : _decayTasks.entrySet())
  205.         {
  206.             ret.append("Class/Name: ");
  207.             ret.append(entry.getKey().getClass().getSimpleName());
  208.             ret.append('/');
  209.             ret.append(entry.getKey().getName());
  210.             ret.append(" decay timer: ");
  211.             ret.append(entry.getValue().getDelay(TimeUnit.MILLISECONDS));
  212.             ret.append(Config.EOL);
  213.         }
  214.        
  215.         return ret.toString();
  216.     }
  217.    
  218.     /**
  219.      * <u><b><font color="FF0000">Read only</font></b></u>
  220.      * @return
  221.      */
  222.     public Map<L2Character, ScheduledFuture<?>> getTasks()
  223.     {
  224.         return _decayTasks;
  225.     }
  226.    
  227.     public static DecayTaskManager getInstance()
  228.     {
  229.         return SingletonHolder._instance;
  230.     }
  231.    
  232.     private static class SingletonHolder
  233.     {
  234.         protected static final DecayTaskManager _instance = new DecayTaskManager();
  235.     }
  236. }
  237.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement