Addition of PlayerAutoSaveTaskManager.

This commit is contained in:
MobiusDevelopment
2021-02-01 23:26:26 +00:00
parent 680b924dac
commit c265292c81
42 changed files with 1905 additions and 576 deletions

View File

@ -342,6 +342,7 @@ import org.l2jmobius.gameserver.network.serverpackets.ValidateLocation;
import org.l2jmobius.gameserver.network.serverpackets.commission.ExResponseCommissionInfo;
import org.l2jmobius.gameserver.network.serverpackets.friend.FriendStatus;
import org.l2jmobius.gameserver.taskmanager.AttackStanceTaskManager;
import org.l2jmobius.gameserver.taskmanager.PlayerAutoSaveTaskManager;
import org.l2jmobius.gameserver.util.Broadcast;
import org.l2jmobius.gameserver.util.EnumIntBitmask;
import org.l2jmobius.gameserver.util.FloodProtectors;
@ -802,8 +803,6 @@ public class PlayerInstance extends Playable
private final Fishing _fishing = new Fishing(this);
private Future<?> _autoSaveTask = null;
public void setPvpFlagLasts(long time)
{
_pvpFlagLasts = time;
@ -6727,7 +6726,8 @@ public class PlayerInstance extends Playable
player.startOnlineTimeUpdateTask();
player.setOnlineStatus(true, false);
player.startAutoSaveTask();
PlayerAutoSaveTaskManager.getInstance().add(player);
}
catch (Exception e)
{
@ -7988,32 +7988,16 @@ public class PlayerInstance extends Playable
return isInCategory(CategoryType.SIXTH_CLASS_GROUP);
}
private void startAutoSaveTask()
{
if ((Config.CHAR_DATA_STORE_INTERVAL > 0) && (_autoSaveTask == null))
{
_autoSaveTask = ThreadPool.scheduleAtFixedRate(this::autoSave, Config.CHAR_DATA_STORE_INTERVAL, Config.CHAR_DATA_STORE_INTERVAL);
}
}
private void stopAutoSaveTask()
{
if (_autoSaveTask != null)
{
_autoSaveTask.cancel(false);
_autoSaveTask = null;
}
}
protected void autoSave()
public void autoSave()
{
storeMe();
storeRecommendations();
if (Config.UPDATE_ITEMS_ON_CHAR_STORE)
{
_inventory.updateDatabase();
getInventory().updateDatabase();
getWarehouse().updateDatabase();
getFreight().updateDatabase();
}
}
@ -11096,7 +11080,8 @@ public class PlayerInstance extends Playable
// Stop all passives and augment options
getEffectList().stopAllPassives(false, false);
getEffectList().stopAllOptions(false, false);
stopAutoSaveTask();
PlayerAutoSaveTaskManager.getInstance().remove(this);
return super.deleteMe();
}
@ -13807,11 +13792,6 @@ public class PlayerInstance extends Playable
_pvpRegTask.cancel(false);
_pvpRegTask = null;
}
if ((_autoSaveTask != null) && !_autoSaveTask.isDone() && !_autoSaveTask.isCancelled())
{
_autoSaveTask.cancel(false);
_autoSaveTask = null;
}
if ((_taskWarnUserTakeBreak != null) && !_taskWarnUserTakeBreak.isDone() && !_taskWarnUserTakeBreak.isCancelled())
{
_taskWarnUserTakeBreak.cancel(false);

View File

@ -0,0 +1,83 @@
/*
* 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.taskmanager;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.l2jmobius.Config;
import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
/**
* @author Mobius
*/
public class PlayerAutoSaveTaskManager
{
private static final Map<PlayerInstance, Long> PLAYER_TIMES = new ConcurrentHashMap<>();
private static boolean _working = false;
public PlayerAutoSaveTaskManager()
{
ThreadPool.scheduleAtFixedRate(() ->
{
if (_working)
{
return;
}
_working = true;
final long time = System.currentTimeMillis();
SEARCH: for (Entry<PlayerInstance, Long> entry : PLAYER_TIMES.entrySet())
{
if (time > entry.getValue().longValue())
{
final PlayerInstance player = entry.getKey();
if ((player != null) && player.isOnline())
{
player.autoSave();
PLAYER_TIMES.put(entry.getKey(), time + Config.CHAR_DATA_STORE_INTERVAL);
break SEARCH; // Prevent SQL flood.
}
}
}
_working = false;
}, 1000, 1000);
}
public void add(PlayerInstance player)
{
PLAYER_TIMES.put(player, System.currentTimeMillis() + Config.CHAR_DATA_STORE_INTERVAL);
}
public void remove(PlayerInstance player)
{
PLAYER_TIMES.remove(player);
}
public static PlayerAutoSaveTaskManager getInstance()
{
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder
{
protected static final PlayerAutoSaveTaskManager INSTANCE = new PlayerAutoSaveTaskManager();
}
}