From b13eb068a109bbf0b4bb83669e4522ee4063fa83 Mon Sep 17 00:00:00 2001 From: MobiusDevelopment <8391001+MobiusDevelopment@users.noreply.github.com> Date: Tue, 31 Mar 2020 12:04:59 +0000 Subject: [PATCH] Dropped TimersManager and corrections for previous commits. --- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../l2jmobius/gameserver/model/actor/Npc.java | 4 +- .../model/actor/instance/PlayerInstance.java | 2 +- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../l2jmobius/gameserver/model/actor/Npc.java | 4 +- .../model/actor/instance/PlayerInstance.java | 2 +- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- .../instancemanager/TimersManager.java | 91 ------------------- .../gameserver/model/actor/Creature.java | 4 - .../l2jmobius/gameserver/model/actor/Npc.java | 35 ++++++- .../model/actor/instance/PlayerInstance.java | 29 +++++- .../model/events/timers/TimerHolder.java | 36 ++++++-- .../gameserver/model/quest/QuestTimer.java | 16 ++-- 96 files changed, 1496 insertions(+), 1713 deletions(-) delete mode 100644 L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java delete mode 100644 L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java diff --git a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/Creature.java index fdb3d6a089..14fe491578 100644 --- a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1681,9 +1680,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/Npc.java index 02ee1824ce..097f994df0 100644 --- a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 1f576a44d1..d7fc3f0a04 100644 --- a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -212,6 +212,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.holders.MovieHolder; @@ -847,6 +848,7 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13853,10 +13855,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13875,6 +13886,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + public boolean isTrueHero() { return _trueHero; diff --git a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/Creature.java index fdb3d6a089..14fe491578 100644 --- a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1681,9 +1680,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/Npc.java index 648559e153..8dc9e749dd 100644 --- a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index c7097a2808..6902d2dbf0 100644 --- a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -213,6 +213,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; @@ -853,6 +854,7 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13860,10 +13862,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13882,6 +13893,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + public boolean isTrueHero() { return _trueHero; diff --git a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/Creature.java index 8b32479ab1..5eb6fe19c8 100644 --- a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1681,9 +1680,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/Npc.java index 648559e153..8dc9e749dd 100644 --- a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 557e507fee..dd1cc8d476 100644 --- a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -214,6 +214,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; @@ -855,6 +856,7 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13863,10 +13865,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13885,6 +13896,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + public boolean isTrueHero() { return _trueHero; diff --git a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/Creature.java index 8b32479ab1..5eb6fe19c8 100644 --- a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1681,9 +1680,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/Npc.java index 648559e153..8dc9e749dd 100644 --- a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 03f7045818..cf2fbfd83e 100644 --- a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -213,6 +213,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; @@ -865,6 +866,7 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13834,10 +13836,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13856,6 +13867,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + public boolean isTrueHero() { return _trueHero; diff --git a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/Creature.java index a0bb8dfde2..92812da903 100644 --- a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1681,9 +1680,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/Npc.java index 648559e153..8dc9e749dd 100644 --- a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 96cd36b6d4..2f6e262653 100644 --- a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -213,6 +213,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; @@ -861,6 +862,7 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13791,10 +13793,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13813,6 +13824,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + public boolean isTrueHero() { return _trueHero; diff --git a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/Creature.java index a0bb8dfde2..92812da903 100644 --- a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1681,9 +1680,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/Npc.java index 648559e153..8dc9e749dd 100644 --- a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index eb443b8b88..ed772a9738 100644 --- a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -213,6 +213,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; @@ -861,6 +862,7 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13794,10 +13796,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13816,6 +13827,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + public boolean isTrueHero() { return _trueHero; diff --git a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/Creature.java index a0bb8dfde2..92812da903 100644 --- a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1681,9 +1680,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/Npc.java index 648559e153..8dc9e749dd 100644 --- a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 71b674e9c4..bf220c973c 100644 --- a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -213,6 +213,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; @@ -861,6 +862,7 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13800,10 +13802,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13822,6 +13833,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + public boolean isTrueHero() { return _trueHero; diff --git a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/Creature.java index b33ba6208b..ed493ebd29 100644 --- a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1681,9 +1680,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/Npc.java index 648559e153..8dc9e749dd 100644 --- a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 1a63aefbe0..6bf20667c4 100644 --- a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -214,6 +214,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; @@ -870,6 +871,7 @@ public class PlayerInstance extends Playable private ScheduledFuture _timedHuntingZoneFinishTask = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13783,10 +13785,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13805,6 +13816,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + public boolean isTrueHero() { return _trueHero; diff --git a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/Npc.java index 97a04ddd2e..14464a962f 100644 --- a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -1327,7 +1327,7 @@ public class Npc extends Creature ((Npc) summoner).removeSummonedNpc(getObjectId()); } - // Stop quest timers + // Stop all timers stopQuestTimers(); // Clear script value @@ -2082,7 +2082,7 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 4ef0560acd..b7d16ed258 100644 --- a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -14411,7 +14411,7 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/Npc.java index 97a04ddd2e..14464a962f 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -1327,7 +1327,7 @@ public class Npc extends Creature ((Npc) summoner).removeSummonedNpc(getObjectId()); } - // Stop quest timers + // Stop all timers stopQuestTimers(); // Clear script value @@ -2082,7 +2082,7 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 6829d2c1c3..5ef1894610 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -14505,7 +14505,7 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/Creature.java index f8a4540137..ad1c29f15c 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1680,9 +1679,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/Npc.java index 9d590a93d9..5bc3f88cea 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index fa9ea3d2f0..bdfd01d4de 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -213,6 +213,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; @@ -849,6 +850,7 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13677,10 +13679,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13699,6 +13710,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + private void startOnlineTimeUpdateTask() { if (_onlineTimeUpdateTask != null) diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/Creature.java index f8a4540137..ad1c29f15c 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1680,9 +1679,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/Npc.java index 9d590a93d9..5bc3f88cea 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 6d4c0e8524..b9dc952b18 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -213,6 +213,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; @@ -849,6 +850,7 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13677,10 +13679,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13699,6 +13710,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + private void startOnlineTimeUpdateTask() { if (_onlineTimeUpdateTask != null) diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/Creature.java index 7dd9c00057..3d62491b61 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1680,9 +1679,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/Npc.java index 9d590a93d9..5bc3f88cea 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index cc832788bf..c787ce48e6 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -213,6 +213,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; @@ -847,6 +848,7 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13662,10 +13664,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13684,6 +13695,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + private void startOnlineTimeUpdateTask() { if (_onlineTimeUpdateTask != null) diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/Creature.java index 21395c1bd3..2ce602f8f5 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -67,7 +67,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1681,9 +1680,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/Npc.java index 4264ce8eb4..6228ea6c0d 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -74,6 +74,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -160,6 +161,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1152,8 +1154,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1896,9 +1899,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 9b734fbb90..53ef5f7c2b 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -216,6 +216,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ElementalSpiritDataHolder; @@ -857,6 +858,7 @@ public class PlayerInstance extends Playable private ElementalType _activeElementalSpiritType; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13704,10 +13706,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13726,6 +13737,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + private void startOnlineTimeUpdateTask() { if (_onlineTimeUpdateTask != null) diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/Creature.java index 21395c1bd3..2ce602f8f5 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -67,7 +67,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1681,9 +1680,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/Npc.java index 4264ce8eb4..6228ea6c0d 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -74,6 +74,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -160,6 +161,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1152,8 +1154,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1896,9 +1899,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index b344bc1116..ec7ae67596 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -216,6 +216,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ElementalSpiritDataHolder; @@ -857,6 +858,7 @@ public class PlayerInstance extends Playable private ElementalType _activeElementalSpiritType; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13704,10 +13706,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13726,6 +13737,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + private void startOnlineTimeUpdateTask() { if (_onlineTimeUpdateTask != null) diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/Creature.java index 51ca2e3981..fe9e577c47 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -67,7 +67,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1681,9 +1680,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/Npc.java index 4264ce8eb4..6228ea6c0d 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -74,6 +74,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -160,6 +161,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1152,8 +1154,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1896,9 +1899,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index d71bf56f0a..0daa25a862 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -217,6 +217,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ElementalSpiritDataHolder; @@ -867,6 +868,7 @@ public class PlayerInstance extends Playable private ScheduledFuture _timedHuntingZoneFinishTask = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13639,10 +13641,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13661,6 +13672,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + private void startOnlineTimeUpdateTask() { if (_onlineTimeUpdateTask != null) diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); } diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java deleted file mode 100644 index c1df762370..0000000000 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/instancemanager/TimersManager.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.instancemanager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.l2jmobius.gameserver.model.actor.Npc; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.events.timers.TimerHolder; - -/** - * @author UnAfraid - */ -public class TimersManager -{ - private final Map>> _timers = new ConcurrentHashMap<>(); - - public void registerTimer(TimerHolder timer) - { - final Npc npc = timer.getNpc(); - if (npc != null) - { - final List> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>()); - synchronized (npcTimers) - { - npcTimers.add(timer); - } - } - - final PlayerInstance player = timer.getPlayer(); - if (player != null) - { - final List> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>()); - synchronized (playerTimers) - { - playerTimers.add(timer); - } - } - } - - public void cancelTimers(int objectId) - { - final List> timers = _timers.remove(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.forEach(TimerHolder::cancelTimer); - } - } - } - - public void unregisterTimer(int objectId, TimerHolder timer) - { - final List> timers = _timers.get(objectId); - if (timers != null) - { - synchronized (timers) - { - timers.remove(timer); - } - } - } - - public static TimersManager getInstance() - { - return SingletonHolder.INSTANCE; - } - - private static class SingletonHolder - { - protected static final TimersManager INSTANCE = new TimersManager(); - } -} diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/Creature.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/Creature.java index f8a4540137..ad1c29f15c 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/Creature.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/Creature.java @@ -66,7 +66,6 @@ import org.l2jmobius.gameserver.geoengine.GeoEngine; import org.l2jmobius.gameserver.idfactory.IdFactory; import org.l2jmobius.gameserver.instancemanager.MapRegionManager; import org.l2jmobius.gameserver.instancemanager.QuestManager; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.instancemanager.ZoneManager; import org.l2jmobius.gameserver.model.AccessLevel; import org.l2jmobius.gameserver.model.CreatureContainer; @@ -1680,9 +1679,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe // Remove all active, passive and option effects, do not broadcast changes. _effectList.stopAllEffectsWithoutExclusions(false, false); - // Cancel all timers related to this Creature - TimersManager.getInstance().cancelTimers(getObjectId()); - // Cancel the BuffFinishTask related to this creature. cancelBuffFinishTask(); diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/Npc.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/Npc.java index 9d590a93d9..5bc3f88cea 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/Npc.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/Npc.java @@ -73,6 +73,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSkillFinishe import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcSpawn; import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcTeleport; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; import org.l2jmobius.gameserver.model.instancezone.Instance; import org.l2jmobius.gameserver.model.items.Weapon; @@ -159,6 +160,7 @@ public class Npc extends Creature private TaxZone _taxZone = null; private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); /** * Constructor of NpcInstance (use Creature constructor).
@@ -1140,8 +1142,9 @@ public class Npc extends Creature instance.removeNpc(this); } - // Stop quest timers + // Stop all timers stopQuestTimers(); + stopTimerHolders(); // Clear script value _scriptValue = 0; @@ -1884,9 +1887,37 @@ public class Npc extends Creature { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } } + + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + + public void stopTimerHolders() + { + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } + } } diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 685ce1041d..34256c4e7f 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -213,6 +213,7 @@ import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerReputa import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerSubChange; import org.l2jmobius.gameserver.model.events.listeners.FunctionEventListener; import org.l2jmobius.gameserver.model.events.returns.TerminateReturn; +import org.l2jmobius.gameserver.model.events.timers.TimerHolder; import org.l2jmobius.gameserver.model.fishing.Fishing; import org.l2jmobius.gameserver.model.holders.AttendanceInfoHolder; import org.l2jmobius.gameserver.model.holders.ItemHolder; @@ -851,6 +852,7 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private final List _questTimers = new ArrayList<>(); + private final List> _timerHolders = new ArrayList<>(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13698,10 +13700,19 @@ public class PlayerInstance extends Playable { for (QuestTimer timer : _questTimers) { - timer.cancel(); + timer.cancelTask(); } _questTimers.clear(); } + + synchronized (_timerHolders) + { + for (TimerHolder timer : _timerHolders) + { + timer.cancelTask(); + } + _timerHolders.clear(); + } } public void addQuestTimer(QuestTimer questTimer) @@ -13720,6 +13731,22 @@ public class PlayerInstance extends Playable } } + public void addTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.add(timer); + } + } + + public void removeTimerHolder(TimerHolder timer) + { + synchronized (_timerHolders) + { + _timerHolders.remove(timer); + } + } + private void startOnlineTimeUpdateTask() { if (_onlineTimeUpdateTask != null) diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java index 7654303a70..b0fc6f93c0 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/events/timers/TimerHolder.java @@ -21,7 +21,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.l2jmobius.commons.concurrent.ThreadPool; -import org.l2jmobius.gameserver.instancemanager.TimersManager; import org.l2jmobius.gameserver.model.StatSet; import org.l2jmobius.gameserver.model.actor.Npc; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; @@ -59,7 +58,16 @@ public class TimerHolder implements Runnable _cancelScript = cancelScript; _postExecutor = postExecutor; _task = isRepeating ? ThreadPool.scheduleAtFixedRate(this, _time, _time) : ThreadPool.schedule(this, _time); - TimersManager.getInstance().registerTimer(this); + + if (npc != null) + { + npc.addTimerHolder(this); + } + + if (player != null) + { + player.addTimerHolder(this); + } } /** @@ -103,28 +111,38 @@ public class TimerHolder implements Runnable } /** - * @return {@code true} if timer for the given event, npc, player were stopped, {@code false} otherwise + * Cancels this timer. */ - public boolean cancelTimer() + public void cancelTimer() { - // Make sure to unregister this timer even if the task is already completed (TimerExecutor#onTimerPostExecute calls this method). if (_npc != null) { - TimersManager.getInstance().unregisterTimer(_npc.getObjectId(), this); + _npc.removeTimerHolder(this); } + if (_player != null) { - TimersManager.getInstance().unregisterTimer(_player.getObjectId(), this); + _player.removeTimerHolder(this); } if ((_task == null) || _task.isCancelled() || _task.isDone()) { - return false; + return; } _task.cancel(true); _cancelScript.onTimerCancel(this); - return true; + } + + /** + * Cancels task related to this quest timer. + */ + public void cancelTask() + { + if ((_task != null) && !_task.isDone() && !_task.isCancelled()) + { + _task.cancel(false); + } } /** diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java index f064c9a657..343bca3261 100644 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/quest/QuestTimer.java @@ -61,11 +61,7 @@ public class QuestTimer public void cancel() { - if (_scheduler != null) - { - _scheduler.cancel(false); - _scheduler = null; - } + cancelTask(); if (_npc != null) { @@ -76,7 +72,15 @@ public class QuestTimer { _player.removeQuestTimer(this); } - + } + + public void cancelTask() + { + if ((_scheduler != null) && !_scheduler.isDone() && !_scheduler.isCancelled()) + { + _scheduler.cancel(false); + _scheduler = null; + } _quest.removeQuestTimer(this); }