Version checker improved + locale-override option

This commit is contained in:
AppleDash 2017-07-15 17:02:04 -04:00
parent ca13e9a810
commit 31d311c229
5 changed files with 70 additions and 15 deletions

View File

@ -13,6 +13,7 @@ import org.appledash.sanelib.command.SaneCommand;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -49,6 +50,10 @@ public class SaneEconomy extends SanePlugin implements ISaneEconomy {
return; return;
} }
if (this.getConfig().getBoolean("locale-override", false)) {
Locale.setDefault(Locale.ENGLISH);
}
loadCommands(); loadCommands();
loadListeners(); loadListeners();

View File

@ -13,9 +13,10 @@ import org.appledash.saneeconomy.utils.WebUtils;
public class GithubVersionChecker { public class GithubVersionChecker {
public static final String DOWNLOAD_URL = "https://github.com/AppleDash/SaneEconomy/releases"; public static final String DOWNLOAD_URL = "https://github.com/AppleDash/SaneEconomy/releases";
private static final String RELEASES_URL = "https://api.github.com/repos/AppleDash/SaneEconomy/releases"; private static final String RELEASES_URL = "https://api.github.com/repos/AppleDash/SaneEconomy/releases";
private static String newestFound;
private boolean updateChecked; private boolean updateChecked;
private boolean updateAvailable; private boolean updateAvailable;
private static String newestVersion;
private final String pluginName; private final String pluginName;
private final String currentVersion; private final String currentVersion;
@ -29,9 +30,8 @@ public class GithubVersionChecker {
JsonArray array = (JsonArray)new JsonParser().parse(jsonContent); JsonArray array = (JsonArray)new JsonParser().parse(jsonContent);
int currentVersion = releaseToInt(this.currentVersion); String currentVersion = this.currentVersion;
int newestVersion = -1; String newestFound = null;
// JsonObject newestObj = null;
for (JsonElement elem : array) { for (JsonElement elem : array) {
if (elem instanceof JsonObject) { if (elem instanceof JsonObject) {
@ -49,22 +49,16 @@ public class GithubVersionChecker {
} }
String versionStr = releaseObj.get("tag_name").getAsString(); String versionStr = releaseObj.get("tag_name").getAsString();
int version = releaseToInt(versionStr);
if (version > newestVersion) { if (VersionComparer.isSemVerGreaterThan(newestFound, versionStr)) {
newestVersion = version; newestFound = versionStr;
GithubVersionChecker.newestVersion = versionStr; GithubVersionChecker.newestFound = versionStr;
// newestObj = releaseObj;
} }
} }
} }
updateChecked = true; updateChecked = true;
updateAvailable = newestVersion > currentVersion; updateAvailable = VersionComparer.isSemVerGreaterThan(currentVersion, newestFound);
}
private int releaseToInt(String release) {
return Integer.valueOf(release.trim().replace(".", ""));
} }
public boolean isUpdateAvailable() { public boolean isUpdateAvailable() {
@ -72,6 +66,6 @@ public class GithubVersionChecker {
} }
public String getNewestVersion() { public String getNewestVersion() {
return newestVersion; return newestFound;
} }
} }

View File

@ -0,0 +1,36 @@
package org.appledash.saneeconomy.updates;
/**
* Created by appledash on 7/15/17.
* Blackjack is best pony.
*/
public class VersionComparer {
public static boolean isSemVerGreaterThan(String first, String second) {
if (first == null) {
return true;
}
if (second == null) {
return false;
}
int[] firstParts = intifyParts(first);
int[] secondParts = intifyParts(second);
if (secondParts[0] > firstParts[0]) {
return true;
}
if (secondParts[1] > firstParts[1]) {
return true;
}
return secondParts[2] > firstParts[2];
}
private static int[] intifyParts(String version) {
String[] firstParts = version.split("\\.");
return new int[] { Integer.valueOf(firstParts[0]), Integer.valueOf(firstParts[1]), Integer.valueOf(firstParts[2]) };
}
}

View File

@ -18,4 +18,5 @@ economy:
notify-start-balance: true notify-start-balance: true
server-account: '$SERVER$' server-account: '$SERVER$'
locale-override: false
debug: false debug: false

View File

@ -0,0 +1,19 @@
package org.appledash.saneeconomy.test;
import org.appledash.saneeconomy.updates.VersionComparer;
import org.junit.Assert;
import org.junit.Test;
/**
* Created by appledash on 7/15/17.
* Blackjack is best pony.
*/
public class VersionComparerTest {
@Test
public void testVersionComparer() {
Assert.assertTrue(VersionComparer.isSemVerGreaterThan("0.12.6", "1.0.0"));
Assert.assertFalse(VersionComparer.isSemVerGreaterThan("2.0.0", "1.0.0"));
Assert.assertTrue(VersionComparer.isSemVerGreaterThan("0.1.0", "0.2.0"));
Assert.assertTrue(VersionComparer.isSemVerGreaterThan("1.0.0", "2.0.0"));
}
}