Dropped TimersManager and corrections for previous commits.

This commit is contained in:
MobiusDevelopment
2020-03-31 12:04:59 +00:00
parent 4ed01c5fba
commit b13eb068a1
96 changed files with 1496 additions and 1713 deletions
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<Integer> _whisperers = ConcurrentHashMap.newKeySet();
private final List<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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;
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<Integer> _whisperers = ConcurrentHashMap.newKeySet();
private final List<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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;
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<Integer> _whisperers = ConcurrentHashMap.newKeySet();
private final List<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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;
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<Integer> _whisperers = ConcurrentHashMap.newKeySet();
private final List<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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;
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<Integer> _whisperers = ConcurrentHashMap.newKeySet();
private final List<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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;
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<Integer> _whisperers = ConcurrentHashMap.newKeySet();
private final List<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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;
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<Integer> _whisperers = ConcurrentHashMap.newKeySet();
private final List<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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;
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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;
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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();
}
@@ -14411,7 +14411,7 @@ public class PlayerInstance extends Playable
{
for (QuestTimer timer : _questTimers)
{
timer.cancel();
timer.cancelTask();
}
_questTimers.clear();
}
@@ -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);
}
@@ -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();
}
@@ -14505,7 +14505,7 @@ public class PlayerInstance extends Playable
{
for (QuestTimer timer : _questTimers)
{
timer.cancel();
timer.cancelTask();
}
_questTimers.clear();
}
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<Integer> _whisperers = ConcurrentHashMap.newKeySet();
private final List<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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)
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<Integer> _whisperers = ConcurrentHashMap.newKeySet();
private final List<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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)
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<Integer> _whisperers = ConcurrentHashMap.newKeySet();
private final List<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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)
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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)
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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)
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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)
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Integer, List<TimerHolder<?>>> _timers = new ConcurrentHashMap<>();
public void registerTimer(TimerHolder<?> timer)
{
final Npc npc = timer.getNpc();
if (npc != null)
{
final List<TimerHolder<?>> npcTimers = _timers.computeIfAbsent(npc.getObjectId(), key -> new ArrayList<>());
synchronized (npcTimers)
{
npcTimers.add(timer);
}
}
final PlayerInstance player = timer.getPlayer();
if (player != null)
{
final List<TimerHolder<?>> playerTimers = _timers.computeIfAbsent(player.getObjectId(), key -> new ArrayList<>());
synchronized (playerTimers)
{
playerTimers.add(timer);
}
}
}
public void cancelTimers(int objectId)
{
final List<TimerHolder<?>> timers = _timers.remove(objectId);
if (timers != null)
{
synchronized (timers)
{
timers.forEach(TimerHolder::cancelTimer);
}
}
}
public void unregisterTimer(int objectId, TimerHolder<?> timer)
{
final List<TimerHolder<?>> 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();
}
}
@@ -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();
@@ -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<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _timerHolders = new ArrayList<>();
/**
* Constructor of NpcInstance (use Creature constructor).<br>
@@ -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();
}
}
}
@@ -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<Integer> _whisperers = ConcurrentHashMap.newKeySet();
private final List<QuestTimer> _questTimers = new ArrayList<>();
private final List<TimerHolder<?>> _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)
@@ -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<T> 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<T> 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);
}
}
/**
@@ -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);
}