From 40d804f971c0693155e3fd1973d812601fe960be Mon Sep 17 00:00:00 2001 From: masmc05 Date: Mon, 19 Feb 2024 22:26:30 +0200 Subject: [PATCH] Multiple changes --- .../0463-Add-paper-version-util-class.patch | 85 +++++++--- .../1046-Add-paper-version-util-class.patch | 158 +++++++++++++++--- 2 files changed, 198 insertions(+), 45 deletions(-) diff --git a/patches/api/0463-Add-paper-version-util-class.patch b/patches/api/0463-Add-paper-version-util-class.patch index da69f4a694..6002781af0 100644 --- a/patches/api/0463-Add-paper-version-util-class.patch +++ b/patches/api/0463-Add-paper-version-util-class.patch @@ -6,14 +6,16 @@ Subject: [PATCH] Add paper version util class diff --git a/src/main/java/io/papermc/paper/util/PaperServerInfo.java b/src/main/java/io/papermc/paper/util/PaperServerInfo.java new file mode 100644 -index 0000000000000000000000000000000000000000..b882b328e87cffa06c32fa6618afeab8d08ab0a1 +index 0000000000000000000000000000000000000000..36d71c598529c77ee0c1012cabfa0ebd4a1692db --- /dev/null +++ b/src/main/java/io/papermc/paper/util/PaperServerInfo.java -@@ -0,0 +1,73 @@ +@@ -0,0 +1,98 @@ +package io.papermc.paper.util; + ++import net.kyori.adventure.key.Key; +import net.kyori.adventure.util.Services; +import org.bukkit.UnsafeValues; ++import org.jetbrains.annotations.NotNull; + +/** + * A utility class to get information about the server @@ -24,11 +26,12 @@ index 0000000000000000000000000000000000000000..b882b328e87cffa06c32fa6618afeab8 + throw new UnsupportedOperationException("This class cannot be instantiated"); + } + private static final PaperServerInfoProvider provider = Services.service(PaperServerInfoProvider.class).orElseThrow(); ++ + /** + * Get the version of the server -+ * @return the version of the server (e.g. "1.20.4") ++ * @return the version of the server (e.g. "1.20.4", "1.20.2 Pre-release 2", "23w31a") + */ -+ public static String version() { ++ public static @NotNull String version() { + return provider.version(); + } + @@ -36,7 +39,7 @@ index 0000000000000000000000000000000000000000..b882b328e87cffa06c32fa6618afeab8 + * Get the api version of the server + * @return the api version of the server (e.g. "1.20.4-R0.1-SNAPSHOT") + */ -+ public static String apiVersion() { ++ public static @NotNull String apiVersion() { + return provider.apiVersion(); + } + @@ -44,53 +47,76 @@ index 0000000000000000000000000000000000000000..b882b328e87cffa06c32fa6618afeab8 + * Get the name of the server + * @return the name of the server (e.g. "Paper") + */ -+ public static String serverName() { ++ public static @NotNull String serverName() { + return provider.serverName(); + } ++ ++ /** ++ * Returns true if the server is a minecraft release version, ++ * false is it's a snapshot, pre-release, etc ++ * @return if the server is stable ++ */ ++ public static boolean isStable() { ++ return provider.isStable(); ++ } ++ + /** + * Returns the unsafe values for the server for unsafe version values + * @return the unsafe values for the server + */ + @Deprecated ++ @NotNull + public static UnsafeValues unsafe() { + return provider.unsafe(); + } + + /** + * Checks if the server runs exactly the specified version ++ *

++ * Note: Will always return false on non release versions + * @param version the version to check (e.g. 1, 20, 4) + * @return true if the server runs exactly the specified version + */ -+ public static boolean is(int... version) { ++ public static boolean is(int @NotNull... version) { + return provider.is(version); + } + + /** -+ * Checks if the server runs at least the specified version ++ * Checks if the server runs exactly the specified version ++ * @param version the version to check (e.g. "1.20.4", "1.20.2 Pre-release 2", "1.20.2-pre2", "23w31a") ++ * @return true if the server runs exactly the specified version ++ */ ++ public static boolean is(@NotNull String version) { ++ return provider.is(version); ++ } ++ ++ /** ++ * Checks if the server runs a version which is after this version or the same + * @param version the version to check (e.g. 1, 20, 4) + * @return true if the server runs on this version or a newer version + */ -+ public static boolean isAtLeast(int... version) { ++ public static boolean isAtLeast(int @NotNull... version) { + return provider.isAtLeast(version); + } + + /** -+ * Checks if the server implements the specified API -+ * @param api the API to check (e.g. "Folia"), case insensitive -+ * @return true if the server implements the specified API ++ * Checks if the server is implementing the specified API ++ * @param api the API to check (e.g. "papermc:folia") ++ * @return true if the server is implementing the specified API + */ -+ public static boolean implement(String api) { -+ return provider.implement(api); ++ public static boolean isImplementing(@NotNull Key api) { ++ return provider.isImplementing(api); + } +} diff --git a/src/main/java/io/papermc/paper/util/PaperServerInfoProvider.java b/src/main/java/io/papermc/paper/util/PaperServerInfoProvider.java new file mode 100644 -index 0000000000000000000000000000000000000000..fbb138623f93fd6e8fe534cf1cf339657f4b5418 +index 0000000000000000000000000000000000000000..79ce6cfafc9d05b696c7bab1d84c8a5455acf162 --- /dev/null +++ b/src/main/java/io/papermc/paper/util/PaperServerInfoProvider.java -@@ -0,0 +1,56 @@ +@@ -0,0 +1,72 @@ +package io.papermc.paper.util; + ++import net.kyori.adventure.key.Key; +import org.bukkit.UnsafeValues; +import org.jetbrains.annotations.NotNull; + @@ -100,7 +126,7 @@ index 0000000000000000000000000000000000000000..fbb138623f93fd6e8fe534cf1cf33965 +public interface PaperServerInfoProvider { + /** + * Get the version of the server -+ * @return the version of the server (e.g. "1.20.4") ++ * @return the version of the server (e.g. "1.20.4", "1.20.2 Pre-release 2", "23w31a") + */ + @NotNull String version(); + @@ -125,23 +151,38 @@ index 0000000000000000000000000000000000000000..fbb138623f93fd6e8fe534cf1cf33965 + UnsafeValues unsafe(); + + /** ++ * Returns true if the server is a release version, false otherwise ++ * @return if the server is stable ++ */ ++ boolean isStable(); ++ ++ /** + * Checks if the server runs exactly the specified version ++ *

++ * Note: Will always return false on non release versions + * @param version the version to check (e.g. 1, 20, 4) + * @return true if the server runs exactly the specified version + */ + boolean is(int @NotNull... version); + + /** -+ * Checks if the server runs at least the specified version ++ * Checks if the server runs exactly the specified version ++ * @param version the version to check (e.g. "1.20.4", "1.20.2 Pre-release 2", "1.20.2-pre2", "23w31a") ++ * @return true if the server runs exactly the specified version ++ */ ++ boolean is(@NotNull String version); ++ ++ /** ++ * Checks if the server runs a version which is after this version or the same + * @param version the version to check (e.g. 1, 20, 4) + * @return true if the server runs on this version or a newer version + */ + boolean isAtLeast(int @NotNull... version); + + /** -+ * Checks if the server implements the specified API -+ * @param api the API to check (e.g. "Folia"), case insensitive -+ * @return true if the server implements the specified API ++ * Checks if the server is implementing the specified API ++ * @param api the API to check (e.g. "papermc:folia") ++ * @return true if the server is implementing the specified API + */ -+ boolean implement(@NotNull String api); ++ boolean isImplementing(@NotNull Key api); +} diff --git a/patches/server/1046-Add-paper-version-util-class.patch b/patches/server/1046-Add-paper-version-util-class.patch index 516141c034..c679870553 100644 --- a/patches/server/1046-Add-paper-version-util-class.patch +++ b/patches/server/1046-Add-paper-version-util-class.patch @@ -6,35 +6,59 @@ Subject: [PATCH] Add paper version util class diff --git a/src/main/java/io/papermc/paper/util/misc/PaperServerInfoProviderImpl.java b/src/main/java/io/papermc/paper/util/misc/PaperServerInfoProviderImpl.java new file mode 100644 -index 0000000000000000000000000000000000000000..4a105d428778b95232e8acb1bb3d09a6c4e94477 +index 0000000000000000000000000000000000000000..d85ba01b83c2f29b1b56775d0f51f6b1d182252c --- /dev/null +++ b/src/main/java/io/papermc/paper/util/misc/PaperServerInfoProviderImpl.java -@@ -0,0 +1,83 @@ +@@ -0,0 +1,195 @@ +package io.papermc.paper.util.misc; + +import com.google.common.base.Preconditions; ++import com.google.common.collect.ImmutableList; ++import com.google.gson.Gson; ++import com.google.gson.JsonParser; ++import com.google.gson.stream.JsonReader; ++import com.mojang.logging.LogUtils; +import io.papermc.paper.util.PaperServerInfoProvider; ++import net.kyori.adventure.key.Key; ++import net.kyori.adventure.key.KeyPattern; +import net.minecraft.SharedConstants; +import org.bukkit.UnsafeValues; +import org.bukkit.craftbukkit.util.CraftMagicNumbers; +import org.bukkit.craftbukkit.util.Versioning; +import org.jetbrains.annotations.NotNull; ++import org.slf4j.Logger; ++import java.io.IOException; ++import java.net.URI; ++import java.net.http.HttpClient; ++import java.net.http.HttpRequest; ++import java.net.http.HttpResponse; ++import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; ++import java.util.List; +import java.util.Locale; ++import java.util.Objects; ++import java.util.Optional; +import java.util.Set; +import java.util.stream.Stream; + +public class PaperServerInfoProviderImpl implements PaperServerInfoProvider { + private final String bukkitVersion = Versioning.getBukkitVersion(); -+ private final Set implementedAPIs; ++ private final Set implementedAPIs; + private final String serverName; -+ private final int[] version; ++ private final VersionInfo version; + public PaperServerInfoProviderImpl() { + ServerImplementationInfo info = new ServerImplementationInfo(); + this.serverName = info.latestName; + this.implementedAPIs = info.implementedAPIs; -+ this.version = Stream.of(this.version().split("\\.")).mapToInt(Integer::parseInt).toArray(); ++ if (SharedConstants.getCurrentVersion().isStable()) { ++ // Release versions, best supported ++ this.version = new StableVersionInfo(); ++ } else { ++ // Snapshot, pre-release, etc ++ // Supported the best we can ++ this.version = new NonStableVersionInfo(); ++ } + } + @Override + public @NotNull String version() { @@ -58,38 +82,126 @@ index 0000000000000000000000000000000000000000..4a105d428778b95232e8acb1bb3d09a6 + } + + @Override ++ public boolean isStable() { ++ return SharedConstants.getCurrentVersion().isStable(); ++ } ++ ++ @Override + public boolean is(final int @NotNull ... version) { -+ return Arrays.equals(this.version, version); ++ return this.version.is(version); ++ } ++ ++ @Override ++ public boolean is(@NotNull final String version) { ++ Preconditions.checkNotNull(version, "The version cannot be null"); ++ return this.version.is(version); + } + + @Override + public boolean isAtLeast(final int @NotNull ... version) { -+ int maxLen = Math.max(this.version.length, version.length); -+ for (int i = 0; i < maxLen; i++) { -+ int a = i < this.version.length ? this.version[i] : 0; -+ int b = i < version.length ? version[i] : 0; -+ if (a != b) { -+ return a > b; -+ } -+ } -+ return true; ++ return this.version.isAtLeast(version); + } + + @Override -+ public boolean implement(final @NotNull String api) { ++ public boolean isImplementing(final @NotNull Key api) { + Preconditions.checkNotNull(api, "The API cannot be null"); -+ return this.implementedAPIs.contains(api.toUpperCase(Locale.ROOT)); ++ return this.implementedAPIs.contains(api); + } + private static class ServerImplementationInfo { -+ private final Set implementedAPIs = new HashSet<>(); ++ private final Set implementedAPIs = new HashSet<>(); + private String latestName; + private ServerImplementationInfo() { -+ this.add("Vanilla"); -+ this.add("Paper"); ++ this.add("minecraft", "Vanilla"); ++ this.add("papermc", "Paper"); + } -+ private void add(String api) { -+ this.implementedAPIs.add(api.toUpperCase(Locale.ROOT)); -+ this.latestName = api; ++ @SuppressWarnings("PatternValidation") ++ private void add(@KeyPattern.Namespace String namespace, String name) { ++ this.implementedAPIs.add(Key.key(namespace, name.toLowerCase(Locale.ROOT))); ++ this.latestName = name; ++ } ++ } ++ private interface VersionInfo { ++ ++ boolean is(int @NotNull ... version); ++ ++ boolean is(@NotNull String version); ++ ++ boolean isAtLeast(int @NotNull ... version); ++ } ++ private static class StableVersionInfo implements VersionInfo { ++ private final String version = SharedConstants.getCurrentVersion().getId(); ++ private final int[] versionArr = Stream.of(this.version.split("\\.")).mapToInt(Integer::parseInt).toArray(); ++ @Override ++ public boolean is(final int @NotNull ... version) { ++ return Arrays.equals(this.versionArr, version); ++ } ++ ++ @Override ++ public boolean is(@NotNull final String version) { ++ return Objects.equals(this.version, version); ++ } ++ ++ @Override ++ public boolean isAtLeast(final int @NotNull ... version) { ++ int maxLen = Math.max(this.versionArr.length, version.length); ++ for (int i = 0; i < maxLen; i++) { ++ int our = i < this.versionArr.length ? this.versionArr[i] : 0; ++ int requested = i < version.length ? version[i] : 0; ++ if (our != requested) { ++ return our > requested; ++ } ++ } ++ return true; ++ } ++ } ++ private static class NonStableVersionInfo implements VersionInfo { ++ private static final Logger LOGGER = LogUtils.getClassLogger(); ++ private static final String manifestURL = Optional.ofNullable(System.getProperty("paper.api.manifest-url")) ++ .orElse("https://piston-meta.mojang.com/mc/game/version_manifest_v2.json"); ++ private final String version = SharedConstants.getCurrentVersion().getName(); ++ private final String id = SharedConstants.getCurrentVersion().getId(); ++ private final List versionList; ++ private NonStableVersionInfo() { ++ HttpClient client = HttpClient.newHttpClient(); ++ HttpRequest request = HttpRequest.newBuilder() ++ .uri(URI.create(manifestURL)) ++ .build(); ++ List versionList = new ArrayList<>(); ++ try { ++ HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); ++ var parsed = JsonParser.parseString(response.body()); ++ var versionArray = parsed.getAsJsonObject().getAsJsonArray("versions"); ++ for (var version : versionArray) { ++ versionList.add(version.getAsJsonObject().get("id").getAsString()); ++ } ++ } catch (IOException | InterruptedException e) { ++ LOGGER.error("Failed to fetch version manifest", e); ++ LOGGER.error("Plugins depending on version check may not work correctly"); ++ } ++ this.versionList = ImmutableList.copyOf(versionList); ++ } ++ @Override ++ public boolean is(final int @NotNull ... version) { ++ return false; ++ } ++ ++ @Override ++ public boolean is(@NotNull final String version) { ++ return this.version.equals(version) || this.id.equals(version); ++ } ++ ++ @Override ++ public boolean isAtLeast(final int @NotNull ... version) { ++ if (version.length == 0) { ++ return true; ++ } ++ var builder = new StringBuilder(); ++ builder.append(version[0]); ++ for (int i = 1; i < version.length; i++) { ++ builder.append('.').append(version[i]); ++ } ++ var versionString = builder.toString(); ++ return this.versionList.indexOf(versionString) > this.versionList.indexOf(this.id); + } + } +}