Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Plugin: add support for config migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFaser committed Nov 8, 2023
1 parent cafba21 commit 05eff4c
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/main/java/net/flectone/chat/manager/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -45,6 +49,22 @@ public static void init() {
}

loadIcons();

checkMigration();
}

public static void checkMigration() {
String fileVersion = config.getString("plugin.version");
String projectVersion = FlectoneChat.getInstance().getDescription().getVersion();

if (compareVersions(fileVersion, projectVersion) == -1) {
Arrays.stream(Type.values()).forEach(Type::update);
} else return;

config.set("plugin.version", projectVersion);
config.save();

FlectoneChat.warning("Your configs have been updated to " + projectVersion + " version");
}

public static FConfiguration loadFile(String filePath) {
Expand Down Expand Up @@ -139,5 +159,29 @@ public FConfiguration load() {
this.file = loadFile(filePath + fileName + ".yml");
return file;
}

public void update() {
if (this.file == null) return;
InputStream inputStream = FlectoneChat.getInstance().getResource(file.getResourceFilePath().replace('\\', '/'));

if (inputStream == null) return;

InputStreamReader defConfigStream = new InputStreamReader(inputStream, StandardCharsets.UTF_8);

YamlConfiguration resourceFile = YamlConfiguration.loadConfiguration(defConfigStream);

resourceFile.getKeys(true).parallelStream()
.filter(string -> {
if (!file.contains(string)) return true;

Object objectA = file.get(string);
Object objectB = resourceFile.get(string);

return objectA != null && objectB != null && !objectA.getClass().equals(objectB.getClass());
})
.forEach(string -> file.set(string, resourceFile.get(string)));

file.save();
}
}
}

0 comments on commit 05eff4c

Please sign in to comment.