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

@@ -228,6 +228,7 @@ import org.l2jmobius.gameserver.network.serverpackets.TradePressOwnOk;
import org.l2jmobius.gameserver.network.serverpackets.TradeStart;
import org.l2jmobius.gameserver.network.serverpackets.UserInfo;
import org.l2jmobius.gameserver.network.serverpackets.ValidateLocation;
import org.l2jmobius.gameserver.taskmanager.PlayerAutoSaveTaskManager;
import org.l2jmobius.gameserver.util.Broadcast;
import org.l2jmobius.gameserver.util.FloodProtectors;
import org.l2jmobius.gameserver.util.IllegalPlayerAction;
@@ -535,7 +536,6 @@ public class PlayerInstance extends Playable
private final HashMap<Integer, Long> _confirmDlgRequests = new HashMap<>();
private int _currentMultiSellId = -1;
private int _partyroom = 0;
private Future<?> _autoSaveTask = null;
/** The table containing all minimum level needed for each Expertise (None, D, C, B, A, S). */
private static final int[] EXPERTISE_LEVELS =
@@ -8284,7 +8284,7 @@ public class PlayerInstance extends Playable
player.restoreFriendList();
player.startAutoSaveTask();
PlayerAutoSaveTaskManager.getInstance().add(player);
}
catch (Exception e)
{
@@ -9566,31 +9566,15 @@ public class PlayerInstance extends Playable
return _hennaDEX;
}
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()
{
store();
if (Config.UPDATE_ITEMS_ON_CHAR_STORE)
{
_inventory.updateDatabase();
getInventory().updateDatabase();
getWarehouse().updateDatabase();
getFreight().updateDatabase();
}
}
@@ -13934,7 +13918,7 @@ public class PlayerInstance extends Playable
// Remove WorldObject object from _allObjects of World
World.getInstance().removeObject(this);
World.getInstance().removeFromAllPlayers(this); // force remove in case of crash during teleport
stopAutoSaveTask();
PlayerAutoSaveTaskManager.getInstance().remove(this);
}
private class ShortBuffTask implements Runnable

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();
}
}