Refactored update checker

This commit is contained in:
extendedclip 2018-04-20 23:01:01 -04:00
parent 763c0aabc1
commit 3a5e3ed950
2 changed files with 55 additions and 76 deletions

View File

@ -84,7 +84,7 @@ public class PlaceholderAPIPlugin extends JavaPlugin {
getLogger().info("Placeholder expansion registration initializing..."); getLogger().info("Placeholder expansion registration initializing...");
expansionManager.registerAllExpansions(); expansionManager.registerAllExpansions();
if (config.checkUpdates()) { if (config.checkUpdates()) {
new UpdateChecker(this); new UpdateChecker(this).fetch();
} }
if (config.isCloudEnabled()) { if (config.isCloudEnabled()) {
enableCloud(); enableCloud();

View File

@ -1,8 +1,8 @@
package me.clip.placeholderapi.updatechecker; package me.clip.placeholderapi.updatechecker;
import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.util.Msg;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -12,94 +12,73 @@ import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.util.regex.Pattern;
/**
* thanks maxim
*/
public class UpdateChecker implements Listener { public class UpdateChecker implements Listener {
private PlaceholderAPIPlugin plugin; private PlaceholderAPIPlugin plugin;
private final int RESOURCE_ID = 6245;
private final int resourceId = 6245; private String spigotVersion, pluginVersion;
private boolean updateAvailable;
private static String latestVersion = "";
private static boolean updateAvailable = false;
public UpdateChecker(PlaceholderAPIPlugin i) { public UpdateChecker(PlaceholderAPIPlugin i) {
plugin = i; plugin = i;
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { pluginVersion = i.getDescription().getVersion();
if (checkForUpdate()) { }
Bukkit.getScheduler().runTask(plugin, () -> {
plugin.getLogger().info("An update for PlaceholderAPI (v" + getLatestVersion() + ") is available at:"); public boolean hasUpdateAvailable() { return updateAvailable; }
plugin.getLogger().info("https://www.spigotmc.org/resources/placeholderapi." + resourceId + "/");
register(); 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() { private boolean spigotIsNewer() {
try { if (spigotVersion == null || spigotVersion.isEmpty()) return false;
HttpsURLConnection con = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=" + resourceId).openConnection(); String plV = toReadable(pluginVersion);
con.setRequestMethod("GET"); String spV = toReadable(spigotVersion);
String version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); return plV.compareTo(spV) < 0;
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 String toReadable(String version) { private String toReadable(String version) {
String[] split = Pattern.compile(".", Pattern.LITERAL).split(version.replace("v", "")); if (version.contains("-DEV-")) {
version = ""; version = version.split("-DEV-")[0];
for (String s : split) { }
version += String.format("%4s", s); return version.replaceAll("\\.","");
}
return version;
} }
@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 + "/");
}
}
} }