Merge meta.json and conf.json, split saved configs into separate files, simplify a few things
This commit is contained in:
parent
f63a1fa42f
commit
4b8c18aab2
|
@ -8,10 +8,12 @@ import me.shedaniel.clothconfig2.api.ConfigCategory;
|
||||||
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
|
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
|
||||||
import net.minecraft.client.gui.screen.FatalErrorScreen;
|
import net.minecraft.client.gui.screen.FatalErrorScreen;
|
||||||
import net.minecraft.client.gui.screen.Screen;
|
import net.minecraft.client.gui.screen.Screen;
|
||||||
import net.minecraft.text.TranslatableText;
|
import net.minecraft.text.*;
|
||||||
|
import net.minecraft.util.Language;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class GuiFactory {
|
public class GuiFactory {
|
||||||
public void buildCategory(JsonObject source, String screenId, ConfigCategory config, ConfigEntryBuilder entryBuilder) {
|
public void buildCategory(JsonObject source, String screenId, ConfigCategory config, ConfigEntryBuilder entryBuilder) {
|
||||||
|
@ -21,8 +23,13 @@ public class GuiFactory {
|
||||||
Respackopts.numVals.put(screenId, new HashMap<>());
|
Respackopts.numVals.put(screenId, new HashMap<>());
|
||||||
if (!Respackopts.strVals.containsKey(screenId))
|
if (!Respackopts.strVals.containsKey(screenId))
|
||||||
Respackopts.strVals.put(screenId, new HashMap<>());
|
Respackopts.strVals.put(screenId, new HashMap<>());
|
||||||
|
String b = "respackopts.field." + screenId;
|
||||||
for (Map.Entry<String, JsonElement> entry : source.entrySet()) {
|
for (Map.Entry<String, JsonElement> entry : source.entrySet()) {
|
||||||
String n = entry.getKey();
|
String n = entry.getKey();
|
||||||
|
if (n.contains(":") || n.contains(".")) {
|
||||||
|
System.err.println(n + " contains invalid characters");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
JsonElement e = entry.getValue();
|
JsonElement e = entry.getValue();
|
||||||
if (e.isJsonPrimitive()) {
|
if (e.isJsonPrimitive()) {
|
||||||
JsonPrimitive p = e.getAsJsonPrimitive();
|
JsonPrimitive p = e.getAsJsonPrimitive();
|
||||||
|
@ -33,7 +40,7 @@ public class GuiFactory {
|
||||||
currentValue = Respackopts.boolVals.get(screenId).get(n);
|
currentValue = Respackopts.boolVals.get(screenId).get(n);
|
||||||
else
|
else
|
||||||
Respackopts.boolVals.get(screenId).put(n, defaultValue);
|
Respackopts.boolVals.get(screenId).put(n, defaultValue);
|
||||||
config.addEntry(entryBuilder.startBooleanToggle(new TranslatableText(n), currentValue)
|
config.addEntry(entryBuilder.startBooleanToggle(getText(n, b), currentValue)
|
||||||
.setDefaultValue(defaultValue)
|
.setDefaultValue(defaultValue)
|
||||||
.setSaveConsumer(v -> Respackopts.boolVals.get(screenId).put(n, v))
|
.setSaveConsumer(v -> Respackopts.boolVals.get(screenId).put(n, v))
|
||||||
.build());
|
.build());
|
||||||
|
@ -45,7 +52,7 @@ public class GuiFactory {
|
||||||
currentValue = Respackopts.numVals.get(screenId).get(n);
|
currentValue = Respackopts.numVals.get(screenId).get(n);
|
||||||
else
|
else
|
||||||
Respackopts.numVals.get(screenId).put(n, defaultValue);
|
Respackopts.numVals.get(screenId).put(n, defaultValue);
|
||||||
config.addEntry(entryBuilder.startDoubleField(new TranslatableText(n), currentValue)
|
config.addEntry(entryBuilder.startDoubleField(getText(n, b), currentValue)
|
||||||
.setDefaultValue(defaultValue)
|
.setDefaultValue(defaultValue)
|
||||||
.setSaveConsumer(v -> Respackopts.numVals.get(screenId).put(n, v))
|
.setSaveConsumer(v -> Respackopts.numVals.get(screenId).put(n, v))
|
||||||
.build());
|
.build());
|
||||||
|
@ -57,7 +64,7 @@ public class GuiFactory {
|
||||||
currentValue = Respackopts.strVals.get(screenId).get(n);
|
currentValue = Respackopts.strVals.get(screenId).get(n);
|
||||||
else
|
else
|
||||||
Respackopts.strVals.get(screenId).put(n, defaultValue);
|
Respackopts.strVals.get(screenId).put(n, defaultValue);
|
||||||
config.addEntry(entryBuilder.startStrField(new TranslatableText(n), currentValue)
|
config.addEntry(entryBuilder.startStrField(getText(n, b), currentValue)
|
||||||
.setDefaultValue(defaultValue)
|
.setDefaultValue(defaultValue)
|
||||||
.setSaveConsumer(v -> Respackopts.strVals.get(screenId).put(n, v))
|
.setSaveConsumer(v -> Respackopts.strVals.get(screenId).put(n, v))
|
||||||
.build());
|
.build());
|
||||||
|
@ -74,10 +81,10 @@ public class GuiFactory {
|
||||||
ConfigBuilder builder;
|
ConfigBuilder builder;
|
||||||
builder = ConfigBuilder.create()
|
builder = ConfigBuilder.create()
|
||||||
.setParentScreen(parent)
|
.setParentScreen(parent)
|
||||||
.setTitle(new TranslatableText("respackopts.title." + resourcepackid));
|
.setTitle(getText(resourcepackid, "respackopts.title"));
|
||||||
ConfigEntryBuilder entryBuilder = builder.entryBuilder();
|
ConfigEntryBuilder entryBuilder = builder.entryBuilder();
|
||||||
builder.setSavingRunnable(respackReload);
|
builder.setSavingRunnable(respackReload);
|
||||||
ConfigCategory config = builder.getOrCreateCategory(new TranslatableText("respackopts.category." + resourcepackid));
|
ConfigCategory config = builder.getOrCreateCategory(getText(resourcepackid, "respackopts.category"));
|
||||||
buildCategory(source, resourcepackid, config, entryBuilder);
|
buildCategory(source, resourcepackid, config, entryBuilder);
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
@ -86,4 +93,10 @@ public class GuiFactory {
|
||||||
return new FatalErrorScreen(new TranslatableText("respackopts.loadFailed"), new TranslatableText("respackopts.loadError"));
|
return new FatalErrorScreen(new TranslatableText("respackopts.loadFailed"), new TranslatableText("respackopts.loadError"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Text getText(String key, String idPrefix) {
|
||||||
|
String k = idPrefix + "." + key;
|
||||||
|
if (Language.getInstance().hasTranslation(k)) return new TranslatableText(k);
|
||||||
|
else return new LiteralText(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class LibCDCompat implements ConditionInitializer {
|
||||||
AtomicBoolean found = new AtomicBoolean(false);
|
AtomicBoolean found = new AtomicBoolean(false);
|
||||||
AtomicBoolean output = new AtomicBoolean(false);
|
AtomicBoolean output = new AtomicBoolean(false);
|
||||||
Respackopts.resPackMetas.forEach((r, v) -> {
|
Respackopts.resPackMetas.forEach((r, v) -> {
|
||||||
if (Objects.equals(v.meta.id, s.split(":")[0])) {
|
if (Objects.equals(v.id, s.split(":")[0])) {
|
||||||
found.set(true);
|
found.set(true);
|
||||||
String name = s.substring(s.indexOf(':') + 1);
|
String name = s.substring(s.indexOf(':') + 1);
|
||||||
output.set(v.conf.get(name).getAsBoolean());
|
output.set(v.conf.get(name).getAsBoolean());
|
||||||
|
|
|
@ -23,8 +23,8 @@ public class MMI implements ModMenuApi {
|
||||||
Respackopts.save();
|
Respackopts.save();
|
||||||
});
|
});
|
||||||
Respackopts.resPackMetas.forEach((s, v) -> {
|
Respackopts.resPackMetas.forEach((s, v) -> {
|
||||||
ConfigCategory config = builder.getOrCreateCategory(new TranslatableText("respackopts.category." + v.meta.id));
|
ConfigCategory config = builder.getOrCreateCategory(new TranslatableText("respackopts.category." + v.id));
|
||||||
Respackopts.factory.buildCategory(v.conf, v.meta.id, config, entryBuilder);
|
Respackopts.factory.buildCategory(v.conf, v.id, config, entryBuilder);
|
||||||
});
|
});
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import net.fabricmc.api.ClientModInitializer;
|
||||||
import net.fabricmc.api.EnvType;
|
import net.fabricmc.api.EnvType;
|
||||||
import net.fabricmc.api.Environment;
|
import net.fabricmc.api.Environment;
|
||||||
import net.fabricmc.loader.api.FabricLoader;
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
import org.lwjgl.stb.STBRPNode;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
@ -24,18 +23,68 @@ public class Respackopts implements ClientModInitializer {
|
||||||
public static final Integer metaVersion = 1;
|
public static final Integer metaVersion = 1;
|
||||||
public static HashMap<String, Respackmeta> resPackMetas = new HashMap<>();
|
public static HashMap<String, Respackmeta> resPackMetas = new HashMap<>();
|
||||||
public static final String ID = "respackopts";
|
public static final String ID = "respackopts";
|
||||||
static final Path p = FabricLoader.getInstance().getConfigDir().resolve("respackopts.json");
|
static final Path p = FabricLoader.getInstance().getConfigDir().resolve("respackopts");
|
||||||
@Override
|
@Override
|
||||||
public void onInitializeClient() {
|
public void onInitializeClient() {
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void save() {
|
||||||
|
deNull();
|
||||||
try {
|
try {
|
||||||
Reader reader = Files.newBufferedReader(p);
|
Files.createDirectories(p);
|
||||||
Config c = g.fromJson(reader, Config.class);
|
|
||||||
boolVals = c.boolVals;
|
|
||||||
numVals = c.numVals;
|
|
||||||
strVals = c.strVals;
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
for (String s : resPackMetas.keySet()) {
|
||||||
|
s = resPackMetas.get(s).id;
|
||||||
|
Config cfg = new Config();
|
||||||
|
if (boolVals.containsKey(s))
|
||||||
|
cfg.bools = boolVals.get(s);
|
||||||
|
if (numVals.containsKey(s))
|
||||||
|
cfg.doubles = numVals.get(s);
|
||||||
|
if (strVals.containsKey(s))
|
||||||
|
cfg.strings = strVals.get(s);
|
||||||
|
Path q = p.resolve(s + ".json");
|
||||||
|
try {
|
||||||
|
Writer writer = Files.newBufferedWriter(q);
|
||||||
|
g.toJson(cfg, writer);
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void load() {
|
||||||
|
try {
|
||||||
|
for (String s : resPackMetas.keySet()) {
|
||||||
|
s = resPackMetas.get(s).id;
|
||||||
|
Path q = p.resolve(s + ".json");
|
||||||
|
if (Files.exists(q)) {
|
||||||
|
try {
|
||||||
|
Reader reader = Files.newBufferedReader(q);
|
||||||
|
Config c = g.fromJson(reader, Config.class);
|
||||||
|
reader.close();
|
||||||
|
if (c.bools != null)
|
||||||
|
boolVals.put(s, c.bools);
|
||||||
|
if (c.doubles != null)
|
||||||
|
numVals.put(s, c.doubles);
|
||||||
|
if (c.strings != null)
|
||||||
|
strVals.put(s, c.strings);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
deNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void deNull() {
|
||||||
if (boolVals == null)
|
if (boolVals == null)
|
||||||
boolVals = new HashMap<>();
|
boolVals = new HashMap<>();
|
||||||
if (numVals == null)
|
if (numVals == null)
|
||||||
|
@ -43,18 +92,4 @@ public class Respackopts implements ClientModInitializer {
|
||||||
if (strVals == null)
|
if (strVals == null)
|
||||||
strVals = new HashMap<>();
|
strVals = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void save() {
|
|
||||||
System.out.println("Yay");
|
|
||||||
Config c = new Config();
|
|
||||||
c.boolVals = boolVals;
|
|
||||||
c.numVals = numVals;
|
|
||||||
c.strVals = strVals;
|
|
||||||
try {
|
|
||||||
Writer writer = Files.newBufferedWriter(p);
|
|
||||||
g.toJson(c, writer);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package io.gitlab.jfronny.respackopts.data;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
public HashMap<String, HashMap<String, Boolean>> boolVals;
|
public HashMap<String, Boolean> bools;
|
||||||
public HashMap<String, HashMap<String, Double>> numVals;
|
public HashMap<String, Double> doubles;
|
||||||
public HashMap<String, HashMap<String, String>> strVals;
|
public HashMap<String, String> strings;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,16 +3,7 @@ package io.gitlab.jfronny.respackopts.data;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
public class Respackmeta {
|
public class Respackmeta {
|
||||||
public JsonMeta meta;
|
|
||||||
public JsonObject conf;
|
public JsonObject conf;
|
||||||
|
public String id;
|
||||||
public Respackmeta(JsonMeta meta, JsonObject conf) {
|
public Integer version;
|
||||||
this.meta = meta;
|
|
||||||
this.conf = conf;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class JsonMeta {
|
|
||||||
public String id;
|
|
||||||
public Integer version;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ public abstract class ResourcePackEntryMixin extends AlwaysSelectedEntryListWidg
|
||||||
if (Respackopts.resPackMetas.containsKey(k) && respackopts$selected) {
|
if (Respackopts.resPackMetas.containsKey(k) && respackopts$selected) {
|
||||||
Respackmeta meta = Respackopts.resPackMetas.get(k);
|
Respackmeta meta = Respackopts.resPackMetas.get(k);
|
||||||
MinecraftClient c = MinecraftClient.getInstance();
|
MinecraftClient c = MinecraftClient.getInstance();
|
||||||
c.openScreen(Respackopts.factory.buildGui(meta.conf, meta.meta.id, c.currentScreen, () -> {
|
c.openScreen(Respackopts.factory.buildGui(meta.conf, meta.id, c.currentScreen, () -> {
|
||||||
Respackopts.save();
|
Respackopts.save();
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,17 +27,22 @@ public class ResourcePackManagerMixin {
|
||||||
@Inject(at = @At("TAIL"), method = "scanPacks()V")
|
@Inject(at = @At("TAIL"), method = "scanPacks()V")
|
||||||
private void scanPacks(CallbackInfo info) {
|
private void scanPacks(CallbackInfo info) {
|
||||||
profiles.forEach((s, v) -> {
|
profiles.forEach((s, v) -> {
|
||||||
if (hasMetadata(v, "meta.json") && hasMetadata(v, "conf.json")) {
|
if (hasMetadata(v, "conf.json")) {
|
||||||
try {
|
try {
|
||||||
Respackmeta.JsonMeta meta = Respackopts.g.fromJson(readMetadata(v, "meta.json", Respackopts.g), Respackmeta.JsonMeta.class);
|
Respackmeta conf = Respackopts.g.fromJson(readMetadata(v, "conf.json", Respackopts.g), Respackmeta.class);
|
||||||
if (Respackopts.metaVersion.equals(meta.version)) {
|
if (Respackopts.metaVersion.equals(conf.version)) {
|
||||||
Respackopts.resPackMetas.put(v.getDisplayName().asString(), new Respackmeta(meta, readMetadata(v, "conf.json", Respackopts.g)));
|
Respackopts.resPackMetas.put(v.getDisplayName().asString(), conf);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.err.println(s + " was not loaded as it specifies a different respackopts version than is installed");
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Respackopts.load();
|
||||||
|
Respackopts.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasMetadata(ResourcePackProfile v, String fname) {
|
private boolean hasMetadata(ResourcePackProfile v, String fname) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user