From 3a5e3ed950d6ca9364adf11d4bf5b43d500215b5 Mon Sep 17 00:00:00 2001 From: extendedclip Date: Fri, 20 Apr 2018 23:01:01 -0400 Subject: [PATCH] Refactored update checker --- .../placeholderapi/PlaceholderAPIPlugin.java | 2 +- .../updatechecker/UpdateChecker.java | 129 ++++++++---------- 2 files changed, 55 insertions(+), 76 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/PlaceholderAPIPlugin.java b/src/main/java/me/clip/placeholderapi/PlaceholderAPIPlugin.java index 293d637..72d803a 100644 --- a/src/main/java/me/clip/placeholderapi/PlaceholderAPIPlugin.java +++ b/src/main/java/me/clip/placeholderapi/PlaceholderAPIPlugin.java @@ -84,7 +84,7 @@ public class PlaceholderAPIPlugin extends JavaPlugin { getLogger().info("Placeholder expansion registration initializing..."); expansionManager.registerAllExpansions(); if (config.checkUpdates()) { - new UpdateChecker(this); + new UpdateChecker(this).fetch(); } if (config.isCloudEnabled()) { enableCloud(); diff --git a/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java b/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java index 6bbc0fb..04e1c2f 100644 --- a/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java +++ b/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java @@ -1,8 +1,8 @@ package me.clip.placeholderapi.updatechecker; import me.clip.placeholderapi.PlaceholderAPIPlugin; +import me.clip.placeholderapi.util.Msg; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -12,94 +12,73 @@ import javax.net.ssl.HttpsURLConnection; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; -import java.util.regex.Pattern; -/** - * thanks maxim - */ public class UpdateChecker implements Listener { private PlaceholderAPIPlugin plugin; - - private final int resourceId = 6245; - - private static String latestVersion = ""; - - private static boolean updateAvailable = false; + private final int RESOURCE_ID = 6245; + private String spigotVersion, pluginVersion; + private boolean updateAvailable; public UpdateChecker(PlaceholderAPIPlugin i) { plugin = i; - Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { - if (checkForUpdate()) { - Bukkit.getScheduler().runTask(plugin, () -> { - plugin.getLogger().info("An update for PlaceholderAPI (v" + getLatestVersion() + ") is available at:"); - plugin.getLogger().info("https://www.spigotmc.org/resources/placeholderapi." + resourceId + "/"); - register(); - }); + pluginVersion = i.getDescription().getVersion(); + } + + public boolean hasUpdateAvailable() { return updateAvailable; } + + public String getSpigotVersion() { + return spigotVersion; + } + + public void fetch() { + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + try { + HttpsURLConnection con = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID).openConnection(); + con.setRequestMethod("GET"); + spigotVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); + } catch (Exception ex) { + plugin.getLogger().info("Failed to check for updates on spigot."); + return; } + + if (spigotVersion == null || spigotVersion.isEmpty()) { + return; + } + + updateAvailable = spigotIsNewer(); + + if (!updateAvailable) { + return; + } + + Bukkit.getScheduler().runTask(plugin, () -> { + plugin.getLogger().info("An update for PlaceholderAPI (v" + getSpigotVersion() + ") is available at:"); + plugin.getLogger().info("https://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + "/"); + Bukkit.getPluginManager().registerEvents(this, plugin); + }); }); } - - private void register() { - Bukkit.getPluginManager().registerEvents(this, plugin); - } - - @EventHandler(priority = EventPriority.MONITOR) - public void onJoin(PlayerJoinEvent e) { - if (e.getPlayer().hasPermission("placeholderapi.updatenotify")) { - e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', "&bAn update for &fPlaceholder&7API &e(&fPlaceholder&7API &fv" + getLatestVersion() + "&e)")); - e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', "&bis available at &ehttps://www.spigotmc.org/resources/placeholderapi." + resourceId + "/")); - } - } - private String getSpigotVersion() { - try { - HttpsURLConnection con = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=" + resourceId).openConnection(); - con.setRequestMethod("GET"); - String version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); - if (version.length() <= 7) { - return version; - } - } catch (Exception ex) { - plugin.getLogger().info("Failed to check for a update on spigot."); - } - return null; - } - - private boolean checkHigher(String currentVersion, String newVersion) { - String current = toReadable(currentVersion); - String newVers = toReadable(newVersion); - return current.compareTo(newVers) < 0; - } - - public boolean checkForUpdate() { - String version = getSpigotVersion(); - if (version != null) { - if (checkHigher(plugin.getDescription().getVersion(), version)) { - latestVersion = version; - updateAvailable = true; - return true; - } - } - return false; - } - - public static boolean updateAvailable() { - return updateAvailable; - } - - public static String getLatestVersion() { - return latestVersion; + private boolean spigotIsNewer() { + if (spigotVersion == null || spigotVersion.isEmpty()) return false; + String plV = toReadable(pluginVersion); + String spV = toReadable(spigotVersion); + return plV.compareTo(spV) < 0; } private String toReadable(String version) { - String[] split = Pattern.compile(".", Pattern.LITERAL).split(version.replace("v", "")); - version = ""; - for (String s : split) { - version += String.format("%4s", s); - } - return version; + if (version.contains("-DEV-")) { + version = version.split("-DEV-")[0]; + } + return version.replaceAll("\\.",""); } - + @EventHandler(priority = EventPriority.MONITOR) + public void onJoin(PlayerJoinEvent e) { + if (e.getPlayer().hasPermission("placeholderapi.updatenotify")) { + Msg.msg(e.getPlayer(), "&bAn update for &fPlaceholder&7API &e(&fPlaceholder&7API &fv" + getSpigotVersion() + "&e)" + , "&bis available at &ehttps://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + "/"); + } + } }