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 1 commit
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
147 changes: 147 additions & 0 deletions patches/api/0463-Add-paper-version-util-class.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
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/PaperServerInfo.java b/src/main/java/io/papermc/paper/util/PaperServerInfo.java
new file mode 100644
index 0000000000000000000000000000000000000000..b882b328e87cffa06c32fa6618afeab8d08ab0a1
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/PaperServerInfo.java
@@ -0,0 +1,73 @@
+package io.papermc.paper.util;
+
+import net.kyori.adventure.util.Services;
+import org.bukkit.UnsafeValues;
+
+/**
+ * A utility class to get information about the server
+ * Works even before Bukkit is initialized (e.g. on bootstrap)
+ */
+public class PaperServerInfo {
+ private PaperServerInfo() {
+ 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")
+ */
+ public static 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 String apiVersion() {
+ return provider.apiVersion();
+ }
+
+ /**
+ * Get the name of the server
+ * @return the name of the server (e.g. "Paper")
+ */
+ public static String serverName() {
+ return provider.serverName();
+ }
+ /**
+ * Returns the unsafe values for the server for unsafe version values
+ * @return the unsafe values for the server
+ */
+ @Deprecated
+ public static UnsafeValues unsafe() {
+ return provider.unsafe();
+ }
+
+ /**
+ * Checks if the server runs exactly the specified version
+ * @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) {
+ return provider.is(version);
+ }
+
+ /**
+ * Checks if the server runs at least the specified version
+ * @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) {
+ 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
+ */
+ public static boolean implement(String api) {
+ return provider.implement(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
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/PaperServerInfoProvider.java
@@ -0,0 +1,56 @@
+package io.papermc.paper.util;
+
+import org.bukkit.UnsafeValues;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * A utility class to get information about the server
+ */
+public interface PaperServerInfoProvider {
+ /**
+ * Get the version of the server
+ * @return the version of the server (e.g. "1.20.4")
+ */
+ @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();
+
+ /**
+ * Checks if the server runs exactly the specified version
+ * @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
+ * @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
+ */
+ boolean implement(@NotNull String api);
+}
102 changes: 102 additions & 0 deletions patches/server/1046-Add-paper-version-util-class.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: masmc05 <masmc05@gmail.com>
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/PaperServerInfoProviderImpl.java b/src/main/java/io/papermc/paper/util/misc/PaperServerInfoProviderImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..4a105d428778b95232e8acb1bb3d09a6c4e94477
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/misc/PaperServerInfoProviderImpl.java
@@ -0,0 +1,83 @@
+package io.papermc.paper.util.misc;
+
+import com.google.common.base.Preconditions;
+import io.papermc.paper.util.PaperServerInfoProvider;
+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 java.util.Arrays;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Set;
+import java.util.stream.Stream;
+
+public class PaperServerInfoProviderImpl implements PaperServerInfoProvider {
+ private final String bukkitVersion = Versioning.getBukkitVersion();
+ private final Set<String> implementedAPIs;
+ private final String serverName;
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ private final int[] version;
+ public PaperServerInfoProviderImpl() {
+ ServerImplementationInfo info = new ServerImplementationInfo();
+ this.serverName = info.latestName;
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ this.implementedAPIs = info.implementedAPIs;
+ this.version = Stream.of(this.version().split("\\.")).mapToInt(Integer::parseInt).toArray();
+ }
+ @Override
+ public @NotNull String version() {
+ return SharedConstants.getCurrentVersion().getName();
+ }
+
+ @Override
+ public @NotNull String apiVersion() {
+ return this.bukkitVersion;
+ }
+
+ @Override
+ public @NotNull String serverName() {
+ return this.serverName;
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ }
+
+ @Override
+ @Deprecated
+ public @NotNull UnsafeValues unsafe() {
+ return CraftMagicNumbers.INSTANCE;
+ }
+
+ @Override
+ public boolean is(final int @NotNull ... version) {
+ return Arrays.equals(this.version, 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;
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public boolean implement(final @NotNull String api) {
+ Preconditions.checkNotNull(api, "The API cannot be null");
+ return this.implementedAPIs.contains(api.toUpperCase(Locale.ROOT));
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ }
+ private static class ServerImplementationInfo {
+ private final Set<String> implementedAPIs = new HashSet<>();
+ private String latestName;
+ private ServerImplementationInfo() {
+ this.add("Vanilla");
+ this.add("Paper");
+ }
+ private void add(String api) {
+ this.implementedAPIs.add(api.toUpperCase(Locale.ROOT));
+ this.latestName = api;
+ }
+ }
+}
diff --git a/src/main/resources/META-INF/services/io.papermc.paper.util.PaperServerInfoProvider b/src/main/resources/META-INF/services/io.papermc.paper.util.PaperServerInfoProvider
new file mode 100644
index 0000000000000000000000000000000000000000..b48ad39218b8d399d60c98c3e4d66d5149717241
--- /dev/null
+++ b/src/main/resources/META-INF/services/io.papermc.paper.util.PaperServerInfoProvider
@@ -0,0 +1 @@
+io.papermc.paper.util.misc.PaperServerInfoProviderImpl