diff --git a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index 44d83c0a22..673aed3443 100644 --- a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -68,6 +70,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -298,17 +303,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -445,7 +460,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } } diff --git a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index 44d83c0a22..673aed3443 100644 --- a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -68,6 +70,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -298,17 +303,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -445,7 +460,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } } diff --git a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index 44d83c0a22..673aed3443 100644 --- a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -68,6 +70,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -298,17 +303,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -445,7 +460,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } } diff --git a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index 44d83c0a22..673aed3443 100644 --- a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -68,6 +70,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -298,17 +303,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -445,7 +460,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } } diff --git a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index 44d83c0a22..673aed3443 100644 --- a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -68,6 +70,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -298,17 +303,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -445,7 +460,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } } diff --git a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index 44d83c0a22..673aed3443 100644 --- a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -68,6 +70,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -298,17 +303,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -445,7 +460,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } } diff --git a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index 44d83c0a22..673aed3443 100644 --- a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -68,6 +70,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -298,17 +303,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -445,7 +460,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } } diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index b704fb23c4..92c252f192 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; @@ -64,6 +66,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -269,43 +274,56 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it - else if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { - final WalkInfo walk = _activeRoutes.get(npc.getObjectId()); - if (walk == null) + if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { - return; + final WalkInfo walk = _activeRoutes.get(npc.getObjectId()); + if (walk == null) + { + return; + } + + // Prevent call simultaneously from scheduled task and onArrived() or temporarily stop walking for resuming in future + if (walk.isBlocked() || walk.isSuspended()) + { + return; + } + + walk.setBlocked(true); + final NpcWalkerNode node = walk.getCurrentNode(); + if (node.runToLocation()) + { + npc.setRunning(); + } + else + { + npc.setWalking(); + } + npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); + walk.setBlocked(false); + walk.setStoppedByAttack(false); } - - // Prevent call simultaneously from scheduled task and onArrived() or temporarily stop walking for resuming in future - if (walk.isBlocked() || walk.isSuspended()) - { - return; - } - - walk.setBlocked(true); - final NpcWalkerNode node = walk.getCurrentNode(); - if (node.runToLocation()) - { - npc.setRunning(); - } - else - { - npc.setWalking(); - } - npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setBlocked(false); - walk.setStoppedByAttack(false); } } } @@ -317,11 +335,10 @@ public final class WalkingManager implements IXmlReader public synchronized void cancelMoving(Npc npc) { final WalkInfo walk = _activeRoutes.remove(npc.getObjectId()); - if (walk == null) + if (walk != null) { - return; + walk.getWalkCheckTask().cancel(true); } - walk.getWalkCheckTask().cancel(true); } /** @@ -331,13 +348,12 @@ public final class WalkingManager implements IXmlReader public void resumeMoving(Npc npc) { final WalkInfo walk = _activeRoutes.get(npc.getObjectId()); - if (walk == null) + if (walk != null) { - return; + walk.setSuspended(false); + walk.setStoppedByAttack(false); + startMoving(npc, walk.getRoute().getName()); } - walk.setSuspended(false); - walk.setStoppedByAttack(false); - startMoving(npc, walk.getRoute().getName()); } /** @@ -411,7 +427,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } /** diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index f69654e44e..f27b78df96 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -66,6 +68,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -296,17 +301,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -440,7 +455,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } } diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index f69654e44e..f27b78df96 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -66,6 +68,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -296,17 +301,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -440,7 +455,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } } diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index f69654e44e..f27b78df96 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -66,6 +68,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -296,17 +301,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -440,7 +455,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } } diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index f69654e44e..f27b78df96 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -66,6 +68,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -296,17 +301,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -440,7 +455,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } } diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java index f69654e44e..f27b78df96 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/instancemanager/WalkingManager.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledFuture; import java.util.logging.Logger; import org.w3c.dom.Document; @@ -66,6 +68,9 @@ public final class WalkingManager implements IXmlReader private final Map _routes = new HashMap<>(); // all available routes private final Map _activeRoutes = new HashMap<>(); // each record represents NPC, moving by predefined route from _routes, and moving progress private final Map _routesToAttach = new HashMap<>(); // each record represents NPC and all available routes for it + private final Map> _startMoveTasks = new ConcurrentHashMap<>(); + private final Map> _repeatMoveTasks = new ConcurrentHashMap<>(); + private final Map> _arriveTasks = new ConcurrentHashMap<>(); protected WalkingManager() { @@ -296,17 +301,27 @@ public final class WalkingManager implements IXmlReader npc.setWalking(); } npc.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, node); - walk.setWalkCheckTask(ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000)); // start walk check task, for resuming walk after fight + + final ScheduledFuture task = _repeatMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + final ScheduledFuture newTask = ThreadPool.scheduleAtFixedRate(new StartMovingTask(npc, routeName), 60000, 60000); + _repeatMoveTasks.put(npc, newTask); + walk.setWalkCheckTask(newTask); // start walk check task, for resuming walk after fight + } _activeRoutes.put(npc.getObjectId(), walk); // register route } else { - ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000); + final ScheduledFuture task = _startMoveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _startMoveTasks.put(npc, ThreadPool.schedule(new StartMovingTask(npc, routeName), 60000)); + } } } - else - // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it + else // walk was stopped due to some reason (arrived to node, script action, fight or something else), resume it { if (_activeRoutes.containsKey(npc.getObjectId()) && ((npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_ACTIVE) || (npc.getAI().getIntention() == CtrlIntention.AI_INTENTION_IDLE))) { @@ -440,7 +455,11 @@ public final class WalkingManager implements IXmlReader npc.broadcastSay(ChatType.NPC_GENERAL, node.getChatText()); } - ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000)); + final ScheduledFuture task = _arriveTasks.get(npc); + if ((task == null) || task.isCancelled() || task.isDone()) + { + _arriveTasks.put(npc, ThreadPool.schedule(new ArrivedTask(npc, walk), 100 + (node.getDelay() * 1000))); + } } } }