Browse Source

ModMenu integration;

umollu 4 years ago
parent
commit
b6f6cf8e2e

+ 10 - 2
build.gradle

@@ -1,5 +1,5 @@
 plugins {
-	id 'fabric-loom' version '0.2.7-SNAPSHOT'
+	id 'fabric-loom' version '0.4-SNAPSHOT'
 	id 'maven-publish'
 }
 
@@ -28,9 +28,17 @@ dependencies {
 	mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
 	modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
 	modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
+
 	modImplementation "io.github.cottonmc:cotton-client-commands:${project.clientcommands_version}"
 	include "io.github.cottonmc:cotton-client-commands:${project.clientcommands_version}"
-	implementation 'com.google.code.gson:gson:2.8.6'
+
+	modApi "me.sargunvohra.mcmods:autoconfig1u:${project.autoconfig_version}"
+	include "me.sargunvohra.mcmods:autoconfig1u:${project.autoconfig_version}"
+
+	modApi "me.shedaniel.cloth:config-2:${project.clothconfig_version}"
+	include "me.shedaniel.cloth:config-2:${project.clothconfig_version}"
+
+	modImplementation "io.github.prospector:modmenu:${project.modmenu_version}"
 }
 
 processResources {

+ 8 - 6
gradle.properties

@@ -3,16 +3,18 @@ org.gradle.jvmargs=-Xmx1G
 
 # Fabric Properties
 	# check these on https://fabricmc.net/use
-	minecraft_version=20w19a
-	yarn_mappings=20w19a+build.6
-	loader_version=0.8.2+build.194
+	minecraft_version=1.16.1
+	yarn_mappings=1.16.1+build.21
+	loader_version=0.9.0+build.204
 
 # Mod Properties
-	mod_version = 1.1.6
+	mod_version = 1.1.9
 	maven_group = com.umollu
 	archives_base_name = ash
 
 # Dependencies
-	# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric
-	fabric_version=0.10.7+build.344-1.16
+	fabric_version=0.16.2+build.385-1.16.1
 	clientcommands_version=1.0.0+1.15.2
+	autoconfig_version=3.2.0-unstable
+	clothconfig_version=4.6.0
+	modmenu_version=1.14.5+build.30

+ 14 - 52
src/main/java/com/umollu/ash/AshCommands.java

@@ -1,86 +1,48 @@
 package com.umollu.ash;
 
-import com.google.gson.Gson;
 import com.mojang.brigadier.CommandDispatcher;
 import com.mojang.brigadier.arguments.IntegerArgumentType;
+import com.umollu.ash.config.AshConfig;
 import io.github.cottonmc.clientcommands.ArgumentBuilders;
 import io.github.cottonmc.clientcommands.ClientCommandPlugin;
 import io.github.cottonmc.clientcommands.CottonClientCommandSource;
-import net.fabricmc.loader.api.FabricLoader;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.IOException;
-
-import static com.umollu.ash.AshMod.MOD_ID;
+import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
 
 public class AshCommands implements ClientCommandPlugin {
 
     public static AshConfig config;
 
     @Override
-    public void registerCommands(CommandDispatcher<CottonClientCommandSource> commandDispatcher) {
-
-        String configPath = FabricLoader.getInstance().getConfigDirectory() + "/" + MOD_ID + ".json";
-
-        Gson gson = new Gson();
-
-        File configFile = new File(configPath);
-
-        if(!configFile.exists()) {
-            config = new AshConfig();
-            String result = gson.toJson(config);
-            try {
-                FileOutputStream out = new FileOutputStream(configFile, false);
+        public void registerCommands(CommandDispatcher<CottonClientCommandSource> commandDispatcher) {
 
-                out.write(result.getBytes());
-                out.flush();
-                out.close();
-
-            } catch (IOException ex) {
-                ex.printStackTrace();
-            }
-        }
-        else {
-
-            try {
-                config = gson.fromJson( new FileReader(configFile), AshConfig.class);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-            finally {
-                config = (config == null? new AshConfig() : config);
-            }
+        if(config == null) {
+            config = AutoConfig.getConfigHolder(AshConfig.class).getConfig();
         }
-
-
         commandDispatcher.register(ArgumentBuilders.literal("toggleash")
             .executes(context -> {
                 config.showHud = !config.showHud;
-                config.saveConfig();
+                AshMod.configManager.save();
                 return 1;
             }));
 
         commandDispatcher.register(ArgumentBuilders.literal("togglefps")
             .executes(context -> {
                 config.showFps = !config.showFps;
-                config.saveConfig();
+                AshMod.configManager.save();
                 return 1;
             }));
 
-
         commandDispatcher.register(ArgumentBuilders.literal("togglecoords")
             .executes(context -> {
                 config.showCoords = !config.showCoords;
-                config.saveConfig();
+                AshMod.configManager.save();
                 return 1;
             }));
 
         commandDispatcher.register(ArgumentBuilders.literal("toggledirection")
             .executes(context -> {
                 config.showDirection = !config.showDirection;
-                config.saveConfig();
+                AshMod.configManager.save();
                 return 1;
             }));
 
@@ -94,14 +56,14 @@ public class AshCommands implements ClientCommandPlugin {
                                         int b = IntegerArgumentType.getInteger(context,"b");
 
                                         config.hudColor = b + (g << 8) + (r << 16);
-                                        config.saveConfig();
+                                        AshMod.configManager.save();
                                         return 1;
                                     })))));
 
         commandDispatcher.register(ArgumentBuilders.literal("resetash")
             .executes(context -> {
                 config = new AshConfig();
-                config.saveConfig();
+                AshMod.configManager.save();
                 return 1;
             }));
 
@@ -109,19 +71,19 @@ public class AshCommands implements ClientCommandPlugin {
             .then(ArgumentBuilders.literal("left")
                     .executes(context -> {
                         config.align = 0;
-                        config.saveConfig();
+                        AshMod.configManager.save();
                         return 1;
                     }))
             .then(ArgumentBuilders.literal("center")
                     .executes(context -> {
                         config.align = 1;
-                        config.saveConfig();
+                        AshMod.configManager.save();
                         return 1;
                     }))
             .then(ArgumentBuilders.literal("right")
                     .executes(context -> {
                         config.align = 2;
-                        config.saveConfig();
+                        AshMod.configManager.save();
                         return 1;
                     })));
     }

