aboutsummaryrefslogtreecommitdiffstats
path: root/Forge
diff options
context:
space:
mode:
Diffstat (limited to 'Forge')
-rw-r--r--Forge/src/main/java/com/aizistral/nochatrestrictions/config/NCRConfig.java100
-rw-r--r--Forge/src/main/java/com/aizistral/nochatrestrictions/core/NCRMixinPlugin.java45
-rw-r--r--Forge/src/main/resources/nochatrestrictions.mixins.json1
3 files changed, 146 insertions, 0 deletions
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..4f77010
--- /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...");
+ 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",