85 lines
3.2 KiB
Java
85 lines
3.2 KiB
Java
package de.nexuslobby.utils;
|
|
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
import com.google.gson.JsonSyntaxException;
|
|
import de.nexuslobby.NexusLobby;
|
|
import org.bukkit.Bukkit;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.function.Consumer;
|
|
|
|
public class UpdateChecker {
|
|
|
|
private final NexusLobby plugin;
|
|
private static final String API_URL = "https://git.viper.ipv64.net/api/v1/repos/M_Viper/NexusLobby/releases/latest";
|
|
private static final int TIMEOUT_MS = 5000;
|
|
|
|
public UpdateChecker(NexusLobby plugin) {
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
public void getVersion(final Consumer<String> consumer) {
|
|
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
|
|
HttpURLConnection connection = null;
|
|
try {
|
|
URL url = new URL(API_URL);
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
connection.setConnectTimeout(TIMEOUT_MS);
|
|
connection.setReadTimeout(TIMEOUT_MS);
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
connection.setRequestProperty("User-Agent", "NexusLobby-UpdateChecker");
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
if (responseCode != HttpURLConnection.HTTP_OK) {
|
|
plugin.getLogger().warning("Update-Check fehlgeschlagen: HTTP " + responseCode);
|
|
return;
|
|
}
|
|
|
|
try (BufferedReader reader = new BufferedReader(
|
|
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
|
|
|
StringBuilder response = new StringBuilder();
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
response.append(line);
|
|
}
|
|
|
|
parseAndNotify(response.toString(), consumer);
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
plugin.getLogger().warning("Update-Check fehlgeschlagen: " + e.getMessage());
|
|
} finally {
|
|
if (connection != null) {
|
|
connection.disconnect();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void parseAndNotify(String jsonResponse, Consumer<String> consumer) {
|
|
try {
|
|
JsonObject json = JsonParser.parseString(jsonResponse).getAsJsonObject();
|
|
|
|
if (json.has("tag_name")) {
|
|
String version = json.get("tag_name").getAsString();
|
|
// Entferne 'v' Präfix falls vorhanden (z.B. v1.0.0 -> 1.0.0)
|
|
if (version.startsWith("v")) {
|
|
version = version.substring(1);
|
|
}
|
|
consumer.accept(version);
|
|
} else {
|
|
plugin.getLogger().warning("Update-Check: 'tag_name' nicht in API-Response gefunden");
|
|
}
|
|
} catch (JsonSyntaxException e) {
|
|
plugin.getLogger().warning("Update-Check: Ungültiges JSON-Format - " + e.getMessage());
|
|
}
|
|
}
|
|
} |