Expose server build information
Co-authored-by: Professor Bloodstone <git@bloodstone.dev> Co-authored-by: Mark Vainomaa <mikroskeem@mikroskeem.eu> Co-authored-by: masmc05 <masmc05@gmail.com> Co-authored-by: Riley Park <rileysebastianpark@gmail.com>
This commit is contained in:
122
paper-api/src/main/java/io/papermc/paper/ServerBuildInfo.java
Normal file
122
paper-api/src/main/java/io/papermc/paper/ServerBuildInfo.java
Normal file
@@ -0,0 +1,122 @@
|
||||
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;
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
|
||||
/**
|
||||
* Information about the current server build.
|
||||
*/
|
||||
@NullMarked
|
||||
@ApiStatus.NonExtendable
|
||||
public interface ServerBuildInfo {
|
||||
/**
|
||||
* The brand id for Paper.
|
||||
*/
|
||||
Key BRAND_PAPER_ID = Key.key("papermc", "paper");
|
||||
|
||||
/**
|
||||
* Gets the {@code ServerBuildInfo}.
|
||||
*
|
||||
* @return the {@code ServerBuildInfo}
|
||||
*/
|
||||
static ServerBuildInfo buildInfo() {
|
||||
//<editor-fold defaultstate="collapsed" desc="Holder">
|
||||
final class Holder {
|
||||
static final Optional<ServerBuildInfo> INSTANCE = Services.service(ServerBuildInfo.class);
|
||||
}
|
||||
//</editor-fold>
|
||||
return Holder.INSTANCE.orElseThrow();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 brandId the brand to check (e.g. "papermc:folia")
|
||||
* @return {@code true} if the server supports the specified brand
|
||||
*/
|
||||
@ApiStatus.Experimental
|
||||
boolean isBrandCompatible(final Key brandId);
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
/**
|
||||
* Creates a string representation of the server build information.
|
||||
*
|
||||
* @param representation the type of representation
|
||||
* @return a string
|
||||
*/
|
||||
String asString(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,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.papermc.paper.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.jar.Manifest;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
@NullMarked
|
||||
@ApiStatus.Internal
|
||||
public final class JarManifests {
|
||||
private JarManifests() {
|
||||
}
|
||||
|
||||
private static final Map<ClassLoader, Manifest> MANIFESTS = Collections.synchronizedMap(new WeakHashMap<>());
|
||||
|
||||
public static @Nullable Manifest manifest(final Class<?> clazz) {
|
||||
return MANIFESTS.computeIfAbsent(clazz.getClassLoader(), classLoader -> {
|
||||
final String classLocation = "/" + clazz.getName().replace(".", "/") + ".class";
|
||||
final URL resource = clazz.getResource(classLocation);
|
||||
if (resource == null) {
|
||||
return null;
|
||||
}
|
||||
final String classFilePath = resource.toString().replace("\\", "/");
|
||||
final String archivePath = classFilePath.substring(0, classFilePath.length() - classLocation.length());
|
||||
try (final InputStream stream = new URL(archivePath + "/META-INF/MANIFEST.MF").openStream()) {
|
||||
return new Manifest(stream);
|
||||
} catch (final IOException ex) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user