chore: Save and load duration from config file
This commit is contained in:
parent
a3d7f9463b
commit
23d289f9f7
@ -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);
|
||||
|
@ -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> animation;
|
||||
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 = "";
|
||||
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> animation, Manager<Ease> ease) {
|
||||
public ConfigManager(File configFile, Manager<Animation> animation, Manager<Ease> ease, ProgressManager progress) {
|
||||
this.configFile = configFile;
|
||||
this.animation = animation;
|
||||
this.ease = ease;
|
||||
this.progress = progress;
|
||||
|
||||
reload();
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user