mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI.git
synced 2024-11-16 07:35:32 +01:00
Refactored update checker
This commit is contained in:
parent
763c0aabc1
commit
3a5e3ed950
@ -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();
|
||||||
|
@ -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:");
|
|
||||||
plugin.getLogger().info("https://www.spigotmc.org/resources/placeholderapi." + resourceId + "/");
|
|
||||||
register();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
private boolean spigotIsNewer() {
|
||||||
Bukkit.getPluginManager().registerEvents(this, plugin);
|
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) {
|
||||||
|
if (version.contains("-DEV-")) {
|
||||||
|
version = version.split("-DEV-")[0];
|
||||||
|
}
|
||||||
|
return version.replaceAll("\\.","");
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.MONITOR)
|
@EventHandler(priority = EventPriority.MONITOR)
|
||||||
public void onJoin(PlayerJoinEvent e) {
|
public void onJoin(PlayerJoinEvent e) {
|
||||||
if (e.getPlayer().hasPermission("placeholderapi.updatenotify")) {
|
if (e.getPlayer().hasPermission("placeholderapi.updatenotify")) {
|
||||||
e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', "&bAn update for &fPlaceholder&7API &e(&fPlaceholder&7API &fv" + getLatestVersion() + "&e)"));
|
Msg.msg(e.getPlayer(), "&bAn update for &fPlaceholder&7API &e(&fPlaceholder&7API &fv" + getSpigotVersion() + "&e)"
|
||||||
e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', "&bis available at &ehttps://www.spigotmc.org/resources/placeholderapi." + resourceId + "/"));
|
, "&bis available at &ehttps://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + "/");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user