Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server version util class #10253

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions patches/api/0477-Add-paper-version-util-class.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
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..931c6b704257b8baac7674c101d04ad7216d84e2
--- /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 {
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ 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() {
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ 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() {
lynxplay marked this conversation as resolved.
Show resolved Hide resolved
+ return provider.serverName();
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ }
+
+ /**
+ * Returns true if the server is a minecraft release version,
+ * false is it's a snapshot, pre-release, etc
+ * @return if the server is on a release version
+ */
+ public static boolean isRelease() {
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ return provider.isRelease();
+ }
+
+ /**
+ * Returns the unsafe values for the server for unsafe version values
+ * @return the unsafe values for the server
+ */
+ @Deprecated
+ @NotNull
+ public static UnsafeValues unsafe() {
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ 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) {
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ 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);
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ }
+
+ /**
+ * 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) {
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ 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..a692c159d82f3233f8211d5b37d1c3edff775088
--- /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();
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+
+ /**
+ * 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 on a release version
+ */
+ boolean isRelease();
+
+ /**
+ * 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);
+}
Loading