From 61a082060d40599226e10d23426b94c6e39a7c93 Mon Sep 17 00:00:00 2001 From: Nico Britze Date: Fri, 6 Sep 2019 17:01:28 +0200 Subject: [PATCH] Add support for latest waterfall --- .../api/protocol/PacketRegistration.java | 26 +++++++++++++++++-- .../protocolize/ProtocolizePlugin.java | 3 +++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/protocolize-api/src/main/java/de/exceptionflug/protocolize/api/protocol/PacketRegistration.java b/protocolize-api/src/main/java/de/exceptionflug/protocolize/api/protocol/PacketRegistration.java index b1898f5c..103366b5 100644 --- a/protocolize-api/src/main/java/de/exceptionflug/protocolize/api/protocol/PacketRegistration.java +++ b/protocolize-api/src/main/java/de/exceptionflug/protocolize/api/protocol/PacketRegistration.java @@ -10,6 +10,7 @@ import java.lang.reflect.*; import java.util.Map; +import java.util.function.Supplier; import java.util.logging.Level; /** @@ -84,7 +85,11 @@ public void registerPacket(final Protocol protocol, final Direction direction, f try { final TIntObjectMap protocols = (TIntObjectMap) protocolsField.get(getDirectionData(protocol, direction)); for(final Integer protocolVersion : protocolIdMapping.keySet()) { - registerPacket(protocols, protocolVersion, protocolIdMapping.get(protocolVersion), clazz); + if(isWaterfall()) { + registerPacketWaterfall(protocols, protocolVersion, protocolIdMapping.get(protocolVersion), clazz); + } else { + registerPacketBungeeCord(protocols, protocolVersion, protocolIdMapping.get(protocolVersion), clazz); + } } ProxyServer.getInstance().getLogger().info("[Protocolize] Injected custom packet: "+clazz.getName()); } catch (final Exception e) { @@ -133,10 +138,27 @@ private Object getDirectionData(final Protocol protocol, final Direction directi return null; } - private void registerPacket(final TIntObjectMap protocols, final int protocolVersion, final int packetId, final Class clazz) throws IllegalAccessException, NoSuchMethodException { + public boolean isWaterfall() { + return protocolDataConstructorsField.getType().equals(Supplier[].class); + } + + private void registerPacketBungeeCord(final TIntObjectMap protocols, final int protocolVersion, final int packetId, final Class clazz) throws IllegalAccessException, NoSuchMethodException { final Object protocolData = protocols.get(protocolVersion); ((TObjectIntMap>)protocolDataPacketMapField.get(protocolData)).put(clazz, packetId); ((Constructor[])protocolDataConstructorsField.get(protocolData))[packetId] = clazz.getDeclaredConstructor(); } + private void registerPacketWaterfall(final TIntObjectMap protocols, final int protocolVersion, final int packetId, final Class clazz) throws IllegalAccessException, NoSuchMethodException { + final Object protocolData = protocols.get(protocolVersion); + ((TObjectIntMap>)protocolDataPacketMapField.get(protocolData)).put(clazz, packetId); + ((Supplier[])protocolDataConstructorsField.get(protocolData))[packetId] = () -> { + try { + return clazz.getDeclaredConstructor().newInstance(); + } catch (ReflectiveOperationException e) { + e.printStackTrace(); + } + return null; + }; + } + } diff --git a/protocolize-plugin/src/main/java/de/exceptionflug/protocolize/ProtocolizePlugin.java b/protocolize-plugin/src/main/java/de/exceptionflug/protocolize/ProtocolizePlugin.java index 8155b23f..04282b47 100644 --- a/protocolize-plugin/src/main/java/de/exceptionflug/protocolize/ProtocolizePlugin.java +++ b/protocolize-plugin/src/main/java/de/exceptionflug/protocolize/ProtocolizePlugin.java @@ -46,6 +46,9 @@ public class ProtocolizePlugin extends Plugin { public void onEnable() { ProxyServer.getInstance().getLogger().info("======= PROTOCOLIZE ======="); ProxyServer.getInstance().getLogger().info("Version " + getDescription().getVersion() + " by " + getDescription().getAuthor()); + if(ProtocolAPI.getPacketRegistration().isWaterfall()) { + ProxyServer.getInstance().getLogger().info("[Protocolize] Running on Waterfall. Please report bugs regarding protocolize under https://github.com/Exceptionflug/protocolize/issues"); + } ProxyServer.getInstance().getPluginManager().registerListener(this, new PlayerListener(this)); try {