Paper/patches/api/0473-Add-paper-version-util...

189 lines
6.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: masmc05 <masmc05@gmail.com>
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..cf39052c7d9f152313762e928fb49c5e3cd22dbc
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/ServerInfo.java
@@ -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
+ * Works even before Bukkit is initialized (e.g. on bootstrap)
+ */
+public class ServerInfo {
+ private ServerInfo() {
+ throw new UnsupportedOperationException("This class cannot be instantiated");
+ }
+ private static final ServerInfoProvider provider = Services.service(ServerInfoProvider.class).orElseThrow();
+
+ /**
+ * Get the version of the server
+ * @return the version of the server (e.g. "1.20.4", "1.20.2 Pre-release 2", "23w31a")
+ */
+ public static @NotNull String version() {
+ return provider.version();
+ }
+
+ /**
+ * Get the api version of the server
+ * @return the api version of the server (e.g. "1.20.4-R0.1-SNAPSHOT")
+ */
+ public static @NotNull String apiVersion() {
+ return provider.apiVersion();
+ }
+
+ /**
+ * Get the name of the server
+ * @return the name of the server (e.g. "Paper")
+ */
+ 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
+ * <p>
+ * 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 @NotNull... version) {
+ return provider.is(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 @NotNull... version) {
+ return provider.isAtLeast(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
+ */
+ public static boolean isImplementing(@NotNull Key api) {
+ return provider.isImplementing(api);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/util/ServerInfoProvider.java b/src/main/java/io/papermc/paper/util/ServerInfoProvider.java
new file mode 100644
index 0000000000000000000000000000000000000000..cee83c849668c79d0aca1e4e2b38698b67f5e948
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/ServerInfoProvider.java
@@ -0,0 +1,72 @@
+package io.papermc.paper.util;
+
+import net.kyori.adventure.key.Key;
+import org.bukkit.UnsafeValues;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * A utility class to get information about the server
+ */
+public interface ServerInfoProvider {
+ /**
+ * Get the version of the server
+ * @return the version of the server (e.g. "1.20.4", "1.20.2 Pre-release 2", "23w31a")
+ */
+ @NotNull String version();
+
+ /**
+ * 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 name of the server
+ * @return the name of the server (e.g. "Paper")
+ */
+ @NotNull String serverName();
+
+ /**
+ * Returns the unsafe values for the server for unsafe data values
+ * @return the unsafe values for the server
+ */
+ @Deprecated
+ @NotNull
+ 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
+ * <p>
+ * 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 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 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);
+}