mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-19 14:59:32 +00:00
135 lines
6.5 KiB
Diff
135 lines
6.5 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: violetc <58360096+s-yh-china@users.noreply.github.com>
|
|
Date: Tue, 26 Oct 2021 14:13:50 +0800
|
|
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..fa446ff1b7284b58ee88566cbbe39d81545121ea 100644
|
|
--- a/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java
|
|
+++ b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java
|
|
@@ -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 <Techcable@outlook.com> 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 3fb07dae586c5511cb048b20264072bf6db54df8..008a3da71071128c6302d881ed51d1d837e17e6f 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
|
|
@@ -496,7 +496,7 @@ public final class CraftMagicNumbers implements UnsafeValues {
|
|
|
|
@Override
|
|
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
|
|
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..18d104d4dc1da05affc67acbfc3c81c981d2a75b
|
|
--- /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.org/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]);
|
|
+ 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.org/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("\"", "");
|
|
+ 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))));
|
|
+
|
|
+ }
|
|
+ }
|
|
+}
|