add auto save setting
This commit is contained in:
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);
|
||||
ConfigManager configManager = new ConfigManager(getDataFolder());
|
||||
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();
|
||||
SkinCache skinCache = new SkinCache(configManager);
|
||||
|
||||
|
@ -140,9 +140,9 @@ public class ZNpcsPlus extends JavaPlugin {
|
|||
npcRegistry.reload();
|
||||
|
||||
shutdownTasks.add(scheduler::cancelAll);
|
||||
shutdownTasks.add(npcRegistry::save);
|
||||
shutdownTasks.add(userManager::shutdown);
|
||||
shutdownTasks.add(adventure::close);
|
||||
if (configManager.getConfig().autoSaveEnabled()) shutdownTasks.add(npcRegistry::save);
|
||||
|
||||
ZApiProvider.register(new ZNPCsPlusApi(npcRegistry));
|
||||
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")
|
||||
@DefaultBoolean(false)
|
||||
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.interaction.ActionRegistry;
|
||||
import lol.pyr.znpcsplus.packets.PacketFactory;
|
||||
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
||||
import lol.pyr.znpcsplus.storage.NpcStorage;
|
||||
import lol.pyr.znpcsplus.util.ZLocation;
|
||||
import org.bukkit.World;
|
||||
|
@ -21,10 +22,15 @@ public class NpcRegistryImpl implements NpcRegistry {
|
|||
private final PacketFactory packetFactory;
|
||||
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);
|
||||
this.packetFactory = packetFactory;
|
||||
this.configManager = configManager;
|
||||
|
||||
if (configManager.getConfig().autoSaveEnabled()) {
|
||||
long delay = configManager.getConfig().autoSaveInterval() * 20L;
|
||||
scheduler.runDelayedTimerAsync(this::save, delay, delay);
|
||||
}
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
|
|
|
@ -23,20 +23,20 @@ public class FoliaScheduler extends TaskScheduler {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void runLaterAsync(Runnable runnable, long ticks) {
|
||||
public void runLaterAsync(Runnable runnable, long delay) {
|
||||
try {
|
||||
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) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runDelayedTimerAsync(Runnable runnable, long delay, long ticks) {
|
||||
public void runDelayedTimerAsync(Runnable runnable, long delay, long interval) {
|
||||
try {
|
||||
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) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
@ -14,13 +14,13 @@ public class SpigotScheduler extends TaskScheduler {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void runLaterAsync(Runnable runnable, long ticks) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, runnable, ticks);
|
||||
public void runLaterAsync(Runnable runnable, long delay) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, runnable, delay);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runDelayedTimerAsync(Runnable runnable, long delay, long ticks) {
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, delay, ticks);
|
||||
public void runDelayedTimerAsync(Runnable runnable, long delay, long interval) {
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, delay, interval);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -11,9 +11,9 @@ public abstract class TaskScheduler {
|
|||
|
||||
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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue