From 3eefb1d315542358b1c58ae5b5dab9121293390a Mon Sep 17 00:00:00 2001 From: tr7zw Date: Mon, 12 Feb 2024 16:55:16 +0100 Subject: [PATCH] Add support for un-remapped Paper 1.20+ --- .../nbtapi/utils/MinecraftVersion.java | 29 +++++++++++++------ .../utils/nmsmappings/ClassWrapper.java | 9 +++++- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/MinecraftVersion.java b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/MinecraftVersion.java index 905253317..8c26cc837 100644 --- a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/MinecraftVersion.java +++ b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/MinecraftVersion.java @@ -1,5 +1,6 @@ package de.tr7zw.changeme.nbtapi.utils; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,6 +42,9 @@ public enum MinecraftVersion { private final int versionId; private final boolean mojangMapping; + private static final Map VERSION_TO_REVISION = Map.of("1.20", MC1_20_R1, "1.20.1", + MC1_20_R1, "1.20.2", MC1_20_R2, "1.20.3", MC1_20_R3, "1.20.4", MC1_20_R3); + MinecraftVersion(int versionId) { this(versionId, false); } @@ -73,7 +77,11 @@ public boolean isMojangMapping() { */ public String getPackageName() { if (this == UNKNOWN) { - return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; + try { + return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; + } catch (Exception ex) { + // ignore, paper without remap, will fail + } } return this.name().replace("MC", "v"); } @@ -108,18 +116,22 @@ public static MinecraftVersion getVersion() { if (version != null) { return version; } - final String ver = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; - logger.info("[NBTAPI] Found Spigot: " + ver + "! Trying to find NMS support"); try { + final String ver = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; + logger.info("[NBTAPI] Found Minecraft: " + ver + "! Trying to find NMS support"); version = MinecraftVersion.valueOf(ver.replace("v", "MC")); - } catch (IllegalArgumentException ex) { - version = MinecraftVersion.UNKNOWN; + } catch (Exception ex) { + logger.info("[NBTAPI] Found Minecraft: " + Bukkit.getServer().getBukkitVersion().split("-")[0] + + "! Trying to find NMS support"); + version = VERSION_TO_REVISION.getOrDefault(Bukkit.getServer().getBukkitVersion().split("-")[0], + MinecraftVersion.UNKNOWN); } if (version != UNKNOWN) { logger.info("[NBTAPI] NMS support '" + version.name() + "' loaded!"); } else { - logger.warning("[NBTAPI] This Server-Version(" + ver + ") is not supported by this NBT-API Version(" - + VERSION + ") located in " + VersionChecker.getPlugin() + logger.warning("[NBTAPI] This Server-Version(" + Bukkit.getServer().getBukkitVersion() + + ") is not supported by this NBT-API Version(" + VERSION + ") located in " + + VersionChecker.getPlugin() + ". The NBT-API will try to work as good as it can! Some functions may not work!"); } init(); @@ -245,8 +257,7 @@ public static boolean isFoliaPresent() { return isFoliaPresent; } try { - logger.info("[NBTAPI] Found Folia: " - + Class.forName("io.papermc.paper.threadedregions.RegionizedServer")); + logger.info("[NBTAPI] Found Folia: " + Class.forName("io.papermc.paper.threadedregions.RegionizedServer")); isFoliaPresent = true; } catch (Exception ex) { isFoliaPresent = false; diff --git a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/ClassWrapper.java b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/ClassWrapper.java index dce91ab8e..2daf8011f 100644 --- a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/ClassWrapper.java +++ b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/nmsmappings/ClassWrapper.java @@ -4,6 +4,8 @@ import java.util.logging.Level; +import org.bukkit.Bukkit; + import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion; /** @@ -62,7 +64,8 @@ public enum ClassWrapper { "net.minecraft.nbt.NbtUtils"), NMS_IBLOCKDATA(PackageWrapper.NMS, "IBlockData", MinecraftVersion.MC1_8_R3, null, "net.minecraft.world.level.block.state", "net.minecraft.world.level.block.state.BlockState"), - NMS_NBTACCOUNTER(PackageWrapper.NMS, "NBTReadLimiter", MinecraftVersion.MC1_20_R3, null, "net.minecraft.nbt", "net.minecraft.nbt.NbtAccounter"), + NMS_NBTACCOUNTER(PackageWrapper.NMS, "NBTReadLimiter", MinecraftVersion.MC1_20_R3, null, "net.minecraft.nbt", + "net.minecraft.nbt.NbtAccounter"), GAMEPROFILE(PackageWrapper.NONE, "com.mojang.authlib.GameProfile", MinecraftVersion.MC1_8_R3, null); private Class clazz; @@ -100,7 +103,11 @@ public enum ClassWrapper { } else if (MinecraftVersion.isForgePresent() && MinecraftVersion.getVersion() == MinecraftVersion.MC1_7_R4 && Forge1710Mappings.getClassMappings().get(this.name()) != null) { clazz = Class.forName(clazzName = Forge1710Mappings.getClassMappings().get(this.name())); + } else if (packageId == PackageWrapper.CRAFTBUKKIT) { + // this also works for un-remapped Paper 1.20+ + clazz = Class.forName(Bukkit.getServer().getClass().getPackage().getName() + "." + clazzName); } else { + // fallback for old versions pre mojmap and in the nms package String version = MinecraftVersion.getVersion().getPackageName(); clazz = Class.forName(packageId.getUri() + "." + version + "." + clazzName); }