Skip to content

Commit

Permalink
Don't crash on empty config, try to fix config writing
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Jan 7, 2025
1 parent d716e9c commit e0acbb4
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public AbstractSave(final String name) {
public void init() {
if (Files.exists(path)) {
try {
read(GSON.fromJson(Files.readString(path), JsonObject.class));
final JsonObject object = GSON.fromJson(Files.readString(path), JsonObject.class);
if (object != null) {
read(object);
} else {
ViaFabricPlusImpl.INSTANCE.logger().error("The file {} is empty!", path.getFileName());
}
} catch (IOException e) {
ViaFabricPlusImpl.INSTANCE.logger().error("Failed to read file: {}!", path.getFileName(), e);
}
Expand All @@ -69,7 +74,8 @@ public void save() {
final JsonObject object = new JsonObject();
write(object);

Files.writeString(path, GSON.toJson(object), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
Files.deleteIfExists(path);
Files.writeString(path, GSON.toJson(object), StandardOpenOption.CREATE);
} catch (IOException e) {
ViaFabricPlusImpl.INSTANCE.logger().error("Failed to write file: {}!", path.getFileName(), e);
}
Expand Down

0 comments on commit e0acbb4

Please sign in to comment.