diff --git a/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java b/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java index 3e0f5b1e0..9aa908dce 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java @@ -109,7 +109,10 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform // Check if it's a spigot build with a protocol mod try { - NMSUtil.nms("PacketEncoder").getDeclaredField("version"); + NMSUtil.nms( + "PacketEncoder", + "net.minecraft.network.PacketEncoder" + ).getDeclaredField("version"); compatSpigotBuild = true; } catch (Exception e) { compatSpigotBuild = false; diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/classgenerator/ClassGenerator.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/classgenerator/ClassGenerator.java index 4266bab1d..9749c6546 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/classgenerator/ClassGenerator.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/classgenerator/ClassGenerator.java @@ -65,8 +65,8 @@ public class ClassGenerator { } if (ViaVersionPlugin.getInstance().isCompatSpigotBuild()) { - Class decodeSuper = NMSUtil.nms("PacketDecoder"); - Class encodeSuper = NMSUtil.nms("PacketEncoder"); + Class decodeSuper = NMSUtil.nms("PacketDecoder", "net.minecraft.network.PacketDecoder"); + Class encodeSuper = NMSUtil.nms("PacketEncoder", "net.minecraft.network.PacketEncoder"); // Generate the classes addSpigotCompatibility(pool, BukkitDecodeHandler.class, decodeSuper); addSpigotCompatibility(pool, BukkitEncodeHandler.class, encodeSuper); @@ -209,7 +209,10 @@ public class ClassGenerator { pool.importPackage("protocolsupport.api.Connection.PacketListener"); pool.importPackage("protocolsupport.api.Connection.PacketListener.PacketEvent"); pool.importPackage("protocolsupport.protocol.ConnectionImpl"); - pool.importPackage(NMSUtil.nms("PacketHandshakingInSetProtocol").getName()); + pool.importPackage(NMSUtil.nms( + "PacketHandshakingInSetProtocol", + "net.minecraft.network.protocol.handshake.PacketHandshakingInSetProtocol" + ).getName()); // Add connection reference field connectListenerClazz.addField(CtField.make("private ConnectionImpl connection;", connectListenerClazz)); // Bake constructor @@ -303,7 +306,10 @@ public class ClassGenerator { public static boolean shouldUseNewHandshakeVersionMethod() { try { - NMSUtil.nms("PacketHandshakingInSetProtocol").getMethod("getProtocolVersion"); + NMSUtil.nms( + "PacketHandshakingInSetProtocol", + "net.minecraft.network.protocol.handshake.PacketHandshakingInSetProtocol" + ).getMethod("getProtocolVersion"); return true; } catch (Exception e) { return false; diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java index d45df86db..e0034703c 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java @@ -39,7 +39,12 @@ public class BukkitEncodeHandler extends MessageToByteEncoder implements ViaCode static { try { - versionField = NMSUtil.nms("PacketEncoder").getDeclaredField("version"); + // Attempt to get any version info from the handler + versionField = NMSUtil.nms( + "PacketEncoder", + "net.minecraft.network.PacketEncoder" + ).getDeclaredField("version"); + versionField.setAccessible(true); } catch (Exception e) { // Not compat version diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java index 467007a95..e43439fc6 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java @@ -204,9 +204,15 @@ public class BukkitViaInjector implements ViaInjector { } try { - Class serverClazz = NMSUtil.nms("MinecraftServer"); + // Grab a static instance of the server + Class serverClazz = NMSUtil.nms("MinecraftServer", "net.minecraft.server.MinecraftServer"); Object server = ReflectionUtil.invokeStatic(serverClazz, "getServer"); - Class pingClazz = NMSUtil.nms("ServerPing"); + + // Grab the ping class and find the field to access it + Class pingClazz = NMSUtil.nms( + "ServerPing", + "net.minecraft.network.protocol.status.ServerPing" + ); Object ping = null; // Search for ping method for (Field f : serverClazz.getDeclaredFields()) { @@ -259,7 +265,10 @@ public class BukkitViaInjector implements ViaInjector { } public static Object getServerConnection() throws Exception { - Class serverClazz = NMSUtil.nms("MinecraftServer"); + Class serverClazz = NMSUtil.nms( + "MinecraftServer", + "net.minecraft.server.MinecraftServer" + ); Object server = ReflectionUtil.invokeStatic(serverClazz, "getServer"); Object connection = null; for (Method m : serverClazz.getDeclaredMethods()) { diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/NMSUtil.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/NMSUtil.java index ec2a6c762..2761f56b0 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/NMSUtil.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/NMSUtil.java @@ -26,7 +26,10 @@ public class NMSUtil { private static boolean loadDebugProperty() { try { - Class serverClass = nms("MinecraftServer"); + Class serverClass = nms( + "MinecraftServer", + "net.minecraft.server.MinecraftServer" + ); Object server = serverClass.getDeclaredMethod("getServer").invoke(null); return (boolean) serverClass.getMethod("isDebugging").invoke(server); } catch (ReflectiveOperationException e) { @@ -38,6 +41,14 @@ public class NMSUtil { return Class.forName(NMS + "." + className); } + public static Class nms(String className, String fallbackFullClassName) throws ClassNotFoundException { + try { + return Class.forName(NMS + "." + className); + } catch (ClassNotFoundException ignored) { + return Class.forName(fallbackFullClassName); + } + } + public static Class obc(String className) throws ClassNotFoundException { return Class.forName(BASE + "." + className); }