Respackopts/src/main/java/io/gitlab/jfronny/respackopts/Respackopts.java

128 lines
4.5 KiB
Java
Raw Normal View History

2020-11-24 22:04:13 +01:00
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.*;
2020-11-24 22:04:13 +01:00
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
2020-11-25 21:58:21 +01:00
import java.util.HashSet;
import java.util.TreeSet;
2020-11-25 21:58:21 +01:00
import java.util.function.Consumer;
2020-11-24 22:04:13 +01:00
@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;
2020-11-24 22:04:13 +01:00
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<>();
2020-11-24 22:04:13 +01:00
@Override
public void onInitializeClient() {
deNull();
2020-11-25 21:58:21 +01:00
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();
2020-12-20 22:28:25 +01:00
if (c.bools != null) {
boolVals.get(id).forEach((i, val) -> {
if (c.bools.containsKey(i)) {
boolVals.get(id).put(i, c.bools.get(i));
} else
System.out.println("Could not find bool " + i + " in " + id);
});
}
if (c.doubles != null) {
numVals.get(id).forEach((i, val) -> {
if (c.doubles.containsKey(i)) {
numVals.get(id).put(i, c.doubles.get(i));
} else
System.out.println("Could not find num " + i + " in " + id);
});
}
if (c.strings != null) {
strVals.get(id).forEach((i, val) -> {
if (c.strings.containsKey(i)) {
strVals.get(id).put(i, c.strings.get(i));
} else
System.out.println("Could not find str " + i + " in " + id);
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void deNull() {
try {
Files.createDirectories(p);
} catch (IOException e) {
e.printStackTrace();
}
2020-11-24 22:04:13 +01:00
if (boolVals == null)
boolVals = new HashMap<>();
if (numVals == null)
numVals = new HashMap<>();
if (strVals == null)
strVals = new HashMap<>();
if (enumKeys == null)
enumKeys = new HashMap<>();
}
2020-11-24 22:04:13 +01:00
}