feat:Some compatibility with paper builds without CraftBukkit relocation

To be fully compatible, the `ClassMapping` and `MethodMapping` classes should finally be removed
and their usages updated.

Announced in https://forums.papermc.io/threads/important-dev-psa-future-removal-of-cb-package-relocation.1106/
This commit is contained in:
Christian Koop 2024-03-29 15:32:39 +01:00
parent 6a0a8203dc
commit d3f9f4c226
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3

View File

@ -3,6 +3,9 @@ package com.craftaro.core.compatibility;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import java.util.HashMap;
import java.util.Map;
public enum ServerVersion { public enum ServerVersion {
UNKNOWN, V1_7, V1_8, V1_9, V1_10, V1_11, V1_12, V1_13, V1_14, V1_15, V1_16, V1_17, V1_18, V1_19, V1_20, V1_21, V1_22, V1_23; UNKNOWN, V1_7, V1_8, V1_9, V1_10, V1_11, V1_12, V1_13, V1_14, V1_15, V1_16, V1_17, V1_18, V1_19, V1_20, V1_21, V1_22, V1_23;
@ -11,7 +14,16 @@ public enum ServerVersion {
private static final ServerVersion serverVersion; private static final ServerVersion serverVersion;
private static final boolean isMocked; private static final boolean isMocked;
private static final Map<String, String> VERSION_TO_REVISION;
static { static {
VERSION_TO_REVISION = new HashMap<>();
VERSION_TO_REVISION.put("1.20", "v1_20_R1");
VERSION_TO_REVISION.put("1.20.1", "v1_20_R1");
VERSION_TO_REVISION.put("1.20.2", "v1_20_R2");
VERSION_TO_REVISION.put("1.20.3", "v1_20_R3");
VERSION_TO_REVISION.put("1.20.4", "v1_20_R3");
if (Bukkit.getServer() != null) { if (Bukkit.getServer() != null) {
String srvPackage = Bukkit.getServer().getClass().getPackage().getName(); String srvPackage = Bukkit.getServer().getClass().getPackage().getName();
isMocked = srvPackage.equals("be.seeseemelk.mockbukkit"); isMocked = srvPackage.equals("be.seeseemelk.mockbukkit");
@ -20,7 +32,19 @@ public enum ServerVersion {
serverPackageVersion = "v" + Bukkit.getServer().getBukkitVersion().replace('.', '_') + "_R0"; serverPackageVersion = "v" + Bukkit.getServer().getBukkitVersion().replace('.', '_') + "_R0";
serverReleaseVersion = "0"; serverReleaseVersion = "0";
} else { } else {
serverPackageVersion = srvPackage.substring(srvPackage.lastIndexOf('.') + 1); String nmsVersion;
try {
nmsVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
} catch (Exception ex) {
String minecraftVersion = Bukkit.getServer().getBukkitVersion().split("-")[0];
nmsVersion = VERSION_TO_REVISION.getOrDefault(minecraftVersion, "");
if (nmsVersion.isEmpty()) {
new RuntimeException("Cannot detect NMS version for server version: " + minecraftVersion).printStackTrace();
}
}
serverPackageVersion = nmsVersion;
serverReleaseVersion = serverPackageVersion.indexOf('R') != -1 ? serverPackageVersion.substring(serverPackageVersion.indexOf('R') + 1) : ""; serverReleaseVersion = serverPackageVersion.indexOf('R') != -1 ? serverPackageVersion.substring(serverPackageVersion.indexOf('R') + 1) : "";
} }
} else { } else {