diff options
9 files changed, 301 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b5cd48..8c5ad74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +#### **\[Build MC1.17.1-v1.1.0\]:** + +- Implemented config file (`/config/NoChatRestrictions.json`) with an `enableMod` option, allowing users to disable the mod without removing it. +- Fixed config debug log message missing the file name. +- Redesigned mod logo. + #### **\[Build MC1.17.1-v1.0.0\]:** - Initial release for Minecraft 1.17/1.17.1. diff --git a/Fabric/gradle.properties b/Fabric/gradle.properties index 3859663..a602c5d 100644 --- a/Fabric/gradle.properties +++ b/Fabric/gradle.properties @@ -12,7 +12,7 @@ loader_version=0.18.4 loom_version=1.13-SNAPSHOT # Mod Properties -mod_version=Fabric-MC1.17.1-v1.0.0 +mod_version=Fabric-MC1.17.1-v1.1.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 new file mode 100644 index 0000000..ce01583 --- /dev/null +++ b/Fabric/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java @@ -0,0 +1,100 @@ +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.Map; +import java.util.Optional; + +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.fabricmc.loader.api.FabricLoader; + +public final class NCRConfig { + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + private static NCRConfig instance = null; + + private final boolean enableMod; + + private NCRConfig(boolean enableMod) { + this.enableMod = enableMod; + } + + public boolean enableMod() { + return this.enableMod; + } + + private static NCRConfig getDefault() { + return new NCRConfig(true); + } + + 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("Mod Enabled: {}", config.enableMod()); + + 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 = getConfigFile(fileName); + + if (!Files.isRegularFile(file)) + return Optional.empty(); + + try (BufferedReader reader = Files.newBufferedReader(file)) { + JsonObject json = GSON.toJsonTree(getDefault()).getAsJsonObject(); + + for (Map.Entry<String, JsonElement> entry : new JsonParser().parse(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. " + + "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 = getConfigFile(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/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/resources/nochatrestrictions.mixins.json b/Fabric/src/main/resources/nochatrestrictions.mixins.json index b58d8b6..db7e726 100644 --- a/Fabric/src/main/resources/nochatrestrictions.mixins.json +++ b/Fabric/src/main/resources/nochatrestrictions.mixins.json @@ -1,6 +1,7 @@ { "required": true, "package": "com.aizistral.nochatrestrictions.mixins", + "plugin": "com.aizistral.nochatrestrictions.core.NCRMixinPlugin", "compatibilityLevel": "JAVA_16", "minVersion": "0.8", "mixins": [], diff --git a/Forge/gradle.properties b/Forge/gradle.properties index 55dd44f..acfc338 100644 --- a/Forge/gradle.properties +++ b/Forge/gradle.properties @@ -3,5 +3,5 @@ org.gradle.jvmargs=-Xmx4G org.gradle.daemon=false -modVersion=Forge-MC1.17.1-v1.0.0 -manifestVersion=1.17.1-v1.0.0
\ No newline at end of file +modVersion=Forge-MC1.17.1-v1.1.0 +manifestVersion=1.17.1-v1.1.0
\ 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 new file mode 100644 index 0000000..7d5cadd --- /dev/null +++ b/Forge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java @@ -0,0 +1,100 @@ +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.Map; +import java.util.Optional; + +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.minecraftforge.fml.loading.FMLPaths; + +public final class NCRConfig { + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + private static NCRConfig instance = null; + + private final boolean enableMod; + + private NCRConfig(boolean enableMod) { + this.enableMod = enableMod; + } + + public boolean enableMod() { + return this.enableMod; + } + + private static NCRConfig getDefault() { + return new NCRConfig(true); + } + + 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("Mod Enabled: {}", config.enableMod()); + + 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 = getConfigFile(fileName); + + if (!Files.isRegularFile(file)) + return Optional.empty(); + + try (BufferedReader reader = Files.newBufferedReader(file)) { + JsonObject json = GSON.toJsonTree(getDefault()).getAsJsonObject(); + + for (Map.Entry<String, JsonElement> entry : new JsonParser().parse(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. " + + "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 = getConfigFile(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/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/resources/nochatrestrictions.mixins.json b/Forge/src/main/resources/nochatrestrictions.mixins.json index 0d6638e..ffa1758 100644 --- a/Forge/src/main/resources/nochatrestrictions.mixins.json +++ b/Forge/src/main/resources/nochatrestrictions.mixins.json @@ -1,6 +1,7 @@ { "required": true, "package": "com.aizistral.nochatrestrictions.mixins", + "plugin": "com.aizistral.nochatrestrictions.core.NCRMixinPlugin", "compatibilityLevel": "JAVA_16", "minVersion": "0.8", "refmap": "nochatrestrictions.refmap.json", |
