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

Commit

Permalink
FileManager: create version comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFaser committed Sep 7, 2023
1 parent 6400129 commit c1561e7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
50 changes: 44 additions & 6 deletions src/main/java/net/flectone/managers/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.flectone.Main;
import net.flectone.misc.files.FYamlConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
Expand All @@ -13,6 +14,7 @@

public class FileManager {

private static String lastVersion = "";
private static final String dataFolder = Main.getInstance().getDataFolder().getAbsolutePath() + File.separator;
private static final String languagesPath = "language" + File.separator;
private static final String iconsPath = "icons" + File.separator;
Expand All @@ -21,19 +23,22 @@ public class FileManager {

public static void initialize() {
config = load("config.yml");
lastVersion = config.getString("version");

String version = Main.getInstance().getDescription().getVersion();
String currentVersion = Main.getInstance().getDescription().getVersion();
boolean needMigrate = false;

if (!version.equals(config.getString("version"))) {
Main.warning("⚠ Your configs have been updated to " + version);
if (compareVersions(currentVersion, lastVersion) == 1) {
Main.warning("⚠ Your configs have been updated to " + currentVersion);

config.set("version", version);
config.set("version", currentVersion);
config.save();

migrate(config);
loadLocale(true);
} else loadLocale(false);
needMigrate = true;
}

loadLocale(needMigrate);
loadIcons();
}

Expand Down Expand Up @@ -100,4 +105,37 @@ private static void migrate(FYamlConfiguration oldFile) {

oldFile.save();
}

public static String getLastVersion() {
return lastVersion;
}

/**
* Compares two version strings and returns:<br>
* -1 if version1 is less than version2,<br>
* 0 if version1 is equal to version2, or<br>
* 1 if version1 is greater than version2.<br>
*
* @param version1 The first version string
* @param version2 The second version string
* @return -1, 0, or 1 based on the comparison result
*/
public static int compareVersions(@NotNull String version1, @NotNull String version2) {
if (version1.isEmpty()) return -1;
if (version2.isEmpty()) return 1;

String[] parts1 = version1.split("\\.");
String[] parts2 = version2.split("\\.");

for (int x = 0; x < parts1.length; x++) {
int num1 = Integer.parseInt(parts1[x]);
int num2 = Integer.parseInt(parts2[x]);


if (num1 > num2) return 1;
else if (num1 < num2) return -1;
}

return 0;
}
}
12 changes: 8 additions & 4 deletions src/main/java/net/flectone/sqlite/SQLite.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.flectone.sqlite;

import net.flectone.Main;
import net.flectone.managers.FileManager;
import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -118,10 +119,13 @@ public void load() {
s.executeUpdate("PRAGMA WAL_AUTOCHECKPOINT=100");
s.close();

DatabaseMetaData md = connection.getMetaData();
if (FileManager.getLastVersion().isEmpty()) {
DatabaseMetaData md = connection.getMetaData();
ResultSet rs = md.getColumns(null, null, "players", "mute_time");
if (!rs.next()) return;
}

ResultSet rs = md.getColumns(null, null, "players", "mute_time");
if (rs.next()) {
if (FileManager.compareVersions(FileManager.getLastVersion(), "3.10.0") == -1) {
setMigrate3_9_0(true);

File oldFile = new File(plugin.getDataFolder(), dbname + ".db");
Expand All @@ -134,7 +138,7 @@ public void load() {
config.save();
}

if (rs.getMetaData().getColumnCount() != 24) {
if (FileManager.compareVersions(FileManager.getLastVersion(), "3.10.2") == -1) {
setMigrate3_10_1(true);
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/flectone/utils/WebUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.flectone.utils;

import net.flectone.Main;
import net.flectone.managers.FileManager;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
Expand All @@ -26,12 +27,11 @@ public static void checkNewerVersion() {
try {
JSONArray json = (JSONArray) parser.parse(s);

String lastVersion = (String) ((JSONObject) json.get(0)).get("version_number");
String currentVersion = Main.getInstance().getDescription().getVersion();
boolean isOld = !currentVersion.equals(lastVersion);
String lastVersion = (String) ((JSONObject) json.get(0)).get("version_number");

if (isOld) {
Main.warning("⚠ Upgrade your " + currentVersion + " version of plugin to " + lastVersion);
if (FileManager.compareVersions(currentVersion, lastVersion) == -1) {
Main.warning("⚠ Upgrade your " + currentVersion + " version of plugin to " + lastVersion);
Main.warning("⚠ Url: https://modrinth.com/plugin/flectonechat/version/" + lastVersion);
}

Expand Down

0 comments on commit c1561e7

Please sign in to comment.