add auto save setting

This commit is contained in:
Pyrbu 2023-05-21 12:53:48 +01:00
parent 4ec734c438
commit 03159ce964
6 changed files with 29 additions and 14 deletions

@ -113,7 +113,7 @@ public class ZNpcsPlus extends JavaPlugin {
BungeeConnector bungeeConnector = new BungeeConnector(this); BungeeConnector bungeeConnector = new BungeeConnector(this);
ConfigManager configManager = new ConfigManager(getDataFolder()); ConfigManager configManager = new ConfigManager(getDataFolder());
ActionRegistry actionRegistry = new ActionRegistry(); ActionRegistry actionRegistry = new ActionRegistry();
NpcRegistryImpl npcRegistry = new NpcRegistryImpl(configManager, this, packetFactory, actionRegistry); NpcRegistryImpl npcRegistry = new NpcRegistryImpl(configManager, this, packetFactory, actionRegistry, scheduler);
UserManager userManager = new UserManager(); UserManager userManager = new UserManager();
SkinCache skinCache = new SkinCache(configManager); SkinCache skinCache = new SkinCache(configManager);
@ -140,9 +140,9 @@ public class ZNpcsPlus extends JavaPlugin {
npcRegistry.reload(); npcRegistry.reload();
shutdownTasks.add(scheduler::cancelAll); shutdownTasks.add(scheduler::cancelAll);
shutdownTasks.add(npcRegistry::save);
shutdownTasks.add(userManager::shutdown); shutdownTasks.add(userManager::shutdown);
shutdownTasks.add(adventure::close); shutdownTasks.add(adventure::close);
if (configManager.getConfig().autoSaveEnabled()) shutdownTasks.add(npcRegistry::save);
ZApiProvider.register(new ZNPCsPlusApi(npcRegistry)); ZApiProvider.register(new ZNPCsPlusApi(npcRegistry));
enabled = true; enabled = true;

@ -36,4 +36,13 @@ public interface MainConfig {
@ConfComments("Set this to true if you don't want to be warned in the console when a skin fails to resolve") @ConfComments("Set this to true if you don't want to be warned in the console when a skin fails to resolve")
@DefaultBoolean(false) @DefaultBoolean(false)
boolean disableSkinFetcherWarnings(); boolean disableSkinFetcherWarnings();
@ConfKey("auto-save-interval")
@ConfComments("How often to auto-save npcs, set this to -1 to disable. This value will only apply on restart")
@DefaultInteger(300)
int autoSaveInterval();
default boolean autoSaveEnabled() {
return autoSaveInterval() != -1;
}
} }

@ -6,6 +6,7 @@ import lol.pyr.znpcsplus.api.npc.NpcType;
import lol.pyr.znpcsplus.config.ConfigManager; import lol.pyr.znpcsplus.config.ConfigManager;
import lol.pyr.znpcsplus.interaction.ActionRegistry; import lol.pyr.znpcsplus.interaction.ActionRegistry;
import lol.pyr.znpcsplus.packets.PacketFactory; import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.storage.NpcStorage; import lol.pyr.znpcsplus.storage.NpcStorage;
import lol.pyr.znpcsplus.util.ZLocation; import lol.pyr.znpcsplus.util.ZLocation;
import org.bukkit.World; import org.bukkit.World;
@ -21,10 +22,15 @@ public class NpcRegistryImpl implements NpcRegistry {
private final PacketFactory packetFactory; private final PacketFactory packetFactory;
private final ConfigManager configManager; private final ConfigManager configManager;
public NpcRegistryImpl(ConfigManager configManager, ZNpcsPlus plugin, PacketFactory packetFactory, ActionRegistry actionRegistry) { public NpcRegistryImpl(ConfigManager configManager, ZNpcsPlus plugin, PacketFactory packetFactory, ActionRegistry actionRegistry, TaskScheduler scheduler) {
storage = configManager.getConfig().storageType().create(configManager, plugin, packetFactory, actionRegistry); storage = configManager.getConfig().storageType().create(configManager, plugin, packetFactory, actionRegistry);
this.packetFactory = packetFactory; this.packetFactory = packetFactory;
this.configManager = configManager; this.configManager = configManager;
if (configManager.getConfig().autoSaveEnabled()) {
long delay = configManager.getConfig().autoSaveInterval() * 20L;
scheduler.runDelayedTimerAsync(this::save, delay, delay);
}
} }
public void reload() { public void reload() {

@ -23,20 +23,20 @@ public class FoliaScheduler extends TaskScheduler {
} }
@Override @Override
public void runLaterAsync(Runnable runnable, long ticks) { public void runLaterAsync(Runnable runnable, long delay) {
try { try {
Object scheduler = Reflections.FOLIA_GET_ASYNC_SCHEDULER.get().invoke(null); Object scheduler = Reflections.FOLIA_GET_ASYNC_SCHEDULER.get().invoke(null);
Reflections.FOLIA_RUN_DELAYED.get().invoke(scheduler, plugin, (Consumer<Object>) o -> runnable.run(), ticks * 50, TimeUnit.MILLISECONDS); Reflections.FOLIA_RUN_DELAYED.get().invoke(scheduler, plugin, (Consumer<Object>) o -> runnable.run(), delay * 50, TimeUnit.MILLISECONDS);
} catch (IllegalAccessException | InvocationTargetException e) { } catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@Override @Override
public void runDelayedTimerAsync(Runnable runnable, long delay, long ticks) { public void runDelayedTimerAsync(Runnable runnable, long delay, long interval) {
try { try {
Object scheduler = Reflections.FOLIA_GET_ASYNC_SCHEDULER.get().invoke(null); Object scheduler = Reflections.FOLIA_GET_ASYNC_SCHEDULER.get().invoke(null);
Reflections.FOLIA_RUN_AT_FIXED_RATE.get().invoke(scheduler, plugin, (Consumer<Object>) o -> runnable.run(), delay * 50, ticks * 50, TimeUnit.MILLISECONDS); Reflections.FOLIA_RUN_AT_FIXED_RATE.get().invoke(scheduler, plugin, (Consumer<Object>) o -> runnable.run(), delay * 50, interval * 50, TimeUnit.MILLISECONDS);
} catch (IllegalAccessException | InvocationTargetException e) { } catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

@ -14,13 +14,13 @@ public class SpigotScheduler extends TaskScheduler {
} }
@Override @Override
public void runLaterAsync(Runnable runnable, long ticks) { public void runLaterAsync(Runnable runnable, long delay) {
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, runnable, ticks); Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, runnable, delay);
} }
@Override @Override
public void runDelayedTimerAsync(Runnable runnable, long delay, long ticks) { public void runDelayedTimerAsync(Runnable runnable, long delay, long interval) {
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, delay, ticks); Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, delay, interval);
} }
@Override @Override

@ -11,9 +11,9 @@ public abstract class TaskScheduler {
public abstract void runSync(Runnable runnable); public abstract void runSync(Runnable runnable);
public abstract void runLaterAsync(Runnable runnable, long ticks); public abstract void runLaterAsync(Runnable runnable, long delay);
public abstract void runDelayedTimerAsync(Runnable runnable, long delay, long ticks); public abstract void runDelayedTimerAsync(Runnable runnable, long delay, long interval);
public abstract void cancelAll(); public abstract void cancelAll();
} }