mirror of
https://github.com/PuzzleMC/Puzzle.git
synced 2025-12-15 19:35:10 +01:00
Fix MidnightConfigLite not being pushed
This commit is contained in:
83
puzzle-base/src/main/java/net/puzzlemc/core/config/MidnightConfigLite.java
Executable file
83
puzzle-base/src/main/java/net/puzzlemc/core/config/MidnightConfigLite.java
Executable file
@@ -0,0 +1,83 @@
|
|||||||
|
package net.puzzlemc.core.config;
|
||||||
|
|
||||||
|
import com.google.gson.ExclusionStrategy;
|
||||||
|
import com.google.gson.FieldAttributes;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
// MidnightConfigLite v0.1.0
|
||||||
|
// Just writing and parsing of config files
|
||||||
|
|
||||||
|
/** Based on https://github.com/Minenash/TinyConfig
|
||||||
|
* Credits to Minenash */
|
||||||
|
|
||||||
|
public class MidnightConfigLite {
|
||||||
|
|
||||||
|
private static final List<EntryInfo> entries = new ArrayList<>();
|
||||||
|
|
||||||
|
protected static class EntryInfo {
|
||||||
|
Field field;
|
||||||
|
Object defaultValue;
|
||||||
|
Object value;
|
||||||
|
String tempValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Map<String,Class<?>> configClass = new HashMap<>();
|
||||||
|
private static Path path;
|
||||||
|
|
||||||
|
private static final Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).excludeFieldsWithModifiers(Modifier.PRIVATE).addSerializationExclusionStrategy(new HiddenAnnotationExclusionStrategy()).setPrettyPrinting().create();
|
||||||
|
|
||||||
|
public static void init(String modid, Class<?> config) {
|
||||||
|
path = FabricLoader.getInstance().getConfigDir().resolve(modid + ".json");
|
||||||
|
configClass.put(modid, config);
|
||||||
|
|
||||||
|
for (Field field : config.getFields()) {
|
||||||
|
EntryInfo info = new EntryInfo();
|
||||||
|
if (field.isAnnotationPresent(Entry.class))
|
||||||
|
try {
|
||||||
|
info.defaultValue = field.get(null);
|
||||||
|
} catch (IllegalAccessException ignored) {}
|
||||||
|
}
|
||||||
|
try { gson.fromJson(Files.newBufferedReader(path), config); }
|
||||||
|
catch (Exception e) { write(modid); }
|
||||||
|
|
||||||
|
for (EntryInfo info : entries) {
|
||||||
|
if (info.field.isAnnotationPresent(Entry.class))
|
||||||
|
try {
|
||||||
|
info.value = info.field.get(null);
|
||||||
|
info.tempValue = info.value.toString();
|
||||||
|
} catch (IllegalAccessException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void write(String modid) {
|
||||||
|
path = FabricLoader.getInstance().getConfigDir().resolve(modid + ".json");
|
||||||
|
try {
|
||||||
|
if (!Files.exists(path)) Files.createFile(path);
|
||||||
|
Files.write(path, gson.toJson(configClass.get(modid).getDeclaredConstructor().newInstance()).getBytes());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
public @interface Entry {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class HiddenAnnotationExclusionStrategy implements ExclusionStrategy {
|
||||||
|
public boolean shouldSkipClass(Class<?> clazz) { return false; }
|
||||||
|
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
|
||||||
|
return fieldAttributes.getAnnotation(Entry.class) == null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user