+ 0 - 39
src/main/java/com/umollu/ash/AshConfig.java

@@ -1,39 +0,0 @@
-package com.umollu.ash;
-
-import com.google.gson.Gson;
-import net.fabricmc.loader.api.FabricLoader;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-public class AshConfig {
-
-    public boolean showHud = true;
-
-    public int hudColor = 0xeeeeee;
-
-    public boolean showFps = true;
-
-    public boolean showCoords = true;
-
-    public boolean showDirection = true;
-
-    public int align = 0;
-
-    public void saveConfig() {
-        String configPath = FabricLoader.getInstance().getConfigDirectory() + "/" + AshMod.MOD_ID + ".json";
-        File configFile = new File(configPath);
-        String result = new Gson().toJson(this);
-        try {
-            FileOutputStream out = new FileOutputStream(configFile, false);
-
-            out.write(result.getBytes());
-            out.flush();
-            out.close();
-
-        } catch (IOException ex) {
-            ex.printStackTrace();
-        }
-    }
-}

+ 52 - 0
src/main/java/com/umollu/ash/AshMod.java

@@ -1,12 +1,64 @@
 package com.umollu.ash;
 
+import com.umollu.ash.config.AshConfig;
+import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
+import me.sargunvohra.mcmods.autoconfig1u.ConfigManager;
+import me.sargunvohra.mcmods.autoconfig1u.gui.registry.GuiRegistry;
+import me.sargunvohra.mcmods.autoconfig1u.serializer.GsonConfigSerializer;
+import me.sargunvohra.mcmods.autoconfig1u.util.Utils;
+import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
 import net.fabricmc.api.ClientModInitializer;
