136 lines
5.7 KiB
Java
136 lines
5.7 KiB
Java
|
package io.gitlab.jfronny.libjf.config;
|
||
|
|
||
|
import io.gitlab.jfronny.libjf.Libjf;
|
||
|
import net.fabricmc.loader.api.FabricLoader;
|
||
|
import net.minecraft.client.gui.widget.ButtonWidget;
|
||
|
import net.minecraft.client.gui.widget.TextFieldWidget;
|
||
|
import net.minecraft.text.*;
|
||
|
import net.minecraft.util.Formatting;
|
||
|
|
||
|
import java.lang.reflect.Field;
|
||
|
import java.nio.file.Files;
|
||
|
import java.nio.file.Path;
|
||
|
import java.util.*;
|
||
|
import java.util.function.BiFunction;
|
||
|
import java.util.function.Function;
|
||
|
import java.util.function.Predicate;
|
||
|
import java.util.regex.Pattern;
|
||
|
|
||
|
/* Based on https://github.com/TeamMidnightDust/MidnightLib
|
||
|
* see https://github.com/TeamMidnightDust/MidnightLib/blob/main/LICENSE
|
||
|
*/
|
||
|
|
||
|
@SuppressWarnings("rawtypes")
|
||
|
public class Config {
|
||
|
private static final Pattern INTEGER_ONLY = Pattern.compile("(-?[0-9]*)");
|
||
|
private static final Pattern DECIMAL_ONLY = Pattern.compile("-?([\\d]+\\.?[\\d]*|[\\d]*\\.?[\\d]+|\\.)");
|
||
|
|
||
|
public Class configClass;
|
||
|
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()) {
|
||
|
Class<?> type = field.getType();
|
||
|
EntryInfo info = new EntryInfo();
|
||
|
|
||
|
Entry e;
|
||
|
try { e = field.getAnnotation(Entry.class); }
|
||
|
catch (Exception ignored) { continue; }
|
||
|
|
||
|
info.width = e.width();
|
||
|
info.field = field;
|
||
|
|
||
|
if (type == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, e.min(), e.max(), true);
|
||
|
else if (type == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(),false);
|
||
|
else if (type == float.class) textField(info, Float::parseFloat, DECIMAL_ONLY, e.min(), e.max(),false);
|
||
|
else if (type == String.class) textField(info, String::length, null, Math.min(e.min(),0), Math.max(e.max(),1),true);
|
||
|
else if (type == boolean.class) {
|
||
|
Function<Object,Text> func = value -> new LiteralText((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED);
|
||
|
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
|
||
|
info.value = !(Boolean) info.value;
|
||
|
button.setMessage(func.apply(info.value));
|
||
|
}, func);
|
||
|
}
|
||
|
else if (type.isEnum()) {
|
||
|
List<?> values = Arrays.asList(field.getType().getEnumConstants());
|
||
|
Function<Object,Text> func = value -> new TranslatableText(translationPrefix + "enum." + type.getSimpleName() + "." + info.value.toString());
|
||
|
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object,Text>>( button -> {
|
||
|
int index = values.indexOf(info.value) + 1;
|
||
|
info.value = values.get(index >= values.size()? 0 : index);
|
||
|
button.setMessage(func.apply(info.value));
|
||
|
}, func);
|
||
|
}
|
||
|
else
|
||
|
continue;
|
||
|
|
||
|
entries.add(info);
|
||
|
|
||
|
try { info.defaultValue = field.get(null); }
|
||
|
catch (IllegalAccessException ignored) {}
|
||
|
|
||
|
try {
|
||
|
info.dynamicTooltip = config.getMethod(e.dynamicTooltip());
|
||
|
info.dynamicTooltip.setAccessible(true);
|
||
|
} catch (Exception ignored) {}
|
||
|
|
||
|
}
|
||
|
|
||
|
try { Libjf.GSON.fromJson(Files.newBufferedReader(path), config); }
|
||
|
catch (Exception e) { write(); }
|
||
|
|
||
|
for (EntryInfo info : entries) {
|
||
|
try {
|
||
|
info.value = info.field.get(null);
|
||
|
info.tempValue = info.value.toString();
|
||
|
}
|
||
|
catch (IllegalAccessException ignored) {}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void textField(EntryInfo info, Function<String,Number> f, Pattern pattern, double min, double max, boolean cast) {
|
||
|
boolean isNumber = pattern != null;
|
||
|
info.widget = (BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) (t, b) -> s -> {
|
||
|
s = s.trim();
|
||
|
if (!(s.isEmpty() || !isNumber || pattern.matcher(s).matches()))
|
||
|
return false;
|
||
|
|
||
|
Number value = 0;
|
||
|
boolean inLimits = false;
|
||
|
System.out.println(((isNumber ^ s.isEmpty())));
|
||
|
System.out.println(!s.equals("-") && !s.equals("."));
|
||
|
info.error = null;
|
||
|
if (!(isNumber && s.isEmpty()) && !s.equals("-") && !s.equals(".")) {
|
||
|
value = f.apply(s);
|
||
|
inLimits = value.doubleValue() >= min && value.doubleValue() <= max;
|
||
|
info.error = inLimits? null : new AbstractMap.SimpleEntry<>(t, new LiteralText(value.doubleValue() < min ?
|
||
|
"§cMinimum " + (isNumber? "value" : "length") + (cast? " is " + (int)min : " is " + min) :
|
||
|
"§cMaximum " + (isNumber? "value" : "length") + (cast? " is " + (int)max : " is " + max)));
|
||
|
}
|
||
|
|
||
|
info.tempValue = s;
|
||
|
t.setEditableColor(inLimits? 0xFFFFFFFF : 0xFFFF7777);
|
||
|
info.inLimits = inLimits;
|
||
|
b.active = entries.stream().allMatch(e -> e.inLimits);
|
||
|
|
||
|
if (inLimits)
|
||
|
info.value = isNumber? value : s;
|
||
|
|
||
|
return true;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
public void write() {
|
||
|
try {
|
||
|
if (!Files.exists(path)) Files.createFile(path);
|
||
|
Files.write(path, Libjf.GSON.toJson(configClass.newInstance()).getBytes());
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
}
|