From ca997b8341ff542ecaca5548ac69a436d81aff7c Mon Sep 17 00:00:00 2001 From: coolGi2007 Date: Mon, 13 Dec 2021 12:04:27 +0000 Subject: [PATCH] Updated config --- .../java/com/seibel/lod/common/Config.java | 2 + .../lod/common/wrappers/config/ConfigGui.java | 71 ++++++++++--------- .../main/resources/assets/lod/lang/en_us.json | 1 + .../lod/fabric/mixins/MixinOptionsScreen.java | 4 +- .../wrappers/config/ModMenuIntegration.java | 8 --- .../lod/forge/mixins/MixinOptionsScreen.java | 4 +- 6 files changed, 48 insertions(+), 42 deletions(-) diff --git a/common/src/main/java/com/seibel/lod/common/Config.java b/common/src/main/java/com/seibel/lod/common/Config.java index e60430260..12304169d 100644 --- a/common/src/main/java/com/seibel/lod/common/Config.java +++ b/common/src/main/java/com/seibel/lod/common/Config.java @@ -54,6 +54,8 @@ public class Config extends ConfigGui @ScreenEntry(to = "client") public static Client client; + @Entry + public static boolean ShowButton = true; public static class Client { diff --git a/common/src/main/java/com/seibel/lod/common/wrappers/config/ConfigGui.java b/common/src/main/java/com/seibel/lod/common/wrappers/config/ConfigGui.java index fc4babc49..e770d424c 100644 --- a/common/src/main/java/com/seibel/lod/common/wrappers/config/ConfigGui.java +++ b/common/src/main/java/com/seibel/lod/common/wrappers/config/ConfigGui.java @@ -1,8 +1,8 @@ package com.seibel.lod.common.wrappers.config; -import com.google.gson.ExclusionStrategy; -import com.google.gson.FieldAttributes; +// Uses https://github.com/mwanji/toml4j for toml import com.moandjiezana.toml.Toml; +// TomlWriter is threadsave while Writer is not import com.moandjiezana.toml.TomlWriter; import com.mojang.blaze3d.vertex.PoseStack; import com.seibel.lod.core.ModInfo; @@ -142,30 +142,17 @@ public abstract class ConfigGui { String category; } - public static final Map> configClass = new HashMap<>(); + public static final Map> configClass = new HashMap<>(); +// public static List nestedClasses = new ArrayList<>(); private static Path path; public static void init(String modid, Class config) { path = Minecraft.getInstance().gameDirectory.toPath().resolve("config").resolve(modid + ".toml"); - configClass.put(modid, config); - for (Field field : config.getFields()) { - EntryInfo info = new EntryInfo(); - if (field.isAnnotationPresent(Entry.class) || field.isAnnotationPresent(Comment.class) || field.isAnnotationPresent(ScreenEntry.class)) - // TODO[CONFIG]: Fix the check for client/server -// if (Minecraft.getInstance().getEnvironmentType() == EnvType.CLIENT) - initClient(modid, field, info); - if (field.isAnnotationPresent(Entry.class)) - try { - info.defaultValue = field.get(null); - } catch (IllegalAccessException ignored) {} - if (field.isAnnotationPresent(ScreenEntry.class)) { - ConfigGui.init(modid, field.getType()); - } + // Goes through all the nested classes and normal classes and inits them + initClass(modid, config, ""); - } - - // File saving stuff + // Save and read the file try { new Toml().read(Files.newBufferedReader(path)).to(config); } catch (Exception e) { @@ -181,8 +168,31 @@ public abstract class ConfigGui { } } } + private static void initClass(String modid, Class config, String category) { + String e = modid + (category != "" ? "." + category : ""); + configClass.put(e, config); +// nestedClasses.add(e); + for (Field field : config.getFields()) { + EntryInfo info = new EntryInfo(); + if (field.isAnnotationPresent(Entry.class) || field.isAnnotationPresent(Comment.class) || field.isAnnotationPresent(ScreenEntry.class)) + // TODO[CONFIG]: Fix the check for client/server +// if (Minecraft.getInstance().getEnvironmentType() == EnvType.CLIENT) + initClient(modid, field, info); + if (field.isAnnotationPresent(Entry.class)) + try { + info.defaultValue = field.get(null); + } catch (IllegalAccessException ignored) {} + if (field.isAnnotationPresent(ScreenEntry.class)) { + String c = field.getAnnotation(Category.class) != null ? field.getAnnotation(Category.class).value() : ""; + initClass(modid, field.getType(), + (c != "" ? c + "." : "") + + field.getAnnotation(ScreenEntry.class).to()); + } + } + } private static void initClient(String modid, Field field, EntryInfo info) { // This adds the buttons to the queue to be rendered + // DONT CALL ON SERVER AS SERVERS CANT RENDER STUFF Class type = field.getType(); Category c = field.getAnnotation(Category.class); Entry e = field.getAnnotation(Entry.class); @@ -268,6 +278,7 @@ public abstract class ConfigGui { try { if (!Files.exists(path)) Files.createFile(path); + new TomlWriter().write(configClass.get(modid).getDeclaredConstructor().newInstance(), path.toFile()); } catch (Exception e) { e.printStackTrace(); @@ -301,11 +312,14 @@ public abstract class ConfigGui { } } private void loadValues() { - try { - new Toml().read(Files.newBufferedReader(path)).to(configClass.get(modid)); - } catch (Exception e) { - write(modid); - } +// for (int i = 0; i < nestedClasses.size(); i++) { + try { +// new Toml().read(Files.newBufferedReader(path)).to(configClass.get(nestedClasses.get(i))); + new Toml().read(Files.newBufferedReader(path)).to(configClass.get(modid)); + } catch (Exception e) { + write(modid); + } +// } for (EntryInfo info : entries) { if (info.field.isAnnotationPresent(Entry.class)) @@ -528,11 +542,4 @@ public abstract class ConfigGui { @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Comment {} - - public static class HiddenAnnotationExclusionStrategy implements ExclusionStrategy { - public boolean shouldSkipClass(Class clazz) { return false; } - public boolean shouldSkipField(FieldAttributes fieldAttributes) { - return fieldAttributes.getAnnotation(Entry.class) == null; - } - } } diff --git a/common/src/main/resources/assets/lod/lang/en_us.json b/common/src/main/resources/assets/lod/lang/en_us.json index e4957b5eb..3aad790a6 100644 --- a/common/src/main/resources/assets/lod/lang/en_us.json +++ b/common/src/main/resources/assets/lod/lang/en_us.json @@ -1,6 +1,7 @@ { "lod.title": "Distant Horizons", "lod.config.title": "Distant Horizons config", + "lod.config.ShowButton": "Show menu button", "lod.config.client": "Client", "lod.config.client.graphics": "Graphics", "lod.config.client.graphics.quality": "Quality options", diff --git a/fabric/src/main/java/com/seibel/lod/fabric/mixins/MixinOptionsScreen.java b/fabric/src/main/java/com/seibel/lod/fabric/mixins/MixinOptionsScreen.java index 6e6b4dbb5..dc525343d 100644 --- a/fabric/src/main/java/com/seibel/lod/fabric/mixins/MixinOptionsScreen.java +++ b/fabric/src/main/java/com/seibel/lod/fabric/mixins/MixinOptionsScreen.java @@ -1,5 +1,6 @@ package com.seibel.lod.fabric.mixins; +import com.seibel.lod.common.Config; import com.seibel.lod.common.wrappers.config.ConfigGui; import com.seibel.lod.common.wrappers.config.TexturedButtonWidget; import com.seibel.lod.core.ModInfo; @@ -31,7 +32,8 @@ public class MixinOptionsScreen extends Screen { @Inject(at = @At("HEAD"),method = "init") private void lodconfig$init(CallbackInfo ci) { - this.addRenderableWidget(new TexturedButtonWidget( + if (Config.ShowButton) + this.addRenderableWidget(new TexturedButtonWidget( // Where the button is on the screen this.width / 2 - 180, this.height / 6 - 12, // Width and height of the button diff --git a/fabric/src/main/java/com/seibel/lod/fabric/wrappers/config/ModMenuIntegration.java b/fabric/src/main/java/com/seibel/lod/fabric/wrappers/config/ModMenuIntegration.java index 410be02c2..95fb32fdb 100644 --- a/fabric/src/main/java/com/seibel/lod/fabric/wrappers/config/ModMenuIntegration.java +++ b/fabric/src/main/java/com/seibel/lod/fabric/wrappers/config/ModMenuIntegration.java @@ -21,12 +21,4 @@ public class ModMenuIntegration implements ModMenuApi { public ConfigScreenFactory getModConfigScreenFactory() { return parent -> Config.getScreen(parent, ModInfo.ID, ""); } - - @Override - public Map> getProvidedConfigScreenFactories() { - HashMap> map = new HashMap<>(); - // For now it goes to the client option by default - Config.configClass.forEach((modid, cClass) -> map.put(modid, parent -> ConfigGui.getScreen(parent, modid, "client"))); - return map; - } } diff --git a/forge/src/main/java/com/seibel/lod/forge/mixins/MixinOptionsScreen.java b/forge/src/main/java/com/seibel/lod/forge/mixins/MixinOptionsScreen.java index cbb77819f..de621b650 100644 --- a/forge/src/main/java/com/seibel/lod/forge/mixins/MixinOptionsScreen.java +++ b/forge/src/main/java/com/seibel/lod/forge/mixins/MixinOptionsScreen.java @@ -1,5 +1,6 @@ package com.seibel.lod.forge.mixins; +import com.seibel.lod.common.Config; import com.seibel.lod.common.wrappers.config.ConfigGui; import com.seibel.lod.common.wrappers.config.TexturedButtonWidget; import com.seibel.lod.core.ModInfo; @@ -31,7 +32,8 @@ public class MixinOptionsScreen extends Screen { @Inject(at = @At("HEAD"),method = "init") private void lodconfig$init(CallbackInfo ci) { - this.addRenderableWidget(new TexturedButtonWidget( + if (Config.ShowButton) + this.addRenderableWidget(new TexturedButtonWidget( // Where the button is on the screen this.width / 2 - 180, this.height / 6 - 12, // Width and height of the button