diff --git a/patches/api/0479-Add-paper-version-util-class.patch b/patches/api/0479-Add-paper-version-util-class.patch new file mode 100644 index 000000000000..7e5c5330a207 --- /dev/null +++ b/patches/api/0479-Add-paper-version-util-class.patch @@ -0,0 +1,97 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: masmc05 +Date: Fri, 16 Feb 2024 14:13:30 +0200 +Subject: [PATCH] Add paper version util class + + +diff --git a/src/main/java/io/papermc/paper/util/ServerInfo.java b/src/main/java/io/papermc/paper/util/ServerInfo.java +new file mode 100644 +index 0000000000000000000000000000000000000000..51730357be027e16cb7360a1580021b8c24733da +--- /dev/null ++++ b/src/main/java/io/papermc/paper/util/ServerInfo.java +@@ -0,0 +1,85 @@ ++package io.papermc.paper.util; ++ ++import net.kyori.adventure.key.Key; ++import net.kyori.adventure.util.Services; ++import org.jetbrains.annotations.ApiStatus; ++import org.jetbrains.annotations.NotNull; ++import java.util.function.Supplier; ++ ++/** ++ * A utility class to get information about the server ++ */ ++@ApiStatus.NonExtendable ++public interface ServerInfo { ++ /** ++ * Get the server info instance ++ * @return the server info instance ++ */ ++ static @NotNull ServerInfo serverInfo() { ++ // ++ final class Holder { ++ private static final Supplier instance = Services.service(ServerInfo.class)::orElseThrow; ++ } ++ // ++ return Holder.instance.get(); ++ } ++ ++ /** ++ * Get the version of the server ++ * @return the version of the server (e.g. "1.20.4", "1.20.2-pre2", "23w31a") ++ * @see #is(String) ++ * @see #versionName() ++ */ ++ @NotNull String version(); ++ ++ /** ++ * Get the version name of the server ++ * @return the version name of the server (e.g. "1.20.4", "1.20.2 Pre-release 2", "23w31a") ++ * @see #version() ++ */ ++ @NotNull String versionName(); ++ ++ /** ++ * Get the api version of the server ++ * @return the api version of the server (e.g. "1.20.4-R0.1-SNAPSHOT") ++ */ ++ @NotNull String apiVersion(); ++ ++ /** ++ * Get the brand of the server ++ * @return the brand of the server (e.g. "Paper") ++ */ ++ @NotNull String serverBrand(); ++ ++ /** ++ * Checks if the server runs exactly the specified version ++ * @param version the version to check (e.g. "1.20.4", "1.20.2-pre2", "23w31a") ++ * @return true if the server runs exactly the specified version ++ * @see #isAtLeast(String) ++ * @see #version() ++ */ ++ boolean is(@NotNull String version); ++ ++ /** ++ * Checks if the server runs a version which is after this version or the same ++ *

++ * Note: This method returns false on unsupported version formats for future compatibility ++ *

++ * Currently supported formats are: ++ *

++ * @param version the version to check (e.g. "1.20.4") ++ * @return true if the server runs on this version or a newer version ++ * @see #is(String) ++ * @see #version() ++ */ ++ boolean isAtLeast(@NotNull String version); ++ ++ /** ++ * 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 isImplementing(@NotNull Key api); ++} diff --git a/patches/server/1049-Add-paper-version-util-class.patch b/patches/server/1049-Add-paper-version-util-class.patch new file mode 100644 index 000000000000..48217a987fe2 --- /dev/null +++ b/patches/server/1049-Add-paper-version-util-class.patch @@ -0,0 +1,119 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: masmc05 +Date: Fri, 16 Feb 2024 14:13:29 +0200 +Subject: [PATCH] Add paper version util class + + +diff --git a/src/main/java/io/papermc/paper/util/misc/ServerInfoImpl.java b/src/main/java/io/papermc/paper/util/misc/ServerInfoImpl.java +new file mode 100644 +index 0000000000000000000000000000000000000000..e33c292e6798b67d817a929cb815c3752aee0b88 +--- /dev/null ++++ b/src/main/java/io/papermc/paper/util/misc/ServerInfoImpl.java +@@ -0,0 +1,100 @@ ++package io.papermc.paper.util.misc; ++ ++import com.google.common.base.Preconditions; ++import io.papermc.paper.util.ServerInfo; ++import net.kyori.adventure.key.Key; ++import net.kyori.adventure.key.KeyPattern; ++import net.minecraft.SharedConstants; ++import org.bukkit.craftbukkit.util.Versioning; ++import org.jetbrains.annotations.NotNull; ++import java.util.HashSet; ++import java.util.Locale; ++import java.util.Set; ++import java.util.regex.Pattern; ++import java.util.stream.Stream; ++ ++public class ServerInfoImpl implements ServerInfo { ++ private final String bukkitVersion = Versioning.getBukkitVersion(); ++ private final String version = SharedConstants.getCurrentVersion().getId(); ++ private final int[] versionArr = this.parseReleaseVersion(this.version); ++ private final Pattern release = Pattern.compile("[0-9](\\.[0-9]{1,2}){1,2}"); ++ private final Set implementedAPIs; ++ private final String serverBrand; ++ ++ public ServerInfoImpl() { ++ ServerImplementationInfo info = new ServerImplementationInfo(); ++ this.serverBrand = info.latestBrand; ++ this.implementedAPIs = info.implementedAPIs; ++ } ++ ++ @Override ++ public @NotNull String version() { ++ return this.version; ++ } ++ ++ @Override ++ public @NotNull String versionName() { ++ return SharedConstants.getCurrentVersion().getName(); ++ } ++ ++ @Override ++ public @NotNull String apiVersion() { ++ return this.bukkitVersion; ++ } ++ ++ @Override ++ public @NotNull String serverBrand() { ++ return this.serverBrand; ++ } ++ ++ @Override ++ public boolean is(@NotNull final String version) { ++ Preconditions.checkNotNull(version, "The version cannot be null"); ++ return this.version.equals(version); ++ } ++ ++ @Override ++ public boolean isAtLeast(final @NotNull String version) { ++ if (this.release.matcher(version).matches()) { ++ return this.isAtLeast(this.parseReleaseVersion(version)); ++ } else { ++ return false; ++ } ++ } ++ ++ private int[] parseReleaseVersion(final String version) { ++ return Stream.of(version.split("\\.")).mapToInt(Integer::parseInt).toArray(); ++ } ++ ++ private boolean isAtLeast(final int[] 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; ++ } ++ ++ @Override ++ public boolean isImplementing(final @NotNull Key api) { ++ Preconditions.checkNotNull(api, "The API cannot be null"); ++ return this.implementedAPIs.contains(api); ++ } ++ ++ private static class ServerImplementationInfo { ++ private final Set implementedAPIs = new HashSet<>(); ++ private String latestBrand; ++ private ServerImplementationInfo() { ++ this.add("minecraft", "Vanilla"); ++ this.add("papermc", "Paper"); ++ } ++ @SuppressWarnings("PatternValidation") ++ private void add(@KeyPattern.Namespace String namespace, String name) { ++ this.implementedAPIs.add(Key.key(namespace, name.toLowerCase(Locale.ROOT))); ++ this.latestBrand = name; ++ } ++ } ++} +diff --git a/src/main/resources/META-INF/services/io.papermc.paper.util.ServerInfo b/src/main/resources/META-INF/services/io.papermc.paper.util.ServerInfo +new file mode 100644 +index 0000000000000000000000000000000000000000..726333c3565c7cfb9840d66ba52c78f28429541e +--- /dev/null ++++ b/src/main/resources/META-INF/services/io.papermc.paper.util.ServerInfo +@@ -0,0 +1 @@ ++io.papermc.paper.util.misc.ServerInfoImpl