package io.gitlab.jfronny.respackopts; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import io.gitlab.jfronny.respackopts.abstractions.JfConfigCategory; import io.gitlab.jfronny.respackopts.abstractions.JfConfigCategoryPrimary; import io.gitlab.jfronny.respackopts.abstractions.JfConfigCategorySub; import me.shedaniel.clothconfig2.api.ConfigBuilder; import me.shedaniel.clothconfig2.api.ConfigCategory; import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry; import me.shedaniel.clothconfig2.impl.builders.DropdownMenuBuilder; import me.shedaniel.clothconfig2.impl.builders.SubCategoryBuilder; import net.minecraft.client.gui.screen.FatalErrorScreen; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.*; import net.minecraft.util.Language; import java.util.HashMap; import java.util.Map; import java.util.TreeSet; public class GuiFactory { public void buildCategory(JsonObject source, String screenId, JfConfigCategory config, ConfigEntryBuilder entryBuilder, String namePrefix) { if (!Respackopts.boolVals.containsKey(screenId)) Respackopts.boolVals.put(screenId, new HashMap<>()); if (!Respackopts.numVals.containsKey(screenId)) Respackopts.numVals.put(screenId, new HashMap<>()); if (!Respackopts.strVals.containsKey(screenId)) Respackopts.strVals.put(screenId, new HashMap<>()); if (!Respackopts.enumKeys.containsKey(screenId)) Respackopts.enumKeys.put(screenId, new HashMap<>()); String b = "respackopts.field." + screenId; for (Map.Entry entry : source.entrySet()) { String n = namePrefix + entry.getKey(); JsonElement e = entry.getValue(); if (e.isJsonPrimitive()) { JsonPrimitive p = e.getAsJsonPrimitive(); if (p.isBoolean()) { boolean defaultValue = p.getAsBoolean(); boolean currentValue = defaultValue; if (Respackopts.boolVals.get(screenId).containsKey(n)) currentValue = Respackopts.boolVals.get(screenId).get(n); else Respackopts.boolVals.get(screenId).put(n, defaultValue); config.addEntry(entryBuilder.startBooleanToggle(getText(n, b), currentValue) .setDefaultValue(defaultValue) .setSaveConsumer(v -> Respackopts.boolVals.get(screenId).put(n, v)) .build()); } else if (p.isNumber()) { double defaultValue = p.getAsDouble(); Double currentValue = defaultValue; if (Respackopts.numVals.get(screenId).containsKey(n)) currentValue = Respackopts.numVals.get(screenId).get(n); else Respackopts.numVals.get(screenId).put(n, defaultValue); config.addEntry(entryBuilder.startDoubleField(getText(n, b), currentValue) .setDefaultValue(defaultValue) .setSaveConsumer(v -> Respackopts.numVals.get(screenId).put(n, v)) .build()); } else if (p.isString()) { String defaultValue = p.getAsString(); String currentValue = defaultValue; if (Respackopts.strVals.get(screenId).containsKey(n)) currentValue = Respackopts.strVals.get(screenId).get(n); else Respackopts.strVals.get(screenId).put(n, defaultValue); config.addEntry(entryBuilder.startStrField(getText(n, b), currentValue) .setDefaultValue(defaultValue) .setSaveConsumer(v -> Respackopts.strVals.get(screenId).put(n, v)) .build()); } } else if (e.isJsonArray()) { TreeSet ev = Respackopts.enumKeys.get(screenId).get(n); Double c = Respackopts.numVals.get(screenId).get(n); String sel = ev.first(); int i = 0; for (String s1 : ev) { if (c.intValue() == i) { sel = s1; } i++; } config.addEntry(entryBuilder.startDropdownMenu(getText(n, b), (DropdownBoxEntry.SelectionTopCellElement) DropdownMenuBuilder.TopCellElementBuilder.of(sel, (s) -> s, (s) -> new LiteralText(s)), new DropdownBoxEntry.DefaultSelectionCellCreator()) .setSuggestionMode(false) .setDefaultValue(ev.first()) .setSelections(() -> ev.iterator()) .setSaveConsumer(v -> { int j = 0; for (String s1 : ev) { if (s1.equals(v)) Respackopts.numVals.get(screenId).put(n, (double) j); j++; } }).build()); } else if (e.isJsonNull()) { System.out.println("[respackopts] Config definition contains null, skipping that entry"); } else if (e.isJsonObject()) { SubCategoryBuilder sc = entryBuilder.startSubCategory(getText(n, "respackopts.title." + screenId)); buildCategory(e.getAsJsonObject(), screenId, new JfConfigCategorySub(sc), entryBuilder, n + "."); config.addEntry(sc.build()); } else { System.err.println("[respackopts] Unsupported non-primitive datatype"); } } } public Screen buildGui(JsonObject source, String resourcepackid, Screen parent) { try { ConfigBuilder builder; builder = ConfigBuilder.create() .setParentScreen(parent) .setTitle(getText(resourcepackid, "respackopts.title")); ConfigEntryBuilder entryBuilder = builder.entryBuilder(); builder.setSavingRunnable(Respackopts::save); ConfigCategory config = builder.getOrCreateCategory(getText(resourcepackid, "respackopts.title")); buildCategory(source, resourcepackid, new JfConfigCategoryPrimary(config), entryBuilder, ""); return builder.build(); } catch (Throwable t) { t.printStackTrace(); 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); } }