Dropped TimersManager and corrections for previous commits.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user