From 33fe846a42af5198dcdedeb06514c588f53cdcbf Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Sun, 7 Jan 2024 23:25:51 +0800 Subject: [PATCH] [ci skip] Leaves API version fetcher --- .../0004-Update-version-fetcher-repo.patch | 162 ++++++++++++------ 1 file changed, 113 insertions(+), 49 deletions(-) diff --git a/patches/server/0004-Update-version-fetcher-repo.patch b/patches/server/0004-Update-version-fetcher-repo.patch index 4505bec1..241aa43d 100644 --- a/patches/server/0004-Update-version-fetcher-repo.patch +++ b/patches/server/0004-Update-version-fetcher-repo.patch @@ -5,66 +5,130 @@ Subject: [PATCH] Update version fetcher repo diff --git a/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java -index 9d687da5bdf398bb3f6c84cdf1249a7213d09f2e..658f32456530cf8f291851da2789c8c3c3de6d27 100644 +index 9d687da5bdf398bb3f6c84cdf1249a7213d09f2e..fa446ff1b7284b58ee88566cbbe39d81545121ea 100644 --- a/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java +++ b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java -@@ -20,7 +20,7 @@ import java.util.stream.StreamSupport; - public class PaperVersionFetcher implements VersionFetcher { - private static final java.util.regex.Pattern VER_PATTERN = java.util.regex.Pattern.compile("^([0-9\\.]*)\\-.*R"); // R is an anchor, will always give '-R' at end - private static final String GITHUB_BRANCH_NAME = "master"; -- private static final String DOWNLOAD_PAGE = "https://papermc.io/downloads/paper"; -+ private static final String DOWNLOAD_PAGE = "https://leavesmc.top/downloads/leaves"; // Leaves - change - private static @Nullable String mcVer; +@@ -38,7 +38,7 @@ public class PaperVersionFetcher implements VersionFetcher { + return history != null ? TextComponent.ofChildren(updateMessage, Component.newline(), history) : updateMessage; + } + +- private static @Nullable String getMinecraftVersion() { ++ protected static @Nullable String getMinecraftVersion() { // Leaves - private -> protected + if (mcVer == null) { + java.util.regex.Matcher matcher = VER_PATTERN.matcher(org.bukkit.Bukkit.getBukkitVersion()); + if (matcher.find()) { +@@ -106,7 +106,7 @@ public class PaperVersionFetcher implements VersionFetcher { + } + + // Contributed by Techcable in GH-65 +- private static int fetchDistanceFromGitHub(@Nonnull String repo, @Nonnull String branch, @Nonnull String hash) { ++ protected static int fetchDistanceFromGitHub(@Nonnull String repo, @Nonnull String branch, @Nonnull String hash) { // Leaves - private -> protected + try { + HttpURLConnection connection = (HttpURLConnection) new URL("https://api.github.com/repos/" + repo + "/compare/" + branch + "..." + hash).openConnection(); + connection.connect(); +@@ -133,7 +133,7 @@ public class PaperVersionFetcher implements VersionFetcher { + } + + @Nullable +- private Component getHistory() { ++ protected Component getHistory() { // Leaves - private -> protected + final VersionHistoryManager.VersionData data = VersionHistoryManager.INSTANCE.getVersionData(); + if (data == null) { + return null; +diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +index 995ae8f2f76bf0255d7eac4190c5b961bfb17f24..ee26068689b9d35bb2c590dbd77fecd420dda28c 100644 +--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java ++++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +@@ -481,7 +481,7 @@ public final class CraftMagicNumbers implements UnsafeValues { @Override -@@ -31,8 +31,10 @@ public class PaperVersionFetcher implements VersionFetcher { - @Nonnull + public com.destroystokyo.paper.util.VersionFetcher getVersionFetcher() { +- return new com.destroystokyo.paper.PaperVersionFetcher(); ++ return new top.leavesmc.leaves.util.LeavesVersionFetcher(); // Leaves - Leaves version fetcher + } + @Override - public Component getVersionMessage(@Nonnull String serverVersion) { -- String[] parts = serverVersion.substring("git-Paper-".length()).split("[-\\s]"); -- final Component updateMessage = getUpdateStatusMessage("PaperMC/Paper", GITHUB_BRANCH_NAME, parts[0]); -+ // Leaves start - we dont have jenkins setup +diff --git a/src/main/java/top/leavesmc/leaves/util/LeavesVersionFetcher.java b/src/main/java/top/leavesmc/leaves/util/LeavesVersionFetcher.java +new file mode 100644 +index 0000000000000000000000000000000000000000..50b9aae7ea944bc8cbdb248c26f0245611a402bb +--- /dev/null ++++ b/src/main/java/top/leavesmc/leaves/util/LeavesVersionFetcher.java +@@ -0,0 +1,78 @@ ++package top.leavesmc.leaves.util; ++ ++import com.destroystokyo.paper.PaperVersionFetcher; ++import com.google.common.base.Charsets; ++import com.google.common.io.Resources; ++import com.google.gson.Gson; ++import com.google.gson.JsonArray; ++import com.google.gson.JsonObject; ++import com.google.gson.JsonSyntaxException; ++import net.kyori.adventure.text.Component; ++import net.kyori.adventure.text.TextComponent; ++import net.kyori.adventure.text.event.ClickEvent; ++import net.kyori.adventure.text.format.NamedTextColor; ++ ++import javax.annotation.Nonnull; ++import javax.annotation.Nullable; ++import java.io.BufferedReader; ++import java.io.IOException; ++import java.net.URL; ++import java.util.stream.StreamSupport; ++ ++public class LeavesVersionFetcher extends PaperVersionFetcher { ++ ++ private static final String DOWNLOAD_PAGE = "https://leavesmc.top/downloads/leaves"; ++ private static final String GITHUB_BRANCH_NAME = "master"; ++ ++ @Nonnull ++ @Override ++ public Component getVersionMessage(@Nonnull String serverVersion) { + String[] parts = serverVersion.substring("git-Leaves-".length()).split("[-\\s]"); + final Component updateMessage = getUpdateStatusMessage("LeavesMC/Leaves", GITHUB_BRANCH_NAME, parts[0]); -+ // Leaves end - we dont have jenkins setup - final Component history = getHistory(); - - return history != null ? TextComponent.ofChildren(updateMessage, Component.newline(), history) : updateMessage; -@@ -56,14 +58,12 @@ public class PaperVersionFetcher implements VersionFetcher { - - private static Component getUpdateStatusMessage(@Nonnull String repo, @Nonnull String branch, @Nonnull String versionInfo) { - int distance; -- try { -- int jenkinsBuild = Integer.parseInt(versionInfo); -- distance = fetchDistanceFromSiteApi(jenkinsBuild, getMinecraftVersion()); -- } catch (NumberFormatException ignored) { -- versionInfo = versionInfo.replace("\"", ""); -- distance = fetchDistanceFromGitHub(repo, branch, versionInfo); -- } - -+ // Leaves start - we dont have jenkins setup ++ final Component history = getHistory(); ++ ++ return history != null ? TextComponent.ofChildren(updateMessage, Component.newline(), history) : updateMessage; ++ } ++ ++ private static int fetchDistanceFromLeavesApiV2(String mcVersion, String hash) { ++ try { ++ try (BufferedReader reader = Resources.asCharSource( ++ new URL("https://api.leavesmc.top/v2/projects/leaves/versions/" + mcVersion + "/differ/" + hash), ++ Charsets.UTF_8 ++ ).openBufferedStream()) { ++ return Integer.parseInt(reader.readLine()); ++ } ++ } catch (IOException e) { ++ e.printStackTrace(); ++ throw new NumberFormatException(); ++ } ++ } ++ ++ private static Component getUpdateStatusMessage(@Nonnull String repo, @Nonnull String branch, @Nonnull String versionInfo) { ++ int distance; ++ + versionInfo = versionInfo.replace("\"", ""); -+ distance = fetchDistanceFromGitHub(repo, branch, versionInfo); -+ // Leaves end - we dont have jenkins setup -+ - switch (distance) { - case -1: - return Component.text("Error obtaining version information", NamedTextColor.YELLOW); -@@ -73,11 +73,12 @@ public class PaperVersionFetcher implements VersionFetcher { - return Component.text("Unknown version", NamedTextColor.YELLOW); - default: - return Component.text("You are " + distance + " version(s) behind", NamedTextColor.YELLOW) -- .append(Component.newline()) -- .append(Component.text("Download the new version at: ") -- .append(Component.text(DOWNLOAD_PAGE, NamedTextColor.GOLD) -- .hoverEvent(Component.text("Click to open", NamedTextColor.WHITE)) -- .clickEvent(ClickEvent.openUrl(DOWNLOAD_PAGE)))); ++ try { ++ distance = fetchDistanceFromLeavesApiV2(getMinecraftVersion(), versionInfo); ++ } catch (NumberFormatException ignored) { ++ distance = fetchDistanceFromGitHub(repo, branch, versionInfo); ++ } ++ ++ switch (distance) { ++ case -1: ++ return Component.text("Error obtaining version information", NamedTextColor.YELLOW); ++ case 0: ++ return Component.text("You are running the latest version", NamedTextColor.GREEN); ++ case -2: ++ return Component.text("Unknown version", NamedTextColor.YELLOW); ++ default: ++ return Component.text("You are " + distance + " version(s) behind", NamedTextColor.YELLOW) + .append(Component.newline()) + .append(Component.text("Download the new version at: ") + .append(Component.text(DOWNLOAD_PAGE, NamedTextColor.GOLD) + .hoverEvent(Component.text("Click to open", NamedTextColor.WHITE)) + .clickEvent(ClickEvent.openUrl(DOWNLOAD_PAGE)))); + - } - } - ++ } ++ } ++}