diff options
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config')
4 files changed, 10 insertions, 88 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/OneConfigConfig.java b/src/main/java/cc/polyfrost/oneconfig/config/OneConfigConfig.java index 7434e49..0eb833f 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/OneConfigConfig.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/OneConfigConfig.java @@ -67,7 +67,7 @@ public class OneConfigConfig extends Config { @Override public void save() { try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Paths.get("OneConfig/" + configFile)), StandardCharsets.UTF_8))) { - writer.write(gson.toJson(this.getClass())); + writer.write(gson.toJson(this)); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/interfaces/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/interfaces/Config.java index 0c8a3b8..e8ffe42 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/interfaces/Config.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/interfaces/Config.java @@ -1,7 +1,5 @@ package cc.polyfrost.oneconfig.config.interfaces; -import cc.polyfrost.oneconfig.gui.elements.config.*; -import com.google.gson.*; import cc.polyfrost.oneconfig.config.annotations.ConfigPage; import cc.polyfrost.oneconfig.config.annotations.Option; import cc.polyfrost.oneconfig.config.core.ConfigCore; @@ -10,7 +8,9 @@ import cc.polyfrost.oneconfig.config.data.OptionCategory; import cc.polyfrost.oneconfig.config.data.OptionPage; import cc.polyfrost.oneconfig.config.profiles.Profiles; import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.gui.elements.config.*; import cc.polyfrost.oneconfig.gui.pages.ModConfigPage; +import com.google.gson.*; import net.minecraft.client.Minecraft; import java.io.*; @@ -24,9 +24,10 @@ import java.util.Map; import java.util.Optional; public class Config { - protected final String configFile; - protected final Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().registerTypeAdapterFactory(OneConfigTypeAdapterFactory.getStaticTypeAdapterFactory()).create(); - private static Mod mod; + transient protected final String configFile; + transient protected final Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().create(); + transient private Mod mod; + public boolean enabled = true; /** * @param modData information about the mod @@ -43,7 +44,7 @@ public class Config { mod.config = this; generateOptionList(this.getClass(), mod.defaultPage, mod); ConfigCore.oneConfigMods.add(mod); - Config.mod = mod; + this.mod = mod; } /** @@ -51,7 +52,7 @@ public class Config { */ public void save() { try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Profiles.getProfileFile(configFile).toPath()), StandardCharsets.UTF_8))) { - writer.write(gson.toJson(this.getClass())); + writer.write(gson.toJson(this)); } catch (IOException e) { e.printStackTrace(); } @@ -170,7 +171,7 @@ public class Config { TypeAdapter<?> adapter = gson.getAdapter(field.getType()); Object object = adapter.fromJsonTree(value); field.setAccessible(true); - field.set(null, object); + field.set(this, object); } catch (NoSuchFieldException | IllegalAccessException ignored) { } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapter.java b/src/main/java/cc/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapter.java deleted file mode 100644 index 3368a7f..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapter.java +++ /dev/null @@ -1,51 +0,0 @@ -package cc.polyfrost.oneconfig.config.interfaces; - -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; -import com.google.gson.TypeAdapter; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; - -import java.io.IOException; -import java.lang.reflect.Field; - -final class OneConfigTypeAdapter<T> extends TypeAdapter<Class<T>> { - - private final Gson gson; - private final JsonParser parser = new JsonParser(); - - private OneConfigTypeAdapter(final Gson gson) { - this.gson = gson; - } - - static <T> TypeAdapter<Class<T>> getStaticTypeAdapter(final Gson gson) { - return new OneConfigTypeAdapter<>(gson); - } - - @Override - public void write(final JsonWriter out, final Class<T> value) throws IOException { - try { - out.beginObject(); - for (Field field : value.getFields()) { - out.name(field.getName()); - field.setAccessible(true); - final TypeAdapter<Object> adapter = (TypeAdapter) gson.getAdapter(field.getType()); - adapter.write(out, field.get(field.getClass())); - } - for (Class<?> clazz : value.getClasses()) { - out.name(clazz.getSimpleName()); - final TypeAdapter<JsonElement> adapter = gson.getAdapter(JsonElement.class); - adapter.write(out, parser.parse(gson.toJson(clazz))); - } - out.endObject(); - } catch (final IllegalAccessException ex) { - throw new IOException(ex); - } - } - - @Override - public Class<T> read(final JsonReader in) throws IOException { - return null; - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapterFactory.java b/src/main/java/cc/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapterFactory.java deleted file mode 100644 index d310f2c..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapterFactory.java +++ /dev/null @@ -1,28 +0,0 @@ -package cc.polyfrost.oneconfig.config.interfaces; - -import com.google.gson.Gson; -import com.google.gson.TypeAdapter; -import com.google.gson.TypeAdapterFactory; -import com.google.gson.reflect.TypeToken; - -import java.lang.reflect.Type; - -public class OneConfigTypeAdapterFactory implements TypeAdapterFactory { - - private static final TypeAdapterFactory staticTypeAdapterFactory = new OneConfigTypeAdapterFactory(); - - public static TypeAdapterFactory getStaticTypeAdapterFactory() { - return staticTypeAdapterFactory; - } - - @Override - public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) { - final Type type = typeToken.getType(); - if (type.equals(Class.class)) { - @SuppressWarnings("unchecked") final TypeAdapter<T> castStaticTypeAdapter = (TypeAdapter<T>) OneConfigTypeAdapter.getStaticTypeAdapter(gson); - return castStaticTypeAdapter; - } - return null; - } - -} |