9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-28 11:29:11 +00:00

Cleanup version fetcher

This commit is contained in:
Dreeam
2025-08-25 18:22:41 -04:00
parent 9e0b3a2093
commit 8b64f4dd42
3 changed files with 66 additions and 54 deletions

View File

@@ -220,7 +220,7 @@ index 94a9ed024d3859793618152ea559a168bbcbb5e2..e60008693e017bec1b4eb49c84be3898
-<http://www.gnu.org/philosophy/why-not-lgpl.html>.
+<https://www.gnu.org/philosophy/why-not-lgpl.html>.
diff --git a/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java
index d0554ed6631535815e5932930911e3fe1dee8710..07b4e19ea672b38b8127da30b4111e903274f944 100644
index d0554ed6631535815e5932930911e3fe1dee8710..b307827f6ae1da2151edb89adccd4d36df544963 100644
--- a/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java
+++ b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java
@@ -9,6 +9,7 @@ import com.google.gson.JsonObject;
@@ -239,7 +239,7 @@ index d0554ed6631535815e5932930911e3fe1dee8710..07b4e19ea672b38b8127da30b4111e90
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
@@ -30,60 +32,36 @@ import org.slf4j.Logger;
@@ -30,117 +32,22 @@ import org.slf4j.Logger;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.format.TextColor.color;
@@ -254,22 +254,13 @@ index d0554ed6631535815e5932930911e3fe1dee8710..07b4e19ea672b38b8127da30b4111e90
- private static final int DISTANCE_ERROR = -1;
- private static final int DISTANCE_UNKNOWN = -2;
- private static final String DOWNLOAD_PAGE = "https://papermc.io/downloads/paper";
+public class PaperVersionFetcher extends org.galemc.gale.version.AbstractPaperVersionFetcher {
-
- @Override
- public long getCacheTime() {
- return 720000;
+ public PaperVersionFetcher() {
+ super(
+ "https://papermc.io/downloads/paper",
+ "PaperMC",
+ "Paper",
+ "PaperMC",
+ "Paper"
+ );
}
@Override
- }
-
- @Override
- public Component getVersionMessage() {
- final Component updateMessage;
- final ServerBuildInfo build = ServerBuildInfo.buildInfo();
@@ -281,10 +272,8 @@ index d0554ed6631535815e5932930911e3fe1dee8710..07b4e19ea672b38b8127da30b4111e90
- final @Nullable Component history = this.getHistory();
-
- return history != null ? Component.textOfChildren(updateMessage, Component.newline(), history) : updateMessage;
+ protected boolean canFetchDistanceFromSiteApi() {
+ return true;
}
- }
-
- private static Component getUpdateStatusMessage(final String repo, final ServerBuildInfo build) {
- int distance = DISTANCE_ERROR;
-
@@ -310,21 +299,30 @@ index d0554ed6631535815e5932930911e3fe1dee8710..07b4e19ea672b38b8127da30b4111e90
- .hoverEvent(text("Click to open", NamedTextColor.WHITE))
- .clickEvent(ClickEvent.openUrl(DOWNLOAD_PAGE))));
- };
+ @Override
+ protected int fetchDistanceFromSiteApi(int jenkinsBuild) {
+ return fetchDistanceFromPaperApi(ServerBuildInfo.buildInfo(), jenkinsBuild);
}
+ // Gale end - branding changes - version fetcher
- }
-
- private static int fetchDistanceFromSiteApi(final ServerBuildInfo build, final int jenkinsBuild) {
+ private static int fetchDistanceFromPaperApi(final ServerBuildInfo build, final int jenkinsBuild) {
try {
try (final BufferedReader reader = Resources.asCharSource(
URI.create("https://api.papermc.io/v2/projects/paper/versions/" + build.minecraftVersionId()).toURL(),
@@ -105,42 +83,4 @@ public class PaperVersionFetcher implements VersionFetcher {
return DISTANCE_ERROR;
}
}
- try {
- try (final BufferedReader reader = Resources.asCharSource(
- URI.create("https://api.papermc.io/v2/projects/paper/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 Paper's downloads API", ex);
- return DISTANCE_ERROR;
- }
- } catch (final IOException e) {
- LOGGER.error("Error while parsing version", e);
- return DISTANCE_ERROR;
- }
- }
-
- // Contributed by Techcable <Techcable@outlook.com> in GH-65
- private static int fetchDistanceFromGitHub(final String repo, final String branch, final String hash) {
@@ -362,7 +360,18 @@ index d0554ed6631535815e5932930911e3fe1dee8710..07b4e19ea672b38b8127da30b4111e90
- }
-
- return text("Previous version: " + oldVersion, NamedTextColor.GRAY, TextDecoration.ITALIC);
- }
+public class PaperVersionFetcher extends org.galemc.gale.version.AbstractPaperVersionFetcher {
+
+ public PaperVersionFetcher() {
+ super(
+ "https://papermc.io/downloads/paper",
+ "PaperMC",
+ "Paper",
+ "PaperMC",
+ "Paper"
+ );
}
+ // Gale end - branding changes - version fetcher
}
diff --git a/src/main/java/com/destroystokyo/paper/console/PaperConsole.java b/src/main/java/com/destroystokyo/paper/console/PaperConsole.java
index 6ee39b534b8d992655bc0cef3c299d12cbae0034..f59879ae7d3c73cd4c4233d30667988bf22f12f1 100644

View File

@@ -13,6 +13,8 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.StreamSupport;
public class LeafVersionFetcher extends AbstractPaperVersionFetcher {
@@ -28,16 +30,20 @@ public class LeafVersionFetcher extends AbstractPaperVersionFetcher {
}
@Override
protected boolean canFetchDistanceFromSiteApi() {
return true;
protected int fetchDistanceFromAPI(final String repo, final ServerBuildInfo build) {
int distance = DISTANCE_ERROR;
final Optional<String> gitBranch = build.gitBranch();
final Optional<String> gitCommit = build.gitCommit();
final OptionalInt buildNumber = build.buildNumber();
if (gitBranch.isPresent() && gitCommit.isPresent() && buildNumber.isPresent()) {
distance = fetchDistanceFromLeafApi(build, buildNumber.getAsInt());
}
return distance;
}
@Override
protected int fetchDistanceFromSiteApi(int jenkinsBuild) {
return fetchDistanceFromLeafApi(ServerBuildInfo.buildInfo(), jenkinsBuild);
}
private static int fetchDistanceFromLeafApi(final ServerBuildInfo build, final int jenkinsBuild) {
private static int fetchDistanceFromLeafApi(final ServerBuildInfo build, final int current) {
try {
try (final BufferedReader reader = Resources.asCharSource(
URI.create("https://api.leafmc.one/v2/projects/leaf/versions/" + build.minecraftVersionId()).toURL(),
@@ -49,7 +55,7 @@ public class LeafVersionFetcher extends AbstractPaperVersionFetcher {
.mapToInt(JsonElement::getAsInt)
.max()
.orElseThrow();
return latest - jenkinsBuild;
return latest - current;
} catch (final JsonSyntaxException ex) {
LOGGER.error("Error parsing json from Leaf's downloads API", ex);
return DISTANCE_ERROR;

View File

@@ -77,16 +77,7 @@ public abstract class AbstractPaperVersionFetcher implements VersionFetcher {
}
// Gale start - branding changes - version fetcher
protected boolean canFetchDistanceFromSiteApi() {
return false;
}
protected int fetchDistanceFromSiteApi(int jenkinsBuild) {
return -1;
}
// Gale end - branding changes - version fetcher
private Component getUpdateStatusMessage(final String repo, final ServerBuildInfo build) {
protected int fetchDistanceFromAPI(final String repo, final ServerBuildInfo build) {
int distance = DISTANCE_ERROR;
// Gale start - branding changes - version fetcher
@@ -95,7 +86,13 @@ public abstract class AbstractPaperVersionFetcher implements VersionFetcher {
if (gitBranch.isPresent() && gitCommit.isPresent()) {
distance = fetchDistanceFromGitHub(repo, gitBranch.get(), gitCommit.get());
}
// Gale end - branding changes - version fetcher
return distance;
}
// Gale end - branding changes - version fetcher
private Component getUpdateStatusMessage(final String repo, final ServerBuildInfo build) {
int distance = fetchDistanceFromAPI(repo, build); // Gale - branding changes - version fetcher
return switch (distance) {
case DISTANCE_ERROR -> text("* Error obtaining version information", NamedTextColor.RED); // Purpur - Rebrand