110 lines
3.7 KiB
Java
110 lines
3.7 KiB
Java
package io.gitlab.jfronny.respackopts;
|
|
|
|
import com.google.gson.Gson;
|
|
import io.gitlab.jfronny.respackopts.data.Config;
|
|
import io.gitlab.jfronny.respackopts.data.Respackmeta;
|
|
import net.fabricmc.api.ClientModInitializer;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
|
|
import javax.swing.*;
|
|
import java.io.*;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.TreeSet;
|
|
import java.util.function.Consumer;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public class Respackopts implements ClientModInitializer {
|
|
public static HashMap<String, HashMap<String, Boolean>> boolVals;
|
|
public static HashMap<String, HashMap<String, Double>> numVals;
|
|
public static HashMap<String, HashMap<String, String>> strVals;
|
|
public static HashMap<String, HashMap<String, TreeSet<String>>> enumKeys;
|
|
public static Gson g = new Gson();
|
|
public static GuiFactory factory = new GuiFactory();
|
|
public static final Integer metaVersion = 1;
|
|
public static HashMap<String, Respackmeta> resPackMetas = new HashMap<>();
|
|
public static final String ID = "respackopts";
|
|
static final Path p = FabricLoader.getInstance().getConfigDir().resolve("respackopts");
|
|
public static final HashSet<Runnable> saveActions = new HashSet<>();
|
|
@Override
|
|
public void onInitializeClient() {
|
|
deNull();
|
|
if (FabricLoader.getInstance().isDevelopmentEnvironment())
|
|
saveActions.add(() -> System.out.println("Save"));
|
|
}
|
|
|
|
public static void save() {
|
|
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();
|
|
}
|
|
}
|
|
for (Runnable action : saveActions) {
|
|
action.run();
|
|
}
|
|
}
|
|
|
|
public static void load() {
|
|
try {
|
|
for (String s : resPackMetas.keySet()) {
|
|
load(resPackMetas.get(s).id);
|
|
}
|
|
} catch (Throwable e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void load(String id) {
|
|
Path q = p.resolve(id + ".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(id, c.bools);
|
|
if (c.doubles != null)
|
|
numVals.put(id, c.doubles);
|
|
if (c.strings != null)
|
|
strVals.put(id, c.strings);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void deNull() {
|
|
try {
|
|
Files.createDirectories(p);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
if (boolVals == null)
|
|
boolVals = new HashMap<>();
|
|
if (numVals == null)
|
|
numVals = new HashMap<>();
|
|
if (strVals == null)
|
|
strVals = new HashMap<>();
|
|
if (enumKeys == null)
|
|
enumKeys = new HashMap<>();
|
|
}
|
|
}
|