From 293d7054e597ba1207456ce56c03d0a45d1ea004 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Sat, 7 May 2022 17:08:16 +0200 Subject: bunch of saving stuff --- .../oneconfig/config/OneConfigConfig.java | 2 +- .../oneconfig/config/interfaces/Config.java | 17 ++++---- .../config/interfaces/OneConfigTypeAdapter.java | 51 ---------------------- .../interfaces/OneConfigTypeAdapterFactory.java | 28 ------------ .../cc/polyfrost/oneconfig/gui/OneConfigGui.java | 1 + .../oneconfig/gui/elements/BasicButton.java | 4 -- .../oneconfig/gui/elements/BasicElement.java | 4 ++ .../polyfrost/oneconfig/gui/elements/ModCard.java | 6 +-- .../gui/elements/config/ConfigUniSelector.java | 6 +-- .../cc/polyfrost/oneconfig/gui/pages/ModsPage.java | 7 ++- 10 files changed, 26 insertions(+), 100 deletions(-) delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapter.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapterFactory.java 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 extends TypeAdapter> { - - private final Gson gson; - private final JsonParser parser = new JsonParser(); - - private OneConfigTypeAdapter(final Gson gson) { - this.gson = gson; - } - - static TypeAdapter> getStaticTypeAdapter(final Gson gson) { - return new OneConfigTypeAdapter<>(gson); - } - - @Override - public void write(final JsonWriter out, final Class value) throws IOException { - try { - out.beginObject(); - for (Field field : value.getFields()) { - out.name(field.getName()); - field.setAccessible(true); - final TypeAdapter adapter = (TypeAdapter) gson.getAdapter(field.getType()); - adapter.write(out, field.get(field.getClass())); - } - for (Class clazz : value.getClasses()) { - out.name(clazz.getSimpleName()); - final TypeAdapter 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 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 TypeAdapter create(final Gson gson, final TypeToken typeToken) { - final Type type = typeToken.getType(); - if (type.equals(Class.class)) { - @SuppressWarnings("unchecked") final TypeAdapter castStaticTypeAdapter = (TypeAdapter) OneConfigTypeAdapter.getStaticTypeAdapter(gson); - return castStaticTypeAdapter; - } - return null; - } - -} diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java index b3c59b0..75c3ae5 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java @@ -168,6 +168,7 @@ public class OneConfigGui extends GuiScreen { if(currentColorSelector != null) currentColorSelector.keyTyped(key, keyCode); currentPage.keyTyped(key, keyCode); } catch (Exception e) { + e.printStackTrace(); System.out.println("this should literally never happen"); } } diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java index 0db6eac..bd2521a 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java @@ -146,10 +146,6 @@ public class BasicButton extends BasicElement { super.update(x, y); } - public void setToggled(boolean state) { - this.toggled = state; - } - public Page getPage() { return page; } diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java index 4a7fb8c..b5805cb 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java @@ -96,6 +96,10 @@ public class BasicElement { return toggled; } + public void setToggled(boolean toggled) { + this.toggled = toggled; + } + public boolean isDisabled() { return disabled; } diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/ModCard.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ModCard.java index fb9f40a..9e6d526 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/ModCard.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ModCard.java @@ -38,6 +38,8 @@ public class ModCard extends BasicElement { toggled = active; this.disabled = disabled; this.favorite = favorite; + favoriteHitbox.setToggled(true); + toggled = active; } @Override @@ -76,10 +78,6 @@ public class ModCard extends BasicElement { if (clicked && favoriteHitbox.hovered) toggled = false; if (clicked && !isHoveredSecondary && active) toggled = true; if (!active & disabled) toggled = false; - //RenderManager.drawString(vg, "active=" + active, x + 150, y + 92, OneConfigConfig.WHITE, 10f, Fonts.INTER_MEDIUM); // debug stuff - //RenderManager.drawString(vg, "disabled=" + disabled, x + 150, y + 103, OneConfigConfig.WHITE, 10f, Fonts.INTER_MEDIUM); - //RenderManager.drawString(vg, "favorite=" + favorite, x + 150, y + 114, OneConfigConfig.WHITE, 10f, Fonts.INTER_MEDIUM); - active = toggled; NanoVG.nvgGlobalAlpha(vg, 1f); diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigUniSelector.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigUniSelector.java index b9bd095..48967d6 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigUniSelector.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigUniSelector.java @@ -40,11 +40,11 @@ public class ConfigUniSelector extends BasicOption { Scissor scissor = ScissorManager.scissor(vg, x + 256, y, 192, 32); if (previous == -1) { - RenderManager.drawString(vg, option, x + 352 - RenderManager.getTextWidth(vg, option, 12f, Fonts.MEDIUM) / 2f, y + 15, OneConfigConfig.WHITE_90, 14f, Fonts.MEDIUM); + RenderManager.drawString(vg, option, x + 352 - RenderManager.getTextWidth(vg, option, 14f, Fonts.MEDIUM) / 2f, y + 15, OneConfigConfig.WHITE_90, 14f, Fonts.MEDIUM); } else { String prevOption = options[previous] + " " + (previous + 1) + "/" + options.length; - RenderManager.drawString(vg, selected < previous ? prevOption : option, x + 352 - RenderManager.getTextWidth(vg, selected < previous ? prevOption : option, 14f, Fonts.MEDIUM) / 2f + 192 * percentMove, y + 15, OneConfigConfig.WHITE_90, 12f, Fonts.MEDIUM); - RenderManager.drawString(vg, selected < previous ? option : prevOption, x + 352 - RenderManager.getTextWidth(vg, selected < previous ? option : prevOption, 14f, Fonts.MEDIUM) / 2f - 192 * (1 - percentMove), y + 15, OneConfigConfig.WHITE_90, 12f, Fonts.MEDIUM); + RenderManager.drawString(vg, selected < previous ? prevOption : option, x + 352 - RenderManager.getTextWidth(vg, selected < previous ? prevOption : option, 14f, Fonts.MEDIUM) / 2f + 192 * percentMove, y + 15, OneConfigConfig.WHITE_90, 14f, Fonts.MEDIUM); + RenderManager.drawString(vg, selected < previous ? option : prevOption, x + 352 - RenderManager.getTextWidth(vg, selected < previous ? option : prevOption, 14f, Fonts.MEDIUM) / 2f - 192 * (1 - percentMove), y + 15, OneConfigConfig.WHITE_90, 14f, Fonts.MEDIUM); } ScissorManager.resetScissor(vg, scissor); diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java b/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java index c7351d3..c7ab51d 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java @@ -21,7 +21,7 @@ public class ModsPage extends Page { public ModsPage() { super("Mods"); for (Mod modData : OneConfig.loadedMods) { - modCards.add(new ModCard(modData, null, true, false, OneConfigConfig.favoriteMods.contains(modData.name))); + modCards.add(new ModCard(modData, null, modData.config == null || modData.config.enabled, false, OneConfigConfig.favoriteMods.contains(modData.name))); } for (ModCard card : modCards) { if (card.isFavorite()) { @@ -80,7 +80,12 @@ public class ModsPage extends Page { OneConfigConfig.favoriteMods.clear(); for (ModCard modCard : modCards) { if (modCard.isFavorite()) OneConfigConfig.favoriteMods.add(modCard.getModData().name); + if (modCard.getModData().config != null && modCard.getModData().config.enabled != modCard.isActive()){ + modCard.getModData().config.enabled = modCard.isActive(); + modCard.getModData().config.save(); + } } + OneConfig.config.save(); } @Override -- cgit