Rewrite the update checker
This commit is contained in:
parent
17ee5dc97b
commit
11a1547242
@ -393,37 +393,84 @@ public class AuthMe extends JavaPlugin {
|
|||||||
private static final String owner = "HaHaWTH";
|
private static final String owner = "HaHaWTH";
|
||||||
private static final String owner_gitee = "Shixuehan114514";
|
private static final String owner_gitee = "Shixuehan114514";
|
||||||
private static final String repo = "AuthMeReReloaded";
|
private static final String repo = "AuthMeReReloaded";
|
||||||
private void checkForUpdates() {
|
// private void checkForUpdates() {
|
||||||
logger.info("Checking for updates...");
|
// logger.info("Checking for updates...");
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
|
// Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
|
||||||
try {
|
// try {
|
||||||
// 从南通集线器获取最新版本号
|
// // 从南通集线器获取最新版本号
|
||||||
|
//
|
||||||
|
// URL url = new URL("https://api.github.com/repos/" + owner + "/" + repo + "/releases/latest");
|
||||||
|
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
// conn.setConnectTimeout(10000); // 设置连接超时为10秒
|
||||||
|
// conn.setReadTimeout(10000); // 设置读取超时为10秒
|
||||||
|
// Scanner scanner = new Scanner(conn.getInputStream());
|
||||||
|
// String response = scanner.useDelimiter("\\Z").next();
|
||||||
|
// scanner.close();
|
||||||
|
//
|
||||||
|
// // 处理JSON响应
|
||||||
|
// String latestVersion = response.substring(response.indexOf("tag_name") + 11);
|
||||||
|
// latestVersion = latestVersion.substring(0, latestVersion.indexOf("\""));
|
||||||
|
// if ((pluginBuild + pluginBuildNumber).equals(latestVersion)) {
|
||||||
|
// getLogger().log(Level.INFO,"You are running the latest version.");
|
||||||
|
// }
|
||||||
|
// if (!(pluginBuild + pluginBuildNumber).equals(latestVersion)) {
|
||||||
|
// // Display update message
|
||||||
|
// String message = "New version available! Latest:" + latestVersion + " Current:" + pluginBuild + pluginBuildNumber;
|
||||||
|
// getLogger().log(Level.INFO, message);
|
||||||
|
// getLogger().log(Level.INFO,"Download from here:github.com/HaHaWTH/AuthMeReReloaded/releases/latest");
|
||||||
|
// }
|
||||||
|
// }catch (IOException e) {
|
||||||
|
// getLogger().log(Level.WARNING,"Error occurred while checking updates from GitHub. Reason: " + e.getMessage());
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
private void checkForUpdates() {
|
||||||
|
logger.info("Checking for updates...");
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
|
||||||
|
try {
|
||||||
|
// 从南通集线器获取最新版本号
|
||||||
|
URL url = new URL("https://api.github.com/repos/" + owner + "/" + repo + "/releases/latest");
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setConnectTimeout(10000); // 设置连接超时为10秒
|
||||||
|
conn.setReadTimeout(10000); // 设置读取超时为10秒
|
||||||
|
Scanner scanner = new Scanner(conn.getInputStream());
|
||||||
|
String response = scanner.useDelimiter("\\Z").next();
|
||||||
|
scanner.close();
|
||||||
|
|
||||||
URL url = new URL("https://api.github.com/repos/" + owner + "/" + repo + "/releases/latest");
|
// 处理JSON响应
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
String latestVersion = response.substring(response.indexOf("tag_name") + 11);
|
||||||
conn.setConnectTimeout(10000); // 设置连接超时为10秒
|
latestVersion = latestVersion.substring(0, latestVersion.indexOf("\""));
|
||||||
conn.setReadTimeout(10000); // 设置读取超时为10秒
|
if (isUpdateAvailable(latestVersion)) {
|
||||||
Scanner scanner = new Scanner(conn.getInputStream());
|
String message = "New version available! Latest:" + latestVersion + " Current:" + pluginBuild + pluginBuildNumber;
|
||||||
String response = scanner.useDelimiter("\\Z").next();
|
getLogger().log(Level.INFO, message);
|
||||||
scanner.close();
|
getLogger().log(Level.INFO, "Download from here: github.com/HaHaWTH/AuthMeReReloaded/releases/latest");
|
||||||
|
} else {
|
||||||
// 处理JSON响应
|
getLogger().log(Level.INFO, "You are running the latest version.");
|
||||||
String latestVersion = response.substring(response.indexOf("tag_name") + 11);
|
}
|
||||||
latestVersion = latestVersion.substring(0, latestVersion.indexOf("\""));
|
} catch (IOException e) {
|
||||||
if ((pluginBuild + pluginBuildNumber).equals(latestVersion)) {
|
getLogger().log(Level.WARNING, "Error occurred while checking updates from GitHub. Reason: " + e.getMessage());
|
||||||
getLogger().log(Level.INFO,"You are running the latest version.");
|
|
||||||
}
|
|
||||||
if (!(pluginBuild + pluginBuildNumber).equals(latestVersion)) {
|
|
||||||
// Display update message
|
|
||||||
String message = "New version available! Latest:" + latestVersion + " Current:" + pluginBuild + pluginBuildNumber;
|
|
||||||
getLogger().log(Level.INFO, message);
|
|
||||||
getLogger().log(Level.INFO,"Download from here:github.com/HaHaWTH/AuthMeReReloaded/releases/latest");
|
|
||||||
}
|
|
||||||
}catch (IOException e) {
|
|
||||||
getLogger().log(Level.WARNING,"Error occurred while checking updates from GitHub. Reason: " + e.getMessage());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
private boolean isUpdateAvailable(String latestVersion) {
|
||||||
|
// Extract the first character and the remaining digits from the version string
|
||||||
|
char latestChar = latestVersion.charAt(0);
|
||||||
|
int latestNumber = Integer.parseInt(latestVersion.substring(1));
|
||||||
|
|
||||||
|
char currentChar = pluginBuild.charAt(0);
|
||||||
|
int currentNumber = Integer.parseInt(pluginBuildNumber);
|
||||||
|
|
||||||
|
// Compare the characters first
|
||||||
|
if (latestChar > currentChar) {
|
||||||
|
return true;
|
||||||
|
} else if (latestChar < currentChar) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
// If the characters are the same, compare the numbers
|
||||||
|
return latestNumber > currentNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void checkServerType() {
|
private void checkServerType() {
|
||||||
if (isClassLoaded("com.destroystokyo.paper.PaperConfig")) {
|
if (isClassLoaded("com.destroystokyo.paper.PaperConfig")) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user