3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-28 17:10:13 +01:00

Fix PS compatibility on Java 8 đź’©

Dieser Commit ist enthalten in:
KennyTV 2021-05-15 10:11:19 +02:00
Ursprung 4f9f3254be
Commit 54b0345017
Es konnte kein GPG-SchlĂĽssel zu dieser Signatur gefunden werden
GPG-SchlĂĽssel-ID: 6BE3B555EBC5982B

Datei anzeigen

@ -46,6 +46,7 @@ import java.lang.reflect.Method;
//TODO maybe clean this up a bit đź‘€ //TODO maybe clean this up a bit đź‘€
public class ClassGenerator { public class ClassGenerator {
private static final boolean useModules = hasModuleMethod();
private static HandlerConstructor constructor = new BasicHandlerConstructor(); private static HandlerConstructor constructor = new BasicHandlerConstructor();
private static String psPackage; private static String psPackage;
private static Class psConnectListener; private static Class psConnectListener;
@ -95,7 +96,7 @@ public class ClassGenerator {
// Import required classes // Import required classes
pool.importPackage("com.viaversion.viaversion.classgenerator.generated"); pool.importPackage("com.viaversion.viaversion.classgenerator.generated");
pool.importPackage("com.viaversion.viaversion.classgenerator"); pool.importPackage("com.viaversion.viaversion.classgenerator");
pool.importPackage("com.viaversion.viaversion.api.data"); pool.importPackage("com.viaversion.viaversion.api.connection");
pool.importPackage("io.netty.handler.codec"); pool.importPackage("io.netty.handler.codec");
// Implement Methods // Implement Methods
generated.addMethod(CtMethod.make("public MessageToByteEncoder newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {\n" + generated.addMethod(CtMethod.make("public MessageToByteEncoder newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {\n" +
@ -105,22 +106,14 @@ public class ClassGenerator {
" return new BukkitDecodeHandler(info, minecraftDecoder);\n" + " return new BukkitDecodeHandler(info, minecraftDecoder);\n" +
" }", generated)); " }", generated));
constructor = (HandlerConstructor) generated.toClass(HandlerConstructor.class.getClassLoader()).newInstance(); constructor = (HandlerConstructor) toClass(generated).newInstance();
} catch (ClassNotFoundException e) { } catch (ReflectiveOperationException | CannotCompileException | NotFoundException e) {
e.printStackTrace();
} catch (CannotCompileException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
private static Class addSpigotCompatibility(ClassPool pool, Class input, Class superclass) { private static void addSpigotCompatibility(ClassPool pool, Class input, Class superclass) {
String newName = "com.viaversion.viaversion.classgenerator.generated." + input.getSimpleName(); String newName = "com.viaversion.viaversion.classgenerator.generated." + input.getSimpleName();
try { try {
@ -146,13 +139,12 @@ public class ClassGenerator {
} }
} }
} }
return generated.toClass(HandlerConstructor.class); toClass(generated);
} catch (NotFoundException e) { } catch (NotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (CannotCompileException e) { } catch (CannotCompileException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null;
} }
private static void addPSCompatibility(ClassPool pool, Class input, Class superclass) { private static void addPSCompatibility(ClassPool pool, Class input, Class superclass) {
@ -195,7 +187,7 @@ public class ClassGenerator {
} }
} }
} }
generated.toClass(HandlerConstructor.class); toClass(generated);
} catch (NotFoundException e) { } catch (NotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (CannotCompileException e) { } catch (CannotCompileException e) {
@ -248,7 +240,7 @@ public class ClassGenerator {
// In any case, remove the packet listener and wrap up. // In any case, remove the packet listener and wrap up.
+ " connection.removePacketListener(this);\n" + " connection.removePacketListener(this);\n"
+ "}", connectListenerClazz)); + "}", connectListenerClazz));
return connectListenerClazz.toClass(HandlerConstructor.class); return toClass(connectListenerClazz);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -318,4 +310,17 @@ public class ClassGenerator {
} }
} }
@SuppressWarnings("deprecation")
private static Class<?> toClass(CtClass ctClass) throws CannotCompileException {
return useModules ? ctClass.toClass(HandlerConstructor.class) : ctClass.toClass(HandlerConstructor.class.getClassLoader());
}
private static boolean hasModuleMethod() {
try {
Class.class.getDeclaredMethod("getModule");
return true;
} catch (NoSuchMethodException e) {
return false;
}
}
} }