Updated config

This commit is contained in:
coolGi2007
2021-12-13 12:11:39 +00:00
parent 66300cbe70
commit 30d4bcd6a1
6 changed files with 46 additions and 41 deletions
@@ -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
{
@@ -1,11 +1,10 @@
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;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
@@ -142,30 +141,17 @@ public abstract class ConfigGui {
String category;
}
public static final Map<String,Class<?>> configClass = new HashMap<>();
public static final Map<String, Class<?>> configClass = new HashMap<>();
// public static List<String> 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 +167,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 +277,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 +311,14 @@ public abstract class ConfigGui {
}
}
private void loadValues() {
// 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) {
} catch (Exception e) {
write(modid);
}
// }
for (EntryInfo info : entries) {
if (info.field.isAnnotationPresent(Entry.class))
@@ -387,7 +400,7 @@ public abstract class ConfigGui {
this.list.addButton(widget, resetButton, null, name);
} else if (info.button) {
Button widget = new Button(this.width / 2 - info.width, this.height - 28, info.width*2, 20, name, (button -> {
Objects.requireNonNull(minecraft).setScreen(ConfigGui.getScreen(this, ModInfo.ID, info.gotoScreen));
Objects.requireNonNull(minecraft).setScreen(ConfigGui.getScreen(this, modid, info.gotoScreen));
}));
this.list.addButton(widget, null, null, null);
} else {
@@ -437,7 +450,7 @@ public abstract class ConfigGui {
this.centerListVertically = false;
textRenderer = minecraftClient.font;
}
// @Override
// @Override
public int getScrollbarPositionX() { return this.width -7; }
public void addButton(AbstractWidget button, AbstractWidget resetButton, AbstractWidget indexButton, Component text) {
@@ -528,11 +541,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;
}
}
}
@@ -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",