2021-04-11 16:00:37 +02:00
|
|
|
package io.gitlab.jfronny.libjf.config;
|
|
|
|
|
|
|
|
import io.gitlab.jfronny.libjf.Libjf;
|
|
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class Config {
|
2021-04-11 20:23:52 +02:00
|
|
|
public Class<?> configClass;
|
2021-04-11 16:00:37 +02:00
|
|
|
public String translationPrefix;
|
|
|
|
public Path path;
|
|
|
|
public final List<EntryInfo> entries = new ArrayList<>();
|
|
|
|
|
|
|
|
public Config(String modid, Class<?> config) {
|
|
|
|
translationPrefix = modid + ".jfconfig.";
|
|
|
|
path = FabricLoader.getInstance().getConfigDir().resolve(modid + ".json");
|
|
|
|
configClass = config;
|
|
|
|
|
|
|
|
for (Field field : config.getFields()) {
|
|
|
|
EntryInfo info = new EntryInfo();
|
|
|
|
|
|
|
|
info.field = field;
|
|
|
|
entries.add(info);
|
|
|
|
|
|
|
|
try { info.defaultValue = field.get(null); }
|
|
|
|
catch (IllegalAccessException ignored) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
try { Libjf.GSON.fromJson(Files.newBufferedReader(path), config); }
|
|
|
|
catch (Exception e) { write(); }
|
|
|
|
|
|
|
|
for (EntryInfo info : entries) {
|
|
|
|
try {
|
|
|
|
info.value = info.field.get(null);
|
|
|
|
}
|
|
|
|
catch (IllegalAccessException ignored) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write() {
|
|
|
|
try {
|
|
|
|
if (!Files.exists(path)) Files.createFile(path);
|
2021-04-11 20:23:52 +02:00
|
|
|
Files.write(path, Libjf.GSON.toJson(configClass.getDeclaredConstructor().newInstance()).getBytes());
|
2021-04-11 16:00:37 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|