9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-25 09:59:15 +00:00

Fetch versions from Leaf download api

This commit is contained in:
Dreeam
2025-08-25 17:30:22 -04:00
parent f5b95a6716
commit 52f643398c
2 changed files with 53 additions and 4 deletions

View File

@@ -1,7 +1,20 @@
package org.dreeam.leaf.version;
import com.google.common.io.Resources;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import io.papermc.paper.ServerBuildInfo;
import org.galemc.gale.version.AbstractPaperVersionFetcher;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.stream.StreamSupport;
public class LeafVersionFetcher extends AbstractPaperVersionFetcher {
public LeafVersionFetcher() {
@@ -13,4 +26,37 @@ public class LeafVersionFetcher extends AbstractPaperVersionFetcher {
"Leaf"
);
}
@Override
protected boolean canFetchDistanceFromSiteApi() {
return true;
}
@Override
protected int fetchDistanceFromSiteApi(int jenkinsBuild) {
return fetchDistanceFromLeafApi(ServerBuildInfo.buildInfo(), jenkinsBuild);
}
private static int fetchDistanceFromLeafApi(final ServerBuildInfo build, final int jenkinsBuild) {
try {
try (final BufferedReader reader = Resources.asCharSource(
URI.create("https://api.leafmc.one/v2/projects/leaf/versions/" + build.minecraftVersionId()).toURL(),
StandardCharsets.UTF_8
).openBufferedStream()) {
final JsonObject json = new Gson().fromJson(reader, JsonObject.class);
final JsonArray builds = json.getAsJsonArray("builds");
final int latest = StreamSupport.stream(builds.spliterator(), false)
.mapToInt(JsonElement::getAsInt)
.max()
.orElseThrow();
return latest - jenkinsBuild;
} catch (final JsonSyntaxException ex) {
LOGGER.error("Error parsing json from Leaf's downloads API", ex);
return DISTANCE_ERROR;
}
} catch (final IOException e) {
LOGGER.error("Error while parsing version", e);
return DISTANCE_ERROR;
}
}
}