Skip to content

Commit

Permalink
Expose server build information
Browse files Browse the repository at this point in the history
Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
Co-authored-by: masmc05 <masmc05@gmail.com>
  • Loading branch information
3 people committed May 15, 2024
1 parent 3fc9358 commit 6bf6a58
Show file tree
Hide file tree
Showing 2 changed files with 662 additions and 0 deletions.
159 changes: 159 additions & 0 deletions patches/api/0480-Expose-server-build-information.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: masmc05 <masmc05@gmail.com>
Date: Tue, 14 May 2024 21:38:49 -0700
Subject: [PATCH] Expose server build information

Co-authored-by: Riley Park <rileysebastianpark@gmail.com>

diff --git a/src/main/java/io/papermc/paper/ServerBuildInfo.java b/src/main/java/io/papermc/paper/ServerBuildInfo.java
new file mode 100644
index 0000000000000000000000000000000000000000..d6ce8364391761afc2e415e116e3dd8fcadfe908
--- /dev/null
+++ b/src/main/java/io/papermc/paper/ServerBuildInfo.java
@@ -0,0 +1,117 @@
+package io.papermc.paper;
+
+import java.time.Instant;
+import java.util.Optional;
+import java.util.OptionalInt;
+import net.kyori.adventure.key.Key;
+import net.kyori.adventure.util.Services;
+import org.jetbrains.annotations.ApiStatus.NonExtendable;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Information about the current server build.
+ */
+@NonExtendable
+public interface ServerBuildInfo {
+ static ServerBuildInfo get() {
+ //<editor-fold defaultstate="collapsed" desc="Holder">
+ final class Holder {
+ static final ServerBuildInfo INSTANCE = Services.service(ServerBuildInfo.class).orElseThrow();
+ }
+ //</editor-fold>
+ return Holder.INSTANCE;
+ }
+
+ /**
+ * Gets the brand id of the server.
+ *
+ * @return the brand id of the server (e.g. "papermc:paper")
+ */
+ Key brandId();
+
+ /**
+ * Checks if the current server supports the specified brand.
+ *
+ * @param brand the brand to check (e.g. "papermc:folia")
+ * @return {@code true} if the server supports the specified brand
+ */
+ boolean isBrandCompatible(final @NotNull Key brand);
+
+ /**
+ * Gets the brand name of the server.
+ *
+ * @return the brand name of the server (e.g. "Paper")
+ */
+ String brandName();
+
+ /**
+ * Gets the Minecraft version id.
+ *
+ * @return the Minecraft version id (e.g. "1.20.4", "1.20.2-pre2", "23w31a")
+ */
+ String minecraftVersionId();
+
+ /**
+ * Gets the Minecraft version name.
+ *
+ * @return the Minecraft version name (e.g. "1.20.4", "1.20.2 Pre-release 2", "23w31a")
+ */
+ String minecraftVersionName();
+
+ /**
+ * Gets the build number.
+ *
+ * @return the build number
+ */
+ OptionalInt buildNumber();
+
+ /**
+ * Gets the build time.
+ *
+ * @return the build time
+ */
+ Instant buildTime();
+
+ /**
+ * Gets the git commit branch.
+ *
+ * @return the git commit branch
+ */
+ Optional<String> gitBranch();
+
+ /**
+ * Gets the git commit hash.
+ *
+ * @return the git commit hash
+ */
+ Optional<String> gitCommit();
+
+ /**
+ * Get the api version.
+ *
+ * @return the api version (e.g. "1.20.4-R0.1-SNAPSHOT")
+ */
+ String apiVersion();
+
+ /**
+ * Creates a string representation of the server build information.
+ *
+ * @param representation the type of representation
+ * @return a string
+ */
+ String toString(final StringRepresentation representation);
+
+ /**
+ * String representation types.
+ */
+ enum StringRepresentation {
+ /**
+ * A simple version string, in format {@code <minecraftVersionId>-<buildNumber>-<gitCommit>}.
+ */
+ VERSION_SIMPLE,
+ /**
+ * A simple version string, in format {@code <minecraftVersionId>-<buildNumber>-<gitBranch>@<gitCommit> (<buildTime>}.
+ */
+ VERSION_FULL,
+ }
+}
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index 5d1b55fdbcbe63f6b42b694d05211a3cc691a09d..4e17b9864441b31838aee31ed522d8a0f105d7a5 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -60,7 +60,6 @@ import org.bukkit.util.CachedServerIcon;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import io.papermc.paper.util.JarManifests; // Paper

/**
* Represents the Bukkit core, for version and Server singleton handling
@@ -120,14 +119,8 @@ public final class Bukkit {
*/
@NotNull
public static String getVersionMessage() {
- final var manifest = JarManifests.manifest(Bukkit.getServer().getClass());
- final String gitBranch = manifest == null ? null : manifest.getMainAttributes().getValue("Git-Branch");
- final String gitCommit = manifest == null ? null : manifest.getMainAttributes().getValue("Git-Commit");
- String branchMsg = " on " + gitBranch;
- if ("master".equals(gitBranch) || "main".equals(gitBranch)) {
- branchMsg = ""; // Don't show branch on main/master
- }
- return "This server is running " + getName() + " version " + getVersion() + " (Implementing API version " + getBukkitVersion() + ") (Git: " + gitCommit + branchMsg + ")";
+ final io.papermc.paper.ServerBuildInfo version = io.papermc.paper.ServerBuildInfo.get();
+ return "This server is running " + getName() + " version " + version.toString(io.papermc.paper.ServerBuildInfo.StringRepresentation.VERSION_FULL) + " (Implementing API version " + getBukkitVersion() + ")";
// Paper end
}

Loading

0 comments on commit 6bf6a58

Please sign in to comment.