feat: improved configuration handling

This commit is contained in:
Adrian 2023-03-11 09:46:46 -05:00
parent 63f7af9046
commit f87051cc23
No known key found for this signature in database
GPG Key ID: FB8EF84DCE1BE452
4 changed files with 35 additions and 61 deletions

View File

@ -17,40 +17,68 @@
package io.github._4drian3d.authmevelocity.common.configuration; package io.github._4drian3d.authmevelocity.common.configuration;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicReference;
import org.spongepowered.configurate.CommentedConfigurationNode; import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.ConfigurateException; import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader; import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
public final class ConfigurationContainer<C> { public final class ConfigurationContainer<C> {
private C config; private final AtomicReference<C> config;
private final HoconConfigurationLoader loader; private final HoconConfigurationLoader loader;
private final Class<C> clazz; private final Class<C> clazz;
public ConfigurationContainer( private ConfigurationContainer(
final C config, final C config,
final Class<C> clazz, final Class<C> clazz,
final HoconConfigurationLoader loader final HoconConfigurationLoader loader
) { ) {
this.config = config; this.config = new AtomicReference<>(config);
this.loader = loader; this.loader = loader;
this.clazz = clazz; this.clazz = clazz;
} }
public C get() { public C get() {
return this.config; return this.config.get();
} }
public CompletableFuture<Void> reload() { public CompletableFuture<Void> reload() {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
try { try {
final CommentedConfigurationNode node = loader.load(); final CommentedConfigurationNode node = loader.load();
config = node.get(clazz); config.set(node.get(clazz));
} catch (ConfigurateException exception) { } catch (ConfigurateException exception) {
throw new CompletionException("Could not load config.conf file", exception); throw new CompletionException("Could not load config.conf file", exception);
} }
}); });
} }
public static <C> ConfigurationContainer<C> load(Path path, Class<C> clazz) throws IOException {
path = path.resolve("config.conf");
final boolean firstCreation = Files.notExists(path);
final HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
.defaultOptions(opts -> opts
.shouldCopyDefaults(true)
.header("""
AuthMeVelocity | by Glyart & 4drian3d
""")
)
.path(path)
.build();
final CommentedConfigurationNode node = loader.load();
final C config = node.get(clazz);
if (firstCreation) {
node.set(clazz, config);
loader.save(node);
}
return new ConfigurationContainer<>(config, clazz, loader);
}
} }

View File

@ -1,52 +0,0 @@
/*
* Copyright (C) 2023 AuthMeVelocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.github._4drian3d.authmevelocity.common.configuration;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
public final class Loader {
private Loader() {}
public static <C> ConfigurationContainer<C> loadMainConfig(Path path, Class<C> clazz) throws IOException {
path = path.resolve("config.conf");
final boolean firstCreation = Files.notExists(path);
final HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
.defaultOptions(opts -> opts
.shouldCopyDefaults(true)
.header("""
AuthMeVelocity | by Glyart & 4drian3d
""")
)
.path(path)
.build();
final CommentedConfigurationNode node = loader.load();
final C config = node.get(clazz);
if (firstCreation) {
node.set(clazz, config);
loader.save(node);
}
return new ConfigurationContainer<>(config, clazz, loader);
}
}

View File

@ -22,7 +22,6 @@ import com.google.common.io.ByteStreams;
import io.github._4drian3d.authmevelocity.common.LibsManager; import io.github._4drian3d.authmevelocity.common.LibsManager;
import io.github._4drian3d.authmevelocity.common.MessageType; import io.github._4drian3d.authmevelocity.common.MessageType;
import io.github._4drian3d.authmevelocity.common.configuration.ConfigurationContainer; import io.github._4drian3d.authmevelocity.common.configuration.ConfigurationContainer;
import io.github._4drian3d.authmevelocity.common.configuration.Loader;
import io.github._4drian3d.authmevelocity.common.configuration.PaperConfiguration; import io.github._4drian3d.authmevelocity.common.configuration.PaperConfiguration;
import io.github._4drian3d.authmevelocity.paper.listeners.AuthMeListener; import io.github._4drian3d.authmevelocity.paper.listeners.AuthMeListener;
import io.github._4drian3d.authmevelocity.paper.listeners.MessageListener; import io.github._4drian3d.authmevelocity.paper.listeners.MessageListener;
@ -44,7 +43,7 @@ public final class AuthMeVelocityPlugin extends JavaPlugin {
new LibsManager(new BukkitLibraryManager(this)).loadLibraries(); new LibsManager(new BukkitLibraryManager(this)).loadLibraries();
try { try {
this.config = Loader.loadMainConfig(getDataFolder().toPath(), PaperConfiguration.class); this.config = ConfigurationContainer.load(getDataFolder().toPath(), PaperConfiguration.class);
} catch (Exception e) { } catch (Exception e) {
getLogger().log(Level.SEVERE, "Could not load config.conf file", e); getLogger().log(Level.SEVERE, "Could not load config.conf file", e);
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);

View File

@ -38,7 +38,6 @@ import io.github._4drian3d.authmevelocity.api.velocity.AuthMeVelocityAPI;
import io.github._4drian3d.authmevelocity.common.Constants; import io.github._4drian3d.authmevelocity.common.Constants;
import io.github._4drian3d.authmevelocity.common.LibsManager; import io.github._4drian3d.authmevelocity.common.LibsManager;
import io.github._4drian3d.authmevelocity.common.configuration.ConfigurationContainer; import io.github._4drian3d.authmevelocity.common.configuration.ConfigurationContainer;
import io.github._4drian3d.authmevelocity.common.configuration.Loader;
import io.github._4drian3d.authmevelocity.common.configuration.ProxyConfiguration; import io.github._4drian3d.authmevelocity.common.configuration.ProxyConfiguration;
import io.github._4drian3d.authmevelocity.velocity.commands.AuthMeCommand; import io.github._4drian3d.authmevelocity.velocity.commands.AuthMeCommand;
import io.github._4drian3d.authmevelocity.velocity.listener.ConnectListener; import io.github._4drian3d.authmevelocity.velocity.listener.ConnectListener;
@ -110,7 +109,7 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI {
libraries.loadLibraries(); libraries.loadLibraries();
try { try {
this.config = Loader.loadMainConfig(pluginDirectory, ProxyConfiguration.class); this.config = ConfigurationContainer.load(pluginDirectory, ProxyConfiguration.class);
} catch (Exception e) { } catch (Exception e) {
logger.error("Could not load config.conf file", e); logger.error("Could not load config.conf file", e);
return; return;