diff options
| author | Aizistral | 2026-11-18 17:15:37 +0100 |
|---|---|---|
| committer | Aizistral | 2026-11-18 17:15:37 +0100 |
| commit | f9e5b8eb83b3569d5dde879198792eb0eb8f7b3d (patch) | |
| tree | 274cf28f18733ba492b49e660aefb21965952013 /Fabric/src/main/java | |
| parent | 896cded172ce1a42d555fe95b75930492ed69d7b (diff) | |
Backport telemetry and profanity filter config options
Diffstat (limited to 'Fabric/src/main/java')
5 files changed, 141 insertions, 25 deletions
diff --git a/Fabric/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java b/Fabric/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java new file mode 100644 index 0000000..ee04b0b --- /dev/null +++ b/Fabric/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/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/NCRCore.java b/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/NCRCore.java index f860b9e..40521d3 100644 --- a/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/NCRCore.java +++ b/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/NCRCore.java @@ -1,29 +1,8 @@ package com.aizistral.nochatrestrictions.core; -import java.util.Map; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import com.google.common.collect.ImmutableSet; -import com.mojang.authlib.minecraft.UserApiService.UserFlag; -import com.mojang.authlib.minecraft.UserApiService.UserProperties; - public class NCRCore { public static final Logger LOGGER = LogManager.getLogger("NoChatRestrictions"); - public static final UserProperties FORCED_USER_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_USER_PROPERTIES = new UserProperties(flags.build(), Map.of()); - } - } diff --git a/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserApiService.java b/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserApiService.java index 36ce804..1b653ee 100644 --- a/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserApiService.java +++ b/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserApiService.java @@ -4,7 +4,11 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.Executor; +import org.jetbrains.annotations.Nullable; + +import com.aizistral.nochatrestrictions.config.NCRConfig; import com.google.common.collect.ImmutableSet; +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; @@ -13,14 +17,18 @@ import com.mojang.authlib.yggdrasil.response.KeyPairResponse; public class WrappedUserApiService implements UserApiService { private final UserApiService service; + private @Nullable UserProperties properties = null; public WrappedUserApiService(UserApiService service) { this.service = service; } @Override - public UserProperties fetchProperties() { - return NCRCore.FORCED_USER_PROPERTIES; + public UserProperties fetchProperties() throws AuthenticationException { + if (this.properties != null) + return this.properties; + else + return this.properties = WrappedUserProperties.of(this.service.fetchProperties()); } @Override @@ -35,7 +43,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/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserProperties.java b/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/WrappedUserProperties.java new file mode 100644 index 0000000..1b59ac2 --- /dev/null +++ b/Fabric/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); + } + } + +} diff --git a/Fabric/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java b/Fabric/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java index 2aedadc..2131920 100644 --- a/Fabric/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java +++ b/Fabric/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java @@ -7,6 +7,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import com.aizistral.nochatrestrictions.core.NCRCore; import com.aizistral.nochatrestrictions.core.WrappedUserApiService; +import com.aizistral.nochatrestrictions.core.WrappedUserProperties; import com.mojang.authlib.minecraft.UserApiService; import com.mojang.authlib.minecraft.UserApiService.UserProperties; import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService; @@ -32,7 +33,11 @@ public class MixinMinecraft { // Everything that gates chat/multiplayer/realms reads them back through here. @Inject(method = "userProperties", at = @At("RETURN"), cancellable = true) public void onGetUserProperties(CallbackInfoReturnable<UserProperties> info) { - info.setReturnValue(NCRCore.FORCED_USER_PROPERTIES); + UserProperties properties = info.getReturnValue(); + + if (properties != null) { + info.setReturnValue(WrappedUserProperties.of(properties)); + } } @Inject(method = { "m_294837_", "isNameBanned" }, at = @At("HEAD"), cancellable = true) |
