Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-09-08 22:02:50 +02:00
Support for new PS when package isn't found
Dieser Commit ist enthalten in:
Ursprung
783542401b
Commit
55fccba711
@ -3,16 +3,12 @@ package us.myles.ViaVersion.bukkit.classgenerator;
|
|||||||
import javassist.*;
|
import javassist.*;
|
||||||
import javassist.expr.ConstructorCall;
|
import javassist.expr.ConstructorCall;
|
||||||
import javassist.expr.ExprEditor;
|
import javassist.expr.ExprEditor;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import us.myles.ViaVersion.api.ViaVersion;
|
import us.myles.ViaVersion.api.ViaVersion;
|
||||||
import us.myles.ViaVersion.bukkit.handlers.BukkitDecodeHandler;
|
import us.myles.ViaVersion.bukkit.handlers.BukkitDecodeHandler;
|
||||||
import us.myles.ViaVersion.bukkit.handlers.BukkitEncodeHandler;
|
import us.myles.ViaVersion.bukkit.handlers.BukkitEncodeHandler;
|
||||||
import us.myles.ViaVersion.bukkit.util.NMSUtil;
|
import us.myles.ViaVersion.bukkit.util.NMSUtil;
|
||||||
import us.myles.ViaVersion.handlers.ViaHandler;
|
|
||||||
|
|
||||||
public class ClassGenerator {
|
public class ClassGenerator {
|
||||||
private static HandlerConstructor constructor = new BasicHandlerConstructor();
|
private static HandlerConstructor constructor = new BasicHandlerConstructor();
|
||||||
@ -37,8 +33,8 @@ public class ClassGenerator {
|
|||||||
addSpigotCompatibility(pool, BukkitDecodeHandler.class, decodeSuper);
|
addSpigotCompatibility(pool, BukkitDecodeHandler.class, decodeSuper);
|
||||||
addSpigotCompatibility(pool, BukkitEncodeHandler.class, encodeSuper);
|
addSpigotCompatibility(pool, BukkitEncodeHandler.class, encodeSuper);
|
||||||
} else {
|
} else {
|
||||||
Class decodeSuper = Class.forName(getPSPackage() + ".wrapped.WrappedDecoder");
|
Class decodeSuper = Class.forName(getPSPackage().equals("unknown") ? "protocolsupport.protocol.pipeline.common.PacketDecoder" : getPSPackage() + ".wrapped.WrappedDecoder");
|
||||||
Class encodeSuper = Class.forName(getPSPackage() + ".wrapped.WrappedEncoder");
|
Class encodeSuper = Class.forName(getPSPackage().equals("unknown") ? "protocolsupport.protocol.pipeline.common.PacketEncoder" : getPSPackage() + ".wrapped.WrappedEncoder");
|
||||||
// Generate the classes
|
// Generate the classes
|
||||||
addPSCompatibility(pool, BukkitDecodeHandler.class, decodeSuper);
|
addPSCompatibility(pool, BukkitDecodeHandler.class, decodeSuper);
|
||||||
addPSCompatibility(pool, BukkitEncodeHandler.class, encodeSuper);
|
addPSCompatibility(pool, BukkitEncodeHandler.class, encodeSuper);
|
||||||
@ -114,6 +110,7 @@ public class ClassGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Class addPSCompatibility(ClassPool pool, Class input, Class superclass) {
|
private static Class addPSCompatibility(ClassPool pool, Class input, Class superclass) {
|
||||||
|
boolean newPS = getPSPackage().equals("unknown");
|
||||||
String newName = "us.myles.ViaVersion.classgenerator.generated." + input.getSimpleName();
|
String newName = "us.myles.ViaVersion.classgenerator.generated." + input.getSimpleName();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -122,32 +119,34 @@ public class ClassGenerator {
|
|||||||
CtClass toExtend = pool.get(superclass.getName());
|
CtClass toExtend = pool.get(superclass.getName());
|
||||||
generated.setSuperclass(toExtend);
|
generated.setSuperclass(toExtend);
|
||||||
|
|
||||||
// Override setRealEncoder / setRealDecoder
|
if (!newPS) {
|
||||||
pool.importPackage(getPSPackage());
|
// Override setRealEncoder / setRealDecoder
|
||||||
pool.importPackage(getPSPackage() + ".wrapped");
|
pool.importPackage(getPSPackage());
|
||||||
if (superclass.getName().endsWith("Decoder")) {
|
pool.importPackage(getPSPackage() + ".wrapped");
|
||||||
// Decoder
|
if (superclass.getName().endsWith("Decoder")) {
|
||||||
generated.addMethod(CtMethod.make("public void setRealDecoder(IPacketDecoder dec) {\n" +
|
// Decoder
|
||||||
" ((WrappedDecoder) this.minecraftDecoder).setRealDecoder(dec);\n" +
|
generated.addMethod(CtMethod.make("public void setRealDecoder(IPacketDecoder dec) {\n" +
|
||||||
" }", generated));
|
" ((WrappedDecoder) this.minecraftDecoder).setRealDecoder(dec);\n" +
|
||||||
} else {
|
" }", generated));
|
||||||
// Encoder
|
} else {
|
||||||
pool.importPackage("protocolsupport.api");
|
// Encoder
|
||||||
pool.importPackage("java.lang.reflect");
|
pool.importPackage("protocolsupport.api");
|
||||||
generated.addMethod(CtMethod.make("public void setRealEncoder(IPacketEncoder enc) {\n" +
|
pool.importPackage("java.lang.reflect");
|
||||||
" try {\n" +
|
generated.addMethod(CtMethod.make("public void setRealEncoder(IPacketEncoder enc) {\n" +
|
||||||
// Tell ProtocolSupport to decode MINECRAFT_FUTURE packets using the default decoder (for 1.9.4)
|
" try {\n" +
|
||||||
" Field field = enc.getClass().getDeclaredField(\"version\");\n" +
|
// Tell ProtocolSupport to decode MINECRAFT_FUTURE packets using the default decoder (for 1.9.4)
|
||||||
" field.setAccessible(true);\n" +
|
" Field field = enc.getClass().getDeclaredField(\"version\");\n" +
|
||||||
" ProtocolVersion version = (ProtocolVersion) field.get(enc);\n" +
|
" field.setAccessible(true);\n" +
|
||||||
|
" ProtocolVersion version = (ProtocolVersion) field.get(enc);\n" +
|
||||||
|
|
||||||
" if (version == ProtocolVersion.MINECRAFT_FUTURE) enc = enc.getClass().getConstructor(\n" +
|
" if (version == ProtocolVersion.MINECRAFT_FUTURE) enc = enc.getClass().getConstructor(\n" +
|
||||||
" new Class[]{ProtocolVersion.class}).newInstance(new Object[] {ProtocolVersion.getLatest()});\n" +
|
" new Class[]{ProtocolVersion.class}).newInstance(new Object[] {ProtocolVersion.getLatest()});\n" +
|
||||||
" } catch (Exception e) {\n" +
|
" } catch (Exception e) {\n" +
|
||||||
// I guess we're not on 1.9.4
|
// I guess we're not on 1.9.4
|
||||||
" }\n" +
|
" }\n" +
|
||||||
" ((WrappedEncoder) this.minecraftEncoder).setRealEncoder(enc);\n" +
|
" ((WrappedEncoder) this.minecraftEncoder).setRealEncoder(enc);\n" +
|
||||||
" }", generated));
|
" }", generated));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return generated.toClass(HandlerConstructor.class.getClassLoader());
|
return generated.toClass(HandlerConstructor.class.getClassLoader());
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren