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 all 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
97 changes: 97 additions & 0 deletions patches/api/0479-Add-paper-version-util-class.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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..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() {
+ //<editor-fold defaultstate="collapsed" desc="Holder">
+ final class Holder {
+ private static final Supplier<ServerInfo> instance = Services.service(ServerInfo.class)::orElseThrow;
+ }
+ //</editor-fold>
+ 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
masmc05 marked this conversation as resolved.
Show resolved Hide resolved
+ * @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
+ * <p>
+ * Note: This method returns false on unsupported version formats for future compatibility
+ * <p>
+ * Currently supported formats are:
+ * <ul>
+ * <li>Release versions</li>
+ * </ul>
+ * @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);
+}
119 changes: 119 additions & 0 deletions patches/server/1049-Add-paper-version-util-class.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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/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<Key> 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<Key> 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