mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-23 15:21:06 +01:00
fix: save users on disable && debug option in config
This commit is contained in:
parent
0c35ff18b0
commit
759140cf6f
7 changed files with 38 additions and 12 deletions
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -5,6 +5,7 @@ import org.slf4j.Logger;
|
|||
|
||||
public class BukkitLogger implements PluginLogger {
|
||||
private final Logger logger;
|
||||
|
||||
public BukkitLogger(Logger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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<List<dev.xhyrom.lighteco.api.model.user.User>> getTopUsers(dev.xhyrom.lighteco.api.model.currency.Currency currency, int length) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue