From 2fe0da569b5323551c293997cb9e6e6b3a3a98e9 Mon Sep 17 00:00:00 2001 From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> Date: Thu, 30 May 2024 23:53:08 +0800 Subject: [PATCH] Cleanup --- src/main/java/fr/xephi/authme/AuthMe.java | 60 +++-------------- .../executable/authme/SpawnCommand.java | 4 +- .../java/fr/xephi/authme/task/Updater.java | 67 +++++++++++++++++++ 3 files changed, 76 insertions(+), 55 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/task/Updater.java diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 3bec3edb..b6d830e4 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -44,6 +44,7 @@ import fr.xephi.authme.settings.properties.EmailSettings; import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.task.CleanupTask; +import fr.xephi.authme.task.Updater; import fr.xephi.authme.task.purge.PurgeService; import fr.xephi.authme.util.ExceptionUtils; import org.bukkit.Server; @@ -55,15 +56,10 @@ import org.jetbrains.annotations.NotNull; import javax.inject.Inject; import java.io.File; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Objects; -import java.util.Scanner; import java.util.function.Consumer; -import java.util.logging.Level; import static fr.xephi.authme.service.BukkitService.TICKS_PER_MINUTE; import static fr.xephi.authme.util.Utils.isClassLoaded; @@ -224,8 +220,6 @@ public class AuthMe extends JavaPlugin { // Purge on start if enabled PurgeService purgeService = injector.getSingleton(PurgeService.class); purgeService.runAutoPurge(); - // 注册玩家加入事件监听 -// register3rdPartyListeners(); logger.info("GitHub: https://github.com/HaHaWTH/AuthMeReReloaded/"); if (settings.getProperty(SecuritySettings.CHECK_FOR_UPDATES)) { checkForUpdates(); @@ -233,8 +227,6 @@ public class AuthMe extends JavaPlugin { } - //Migrated - /** * Load the version and build number of the plugin from the description file. * @@ -422,55 +414,19 @@ public class AuthMe extends JavaPlugin { ConsoleLogger.closeFileWriter(); } - private static final String owner = "HaHaWTH"; -// private static final String owner_gitee = "Shixuehan114514"; - private static final String repo = "AuthMeReReloaded"; - private void checkForUpdates() { logger.info("Checking for updates..."); + Updater updater = new Updater(pluginBuild + pluginBuildNumber); bukkitService.runTaskAsynchronously(() -> { - 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 (isUpdateAvailable(latestVersion)) { - String message = "New version available! Latest:" + latestVersion + " Current:" + pluginBuild + pluginBuildNumber; - getLogger().log(Level.WARNING, message); - getLogger().log(Level.WARNING, "Download from here: https://github.com/HaHaWTH/AuthMeReReloaded/releases/latest"); - } else { - getLogger().log(Level.INFO, "You are running the latest version."); - } - } catch (IOException ignored) { + if (updater.isUpdateAvailable()) { + String message = "New version available! Latest:" + updater.getLatestVersion() + " Current:" + pluginBuild + pluginBuildNumber; + logger.warning(message); + logger.warning("Download from here: https://github.com/HaHaWTH/AuthMeReReloaded/releases/latest"); + } else { + logger.info("You are running the latest version."); } }); } - 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() { diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/SpawnCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/SpawnCommand.java index a506c8f4..92ad0a30 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/SpawnCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/SpawnCommand.java @@ -21,9 +21,7 @@ public class SpawnCommand extends PlayerCommand { if (spawnLoader.getSpawn() == null) { player.sendMessage("[AuthMe] Spawn has failed, please try to define the spawn"); } else { - bukkitService.runTaskIfFolia(player, () -> { - TeleportUtils.teleport(player, spawnLoader.getSpawn()); - }); + bukkitService.runTaskIfFolia(player, () -> TeleportUtils.teleport(player, spawnLoader.getSpawn())); } } } diff --git a/src/main/java/fr/xephi/authme/task/Updater.java b/src/main/java/fr/xephi/authme/task/Updater.java new file mode 100644 index 00000000..5616d88a --- /dev/null +++ b/src/main/java/fr/xephi/authme/task/Updater.java @@ -0,0 +1,67 @@ +package fr.xephi.authme.task; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URL; +import java.util.Scanner; + +public class Updater { + private final String currentVersion; + private String latestVersion; + private static boolean isUpdateAvailable = false; + private static final String owner = "HaHaWTH"; + private static final String repo = "AuthMeReReloaded"; + private static final String UPDATE_URL = "https://api.github.com/repos/" + owner + "/" + repo + "/releases/latest"; + + public Updater(String currentVersion) { + this.currentVersion = currentVersion; + } + + + /** + * Check if there is an update available + * Note: This method will perform a network request! + * + * @return true if there is an update available, false otherwise + */ + public boolean isUpdateAvailable() { + URI uri = URI.create(UPDATE_URL); + try { + URL url = uri.toURL(); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setConnectTimeout(10000); + conn.setReadTimeout(10000); + Scanner scanner = new Scanner(conn.getInputStream()); + String response = scanner.useDelimiter("\\Z").next(); + scanner.close(); + String latestVersion = response.substring(response.indexOf("tag_name") + 11); + latestVersion = latestVersion.substring(0, latestVersion.indexOf("\"")); + this.latestVersion = latestVersion; + isUpdateAvailable = !currentVersion.equals(latestVersion); + return isUpdateAvailable; + } catch (IOException ignored) { + this.latestVersion = null; + isUpdateAvailable = false; + return false; + } + } + + public String getLatestVersion() { + return latestVersion; + } + + public String getCurrentVersion() { + return currentVersion; + } + + /** + * Returns true if there is an update available, false otherwise + * Must be called after {@link Updater#isUpdateAvailable()} + * + * @return A boolean indicating whether there is an update available + */ + public static boolean hasUpdate() { + return isUpdateAvailable; + } +}