64 lines
2.7 KiB
Java
64 lines
2.7 KiB
Java
package com.adzel.velocitybroadcast.util;
|
|
|
|
import com.velocitypowered.api.proxy.ProxyServer;
|
|
import net.kyori.adventure.text.Component;
|
|
import com.velocitypowered.api.proxy.Player;
|
|
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.logging.Logger;
|
|
import org.json.JSONObject;
|
|
import org.json.JSONTokener;
|
|
|
|
public class UpdateChecker {
|
|
private static final String ENDPOINT = "https://api.github.com/repos/AdzelFirestar/velocitybroadcast-reborn/releases/latest";
|
|
private final String currentVersion;
|
|
private final Logger logger;
|
|
|
|
public UpdateChecker(String currentVersion, Logger logger) {
|
|
this.currentVersion = currentVersion;
|
|
this.logger = logger;
|
|
}
|
|
|
|
public CompletableFuture<String> checkForUpdate() {
|
|
return CompletableFuture.supplyAsync(() -> {
|
|
try {
|
|
HttpURLConnection connection = (HttpURLConnection) new URL(ENDPOINT).openConnection();
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
connection.setConnectTimeout(3000);
|
|
connection.setReadTimeout(3000);
|
|
|
|
try (InputStreamReader reader = new InputStreamReader(connection.getInputStream())) {
|
|
JSONObject json = new JSONObject(new JSONTokener(reader));
|
|
String latestVersion = json.getString("tag_name").trim();
|
|
|
|
if (!latestVersion.equalsIgnoreCase(currentVersion)) {
|
|
logger.info("[VelocityBroadcast] Update available: " + latestVersion + " (you are on " + currentVersion + ")");
|
|
return latestVersion;
|
|
} else {
|
|
logger.info("[VelocityBroadcast] You are running the latest version (" + currentVersion + ").");
|
|
return null;
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
logger.warning("[VelocityBroadcast] Failed to check for updates: " + e.getMessage());
|
|
return null;
|
|
}
|
|
});
|
|
}
|
|
|
|
public void notifyPlayerIfOutdated(ProxyServer server, String latestVersion) {
|
|
if (latestVersion != null && !latestVersion.equalsIgnoreCase(currentVersion)) {
|
|
String message = String.format("[VelocityBroadcast] A new version (%s) is available! You are running %s.", latestVersion, currentVersion);
|
|
Component component = Component.text(message);
|
|
for (Player player : server.getAllPlayers()) {
|
|
if (player.hasPermission("vb.admin")) {
|
|
player.sendMessage(component);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|