diff --git a/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java b/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java index 6517aaf..57b5545 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java +++ b/src/main/java/me/topchetoeu/animatedchunks/AnimatedChunks.java @@ -134,7 +134,7 @@ public final class AnimatedChunks implements ClientModInitializer, ModMenuApi { registerEases(ease); registerAnimations(animation); - config = new ConfigManager(new File("config/animated-chunks.dat"), animation, ease); + config = new ConfigManager(new File("config/animated-chunks.dat"), animation, ease, progress); EASES_REGISTERING.invoker().register(ease); ANIMATIONS_REGISTERING.invoker().register(animation); diff --git a/src/main/java/me/topchetoeu/animatedchunks/ConfigManager.java b/src/main/java/me/topchetoeu/animatedchunks/ConfigManager.java index a9bbfe0..d64ca11 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/ConfigManager.java +++ b/src/main/java/me/topchetoeu/animatedchunks/ConfigManager.java @@ -1,22 +1,25 @@ package me.topchetoeu.animatedchunks; +import java.io.EOFException; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.ByteBuffer; import me.topchetoeu.animatedchunks.animation.Animation; +import me.topchetoeu.animatedchunks.animation.ProgressManager; import me.topchetoeu.animatedchunks.easing.Ease; public class ConfigManager { public final File configFile; private final Manager animation; private final Manager ease; + private final ProgressManager progress; - private String readString(InputStreamReader reader) throws IOException { + private String readString(InputStream reader) throws IOException { String res = ""; int i; @@ -27,36 +30,46 @@ public class ConfigManager { return res; } - private void writeString(OutputStreamWriter writer, String str) throws IOException { + private void writeString(OutputStream writer, String str) throws IOException { for (int i = 0; i < str.length(); i++) { - writer.write((char)str.charAt(i)); + writer.write((byte)str.charAt(i)); + } + writer.write((byte)0); + } + + private float readFloat(InputStream reader) throws IOException { + try { + var bytes = reader.readNBytes(4); + return ByteBuffer.wrap(bytes).getFloat(); + } + catch (IndexOutOfBoundsException e) { + throw new EOFException(); } - writer.write(0); } public void reload() { try { - var reader = new FileReader(configFile); + var reader = new FileInputStream(configFile); String animation = readString(reader); String ease = readString(reader); + float duration = readFloat(reader); reader.close(); this.animation.set(animation); this.ease.set(ease); - } - catch (FileNotFoundException e) { - save(); + this.progress.setDuration(duration); } catch (IOException e) { - throw new RuntimeException(e); + save(); } } public void save() { try { - var writer = new FileWriter(configFile); + var writer = new FileOutputStream(configFile); writeString(writer, animation.get().getName()); writeString(writer, ease.get().getName()); + writer.write(ByteBuffer.allocate(4).putFloat(progress.getDuration()).array()); writer.close(); } catch (IOException e) { @@ -64,10 +77,11 @@ public class ConfigManager { } } - public ConfigManager(File configFile, Manager animation, Manager ease) { + public ConfigManager(File configFile, Manager animation, Manager ease, ProgressManager progress) { this.configFile = configFile; this.animation = animation; this.ease = ease; + this.progress = progress; reload(); } diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java b/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java index c432f82..ffec4a8 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/AnimatedChunksScreen.java @@ -100,7 +100,7 @@ public class AnimatedChunksScreen extends Screen { private Section durationSection() { var res = new HorizontalSection(); res.setTargetWidth(width / 2); - var input = new NumberInput(res, 5, 5, (sender, val) -> { + var input = new NumberInput(res, 5, 5, progress.getDuration(), (sender, val) -> { if (val <= 0) sender.invalid = true; else { progress.setDuration(val); diff --git a/src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java b/src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java index 92476b3..5393b1d 100644 --- a/src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java +++ b/src/main/java/me/topchetoeu/animatedchunks/gui/NumberInput.java @@ -18,7 +18,7 @@ public class NumberInput extends Input { return this; } - public NumberInput(ParentElement parent, int x, int y, InputAction action) { + public NumberInput(ParentElement parent, int x, int y, float value, InputAction action) { super(parent, x, y, null); super.action = (sender, val) -> { try { @@ -30,6 +30,9 @@ public class NumberInput extends Input { } }; this.action = action; - setContent(""); + setContent(Float.toString(value)); + } + public NumberInput(ParentElement parent, int x, int y, InputAction action) { + this(parent, x, y, 0, action); } }