From 0da2b13c35b7bceb49b1c67a581d80af397c54fe Mon Sep 17 00:00:00 2001 From: Aizistral Date: Tue, 30 Aug 2022 13:52:41 +0200 Subject: Store server whitelist in a separate file --- .../nochatreports/NoChatReportsClient.java | 2 +- .../nochatreports/config/ModMenuIntegration.java | 6 +-- .../aizistral/nochatreports/config/NCRConfig.java | 29 +++++++++---- .../nochatreports/config/NCRConfigClient.java | 26 ------------ .../nochatreports/config/NCRServerWhitelist.java | 48 ++++++++++++++++++++++ .../nochatreports/gui/UnsafeServerScreen.java | 4 +- 6 files changed, 74 insertions(+), 41 deletions(-) create mode 100644 src/main/java/com/aizistral/nochatreports/config/NCRServerWhitelist.java (limited to 'src/main/java') diff --git a/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java b/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java index df919d2..e068559 100644 --- a/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java +++ b/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java @@ -67,7 +67,7 @@ public final class NoChatReportsClient implements ClientModInitializer { return; } else if (KEY_DISCONNECT_REASONS.contains(contents.getKey())) { if (ServerSafetyState.getLastServerAddress() != null) { - if (!NCRConfig.getClient().isWhitelistedServer(ServerSafetyState.getLastServerAddress()) && !NCRConfig.getClient().whitelistAllServers()) { + if (!NCRConfig.getServerWhitelist().isWhitelisted(ServerSafetyState.getLastServerAddress()) && !NCRConfig.getClient().whitelistAllServers()) { client.setScreen(new UnsafeServerScreen()); } else { if (ServerSafetyState.getReconnectCount() <= 0) { diff --git a/src/main/java/com/aizistral/nochatreports/config/ModMenuIntegration.java b/src/main/java/com/aizistral/nochatreports/config/ModMenuIntegration.java index 5253fe1..9f42b67 100644 --- a/src/main/java/com/aizistral/nochatreports/config/ModMenuIntegration.java +++ b/src/main/java/com/aizistral/nochatreports/config/ModMenuIntegration.java @@ -168,14 +168,14 @@ public final class ModMenuIntegration implements ModMenuApi { .build()); // Add or remove whitelisted servers - whitelistedServers.addEntry(entryBuilder.startStrList(Component.translatable("option.NoChatReports.whitelistedServers"), NCRConfig.getClient().whitelistedServers) + whitelistedServers.addEntry(entryBuilder.startStrList(Component.translatable("option.NoChatReports.whitelistedServers"), NCRConfig.getServerWhitelist().whitelistedServers) .setExpanded(true) .setInsertInFront(true) - .setDefaultValue(NCRConfigClient.DEFAULT_WHITELISTED_SERVERS) + .setDefaultValue(NCRServerWhitelist.DEFAULT_WHITELISTED_SERVERS) .setTooltip(this.makeTooltip("option.NoChatReports.whitelistedServers.tooltip")) .setAddButtonTooltip(Component.translatable("option.NoChatReports.whitelistedServers.addButtonTooltip")) .setRemoveButtonTooltip(Component.translatable("option.NoChatReports.whitelistedServers.removeButtonTooltip")) - .setSaveConsumer(newValue -> NCRConfig.getClient().whitelistedServers = newValue) + .setSaveConsumer(newValue -> NCRConfig.getServerWhitelist().whitelistedServers = newValue) .setCreateNewInstance(baseListEntry -> new StringListListEntry.StringListCell(Language.getInstance().getOrDefault(("option.NoChatReports.whitelistedServers.serverAddress")), baseListEntry)) .build()); diff --git a/src/main/java/com/aizistral/nochatreports/config/NCRConfig.java b/src/main/java/com/aizistral/nochatreports/config/NCRConfig.java index d1b51ff..c18be5d 100644 --- a/src/main/java/com/aizistral/nochatreports/config/NCRConfig.java +++ b/src/main/java/com/aizistral/nochatreports/config/NCRConfig.java @@ -5,6 +5,9 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Objects; +import java.util.function.Supplier; + +import javax.annotation.Nullable; import com.aizistral.nochatreports.NoChatReports; @@ -15,26 +18,32 @@ import net.fabricmc.loader.api.FabricLoader; public final class NCRConfig { private static NCRConfigCommon common = null; private static NCRConfigClient client = null; + private static NCRServerWhitelist whitelist = null; private NCRConfig() { throw new IllegalStateException("Can't touch this"); } public static NCRConfigCommon getCommon() { - if (common == null) { - load(); - } - - return common; + return checkLoaded(() -> common); } @Environment(EnvType.CLIENT) public static NCRConfigClient getClient() { - if (client == null) { + return checkLoaded(() -> client); + } + + @Environment(EnvType.CLIENT) + public static NCRServerWhitelist getServerWhitelist() { + return checkLoaded(() -> whitelist); + } + + private static T checkLoaded(Supplier config) { + if (config.get() == null) { load(); } - return client; + return config.get(); } public static void load() { @@ -42,16 +51,18 @@ public final class NCRConfig { if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { client = JSONConfig.loadConfig(NCRConfigClient.class, NCRConfigClient::new, NCRConfigClient.FILE_NAME); + whitelist = JSONConfig.loadConfig(NCRServerWhitelist.class, NCRServerWhitelist::new, NCRServerWhitelist.FILE_NAME); } save(); } public static void save() { - Objects.requireNonNull(common, "Cannot save config because it was not loaded!").saveFile(); + checkLoaded(() -> common).saveFile(); if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { - Objects.requireNonNull(client, "Cannot save config because it was not loaded!").saveFile(); + checkLoaded(() -> client).saveFile(); + checkLoaded(() -> whitelist).saveFile(); } Path readme = JSONConfig.CONFIG_DIR.resolve("NoChatReports/README.md"); diff --git a/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java b/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java index 4ae79cd..1d78df2 100644 --- a/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java +++ b/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java @@ -12,14 +12,12 @@ import net.minecraft.client.multiplayer.resolver.ServerAddress; public final class NCRConfigClient extends JSONConfig { protected static final String FILE_NAME = "NoChatReports/NCR-Client.json"; - protected static final List DEFAULT_WHITELISTED_SERVERS = new ArrayList<>(); protected boolean demandOnServer = false, showServerSafety = true, hideRedChatIndicators = true, hideYellowChatIndicators = true, hideGrayChatIndicators = true, hideWarningToast = true, alwaysHideReportButton = false, disableTelemetry = true, showReloadButton = true, whitelistAllServers = false, verifiedIconEnabled = true; protected int verifiedIconOffsetX = 0, verifiedIconOffsetY = 0; - protected List whitelistedServers = DEFAULT_WHITELISTED_SERVERS; protected NCRConfigClient() { super(FILE_NAME); @@ -55,30 +53,6 @@ public final class NCRConfigClient extends JSONConfig { return this.showServerSafety; } - /** - * @return Current list of whitelisted unsafe servers. Used on client to remember which servers - * user disabled unsafety warnings for.

- * - * This is empty by default. - * - * @see UnsafeServerScreen - */ - - public List getWhitelistedServers() { - return this.whitelistedServers; - } - - /** - * @param address Address of the server to be check. - * @return Whether this server is in the whitelist of unsafe servers, as returned by - * {@link #getWhitelistedServers()}. - * @see UnsafeServerScreen - */ - - public boolean isWhitelistedServer(ServerAddress address) { - return this.getWhitelistedServers().contains(address.getHost() + ":" + address.getPort()); - } - /** * @return True if vanilla's unsigned messages trust indicator should be removed.

* diff --git a/src/main/java/com/aizistral/nochatreports/config/NCRServerWhitelist.java b/src/main/java/com/aizistral/nochatreports/config/NCRServerWhitelist.java new file mode 100644 index 0000000..48658e7 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/config/NCRServerWhitelist.java @@ -0,0 +1,48 @@ +package com.aizistral.nochatreports.config; + +import java.util.ArrayList; +import java.util.List; + +import com.aizistral.nochatreports.gui.UnsafeServerScreen; + +import net.minecraft.client.multiplayer.resolver.ServerAddress; + +public class NCRServerWhitelist extends JSONConfig { + protected static final String FILE_NAME = "NoChatReports/NCR-ServerWhitelist.json"; + protected static final List DEFAULT_WHITELISTED_SERVERS = new ArrayList<>(); + protected List whitelistedServers = DEFAULT_WHITELISTED_SERVERS; + + protected NCRServerWhitelist() { + super(FILE_NAME); + } + + @Override + public NCRServerWhitelist getDefault() { + return new NCRServerWhitelist(); + } + + /** + * @return Current list of whitelisted unsafe servers. Used on client to remember which servers + * user disabled unsafety warnings for.

+ * + * This is empty by default. + * + * @see UnsafeServerScreen + */ + + public List getList() { + return this.whitelistedServers; + } + + /** + * @param address Address of the server to be check. + * @return Whether this server is in the whitelist of unsafe servers, as returned by + * {@link #getList()}. + * @see UnsafeServerScreen + */ + + public boolean isWhitelisted(ServerAddress address) { + return this.getList().contains(address.getHost() + ":" + address.getPort()); + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/gui/UnsafeServerScreen.java b/src/main/java/com/aizistral/nochatreports/gui/UnsafeServerScreen.java index 997ddd3..2547c77 100644 --- a/src/main/java/com/aizistral/nochatreports/gui/UnsafeServerScreen.java +++ b/src/main/java/com/aizistral/nochatreports/gui/UnsafeServerScreen.java @@ -48,8 +48,8 @@ public final class UnsafeServerScreen extends WarningScreen { if (address != null) { if (this.stopShowing.selected()) { - NCRConfig.getClient().getWhitelistedServers().add(address.getHost() + ":" + address.getPort()); - NCRConfig.getClient().saveFile(); + NCRConfig.getServerWhitelist().getList().add(address.getHost() + ":" + address.getPort()); + NCRConfig.getServerWhitelist().saveFile(); } ServerSafetyState.setAllowsUnsafeServer(true); -- cgit v1.2.3