Spigot 1.17 compatibility

This commit is contained in:
Myles 2021-06-11 20:19:06 +01:00
parent 317af7ebc5
commit 68ccc6634d
5 changed files with 44 additions and 10 deletions

View File

@ -109,7 +109,10 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform<Player>
// Check if it's a spigot build with a protocol mod // Check if it's a spigot build with a protocol mod
try { try {
NMSUtil.nms("PacketEncoder").getDeclaredField("version"); NMSUtil.nms(
"PacketEncoder",
"net.minecraft.network.PacketEncoder"
).getDeclaredField("version");
compatSpigotBuild = true; compatSpigotBuild = true;
} catch (Exception e) { } catch (Exception e) {
compatSpigotBuild = false; compatSpigotBuild = false;

View File

@ -65,8 +65,8 @@ public class ClassGenerator {
} }
if (ViaVersionPlugin.getInstance().isCompatSpigotBuild()) { if (ViaVersionPlugin.getInstance().isCompatSpigotBuild()) {
Class decodeSuper = NMSUtil.nms("PacketDecoder"); Class decodeSuper = NMSUtil.nms("PacketDecoder", "net.minecraft.network.PacketDecoder");
Class encodeSuper = NMSUtil.nms("PacketEncoder"); Class encodeSuper = NMSUtil.nms("PacketEncoder", "net.minecraft.network.PacketEncoder");
// Generate the classes // Generate the classes
addSpigotCompatibility(pool, BukkitDecodeHandler.class, decodeSuper); addSpigotCompatibility(pool, BukkitDecodeHandler.class, decodeSuper);
addSpigotCompatibility(pool, BukkitEncodeHandler.class, encodeSuper); addSpigotCompatibility(pool, BukkitEncodeHandler.class, encodeSuper);
@ -209,7 +209,10 @@ public class ClassGenerator {
pool.importPackage("protocolsupport.api.Connection.PacketListener"); pool.importPackage("protocolsupport.api.Connection.PacketListener");
pool.importPackage("protocolsupport.api.Connection.PacketListener.PacketEvent"); pool.importPackage("protocolsupport.api.Connection.PacketListener.PacketEvent");
pool.importPackage("protocolsupport.protocol.ConnectionImpl"); 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 // Add connection reference field
connectListenerClazz.addField(CtField.make("private ConnectionImpl connection;", connectListenerClazz)); connectListenerClazz.addField(CtField.make("private ConnectionImpl connection;", connectListenerClazz));
// Bake constructor // Bake constructor
@ -303,7 +306,10 @@ public class ClassGenerator {
public static boolean shouldUseNewHandshakeVersionMethod() { public static boolean shouldUseNewHandshakeVersionMethod() {
try { try {
NMSUtil.nms("PacketHandshakingInSetProtocol").getMethod("getProtocolVersion"); NMSUtil.nms(
"PacketHandshakingInSetProtocol",
"net.minecraft.network.protocol.handshake.PacketHandshakingInSetProtocol"
).getMethod("getProtocolVersion");
return true; return true;
} catch (Exception e) { } catch (Exception e) {
return false; return false;

View File

@ -39,7 +39,12 @@ public class BukkitEncodeHandler extends MessageToByteEncoder implements ViaCode
static { static {
try { 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); versionField.setAccessible(true);
} catch (Exception e) { } catch (Exception e) {
// Not compat version // Not compat version

View File

@ -204,9 +204,15 @@ public class BukkitViaInjector implements ViaInjector {
} }
try { 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"); 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; Object ping = null;
// Search for ping method // Search for ping method
for (Field f : serverClazz.getDeclaredFields()) { for (Field f : serverClazz.getDeclaredFields()) {
@ -259,7 +265,10 @@ public class BukkitViaInjector implements ViaInjector {
} }
public static Object getServerConnection() throws Exception { 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 server = ReflectionUtil.invokeStatic(serverClazz, "getServer");
Object connection = null; Object connection = null;
for (Method m : serverClazz.getDeclaredMethods()) { for (Method m : serverClazz.getDeclaredMethods()) {

View File

@ -26,7 +26,10 @@ public class NMSUtil {
private static boolean loadDebugProperty() { private static boolean loadDebugProperty() {
try { try {
Class<?> serverClass = nms("MinecraftServer"); Class<?> serverClass = nms(
"MinecraftServer",
"net.minecraft.server.MinecraftServer"
);
Object server = serverClass.getDeclaredMethod("getServer").invoke(null); Object server = serverClass.getDeclaredMethod("getServer").invoke(null);
return (boolean) serverClass.getMethod("isDebugging").invoke(server); return (boolean) serverClass.getMethod("isDebugging").invoke(server);
} catch (ReflectiveOperationException e) { } catch (ReflectiveOperationException e) {
@ -38,6 +41,14 @@ public class NMSUtil {
return Class.forName(NMS + "." + className); 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 { public static Class<?> obc(String className) throws ClassNotFoundException {
return Class.forName(BASE + "." + className); return Class.forName(BASE + "." + className);
} }