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...");
expansionManager.registerAllExpansions();
if (config.checkUpdates()) {
new UpdateChecker(this);
new UpdateChecker(this).fetch();
}
if (config.isCloudEnabled()) {
enableCloud();

View File

@ -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 + "/");
}
}
}