+import net.minecraft.text.LiteralText;
+import net.minecraft.text.TranslatableText;
+
+import java.util.Collections;
 
 public class AshMod implements ClientModInitializer {
 
     public static final String MOD_ID = "umollu_ash";
+    public static ConfigManager configManager;
 
     @Override
     public void onInitializeClient() {
+        configManager = (ConfigManager) AutoConfig.register(AshConfig.class, GsonConfigSerializer::new);
+
+        GuiRegistry registry = AutoConfig.getGuiRegistry(AshConfig.class);
+        registry.registerPredicateProvider((i13n, field, config, defaults, guiProvider) -> {
+
+            ConfigEntryBuilder ENTRY_BUILDER = ConfigEntryBuilder.create();
+
+            String[] ints = new String[3];
+
+            ints[0] = "Left";
+            ints[1] = "Center";
+            ints[2] = "Right";
+
+            return Collections.singletonList(ENTRY_BUILDER.startSelector(new TranslatableText(i13n), ints, alignToString((Integer) Utils.getUnsafely(field, config, (Object)null))).setDefaultValue(() -> {
+                return alignToString(Utils.getUnsafely(field, defaults));
+            }).setSaveConsumer((newValue) -> {
+                int intValue = 0;
+                if(newValue.equals("Center"))
+                    intValue = 1;
+                else if(newValue.equals("Right"))
+                    intValue = 2;
+
+                Utils.setUnsafely(field, config, intValue);
+            }).build());
+        }, (field) -> {
+            return field.getName().equals("align");
+        });
+    }
+
+    private String alignToString(int align) {
+        switch (align) {
+            case 0:
+                return "Left";
+            case 1:
+                return "Center";
+            case 2:
+                return "Right";
+            default:
+                return "";
+        }
     }
 }

+ 22 - 0
src/main/java/com/umollu/ash/config/AshConfig.java

@@ -0,0 +1,22 @@
+package com.umollu.ash.config;
+
+import com.umollu.ash.AshMod;
+import me.sargunvohra.mcmods.autoconfig1u.ConfigData;
+import me.sargunvohra.mcmods.autoconfig1u.annotation.Config;
+import me.sargunvohra.mcmods.autoconfig1u.annotation.ConfigEntry;
+
+@Config(name = AshMod.MOD_ID)
+public class AshConfig implements ConfigData {
+    public boolean showHud = true;
+
+    @ConfigEntry.ColorPicker
+    public int hudColor = 0xeeeeee;
+
+    public boolean showFps = true;
+
+    public boolean showCoords = true;
+
+    public boolean showDirection = true;
+
+    public int align = 0;
+}

+ 19 - 0
src/main/java/com/umollu/ash/config/AshMenuIntegration.java

@@ -0,0 +1,19 @@
+package com.umollu.ash.config;
+
+import com.umollu.ash.AshMod;
+import io.github.prospector.modmenu.api.ConfigScreenFactory;
+import io.github.prospector.modmenu.api.ModMenuApi;
+import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
+
+public class AshMenuIntegration implements ModMenuApi {
+
+    @Override
+    public String getModId() {
+        return AshMod.MOD_ID;
+    }
+
+    @Override
+    public ConfigScreenFactory<?> getModConfigScreenFactory() {
+        return parent -> AutoConfig.getConfigScreen(AshConfig.class, parent).get();
+    }
+}

+ 9 - 0
src/main/resources/assets/umollu_ash/lang/en_us.json

@@ -0,0 +1,9 @@
+{
+  "text.autoconfig.umollu_ash.title" : "ASH Config",
+  "text.autoconfig.umollu_ash.option.showHud" : "Show HUD",
+  "text.autoconfig.umollu_ash.option.hudColor" : "HUD text color",
+  "text.autoconfig.umollu_ash.option.showFps" : "Show FPS",
+  "text.autoconfig.umollu_ash.option.showCoords" : "Show coordinates",
+  "text.autoconfig.umollu_ash.option.showDirection" : "Show direction",
+  "text.autoconfig.umollu_ash.option.align" : "HUD screen alignment"
+}

+ 3 - 0
src/main/resources/fabric.mod.json

@@ -9,6 +9,9 @@
     "client": [
       "com.umollu.ash.AshMod"
     ],
+    "modmenu": [
+      "com.umollu.ash.config.AshMenuIntegration"
+    ],
     "cotton-client-commands": ["com.umollu.ash.AshCommands"]
   },
   "depends": {