chore: Save and load duration from config file

This commit is contained in:
TopchetoEU 2022-09-22 12:30:02 +03:00
parent a3d7f9463b
commit 23d289f9f7
No known key found for this signature in database
GPG Key ID: 0F2543CA49C81E3A
4 changed files with 37 additions and 20 deletions

View File

@ -134,7 +134,7 @@ public final class AnimatedChunks implements ClientModInitializer, ModMenuApi {
registerEases(ease); registerEases(ease);
registerAnimations(animation); 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); EASES_REGISTERING.invoker().register(ease);
ANIMATIONS_REGISTERING.invoker().register(animation); ANIMATIONS_REGISTERING.invoker().register(animation);

View File

@ -1,22 +1,25 @@
package me.topchetoeu.animatedchunks; package me.topchetoeu.animatedchunks;
import java.io.EOFException;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileInputStream;
import java.io.FileReader; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStream;
import java.io.OutputStreamWriter; import java.io.OutputStream;
import java.nio.ByteBuffer;
import me.topchetoeu.animatedchunks.animation.Animation; import me.topchetoeu.animatedchunks.animation.Animation;
import me.topchetoeu.animatedchunks.animation.ProgressManager;
import me.topchetoeu.animatedchunks.easing.Ease; import me.topchetoeu.animatedchunks.easing.Ease;
public class ConfigManager { public class ConfigManager {
public final File configFile; public final File configFile;
private final Manager<Animation> animation; private final Manager<Animation> animation;
private final Manager<Ease> ease; private final Manager<Ease> ease;
private final ProgressManager progress;
private String readString(InputStreamReader reader) throws IOException { private String readString(InputStream reader) throws IOException {
String res = ""; String res = "";
int i; int i;
@ -27,36 +30,46 @@ public class ConfigManager {
return res; 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++) { 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() { public void reload() {
try { try {
var reader = new FileReader(configFile); var reader = new FileInputStream(configFile);
String animation = readString(reader); String animation = readString(reader);
String ease = readString(reader); String ease = readString(reader);
float duration = readFloat(reader);
reader.close(); reader.close();
this.animation.set(animation); this.animation.set(animation);
this.ease.set(ease); this.ease.set(ease);
} this.progress.setDuration(duration);
catch (FileNotFoundException e) {
save();
} }
catch (IOException e) { catch (IOException e) {
throw new RuntimeException(e); save();
} }
} }
public void save() { public void save() {
try { try {
var writer = new FileWriter(configFile); var writer = new FileOutputStream(configFile);
writeString(writer, animation.get().getName()); writeString(writer, animation.get().getName());
writeString(writer, ease.get().getName()); writeString(writer, ease.get().getName());
writer.write(ByteBuffer.allocate(4).putFloat(progress.getDuration()).array());
writer.close(); writer.close();
} }
catch (IOException e) { catch (IOException e) {
@ -64,10 +77,11 @@ public class ConfigManager {
} }
} }
public ConfigManager(File configFile, Manager<Animation> animation, Manager<Ease> ease) { public ConfigManager(File configFile, Manager<Animation> animation, Manager<Ease> ease, ProgressManager progress) {
this.configFile = configFile; this.configFile = configFile;
this.animation = animation; this.animation = animation;
this.ease = ease; this.ease = ease;
this.progress = progress;
reload(); reload();
} }

View File

@ -100,7 +100,7 @@ public class AnimatedChunksScreen extends Screen {
private Section durationSection() { private Section durationSection() {
var res = new HorizontalSection(); var res = new HorizontalSection();
res.setTargetWidth(width / 2); 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; if (val <= 0) sender.invalid = true;
else { else {
progress.setDuration(val); progress.setDuration(val);

View File

@ -18,7 +18,7 @@ public class NumberInput extends Input {
return this; 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(parent, x, y, null);
super.action = (sender, val) -> { super.action = (sender, val) -> {
try { try {
@ -30,6 +30,9 @@ public class NumberInput extends Input {
} }
}; };
this.action = action; this.action = action;
setContent(""); setContent(Float.toString(value));
}
public NumberInput(ParentElement parent, int x, int y, InputAction action) {
this(parent, x, y, 0, action);
} }
} }