Skip to content

Commit

Permalink
fix local default config version not properly write to json
Browse files Browse the repository at this point in the history
  • Loading branch information
frank89722 committed Sep 22, 2021
1 parent 2014455 commit a028abc
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/frankv/jmi/config/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ClientConfig() {
builder.pop();

builder.push("JourneyMap Default Config");
defaultConfigVersion = builder.comment("When local JM default config version is older than `defaultConfigVersion`, it will copy everything under `/config/jmdefaultconfig/` to `/journeymap/` and replace the existing files. Set to -1 to disable.")
defaultConfigVersion = builder.comment("When local JM default config version is older than `defaultConfigVersion` it will copy everything under `/config/jmdefaultconfig/` to `/journeymap/` and replace the existing files. Set to -1 to disable.")
.defineInRange("defaultConfigVersion", -1, -1, Integer.MAX_VALUE);
builder.pop();
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/frankv/jmi/jmdefaultconfig/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class FileManager {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static File versionFile = new File(System.getProperty("user.dir") + "/journeymap/defaultconfig.json");

public static Version readConfigVersion() throws IOException {
public static Version readConfigVersion() {
Version version;
try (FileReader fileReader = new FileReader(versionFile)) {
version = GSON.fromJson(fileReader, Version.class);
Expand All @@ -26,9 +26,6 @@ public static Version readConfigVersion() throws IOException {
} catch (JsonParseException | NullPointerException | IOException e) {
version = new Version(-1);
versionFile.getParentFile().mkdirs();
try (FileWriter fileWriter = new FileWriter(versionFile)) {
GSON.toJson(version, fileWriter);
}
}
return version;
}
Expand All @@ -38,6 +35,8 @@ private static void updateVersionFile(int newVersion) throws IOException {
v = new Version(newVersion);
FileWriter fileWriter = new FileWriter(versionFile);
GSON.toJson(v, fileWriter);
fileWriter.flush();
fileWriter.close();
}

public static void writeJMDefaultConfig(int newVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

import frankv.jmi.JMI;

import java.io.IOException;

public class JMDefualtConfig {
private static Version existVersion;

public static void tryWriteJMDefaultConfig() {
try {
existVersion = FileManager.readConfigVersion();
} catch (IOException e) {
JMI.LOGGER.error("Failed to read existed default config version.");
return;
}
existVersion = FileManager.readConfigVersion();

if (existVersion.version < JMI.CLIENT_CONFIG.getDefaultConfigVersion()) {
FileManager.writeJMDefaultConfig(JMI.CLIENT_CONFIG.getDefaultConfigVersion());
Expand Down

0 comments on commit a028abc

Please sign in to comment.