Skip to content

Commit

Permalink
Fix Hangar update checker
Browse files Browse the repository at this point in the history
Not tested!
  • Loading branch information
srnyx committed Jun 16, 2024
1 parent 93d7025 commit 6f6e2d5
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/main/java/xyz/srnyx/annoyingapi/AnnoyingUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -16,6 +17,9 @@
import xyz.srnyx.javautilities.objects.SemanticVersion;
import xyz.srnyx.javautilities.parents.Stringable;

import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;


Expand Down Expand Up @@ -163,17 +167,48 @@ private String modrinth(@NotNull String identifier) {
*/
@Nullable
private String hangar(@NotNull PluginPlatform platform) {
final JsonElement json = HttpUtility.getJson(userAgent, "https://hangar.papermc.io/api/v1/projects/" + platform.author + "/" + platform.identifier + "/versions?limit=1&offset=0&platform=PAPER&platformVersion=" + AnnoyingPlugin.MINECRAFT_VERSION.version);
final JsonElement json = HttpUtility.getJson(userAgent, "https://hangar.papermc.io/api/v1/projects/" + platform.author + "/" + platform.identifier + "/versions");

// Request failed
if (json == null) return fail(PluginPlatform.Platform.HANGAR);

// Get versions
final JsonArray result = json.getAsJsonObject().get("result").getAsJsonArray();
if (result.size() == 0) return fail(PluginPlatform.Platform.HANGAR);
// Get supported versions
final Map<String, OffsetDateTime> result = new HashMap<>();
final String minecraftVersion = AnnoyingPlugin.MINECRAFT_VERSION.version;
try {
for (final JsonElement versionElement : json.getAsJsonObject().get("versions").getAsJsonArray()) {
final JsonObject version = versionElement.getAsJsonObject();
final JsonObject platforms = version.getAsJsonObject("platformDependencies");
if (platforms == null) continue;
final JsonArray paper = platforms.getAsJsonArray("paper");
if (paper != null) for (final JsonElement paperElement : paper) {
final String paperVersion = paperElement.getAsString();
if (paperVersion.equals(minecraftVersion)) {
final String name = version.get("name").getAsString();
final OffsetDateTime createdAt = OffsetDateTime.parse(version.get("createdAt").getAsString());

// If it's a duplicate version, keep the latest
final OffsetDateTime existing = result.get(name);
if (existing != null) {
if (createdAt.isAfter(existing)) result.put(name, createdAt);
break;
}

// Add the version to results
result.put(name, createdAt);
break;
}
}
}
} catch (final Exception e) {
return fail(PluginPlatform.Platform.HANGAR);
}

// Return the latest version
return result.get(0).getAsJsonObject().get("name").getAsString();
// Get the latest version
return result.entrySet().stream()
.max(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.orElse(fail(PluginPlatform.Platform.HANGAR));
}

/**
Expand Down

0 comments on commit 6f6e2d5

Please sign in to comment.