Update from Git Manager GUI
This commit is contained in:
@@ -1,18 +1,24 @@
|
||||
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.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Scanner;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class UpdateChecker {
|
||||
|
||||
private final NexusLobby plugin;
|
||||
// URL zur Gitea API für das neueste Release
|
||||
private final String url = "https://git.viper.ipv64.net/api/v1/repos/M_Viper/NexusLobby/releases/latest";
|
||||
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;
|
||||
@@ -20,23 +26,60 @@ public class UpdateChecker {
|
||||
|
||||
public void getVersion(final Consumer<String> consumer) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
|
||||
try (InputStream inputStream = new URL(url).openStream(); Scanner scanner = new Scanner(inputStream)) {
|
||||
StringBuilder response = new StringBuilder();
|
||||
while (scanner.hasNextLine()) {
|
||||
response.append(scanner.nextLine());
|
||||
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;
|
||||
}
|
||||
|
||||
String content = response.toString();
|
||||
// Einfaches Parsing des JSON "tag_name" Feldes
|
||||
if (content.contains("\"tag_name\":")) {
|
||||
String version = content.split("\"tag_name\":\"")[1].split("\"")[0];
|
||||
// Entferne ein eventuelles 'v' Präfix (z.B. v1.0.0 -> 1.0.0)
|
||||
version = version.replace("v", "");
|
||||
consumer.accept(version);
|
||||
|
||||
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();
|
||||
}
|
||||
} catch (IOException exception) {
|
||||
this.plugin.getLogger().warning("Update-Check fehlgeschlagen: " + exception.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user