From 28a845d566ba6fb62bc29b5d9f1e180617b04a06 Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Tue, 12 Jul 2022 16:06:57 +0000 Subject: [PATCH] misc: Improved config loading --- .../proxy/AuthMeVelocityPlugin.java | 46 ++++++++----------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java index 58fcedc..4473912 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -21,6 +21,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; import java.util.HashSet; +import java.util.Objects; import java.util.Set; import java.util.UUID; @@ -32,7 +33,7 @@ public class AuthMeVelocityPlugin { private final Path pluginDirectory; private AuthmeVelocityAPI api; - protected final Set loggedPlayers = Collections.synchronizedSet(new HashSet<>()); + protected final Set loggedPlayers = Collections.synchronizedSet(new HashSet<>()); @Inject public AuthMeVelocityPlugin(ProxyServer proxy, Logger logger, @DataDirectory Path dataDirectory) { @@ -54,17 +55,17 @@ public class AuthMeVelocityPlugin { proxy.getEventManager().register(this, new ProxyListener(config, api, logger, proxy)); proxy.getEventManager().register(this, new PluginMessageListener(proxy, logger, config, api)); - if(proxy.getPluginManager().isLoaded("fastlogin")){ + if (proxy.getPluginManager().isLoaded("fastlogin")) { proxy.getEventManager().register(this, new FastLoginListener(proxy, api)); } - if(proxy.getPluginManager().isLoaded("miniplaceholders")){ + if (proxy.getPluginManager().isLoaded("miniplaceholders")) { AuthmePlaceholders.getExpansion(this).register(); } logger.info("-- AuthMeVelocity enabled --"); logger.info("AuthServers: {}", config.getAuthServers()); - if(config.getToServerOptions().sendToServer()){ + if (config.getToServerOptions().sendToServer()) { logger.info("LobbyServers: {}", config.getToServerOptions().getTeleportServers()); } } @@ -78,32 +79,23 @@ public class AuthMeVelocityPlugin { } private Toml loadConfig(Path path){ - if(!Files.exists(path)){ - try { - Files.createDirectory(path); - } catch(IOException e){ - configError(e); - return null; - } - } - Path configPath = path.resolve("config.toml"); - if(!Files.exists(configPath)){ - try(InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.toml")){ - Files.copy(in, configPath); - } catch(IOException e){ - configError(e); - return null; - } - } try { + if (Files.notExists(path)) { + Files.createDirectory(path); + } + + Path configPath = path.resolve("config.toml"); + if (Files.notExists(configPath)) { + try (InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.toml")) { + Files.copy(Objects.requireNonNull(in, "The configuration does not exists"), configPath); + } + } + return new Toml().read(Files.newInputStream(configPath)); - } catch(IOException e){ - configError(e); + } catch (IOException ex) { + logger.info("An error ocurred on configuration initialization", ex); return null; } - } - - private void configError(Exception ex){ - logger.info("An error ocurred on configuration initialization: {}", ex.getMessage()); + } }