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.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<UUID> loggedPlayers = Collections.<UUID>synchronizedSet(new HashSet<>());
protected final Set<UUID> loggedPlayers = Collections.synchronizedSet(new HashSet<>());
@Inject
public AuthMeVelocityPlugin(ProxyServer proxy, Logger logger, @DataDirectory Path dataDirectory) {
@ -78,32 +79,23 @@ public class AuthMeVelocityPlugin {
}
private Toml loadConfig(Path path){
if(!Files.exists(path)){
try {
if (Files.notExists(path)) {
Files.createDirectory(path);
} catch(IOException e){
configError(e);
return null;
}
}
Path configPath = path.resolve("config.toml");
if(!Files.exists(configPath)){
if (Files.notExists(configPath)) {
try (InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.toml")) {
Files.copy(in, configPath);
} catch(IOException e){
configError(e);
return null;
}
}
try {
return new Toml().read(Files.newInputStream(configPath));
} catch(IOException e){
configError(e);
return null;
Files.copy(Objects.requireNonNull(in, "The configuration does not exists"), configPath);
}
}
private void configError(Exception ex){
logger.info("An error ocurred on configuration initialization: {}", ex.getMessage());
return new Toml().read(Files.newInputStream(configPath));
} catch (IOException ex) {
logger.info("An error ocurred on configuration initialization", ex);
return null;
}
}
}