2020-11-24 22:04:13 +01:00
|
|
|
package io.gitlab.jfronny.respackopts;
|
|
|
|
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonPrimitive;
|
2020-12-21 13:18:13 +01:00
|
|
|
import io.gitlab.jfronny.respackopts.abstractions.JfConfigCategory;
|
|
|
|
import io.gitlab.jfronny.respackopts.abstractions.JfConfigCategoryPrimary;
|
|
|
|
import io.gitlab.jfronny.respackopts.abstractions.JfConfigCategorySub;
|
2020-11-24 22:04:13 +01:00
|
|
|
import me.shedaniel.clothconfig2.api.ConfigBuilder;
|
|
|
|
import me.shedaniel.clothconfig2.api.ConfigCategory;
|
|
|
|
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
|
2020-11-26 20:11:38 +01:00
|
|
|
import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry;
|
|
|
|
import me.shedaniel.clothconfig2.impl.builders.DropdownMenuBuilder;
|
2020-12-21 13:18:13 +01:00
|
|
|
import me.shedaniel.clothconfig2.impl.builders.SubCategoryBuilder;
|
2020-11-24 22:04:13 +01:00
|
|
|
import net.minecraft.client.gui.screen.FatalErrorScreen;
|
|
|
|
import net.minecraft.client.gui.screen.Screen;
|
2020-11-25 14:28:29 +01:00
|
|
|
import net.minecraft.text.*;
|
|
|
|
import net.minecraft.util.Language;
|
2020-11-24 22:04:13 +01:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2020-11-26 20:11:38 +01:00
|
|
|
import java.util.TreeSet;
|
2020-11-24 22:04:13 +01:00
|
|
|
|
|
|
|
public class GuiFactory {
|
2020-12-21 13:18:13 +01:00
|
|
|
public void buildCategory(JsonObject source, String screenId, JfConfigCategory config, ConfigEntryBuilder entryBuilder, String namePrefix) {
|
2020-11-24 22:04:13 +01:00
|
|
|
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<>());
|
2020-11-26 20:11:38 +01:00
|
|
|
if (!Respackopts.enumKeys.containsKey(screenId))
|
|
|
|
Respackopts.enumKeys.put(screenId, new HashMap<>());
|
2020-11-25 14:28:29 +01:00
|
|
|
String b = "respackopts.field." + screenId;
|
2020-11-24 22:04:13 +01:00
|
|
|
for (Map.Entry<String, JsonElement> entry : source.entrySet()) {
|
2020-12-21 13:18:13 +01:00
|
|
|
String n = namePrefix + entry.getKey();
|
2020-11-24 22:04:13 +01:00
|
|
|
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);
|
2020-11-25 14:28:29 +01:00
|
|
|
config.addEntry(entryBuilder.startBooleanToggle(getText(n, b), currentValue)
|
2020-11-24 22:04:13 +01:00
|
|
|
.setDefaultValue(defaultValue)
|
2020-11-26 20:11:38 +01:00
|
|
|
.setSaveConsumer(v -> Respackopts.boolVals.get(screenId).put(n, v))
|
2020-11-24 22:04:13 +01:00
|
|
|
.build());
|
|
|
|
}
|
|
|
|
else if (p.isNumber()) {
|
|
|
|
double defaultValue = p.getAsDouble();
|
2020-11-26 20:11:38 +01:00
|
|
|
Double currentValue = defaultValue;
|
2020-11-24 22:04:13 +01:00
|
|
|
if (Respackopts.numVals.get(screenId).containsKey(n))
|
|
|
|
currentValue = Respackopts.numVals.get(screenId).get(n);
|
|
|
|
else
|
|
|
|
Respackopts.numVals.get(screenId).put(n, defaultValue);
|
2020-11-25 14:28:29 +01:00
|
|
|
config.addEntry(entryBuilder.startDoubleField(getText(n, b), currentValue)
|
2020-11-24 22:04:13 +01:00
|
|
|
.setDefaultValue(defaultValue)
|
2020-11-26 20:11:38 +01:00
|
|
|
.setSaveConsumer(v -> Respackopts.numVals.get(screenId).put(n, v))
|
2020-11-24 22:04:13 +01:00
|
|
|
.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);
|
2020-11-25 14:28:29 +01:00
|
|
|
config.addEntry(entryBuilder.startStrField(getText(n, b), currentValue)
|
2020-11-24 22:04:13 +01:00
|
|
|
.setDefaultValue(defaultValue)
|
2020-11-26 20:11:38 +01:00
|
|
|
.setSaveConsumer(v -> Respackopts.strVals.get(screenId).put(n, v))
|
2020-11-24 22:04:13 +01:00
|
|
|
.build());
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 20:11:38 +01:00
|
|
|
else if (e.isJsonArray()) {
|
|
|
|
TreeSet<String> 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++;
|
|
|
|
}
|
2020-12-20 22:28:25 +01:00
|
|
|
config.addEntry(entryBuilder.startDropdownMenu(getText(n, b), (DropdownBoxEntry.SelectionTopCellElement) DropdownMenuBuilder.TopCellElementBuilder.of(sel, (s) -> s, (s) -> new LiteralText(s)), new DropdownBoxEntry.DefaultSelectionCellCreator())
|
2020-11-26 20:11:38 +01:00
|
|
|
.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());
|
|
|
|
}
|
2020-12-21 13:18:13 +01:00
|
|
|
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());
|
|
|
|
}
|
2020-11-24 22:04:13 +01:00
|
|
|
else {
|
|
|
|
System.err.println("[respackopts] Unsupported non-primitive datatype");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 21:58:21 +01:00
|
|
|
public Screen buildGui(JsonObject source, String resourcepackid, Screen parent) {
|
2020-11-24 22:04:13 +01:00
|
|
|
try {
|
|
|
|
ConfigBuilder builder;
|
|
|
|
builder = ConfigBuilder.create()
|
|
|
|
.setParentScreen(parent)
|
2020-11-25 14:28:29 +01:00
|
|
|
.setTitle(getText(resourcepackid, "respackopts.title"));
|
2020-11-24 22:04:13 +01:00
|
|
|
ConfigEntryBuilder entryBuilder = builder.entryBuilder();
|
2020-11-26 20:11:38 +01:00
|
|
|
builder.setSavingRunnable(Respackopts::save);
|
2020-11-26 20:38:54 +01:00
|
|
|
ConfigCategory config = builder.getOrCreateCategory(getText(resourcepackid, "respackopts.title"));
|
2020-12-21 13:18:13 +01:00
|
|
|
buildCategory(source, resourcepackid, new JfConfigCategoryPrimary(config), entryBuilder, "");
|
2020-11-24 22:04:13 +01:00
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
return new FatalErrorScreen(new TranslatableText("respackopts.loadFailed"), new TranslatableText("respackopts.loadError"));
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 14:28:29 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2020-11-24 22:04:13 +01:00
|
|
|
}
|