fix: Fixed random config reseting

This commit is contained in:
4drian3d 2022-08-15 21:47:45 +00:00
parent ed50af2fef
commit befe6e87cd
2 changed files with 13 additions and 7 deletions

View File

@ -45,8 +45,6 @@ public class ConfigurationContainer<C> {
try { try {
final CommentedConfigurationNode node = loader.load(); final CommentedConfigurationNode node = loader.load();
newConfig = node.get(clazz); newConfig = node.get(clazz);
node.set(clazz, config);
loader.save(node);
return true; return true;
} catch (ConfigurateException exception) { } catch (ConfigurateException exception) {
logger.error("Could not load config.conf file", exception); logger.error("Could not load config.conf file", exception);

View File

@ -1,5 +1,6 @@
package me.adrianed.authmevelocity.common.configuration; package me.adrianed.authmevelocity.common.configuration;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -11,21 +12,28 @@ import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
public final class Loader { public final class Loader {
private Loader() {} private Loader() {}
public static <C> ConfigurationContainer<C> loadMainConfig(final Path path, Class<C> clazz, Logger logger){ public static <C> ConfigurationContainer<C> loadMainConfig(Path path, Class<C> clazz, Logger logger) {
path = path.resolve("config.conf");
final boolean firstCreation = Files.notExists(path);
final HoconConfigurationLoader loader = HoconConfigurationLoader.builder() final HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
.defaultOptions(opts -> opts .defaultOptions(opts -> opts
.shouldCopyDefaults(true) .shouldCopyDefaults(true)
.header("AuthMeVelocity | by Glyart & 4drian3d\n") .header("""
AuthMeVelocity | by Glyart & 4drian3d
""")
) )
.path(path.resolve("config.conf")) .path(path)
.build(); .build();
final C config; final C config;
try { try {
final CommentedConfigurationNode node = loader.load(); final CommentedConfigurationNode node = loader.load();
config = node.get(clazz); config = node.get(clazz);
node.set(clazz, config); if (firstCreation) {
loader.save(node); node.set(clazz, config);
loader.save(node);
}
} catch (ConfigurateException exception){ } catch (ConfigurateException exception){
logger.error("Could not load config.conf file", exception); logger.error("Could not load config.conf file", exception);
return null; return null;