From 904d6017c3af1c734c5ffbeb616bfd3732c3a659 Mon Sep 17 00:00:00 2001 From: Aurora Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Sat, 4 Jun 2022 16:47:35 +0300 Subject: [PATCH] Refactor ServerFileLoader to do config file access properly Changes the order defaults and actual values are loaded in - It may affect the issue Affects issues: - Fixed #2254 --- .../storage/ServerFileLoader.java | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/identification/storage/ServerFileLoader.java b/Plan/common/src/main/java/com/djrapitops/plan/identification/storage/ServerFileLoader.java index e011e0a45..e8326e0f8 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/identification/storage/ServerFileLoader.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/identification/storage/ServerFileLoader.java @@ -32,15 +32,16 @@ import javax.inject.Singleton; import java.io.IOException; import java.util.Objects; import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; @Singleton -public class ServerFileLoader extends Config implements ServerLoader { +public class ServerFileLoader implements ServerLoader { private final String currentVersion; private final PlanFiles files; private final PlanConfig config; - private boolean prepared; + private final AtomicReference serverInfoConfig = new AtomicReference<>(); @Inject public ServerFileLoader( @@ -48,37 +49,43 @@ public class ServerFileLoader extends Config implements ServerLoader { PlanFiles files, PlanConfig config ) { - super(files.getFileFromPluginFolder("ServerInfoFile.yml")); this.currentVersion = currentVersion; this.files = files; this.config = config; - - prepared = false; } - public void prepare() throws IOException { - read(); + + private boolean isNotPrepared() { + return serverInfoConfig.get() == null; + } + + private void prepare() throws IOException { + serverInfoConfig.set(new Config( + files.getFileFromPluginFolder("ServerInfoFile.yml"), + readDefaults() + )); + } + + private Config readDefaults() throws IOException { try (ConfigReader reader = new ConfigReader(files.getResourceFromJar("DefaultServerInfoFile.yml").asInputStream())) { - copyMissing(reader.read()); + return reader.read(); } - save(); - prepared = true; } @Override public Optional load(ServerUUID loaded) { try { - if (!prepared) prepare(); + if (isNotPrepared()) prepare(); - String serverUUIDString = getString("Server.UUID"); + String serverUUIDString = serverInfoConfig.get().getString("Server.UUID"); if (serverUUIDString == null) return Optional.empty(); - Integer id = getInteger("Server.ID"); + Integer id = serverInfoConfig.get().getInteger("Server.ID"); ServerUUID serverUUID = ServerUUID.fromString(serverUUIDString); String name = config.getNode(PluginSettings.SERVER_NAME.getPath()) .map(ConfigNode::getString) .orElse("Proxy"); - String address = getString("Server.Web_address"); + String address = serverInfoConfig.get().getString("Server.Web_address"); return Optional.of(new Server(id, serverUUID, name, address, false, currentVersion)); } catch (IOException e) { @@ -89,15 +96,15 @@ public class ServerFileLoader extends Config implements ServerLoader { @Override public void save(Server server) { try { - if (!prepared) prepare(); + if (isNotPrepared()) prepare(); - server.getId().ifPresent(id -> set("Server.ID", id)); - set("Server.UUID", server.getUuid().toString()); - set("Server.Web_address", server.getWebAddress()); + server.getId().ifPresent(id -> serverInfoConfig.get().set("Server.ID", id)); + serverInfoConfig.get().set("Server.UUID", server.getUuid().toString()); + serverInfoConfig.get().set("Server.Web_address", server.getWebAddress()); - save(); + serverInfoConfig.get().save(); } catch (IOException e) { - throw new EnableException("Failed to write ServerInfoFile.yml: " + e.getMessage()); + throw new EnableException("Failed to write ServerInfoFile.yml: " + e.getMessage(), e); } }