aboutsummaryrefslogtreecommitdiffstats
path: root/NeoForge/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'NeoForge/src/main/java')
-rw-r--r--NeoForge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java79
-rw-r--r--NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserApiService.java34
-rw-r--r--NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserProperties.java42
3 files changed, 135 insertions, 20 deletions
diff --git a/NeoForge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java
new file mode 100644
index 0000000..ee04b0b
--- /dev/null
+++ b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java
@@ -0,0 +1,79 @@
+package com.aizistral.nochatrestrictions.config;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Optional;
+
+import org.jetbrains.annotations.Nullable;
+
+import com.aizistral.nochatrestrictions.core.NCRCore;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+import net.minecraft.client.Minecraft;
+
+public record NCRConfig(boolean allowTelemetry, boolean allowProfanityFilter) {
+ private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
+ private static @Nullable NCRConfig instance = null;
+
+ private static NCRConfig getDefault() {
+ return new NCRConfig(false, false);
+ }
+
+ public static NCRConfig getInstance() {
+ if (instance == null) {
+ instance = loadConfig("NoChatRestrictions.json");
+ }
+
+ return instance;
+ }
+
+ private static NCRConfig loadConfig(String fileName) {
+ NCRCore.LOGGER.debug("Reading config file {}...", fileName);
+ NCRConfig config = readFile(fileName).orElseGet(NCRConfig::getDefault);
+
+ NCRCore.LOGGER.info("Telemetry Allowed: {}", config.allowTelemetry());
+ NCRCore.LOGGER.info("Profanity Filter Allowed: {}", config.allowProfanityFilter());
+
+ NCRCore.LOGGER.debug("Writing config file {} back to disk...");
+ writeFile(fileName, config);
+
+ return config;
+ }
+
+ private static Optional<NCRConfig> readFile(String fileName) {
+ Path file = Minecraft.getInstance().gameDirectory.toPath().resolve("config").resolve(fileName);
+
+ if (!Files.isRegularFile(file))
+ return Optional.empty();
+
+ try (BufferedReader reader = Files.newBufferedReader(file)) {
+ return Optional.of(GSON.fromJson(reader, NCRConfig.class));
+ } catch (Exception ex) {
+ NCRCore.LOGGER.fatal("Could not read config file: {}", file);
+ NCRCore.LOGGER.fatal("This likely indicates the file is corrupted. "
+ + "You can try deleting it to fix this problem. Full stacktrace below:");
+ NCRCore.LOGGER.catching(ex);
+ throw new RuntimeException("Could not read config file: " + file, ex);
+ }
+ }
+
+ private static void writeFile(String fileName, NCRConfig config) {
+ Path file = Minecraft.getInstance().gameDirectory.toPath().resolve("config").resolve(fileName);
+
+ try {
+ Files.createDirectories(file.getParent());
+ try (BufferedWriter writer = Files.newBufferedWriter(file)) {
+ GSON.toJson(config, writer);
+ }
+ } catch (Exception ex) {
+ NCRCore.LOGGER.fatal("Could not write config file: {}", file);
+ NCRCore.LOGGER.fatal("Full stacktrace below:");
+ NCRCore.LOGGER.catching(ex);
+ throw new RuntimeException("Could not write config file: " + file,ex);
+ }
+ }
+
+}
diff --git a/NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserApiService.java b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserApiService.java
index e3e367d..40bcbba 100644
--- a/NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserApiService.java
+++ b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserApiService.java
@@ -1,10 +1,12 @@
package com.aizistral.nochatrestrictions.core;
-import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executor;
-import com.google.common.collect.ImmutableSet;
+import org.jetbrains.annotations.Nullable;
+
+import com.aizistral.nochatrestrictions.config.NCRConfig;
+import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.minecraft.TelemetrySession;
import com.mojang.authlib.minecraft.UserApiService;
import com.mojang.authlib.minecraft.report.AbuseReportLimits;
@@ -12,30 +14,19 @@ import com.mojang.authlib.yggdrasil.request.AbuseReportRequest;
import com.mojang.authlib.yggdrasil.response.KeyPairResponse;
public class WrappedUserApiService implements UserApiService {
- private static final UserProperties FORCED_PROPERTIES;
-
- static {
- ImmutableSet.Builder<UserFlag> flags = ImmutableSet.builder();
-
- flags.add(UserFlag.CHAT_ALLOWED); // always let the player access chat
- flags.add(UserFlag.SERVERS_ALLOWED); // always let the player open multiplayer menu
- flags.add(UserFlag.REALMS_ALLOWED); // always let the player open Realms menu
- // flags.add(UserFlag.TELEMETRY_ENABLED); // not adding this for obvious reasons
- // flags.add(UserFlag.OPTIONAL_TELEMETRY_AVAILABLE); // thanks but no thanks
- // flags.add(UserFlag.PROFANITY_FILTER_ENABLED) // not adding this one either
-
- FORCED_PROPERTIES = new UserProperties(flags.build(), Map.of());
- }
-
private final UserApiService service;
+ private @Nullable UserProperties properties = null;
public WrappedUserApiService(UserApiService service) {
this.service = service;
}
@Override
- public UserProperties fetchProperties() {
- return FORCED_PROPERTIES;
+ public UserProperties fetchProperties() throws AuthenticationException {
+ if (this.properties != null)
+ return this.properties;
+ else
+ return this.properties = WrappedUserProperties.of(this.service.fetchProperties());
}
@Override
@@ -50,7 +41,10 @@ public class WrappedUserApiService implements UserApiService {
@Override
public TelemetrySession newTelemetrySession(Executor executor) {
- return TelemetrySession.DISABLED;
+ if (NCRConfig.getInstance().allowTelemetry())
+ return this.service.newTelemetrySession(executor);
+ else
+ return TelemetrySession.DISABLED;
}
// Methods below primarily concern chat reporting. Not doing anything with them
diff --git a/NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserProperties.java b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserProperties.java
new file mode 100644
index 0000000..1b59ac2
--- /dev/null
+++ b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserProperties.java
@@ -0,0 +1,42 @@
+package com.aizistral.nochatrestrictions.core;
+
+import java.util.Map;
+
+import com.aizistral.nochatrestrictions.config.NCRConfig;
+import com.google.common.collect.ImmutableSet;
+import com.mojang.authlib.minecraft.UserApiService.UserFlag;
+import com.mojang.authlib.minecraft.UserApiService.UserProperties;
+
+public final class WrappedUserProperties {
+
+ private WrappedUserProperties() {
+ throw new UnsupportedOperationException("Can't touch this");
+ }
+
+ public static UserProperties of(UserProperties properties) {
+ NCRConfig config = NCRConfig.getInstance();
+ ImmutableSet.Builder<UserFlag> flags = ImmutableSet.builder();
+
+ flags.add(UserFlag.CHAT_ALLOWED);
+ flags.add(UserFlag.SERVERS_ALLOWED);
+ flags.add(UserFlag.REALMS_ALLOWED);
+
+ if (config.allowTelemetry()) {
+ addOptionalFlag(UserFlag.TELEMETRY_ENABLED, flags, properties);
+ addOptionalFlag(UserFlag.OPTIONAL_TELEMETRY_AVAILABLE, flags, properties);
+ }
+
+ if (config.allowProfanityFilter()) {
+ addOptionalFlag(UserFlag.PROFANITY_FILTER_ENABLED, flags, properties);
+ }
+
+ return new UserProperties(flags.build(), Map.of());
+ }
+
+ private static void addOptionalFlag(UserFlag flag, ImmutableSet.Builder<UserFlag> builder, UserProperties properties) {
+ if (properties.flag(flag)) {
+ builder.add(flag);
+ }
+ }
+
+}