misc: Improved config loading

This commit is contained in:
4drian3d 2022-07-12 16:06:57 +00:00
parent 64f4fd5176
commit 28a845d566

View File

@ -21,6 +21,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -32,7 +33,7 @@ public class AuthMeVelocityPlugin {
private final Path pluginDirectory; private final Path pluginDirectory;
private AuthmeVelocityAPI api; private AuthmeVelocityAPI api;
protected final Set<UUID> loggedPlayers = Collections.<UUID>synchronizedSet(new HashSet<>()); protected final Set<UUID> loggedPlayers = Collections.synchronizedSet(new HashSet<>());
@Inject @Inject
public AuthMeVelocityPlugin(ProxyServer proxy, Logger logger, @DataDirectory Path dataDirectory) { 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 ProxyListener(config, api, logger, proxy));
proxy.getEventManager().register(this, new PluginMessageListener(proxy, logger, config, api)); 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)); proxy.getEventManager().register(this, new FastLoginListener(proxy, api));
} }
if(proxy.getPluginManager().isLoaded("miniplaceholders")){ if (proxy.getPluginManager().isLoaded("miniplaceholders")) {
AuthmePlaceholders.getExpansion(this).register(); AuthmePlaceholders.getExpansion(this).register();
} }
logger.info("-- AuthMeVelocity enabled --"); logger.info("-- AuthMeVelocity enabled --");
logger.info("AuthServers: {}", config.getAuthServers()); logger.info("AuthServers: {}", config.getAuthServers());
if(config.getToServerOptions().sendToServer()){ if (config.getToServerOptions().sendToServer()) {
logger.info("LobbyServers: {}", config.getToServerOptions().getTeleportServers()); logger.info("LobbyServers: {}", config.getToServerOptions().getTeleportServers());
} }
} }
@ -78,32 +79,23 @@ public class AuthMeVelocityPlugin {
} }
private Toml loadConfig(Path path){ private Toml loadConfig(Path path){
if(!Files.exists(path)){
try { try {
if (Files.notExists(path)) {
Files.createDirectory(path); Files.createDirectory(path);
} catch(IOException e){
configError(e);
return null;
}
} }
Path configPath = path.resolve("config.toml"); Path configPath = path.resolve("config.toml");
if(!Files.exists(configPath)){ if (Files.notExists(configPath)) {
try(InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.toml")){ try (InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.toml")) {
Files.copy(in, configPath); Files.copy(Objects.requireNonNull(in, "The configuration does not exists"), configPath);
} catch(IOException e){
configError(e);
return null;
}
}
try {
return new Toml().read(Files.newInputStream(configPath));
} catch(IOException e){
configError(e);
return null;
} }
} }
private void configError(Exception ex){ return new Toml().read(Files.newInputStream(configPath));
logger.info("An error ocurred on configuration initialization: {}", ex.getMessage()); } catch (IOException ex) {
logger.info("An error ocurred on configuration initialization", ex);
return null;
}
} }
} }