We now support latest ProtocolLib snapshots. I'm sort of perplexed why this works, but seems like our initializer is wrapping itself somehow? Well it works

This commit is contained in:
retrooper committed 2022-03-08 19:20:14 +01:00
1 parent 0b458c2e4c
commit dc5d15ba85
2 files changed
+2 -69

No files matched your search

@@ -39,12 +39,12 @@ public class PEChannelInitializerModern extends ChannelInitializer<Channel> {
public static void postInitChannel(Channel channel) {
if (channel.getClass().equals(NioSocketChannel.class)
|| channel.getClass().equals(EpollSocketChannel.class)) {
|| channel.getClass().equals(EpollSocketChannel.class)) {
PlayerChannelHandlerModern channelHandler = new PlayerChannelHandlerModern();
if (channel.pipeline().get("packet_handler") != null) {
String handlerName = PacketEvents.get().getHandlerName();
if (channel.pipeline().get(handlerName) != null) {
PacketEvents.get().getPlugin().getLogger().warning("[PacketEvents] Attempted to initialize a channel twice!");
//PacketEvents.get().getPlugin().getLogger().warning("[PacketEvents] Attempted to initialize a channel twice!");
} else {
channel.pipeline().addBefore("packet_handler", handlerName, channelHandler);
}
@@ -1,67 +0,0 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2021 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.retrooper.packetevents.injector.modern.early;
import io.github.retrooper.packetevents.PacketEvents;
import io.netty.channel.Channel;
import net.kyori.adventure.key.Key;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class PaperChannelInjector {
public static final boolean PAPER_INJECTION_METHOD = hasPaperInjectionMethod();
private PaperChannelInjector() {
}
public static void setPaperChannelInitializeListener() throws ReflectiveOperationException {
// Call io.papermc.paper.network.ChannelInitializeListenerHolder.addListener(net.kyori.adventure.key.Key, io.papermc.paper.network.ChannelInitializeListener)
// Create an interface proxy of ChannelInitializeListener
Class<?> listenerClass = Class.forName("io.papermc.paper.network.ChannelInitializeListener");
Object channelInitializeListener = Proxy.newProxyInstance(EarlyChannelInjectorModern.class.getClassLoader(), new Class[]{listenerClass}, (proxy, method, args) -> {
if (method.getName().equals("afterInitChannel")) {
PEChannelInitializerModern.postInitChannel((Channel) args[0]);
return null;
}
return method.invoke(proxy, args);
});
Class<?> holderClass = Class.forName("io.papermc.paper.network.ChannelInitializeListenerHolder");
Method addListenerMethod = holderClass.getDeclaredMethod("addListener", Key.class, listenerClass);
Key key = Key.key(PacketEvents.get().getHandlerName().toLowerCase(), "injector");
addListenerMethod.invoke(null, key, channelInitializeListener);
}
public static void removePaperChannelInitializeListener() throws ReflectiveOperationException {
Class<?> holderClass = Class.forName("io.papermc.paper.network.ChannelInitializeListenerHolder");
Method addListenerMethod = holderClass.getDeclaredMethod("removeListener", Key.class);
Key key = Key.key(PacketEvents.get().getHandlerName().toLowerCase(), "injector");
addListenerMethod.invoke(null, key);
}
private static boolean hasPaperInjectionMethod() {
try {
Class.forName("io.papermc.paper.network.ChannelInitializeListener");
return true;
} catch (ReflectiveOperationException e) {
return false;
}
}
}