Add paper version util class

This commit is contained in:
masmc05 2024-02-16 14:18:25 +02:00
parent 37df12143c
commit e1ce2502ac
2 changed files with 249 additions and 0 deletions

View File

@ -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);
+}

View File

@ -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;
+ private final int[] 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();
+ }
+ @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;
+ }
+
+ @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;
+ }
+ }
+ 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));
+ }
+ 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