Skip to content

Commit

Permalink
Add support for un-remapped Paper 1.20+
Browse files Browse the repository at this point in the history
  • Loading branch information
tr7zw committed Feb 12, 2024
1 parent a37a2c6 commit 3eefb1d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tr7zw.changeme.nbtapi.utils;

import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -41,6 +42,9 @@ public enum MinecraftVersion {
private final int versionId;
private final boolean mojangMapping;

private static final Map<String, MinecraftVersion> 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);
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import java.util.logging.Level;

import org.bukkit.Bukkit;

import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 3eefb1d

Please sign in to comment.