diff options
19 files changed, 252 insertions, 212 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d8e3ce0..2df5f5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +#### **\[Build MC26.3-v1.4.0\]:** + +- Added `enableMod` option to the config file, allowing users to disable the mod without removing it. +- Fixed incompatibility with [In-Game Account Switcher](https://modrinth.com/mod/in-game-account-switcher) on Forge and NeoForge ([based on Sw3ys' fix, #27](https://github.com/Aizistral-Studios/No-Chat-Restrictions/pull/27)). +- Fixed config debug log message missing the file name. +- Redesigned mod logo. + #### **\[Build MC26.3-v1.3.0\]:** - Initial release for Minecraft 26.3 ([thanks to Shadowcat for some worthwhile improvements, #30](https://github.com/Aizistral-Studios/No-Chat-Restrictions/pull/30)). diff --git a/Fabric/gradle.properties b/Fabric/gradle.properties index 2511b71..c3993f6 100644 --- a/Fabric/gradle.properties +++ b/Fabric/gradle.properties @@ -12,7 +12,7 @@ loader_version=0.19.5 loom_version=1.17.21 # Mod Properties -mod_version=Fabric-MC26.3-v1.3.0 +mod_version=Fabric-MC26.3-v1.4.0 maven_group=com.aizistral.nochatrestrictions archives_base_name=NoChatRestrictions diff --git a/Fabric/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java b/Fabric/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java index ee04b0b..860a6c6 100644 --- a/Fabric/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java +++ b/Fabric/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java @@ -4,6 +4,7 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Map; import java.util.Optional; import org.jetbrains.annotations.Nullable; @@ -11,15 +12,18 @@ import org.jetbrains.annotations.Nullable; import com.aizistral.nochatrestrictions.core.NCRCore; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; -import net.minecraft.client.Minecraft; +import net.fabricmc.loader.api.FabricLoader; -public record NCRConfig(boolean allowTelemetry, boolean allowProfanityFilter) { +public record NCRConfig(boolean enableMod, 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); + return new NCRConfig(true, false, false); } public static NCRConfig getInstance() { @@ -34,23 +38,34 @@ public record NCRConfig(boolean allowTelemetry, boolean allowProfanityFilter) { NCRCore.LOGGER.debug("Reading config file {}...", fileName); NCRConfig config = readFile(fileName).orElseGet(NCRConfig::getDefault); + NCRCore.LOGGER.info("Mod Enabled: {}", config.enableMod()); NCRCore.LOGGER.info("Telemetry Allowed: {}", config.allowTelemetry()); NCRCore.LOGGER.info("Profanity Filter Allowed: {}", config.allowProfanityFilter()); - NCRCore.LOGGER.debug("Writing config file {} back to disk..."); + NCRCore.LOGGER.debug("Writing config file {} back to disk...", fileName); writeFile(fileName, config); return config; } + private static Path getConfigFile(String fileName) { + return FabricLoader.getInstance().getConfigDir().resolve(fileName); + } + private static Optional<NCRConfig> readFile(String fileName) { - Path file = Minecraft.getInstance().gameDirectory.toPath().resolve("config").resolve(fileName); + Path file = getConfigFile(fileName); if (!Files.isRegularFile(file)) return Optional.empty(); try (BufferedReader reader = Files.newBufferedReader(file)) { - return Optional.of(GSON.fromJson(reader, NCRConfig.class)); + JsonObject json = GSON.toJsonTree(getDefault()).getAsJsonObject(); + + for (Map.Entry<String, JsonElement> entry : JsonParser.parseReader(reader).getAsJsonObject().entrySet()) { + json.add(entry.getKey(), entry.getValue()); + } + + return Optional.of(GSON.fromJson(json, NCRConfig.class)); } catch (Exception ex) { NCRCore.LOGGER.fatal("Could not read config file: {}", file); NCRCore.LOGGER.fatal("This likely indicates the file is corrupted. " @@ -61,7 +76,7 @@ public record NCRConfig(boolean allowTelemetry, boolean allowProfanityFilter) { } private static void writeFile(String fileName, NCRConfig config) { - Path file = Minecraft.getInstance().gameDirectory.toPath().resolve("config").resolve(fileName); + Path file = getConfigFile(fileName); try { Files.createDirectories(file.getParent()); diff --git a/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/NCRMixinPlugin.java b/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/NCRMixinPlugin.java new file mode 100644 index 0000000..cac9d7f --- /dev/null +++ b/Fabric/src/main/java/com/aizistral/nochatrestrictions/core/NCRMixinPlugin.java @@ -0,0 +1,45 @@ +package com.aizistral.nochatrestrictions.core; + +import java.util.List; +import java.util.Set; + +import org.objectweb.asm.tree.ClassNode; +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; +import org.spongepowered.asm.mixin.extensibility.IMixinInfo; + +import com.aizistral.nochatrestrictions.config.NCRConfig; + +public class NCRMixinPlugin implements IMixinConfigPlugin { + + @Override + public void onLoad(String mixinPackage) { + } + + @Override + public String getRefMapperConfig() { + return null; + } + + @Override + public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { + return NCRConfig.getInstance().enableMod(); + } + + @Override + public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) { + } + + @Override + public List<String> getMixins() { + return null; + } + + @Override + public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + } + + @Override + public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + } + +} 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 2a8f935..862d381 100644 --- a/Fabric/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java +++ b/Fabric/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java @@ -7,7 +7,9 @@ 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.services.MinecraftServicesDiscoveryService; import net.minecraft.client.Minecraft; @@ -26,6 +28,15 @@ public class MixinMinecraft { NCRCore.LOGGER.info("Successfully supplanted UserApiService with a wrapped version."); } + @Inject(method = "userProperties", at = @At("RETURN"), cancellable = true) + private void onGetUserProperties(CallbackInfoReturnable<UserProperties> info) { + UserProperties properties = info.getReturnValue(); + + if (properties != null) { + info.setReturnValue(WrappedUserProperties.of(properties)); + } + } + @Inject(method = "isNameBanned", at = @At("HEAD"), cancellable = true) private void onCheckNameBan(CallbackInfoReturnable<Boolean> info) { info.setReturnValue(Boolean.FALSE); diff --git a/Fabric/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraftServicesUserApiService.java b/Fabric/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraftServicesUserApiService.java deleted file mode 100644 index f38140b..0000000 --- a/Fabric/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraftServicesUserApiService.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.aizistral.nochatrestrictions.mixins; - -import java.util.concurrent.Executor; - -import org.jetbrains.annotations.Nullable; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import com.aizistral.nochatrestrictions.config.NCRConfig; -import com.aizistral.nochatrestrictions.core.NCRCore; -import com.aizistral.nochatrestrictions.core.WrappedUserProperties; -import com.mojang.authlib.minecraft.TelemetrySession; -import com.mojang.authlib.minecraft.UserApiService.UserProperties; -import com.mojang.authlib.services.MinecraftServicesUserApiService; - -/** - * Applies the chat/multiplayer restriction removal directly on the concrete - * service class instead of just wrapping it once at {@code Minecraft#createUserApiService}. - * - * In-game account switchers (e.g. IAS) build a brand new {@link MinecraftServicesUserApiService} - * and swap it onto the Minecraft instance without going through the original wrapping - * path, which is why restrictions used to come back after switching accounts. By hooking - * the service class itself, every instance the game ever uses returns permissive - * properties, regardless of who created it or when. - */ -@Mixin(value = MinecraftServicesUserApiService.class, remap = false) -public class MixinMinecraftServicesUserApiService { - - static { - NCRCore.LOGGER.info("MixinMinecraftServicesUserApiService initialized successfully."); - } - - private @Unique @Nullable UserProperties wrappedProperties; - - @Inject(method = "fetchProperties", at = @At("RETURN"), cancellable = true) - private void onFetchProperties(CallbackInfoReturnable<UserProperties> info) { - UserProperties original = info.getReturnValue(); - - if (original == null) - return; - else if (this.wrappedProperties != null) { - info.setReturnValue(this.wrappedProperties); - return; - } else { - info.setReturnValue(this.wrappedProperties = WrappedUserProperties.of(original)); - return; - } - } - - @Inject(method = "newTelemetrySession", at = @At("HEAD"), cancellable = true) - private void onNewTelemetrySession(Executor executor, CallbackInfoReturnable<TelemetrySession> info) { - if (!NCRConfig.getInstance().allowTelemetry()) { - info.setReturnValue(TelemetrySession.DISABLED); - } - } - -} diff --git a/Fabric/src/main/resources/nochatrestrictions.mixins.json b/Fabric/src/main/resources/nochatrestrictions.mixins.json index 8700b64..fec6468 100644 --- a/Fabric/src/main/resources/nochatrestrictions.mixins.json +++ b/Fabric/src/main/resources/nochatrestrictions.mixins.json @@ -1,11 +1,11 @@ { "required": true, "package": "com.aizistral.nochatrestrictions.mixins", + "plugin": "com.aizistral.nochatrestrictions.core.NCRMixinPlugin", "minVersion": "0.8", "mixins": [], "client": [ - "MixinMinecraft", - "MixinMinecraftServicesUserApiService" + "MixinMinecraft" ], "injectors": { "defaultRequire": 1 diff --git a/Forge/gradle.properties b/Forge/gradle.properties index 475c5b7..81fa167 100644 --- a/Forge/gradle.properties +++ b/Forge/gradle.properties @@ -8,7 +8,7 @@ minecraft_version=26.3 forge_version=66.0.8 archivesBaseName=NoChatRestrictions -modVersion=Forge-MC26.3-v1.3.0 -manifestVersion=26.3-v1.3.0 +modVersion=Forge-MC26.3-v1.4.0 +manifestVersion=26.3-v1.4.0 net.minecraftforge.gradle.merge-source-sets=true
\ No newline at end of file diff --git a/Forge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java b/Forge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java index ee04b0b..590e4ab 100644 --- a/Forge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java +++ b/Forge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java @@ -4,6 +4,7 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Map; import java.util.Optional; import org.jetbrains.annotations.Nullable; @@ -11,15 +12,18 @@ import org.jetbrains.annotations.Nullable; import com.aizistral.nochatrestrictions.core.NCRCore; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; -import net.minecraft.client.Minecraft; +import net.minecraftforge.fml.loading.FMLPaths; -public record NCRConfig(boolean allowTelemetry, boolean allowProfanityFilter) { +public record NCRConfig(boolean enableMod, 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); + return new NCRConfig(true, false, false); } public static NCRConfig getInstance() { @@ -34,23 +38,34 @@ public record NCRConfig(boolean allowTelemetry, boolean allowProfanityFilter) { NCRCore.LOGGER.debug("Reading config file {}...", fileName); NCRConfig config = readFile(fileName).orElseGet(NCRConfig::getDefault); + NCRCore.LOGGER.info("Mod Enabled: {}", config.enableMod()); NCRCore.LOGGER.info("Telemetry Allowed: {}", config.allowTelemetry()); NCRCore.LOGGER.info("Profanity Filter Allowed: {}", config.allowProfanityFilter()); - NCRCore.LOGGER.debug("Writing config file {} back to disk..."); + NCRCore.LOGGER.debug("Writing config file {} back to disk...", fileName); writeFile(fileName, config); return config; } + private static Path getConfigFile(String fileName) { + return FMLPaths.CONFIGDIR.get().resolve(fileName); + } + private static Optional<NCRConfig> readFile(String fileName) { - Path file = Minecraft.getInstance().gameDirectory.toPath().resolve("config").resolve(fileName); + Path file = getConfigFile(fileName); if (!Files.isRegularFile(file)) return Optional.empty(); try (BufferedReader reader = Files.newBufferedReader(file)) { - return Optional.of(GSON.fromJson(reader, NCRConfig.class)); + JsonObject json = GSON.toJsonTree(getDefault()).getAsJsonObject(); + + for (Map.Entry<String, JsonElement> entry : JsonParser.parseReader(reader).getAsJsonObject().entrySet()) { + json.add(entry.getKey(), entry.getValue()); + } + + return Optional.of(GSON.fromJson(json, NCRConfig.class)); } catch (Exception ex) { NCRCore.LOGGER.fatal("Could not read config file: {}", file); NCRCore.LOGGER.fatal("This likely indicates the file is corrupted. " @@ -61,7 +76,7 @@ public record NCRConfig(boolean allowTelemetry, boolean allowProfanityFilter) { } private static void writeFile(String fileName, NCRConfig config) { - Path file = Minecraft.getInstance().gameDirectory.toPath().resolve("config").resolve(fileName); + Path file = getConfigFile(fileName); try { Files.createDirectories(file.getParent()); diff --git a/Forge/src/main/java/com/aizistral/nochatrestrictions/core/NCRMixinPlugin.java b/Forge/src/main/java/com/aizistral/nochatrestrictions/core/NCRMixinPlugin.java new file mode 100644 index 0000000..cac9d7f --- /dev/null +++ b/Forge/src/main/java/com/aizistral/nochatrestrictions/core/NCRMixinPlugin.java @@ -0,0 +1,45 @@ +package com.aizistral.nochatrestrictions.core; + +import java.util.List; +import java.util.Set; + +import org.objectweb.asm.tree.ClassNode; +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; +import org.spongepowered.asm.mixin.extensibility.IMixinInfo; + +import com.aizistral.nochatrestrictions.config.NCRConfig; + +public class NCRMixinPlugin implements IMixinConfigPlugin { + + @Override + public void onLoad(String mixinPackage) { + } + + @Override + public String getRefMapperConfig() { + return null; + } + + @Override + public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { + return NCRConfig.getInstance().enableMod(); + } + + @Override + public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) { + } + + @Override + public List<String> getMixins() { + return null; + } + + @Override + public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + } + + @Override + public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + } + +} diff --git a/Forge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java b/Forge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java index 2a8f935..862d381 100644 --- a/Forge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java +++ b/Forge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java @@ -7,7 +7,9 @@ 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.services.MinecraftServicesDiscoveryService; import net.minecraft.client.Minecraft; @@ -26,6 +28,15 @@ public class MixinMinecraft { NCRCore.LOGGER.info("Successfully supplanted UserApiService with a wrapped version."); } + @Inject(method = "userProperties", at = @At("RETURN"), cancellable = true) + private void onGetUserProperties(CallbackInfoReturnable<UserProperties> info) { + UserProperties properties = info.getReturnValue(); + + if (properties != null) { + info.setReturnValue(WrappedUserProperties.of(properties)); + } + } + @Inject(method = "isNameBanned", at = @At("HEAD"), cancellable = true) private void onCheckNameBan(CallbackInfoReturnable<Boolean> info) { info.setReturnValue(Boolean.FALSE); diff --git a/Forge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraftServicesUserApiService.java b/Forge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraftServicesUserApiService.java deleted file mode 100644 index f38140b..0000000 --- a/Forge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraftServicesUserApiService.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.aizistral.nochatrestrictions.mixins; - -import java.util.concurrent.Executor; - -import org.jetbrains.annotations.Nullable; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import com.aizistral.nochatrestrictions.config.NCRConfig; -import com.aizistral.nochatrestrictions.core.NCRCore; -import com.aizistral.nochatrestrictions.core.WrappedUserProperties; -import com.mojang.authlib.minecraft.TelemetrySession; -import com.mojang.authlib.minecraft.UserApiService.UserProperties; -import com.mojang.authlib.services.MinecraftServicesUserApiService; - -/** - * Applies the chat/multiplayer restriction removal directly on the concrete - * service class instead of just wrapping it once at {@code Minecraft#createUserApiService}. - * - * In-game account switchers (e.g. IAS) build a brand new {@link MinecraftServicesUserApiService} - * and swap it onto the Minecraft instance without going through the original wrapping - * path, which is why restrictions used to come back after switching accounts. By hooking - * the service class itself, every instance the game ever uses returns permissive - * properties, regardless of who created it or when. - */ -@Mixin(value = MinecraftServicesUserApiService.class, remap = false) -public class MixinMinecraftServicesUserApiService { - - static { - NCRCore.LOGGER.info("MixinMinecraftServicesUserApiService initialized successfully."); - } - - private @Unique @Nullable UserProperties wrappedProperties; - - @Inject(method = "fetchProperties", at = @At("RETURN"), cancellable = true) - private void onFetchProperties(CallbackInfoReturnable<UserProperties> info) { - UserProperties original = info.getReturnValue(); - - if (original == null) - return; - else if (this.wrappedProperties != null) { - info.setReturnValue(this.wrappedProperties); - return; - } else { - info.setReturnValue(this.wrappedProperties = WrappedUserProperties.of(original)); - return; - } - } - - @Inject(method = "newTelemetrySession", at = @At("HEAD"), cancellable = true) - private void onNewTelemetrySession(Executor executor, CallbackInfoReturnable<TelemetrySession> info) { - if (!NCRConfig.getInstance().allowTelemetry()) { - info.setReturnValue(TelemetrySession.DISABLED); - } - } - -} diff --git a/Forge/src/main/resources/nochatrestrictions.mixins.json b/Forge/src/main/resources/nochatrestrictions.mixins.json index d50b07b..5f90fba 100644 --- a/Forge/src/main/resources/nochatrestrictions.mixins.json +++ b/Forge/src/main/resources/nochatrestrictions.mixins.json @@ -1,11 +1,11 @@ { "required": true, "package": "com.aizistral.nochatrestrictions.mixins", + "plugin": "com.aizistral.nochatrestrictions.core.NCRMixinPlugin", "minVersion": "0.8", "mixins": [], "client": [ - "MixinMinecraft", - "MixinMinecraftServicesUserApiService" + "MixinMinecraft" ], "injectors": { "defaultRequire": 1 diff --git a/NeoForge/gradle.properties b/NeoForge/gradle.properties index c23b501..d0fea0d 100644 --- a/NeoForge/gradle.properties +++ b/NeoForge/gradle.properties @@ -6,5 +6,5 @@ org.gradle.configuration-cache=false ## Environment Properties minecraft_version=26.3 neo_version=26.3.0.36-beta -mod_version=NeoForge-MC26.3-v1.3.0 -manifest_version=26.3-v1.3.0 +mod_version=NeoForge-MC26.3-v1.4.0 +manifest_version=26.3-v1.4.0 diff --git a/NeoForge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java index ee04b0b..5b21b53 100644 --- a/NeoForge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java +++ b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java @@ -4,6 +4,7 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Map; import java.util.Optional; import org.jetbrains.annotations.Nullable; @@ -11,15 +12,18 @@ import org.jetbrains.annotations.Nullable; import com.aizistral.nochatrestrictions.core.NCRCore; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; -import net.minecraft.client.Minecraft; +import net.neoforged.fml.loading.FMLPaths; -public record NCRConfig(boolean allowTelemetry, boolean allowProfanityFilter) { +public record NCRConfig(boolean enableMod, 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); + return new NCRConfig(true, false, false); } public static NCRConfig getInstance() { @@ -34,23 +38,34 @@ public record NCRConfig(boolean allowTelemetry, boolean allowProfanityFilter) { NCRCore.LOGGER.debug("Reading config file {}...", fileName); NCRConfig config = readFile(fileName).orElseGet(NCRConfig::getDefault); + NCRCore.LOGGER.info("Mod Enabled: {}", config.enableMod()); NCRCore.LOGGER.info("Telemetry Allowed: {}", config.allowTelemetry()); NCRCore.LOGGER.info("Profanity Filter Allowed: {}", config.allowProfanityFilter()); - NCRCore.LOGGER.debug("Writing config file {} back to disk..."); + NCRCore.LOGGER.debug("Writing config file {} back to disk...", fileName); writeFile(fileName, config); return config; } + private static Path getConfigFile(String fileName) { + return FMLPaths.CONFIGDIR.get().resolve(fileName); + } + private static Optional<NCRConfig> readFile(String fileName) { - Path file = Minecraft.getInstance().gameDirectory.toPath().resolve("config").resolve(fileName); + Path file = getConfigFile(fileName); if (!Files.isRegularFile(file)) return Optional.empty(); try (BufferedReader reader = Files.newBufferedReader(file)) { - return Optional.of(GSON.fromJson(reader, NCRConfig.class)); + JsonObject json = GSON.toJsonTree(getDefault()).getAsJsonObject(); + + for (Map.Entry<String, JsonElement> entry : JsonParser.parseReader(reader).getAsJsonObject().entrySet()) { + json.add(entry.getKey(), entry.getValue()); + } + + return Optional.of(GSON.fromJson(json, NCRConfig.class)); } catch (Exception ex) { NCRCore.LOGGER.fatal("Could not read config file: {}", file); NCRCore.LOGGER.fatal("This likely indicates the file is corrupted. " @@ -61,7 +76,7 @@ public record NCRConfig(boolean allowTelemetry, boolean allowProfanityFilter) { } private static void writeFile(String fileName, NCRConfig config) { - Path file = Minecraft.getInstance().gameDirectory.toPath().resolve("config").resolve(fileName); + Path file = getConfigFile(fileName); try { Files.createDirectories(file.getParent()); diff --git a/NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/NCRMixinPlugin.java b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/NCRMixinPlugin.java new file mode 100644 index 0000000..cac9d7f --- /dev/null +++ b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/core/NCRMixinPlugin.java @@ -0,0 +1,45 @@ +package com.aizistral.nochatrestrictions.core; + +import java.util.List; +import java.util.Set; + +import org.objectweb.asm.tree.ClassNode; +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; +import org.spongepowered.asm.mixin.extensibility.IMixinInfo; + +import com.aizistral.nochatrestrictions.config.NCRConfig; + +public class NCRMixinPlugin implements IMixinConfigPlugin { + + @Override + public void onLoad(String mixinPackage) { + } + + @Override + public String getRefMapperConfig() { + return null; + } + + @Override + public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { + return NCRConfig.getInstance().enableMod(); + } + + @Override + public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) { + } + + @Override + public List<String> getMixins() { + return null; + } + + @Override + public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + } + + @Override + public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + } + +} diff --git a/NeoForge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java index 2a8f935..862d381 100644 --- a/NeoForge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java +++ b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraft.java @@ -7,7 +7,9 @@ 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.services.MinecraftServicesDiscoveryService; import net.minecraft.client.Minecraft; @@ -26,6 +28,15 @@ public class MixinMinecraft { NCRCore.LOGGER.info("Successfully supplanted UserApiService with a wrapped version."); } + @Inject(method = "userProperties", at = @At("RETURN"), cancellable = true) + private void onGetUserProperties(CallbackInfoReturnable<UserProperties> info) { + UserProperties properties = info.getReturnValue(); + + if (properties != null) { + info.setReturnValue(WrappedUserProperties.of(properties)); + } + } + @Inject(method = "isNameBanned", at = @At("HEAD"), cancellable = true) private void onCheckNameBan(CallbackInfoReturnable<Boolean> info) { info.setReturnValue(Boolean.FALSE); diff --git a/NeoForge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraftServicesUserApiService.java b/NeoForge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraftServicesUserApiService.java deleted file mode 100644 index f38140b..0000000 --- a/NeoForge/src/main/java/com/aizistral/nochatrestrictions/mixins/MixinMinecraftServicesUserApiService.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.aizistral.nochatrestrictions.mixins; - -import java.util.concurrent.Executor; - -import org.jetbrains.annotations.Nullable; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import com.aizistral.nochatrestrictions.config.NCRConfig; -import com.aizistral.nochatrestrictions.core.NCRCore; -import com.aizistral.nochatrestrictions.core.WrappedUserProperties; -import com.mojang.authlib.minecraft.TelemetrySession; -import com.mojang.authlib.minecraft.UserApiService.UserProperties; -import com.mojang.authlib.services.MinecraftServicesUserApiService; - -/** - * Applies the chat/multiplayer restriction removal directly on the concrete - * service class instead of just wrapping it once at {@code Minecraft#createUserApiService}. - * - * In-game account switchers (e.g. IAS) build a brand new {@link MinecraftServicesUserApiService} - * and swap it onto the Minecraft instance without going through the original wrapping - * path, which is why restrictions used to come back after switching accounts. By hooking - * the service class itself, every instance the game ever uses returns permissive - * properties, regardless of who created it or when. - */ -@Mixin(value = MinecraftServicesUserApiService.class, remap = false) -public class MixinMinecraftServicesUserApiService { - - static { - NCRCore.LOGGER.info("MixinMinecraftServicesUserApiService initialized successfully."); - } - - private @Unique @Nullable UserProperties wrappedProperties; - - @Inject(method = "fetchProperties", at = @At("RETURN"), cancellable = true) - private void onFetchProperties(CallbackInfoReturnable<UserProperties> info) { - UserProperties original = info.getReturnValue(); - - if (original == null) - return; - else if (this.wrappedProperties != null) { - info.setReturnValue(this.wrappedProperties); - return; - } else { - info.setReturnValue(this.wrappedProperties = WrappedUserProperties.of(original)); - return; - } - } - - @Inject(method = "newTelemetrySession", at = @At("HEAD"), cancellable = true) - private void onNewTelemetrySession(Executor executor, CallbackInfoReturnable<TelemetrySession> info) { - if (!NCRConfig.getInstance().allowTelemetry()) { - info.setReturnValue(TelemetrySession.DISABLED); - } - } - -} diff --git a/NeoForge/src/main/resources/nochatrestrictions.mixins.json b/NeoForge/src/main/resources/nochatrestrictions.mixins.json index d50b07b..5f90fba 100644 --- a/NeoForge/src/main/resources/nochatrestrictions.mixins.json +++ b/NeoForge/src/main/resources/nochatrestrictions.mixins.json @@ -1,11 +1,11 @@ { "required": true, "package": "com.aizistral.nochatrestrictions.mixins", + "plugin": "com.aizistral.nochatrestrictions.core.NCRMixinPlugin", "minVersion": "0.8", "mixins": [], "client": [ - "MixinMinecraft", - "MixinMinecraftServicesUserApiService" + "MixinMinecraft" ], "injectors": { "defaultRequire": 1 |
