|
| 1 | +package com.zetaplugins.zetacore.services.updatechecker; |
| 2 | + |
| 3 | +import org.bukkit.plugin.java.JavaPlugin; |
| 4 | +import org.json.simple.JSONArray; |
| 5 | +import org.json.simple.JSONObject; |
| 6 | +import org.json.simple.parser.JSONParser; |
| 7 | + |
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStreamReader; |
| 11 | +import java.net.HttpURLConnection; |
| 12 | +import java.net.URL; |
| 13 | +import java.net.URLEncoder; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | + |
| 16 | +public class HangarUpdateChecker extends UpdateChecker { |
| 17 | + private final String slugOrId; |
| 18 | + private final String ownerString; |
| 19 | + |
| 20 | + /** |
| 21 | + * Constructs a HangarUpdateChecker for the given plugin and Hangar slug or ID. |
| 22 | + * @param plugin The JavaPlugin instance |
| 23 | + * @param ownerString The Hangar owner string |
| 24 | + * @param slugOrId Hangar project slug or ID |
| 25 | + */ |
| 26 | + public HangarUpdateChecker(JavaPlugin plugin, String ownerString, String slugOrId) { |
| 27 | + super(plugin); |
| 28 | + this.ownerString = ownerString; |
| 29 | + this.slugOrId = slugOrId; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void checkForUpdates(boolean logMessage) { |
| 34 | + String latestVersion = fetchLatestVersion(); |
| 35 | + if (latestVersion == null) return; |
| 36 | + |
| 37 | + String currentVersion = getPlugin().getDescription().getVersion(); |
| 38 | + if (hangarVersionIsNewer(latestVersion, currentVersion)) { |
| 39 | + setNewVersionAvailable(true); |
| 40 | + setLatestVersion(latestVersion); |
| 41 | + |
| 42 | + getLogger().info(getNewVersionConsoleMessage( |
| 43 | + latestVersion, |
| 44 | + currentVersion, |
| 45 | + getVersionUrl(latestVersion) |
| 46 | + )); |
| 47 | + } else { |
| 48 | + setNewVersionAvailable(false); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getLatestVersionUrl() { |
| 54 | + return getVersionUrl(getLatestVersion()); |
| 55 | + } |
| 56 | + |
| 57 | + public String getVersionUrl(String version) { |
| 58 | + return "https://hangar.papermc.io/" + ownerString + "/" + slugOrId + "/versions/" + version; |
| 59 | + } |
| 60 | + |
| 61 | + private boolean hangarVersionIsNewer(String latestVersion, String currentVersion) { |
| 62 | + try { |
| 63 | + SemanticVersion latest = new SemanticVersion(latestVersion); |
| 64 | + SemanticVersion current = new SemanticVersion(currentVersion); |
| 65 | + return latest.isGreaterThan(current); |
| 66 | + } catch (IllegalArgumentException e) { |
| 67 | + // fallback to string comparison if parsing fails |
| 68 | + return !latestVersion.trim().equals(currentVersion.trim()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private String fetchLatestVersion() { |
| 73 | + String mcVersion = getPlugin().getServer().getMinecraftVersion(); |
| 74 | + String encodedGameVersion = URLEncoder.encode(mcVersion, StandardCharsets.UTF_8); |
| 75 | + |
| 76 | + String versionsUrl = "https://hangar.papermc.io/api/v1/projects/" + slugOrId + "/versions?platform=PAPER&platformVersion=" + encodedGameVersion; |
| 77 | + |
| 78 | + JSONObject json = fetchJsonObjectFromUrl(versionsUrl); |
| 79 | + if (json == null) return null; |
| 80 | + |
| 81 | + JSONArray results = (JSONArray) json.get("result"); |
| 82 | + if (results == null || results.isEmpty()) return null; |
| 83 | + |
| 84 | + JSONObject latest = (JSONObject) results.get(0); |
| 85 | + return (String) latest.get("name"); |
| 86 | + } |
| 87 | + |
| 88 | + private JSONObject fetchJsonObjectFromUrl(String urlString) { |
| 89 | + try { |
| 90 | + HttpURLConnection connection = createHttpConnection(urlString); |
| 91 | + if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { |
| 92 | + String response = readResponse(connection); |
| 93 | + return (JSONObject) new JSONParser().parse(response); |
| 94 | + } else { |
| 95 | + getLogger().warning("Failed to retrieve data from " + urlString + |
| 96 | + " Response code: " + connection.getResponseCode()); |
| 97 | + } |
| 98 | + } catch (IOException | org.json.simple.parser.ParseException e) { |
| 99 | + getLogger().warning("Error fetching data: " + e.getMessage()); |
| 100 | + } |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Creates an HTTP connection to the specified URL. |
| 106 | + * @param urlString The URL to connect to. |
| 107 | + * @return An HttpURLConnection object for the specified URL. |
| 108 | + * @throws IOException If an I/O error occurs while opening the connection. |
| 109 | + */ |
| 110 | + private HttpURLConnection createHttpConnection(String urlString) throws IOException { |
| 111 | + URL url = new URL(urlString); |
| 112 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 113 | + connection.setRequestMethod("GET"); |
| 114 | + connection.setRequestProperty("Accept", "application/json"); |
| 115 | + return connection; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Reads the response from the given HttpURLConnection. |
| 120 | + * @param connection The HttpURLConnection to read the response from. |
| 121 | + * @return The response as a String. |
| 122 | + * @throws IOException If an I/O error occurs while reading the response. |
| 123 | + */ |
| 124 | + private String readResponse(HttpURLConnection connection) throws IOException { |
| 125 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { |
| 126 | + StringBuilder response = new StringBuilder(); |
| 127 | + String line; |
| 128 | + while ((line = reader.readLine()) != null) { |
| 129 | + response.append(line); |
| 130 | + } |
| 131 | + return response.toString(); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments