From 759140cf6fc7935bc1481cfb42590f4df07b2931 Mon Sep 17 00:00:00 2001 From: xHyroM Date: Wed, 30 Aug 2023 08:33:24 +0200 Subject: [PATCH] fix: save users on disable && debug option in config --- .../lighteco/bukkit/BukkitLightEcoBootstrap.java | 9 +++------ .../lighteco/bukkit/BukkitLightEcoLoader.java | 7 +++++-- .../xhyrom/lighteco/bukkit/logger/BukkitLogger.java | 1 + .../dev/xhyrom/lighteco/common/config/Config.java | 4 ++++ .../common/plugin/AbstractLightEcoPlugin.java | 8 +++++++- .../dev/xhyrom/lighteco/common/storage/Storage.java | 8 ++++++++ .../xhyrom/lighteco/common/task/UserSaveTask.java | 13 ++++++++++--- 7 files changed, 38 insertions(+), 12 deletions(-) diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java index 086541e..bba9b26 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoBootstrap.java @@ -10,7 +10,6 @@ import org.bukkit.Server; import org.bukkit.entity.Entity; import org.bukkit.plugin.java.JavaPlugin; -import java.io.File; import java.io.InputStream; import java.nio.file.Path; import java.util.List; @@ -24,11 +23,14 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr @Getter private final PluginLogger logger; + @Getter + private final SchedulerAdapter scheduler; public BukkitLightEcoBootstrap(JavaPlugin loader) { this.loader = loader; this.logger = new BukkitLogger(loader.getSLF4JLogger()); + this.scheduler = new BukkitSchedulerAdapter(this); } @Override @@ -50,11 +52,6 @@ public class BukkitLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr return this.loader.getServer(); } - @Override - public SchedulerAdapter getScheduler() { - return new BukkitSchedulerAdapter(this); - } - @Override public Path getDataDirectory() { return this.loader.getDataFolder().toPath(); diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java index 61777ed..07c5a31 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/BukkitLightEcoLoader.java @@ -3,10 +3,11 @@ package dev.xhyrom.lighteco.bukkit; import dev.jorel.commandapi.CommandAPI; import dev.jorel.commandapi.CommandAPIBukkitConfig; import dev.xhyrom.lighteco.bukkit.hooks.PlaceholderAPIExpansion; -import dev.xhyrom.lighteco.common.plugin.bootstrap.LoaderBootstrap; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +// Used inside plugin.yml +@SuppressWarnings("unused") public class BukkitLightEcoLoader extends JavaPlugin { private final BukkitLightEcoBootstrap bootstrap; @@ -16,8 +17,10 @@ public class BukkitLightEcoLoader extends JavaPlugin { @Override public void onLoad() { - CommandAPI.onLoad(new CommandAPIBukkitConfig(this).verboseOutput(true)); this.bootstrap.onLoad(); + CommandAPI.onLoad(new CommandAPIBukkitConfig(this) + .verboseOutput(this.bootstrap.getPlugin().getConfig().debug) + ); } @Override diff --git a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/logger/BukkitLogger.java b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/logger/BukkitLogger.java index 35812da..3c9d750 100644 --- a/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/logger/BukkitLogger.java +++ b/bukkit/src/main/java/dev/xhyrom/lighteco/bukkit/logger/BukkitLogger.java @@ -5,6 +5,7 @@ import org.slf4j.Logger; public class BukkitLogger implements PluginLogger { private final Logger logger; + public BukkitLogger(Logger logger) { this.logger = logger; } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java b/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java index cffc2b5..d30c733 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/config/Config.java @@ -22,4 +22,8 @@ public class Config extends OkaeriConfig { @Comment("Messages") public MessageConfig messages = new MessageConfig(); + + @Comment("Debug mode") + @Comment("Prints additional information to the console.") + public boolean debug = false; } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java index 9ef7508..26f9258 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/plugin/AbstractLightEcoPlugin.java @@ -6,6 +6,7 @@ import dev.xhyrom.lighteco.common.api.LightEcoApi; import dev.xhyrom.lighteco.common.config.Config; import dev.xhyrom.lighteco.common.dependencies.DependencyManager; import dev.xhyrom.lighteco.common.dependencies.DependencyManagerImpl; +import dev.xhyrom.lighteco.common.model.user.User; import dev.xhyrom.lighteco.common.storage.Storage; import dev.xhyrom.lighteco.common.storage.StorageFactory; import dev.xhyrom.lighteco.common.task.UserSaveTask; @@ -25,6 +26,8 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { private Storage storage; private LightEcoApi api; + private UserSaveTask userSaveTask; + public final void load() { this.dependencyManager = new DependencyManagerImpl(this); @@ -55,10 +58,13 @@ public abstract class AbstractLightEcoPlugin implements LightEcoPlugin { LightEcoProvider.set(this.api); this.registerApiOnPlatform(this.api); - this.getBootstrap().getScheduler().asyncRepeating(new UserSaveTask(this), this.config.saveInterval, TimeUnit.SECONDS); + this.userSaveTask = new UserSaveTask(this); + this.getBootstrap().getScheduler().asyncRepeating(userSaveTask, this.config.saveInterval, TimeUnit.SECONDS); } public final void disable() { + this.userSaveTask.run(); // save all users synchronously + this.storage.shutdown(); } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/Storage.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/Storage.java index 2f3049e..661e7c3 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/Storage.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/Storage.java @@ -86,6 +86,14 @@ public class Storage { return future(() -> this.provider.saveUsers(users)); } + public void saveUsersSync(dev.xhyrom.lighteco.api.model.user.User... users) { + try { + this.provider.saveUsers(users); + } catch (Exception e) { + throw new RuntimeException("Failed to save users", e); + } + } + // Return ApiUser instead of User // We don't do anything with this public CompletableFuture> getTopUsers(dev.xhyrom.lighteco.api.model.currency.Currency currency, int length) { diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/task/UserSaveTask.java b/common/src/main/java/dev/xhyrom/lighteco/common/task/UserSaveTask.java index 00f5001..d26991f 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/task/UserSaveTask.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/task/UserSaveTask.java @@ -3,7 +3,7 @@ package dev.xhyrom.lighteco.common.task; import dev.xhyrom.lighteco.common.model.user.User; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; -import java.util.concurrent.ExecutionException; +import java.util.Arrays; public class UserSaveTask implements Runnable { private final LightEcoPlugin plugin; @@ -14,6 +14,9 @@ public class UserSaveTask implements Runnable { @Override public void run() { + if (this.plugin.getConfig().debug) + this.plugin.getBootstrap().getLogger().info("Saving users in task"); + User[] users = this.plugin.getUserManager().values().stream() .filter(User::isDirty) .toArray(User[]::new); @@ -23,12 +26,16 @@ public class UserSaveTask implements Runnable { } try { - this.plugin.getUserManager().saveUsers(users).get(); + this.plugin.getStorage().saveUsersSync( + Arrays.stream(users) + .map(User::getProxy) + .toArray(dev.xhyrom.lighteco.api.model.user.User[]::new) + ); for (User user : users) { user.setDirty(false); } - } catch (InterruptedException | RuntimeException | ExecutionException e) { + } catch (RuntimeException e) { this.plugin.getBootstrap().getLogger().error("Failed to save users", e); } }