Search in sources :

Example 11 with Channel

use of net.minecraft.util.io.netty.channel.Channel in project packetevents by retrooper.

the class EarlyChannelInjectorLegacy method inject.

@Override
public void inject() {
    try {
        Object serverConnection = NMSUtils.getMinecraftServerConnection();
        for (Field field : serverConnection.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            Object value = null;
            try {
                value = field.get(serverConnection);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if (value instanceof List) {
                // Get the list.
                List<?> listWrapper = new ListWrapper((List<?>) value) {

                    @Override
                    public void processAdd(Object o) {
                        if (o instanceof ChannelFuture) {
                            try {
                                injectChannelFuture((ChannelFuture) o);
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                };
                HashMap<Field, Object> map = new HashMap<>();
                map.put(field, serverConnection);
                injectedLists.add(map);
                field.set(serverConnection, listWrapper);
                synchronized (listWrapper) {
                    for (Object serverChannel : (List<?>) value) {
                        // Is this the server channel future list?
                        if (serverChannel instanceof ChannelFuture) {
                            // Yes it is...
                            injectChannelFuture((ChannelFuture) serverChannel);
                        } else {
                            // Wrong list
                            break;
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        PacketEvents.get().getPlugin().getLogger().severe("PacketEvents failed to inject!");
        ex.printStackTrace();
    }
    // Player channels might have been registered already. Let us add our handlers. We are a little late though.
    // This only happens when you join extremely early on older versions of minecraft.
    List<Object> networkManagers = NMSUtils.getNetworkManagers();
    synchronized (networkManagers) {
        for (Object networkManager : networkManagers) {
            WrappedPacket networkManagerWrapper = new WrappedPacket(new NMSPacket(networkManager), NMSUtils.networkManagerClass);
            Channel channel = (Channel) networkManagerWrapper.readObject(0, NMSUtils.nettyChannelClass);
            if (channel == null || (!(channel.getClass().equals(NioSocketChannel.class))) && !(channel.getClass().equals(EpollSocketChannel.class))) {
                continue;
            }
            if (channel.pipeline().get(PacketEvents.get().getHandlerName()) != null) {
                channel.pipeline().remove(PacketEvents.get().getHandlerName());
            }
            if (channel.pipeline().get("packet_handler") != null) {
                channel.pipeline().addBefore("packet_handler", PacketEvents.get().getHandlerName(), new PlayerChannelHandlerLegacy());
            }
        }
    }
}
Also used : ChannelFuture(net.minecraft.util.io.netty.channel.ChannelFuture) HashMap(java.util.HashMap) WrappedPacket(io.github.retrooper.packetevents.packetwrappers.WrappedPacket) Channel(net.minecraft.util.io.netty.channel.Channel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) NioSocketChannel(net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel) ListWrapper(io.github.retrooper.packetevents.utils.list.ListWrapper) NioSocketChannel(net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel) Field(java.lang.reflect.Field) PlayerChannelHandlerLegacy(io.github.retrooper.packetevents.injector.legacy.PlayerChannelHandlerLegacy) ArrayList(java.util.ArrayList) List(java.util.List) NMSPacket(io.github.retrooper.packetevents.packetwrappers.NMSPacket)

Example 12 with Channel

use of net.minecraft.util.io.netty.channel.Channel in project packetevents by retrooper.

the class LateChannelInjectorLegacy method flushPackets.

@Override
public void flushPackets(Object ch) {
    Channel channel = (Channel) ch;
    channel.flush();
}
Also used : EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) NioSocketChannel(net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel) Channel(net.minecraft.util.io.netty.channel.Channel)

Example 13 with Channel

use of net.minecraft.util.io.netty.channel.Channel in project packetevents by retrooper.

the class LateChannelInjectorLegacy method writePacket.

@Override
public void writePacket(Object ch, Object rawNMSPacket) {
    Channel channel = (Channel) ch;
    channel.write(rawNMSPacket);
}
Also used : EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) NioSocketChannel(net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel) Channel(net.minecraft.util.io.netty.channel.Channel)

Example 14 with Channel

use of net.minecraft.util.io.netty.channel.Channel in project packetevents by retrooper.

the class LateChannelInjectorLegacy method injectPlayer.

@Override
public void injectPlayer(Player player) {
    PlayerChannelHandlerLegacy playerChannelHandlerLegacy = new PlayerChannelHandlerLegacy();
    playerChannelHandlerLegacy.player = player;
    Channel channel = (Channel) PacketEvents.get().getPlayerUtils().getChannel(player);
    if (channel.getClass().equals(NioSocketChannel.class) || channel.getClass().equals(EpollSocketChannel.class)) {
        channel.pipeline().addBefore("packet_handler", PacketEvents.get().getHandlerName(), playerChannelHandlerLegacy);
    }
}
Also used : NioSocketChannel(net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel) PlayerChannelHandlerLegacy(io.github.retrooper.packetevents.injector.legacy.PlayerChannelHandlerLegacy) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) NioSocketChannel(net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel) Channel(net.minecraft.util.io.netty.channel.Channel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel)

Example 15 with Channel

use of net.minecraft.util.io.netty.channel.Channel in project TheAPI by TheDevTec.

the class PacketHandler_Legacy method registerChannelHandler.

private void registerChannelHandler() {
    networkManagers = (List<?>) (Ref.get(serverConnection, "e") != null ? Ref.get(serverConnection, "e") : Ref.get(serverConnection, "f"));
    if (networkManagers == null) {
        // modded server
        for (Field f : Ref.getAllFields(Ref.nms("ServerConnection"))) if (List.class == f.getType()) {
            networkManagers = (List<?>) Ref.get(serverConnection, f);
            break;
        }
    }
    if (networkManagers == null)
        return;
    if (networkManagers.isEmpty()) {
        networkManagers = (List<?>) (Ref.get(serverConnection, "f") != null ? Ref.get(serverConnection, "f") : Ref.get(serverConnection, "e"));
        if (networkManagers == null) {
            // modded server
            for (Field f : Ref.getAllFields(Ref.nms("ServerConnection"))) if (List.class == f.getType()) {
                networkManagers = (List<?>) Ref.get(serverConnection, f);
                break;
            }
        }
    }
    if (networkManagers == null)
        return;
    createServerChannelHandler();
    for (Object item : networkManagers) {
        if (!ChannelFuture.class.isInstance(item))
            continue;
        Channel serverChannel = ((ChannelFuture) item).channel();
        serverChannels.add(serverChannel);
        serverChannel.pipeline().addFirst(serverChannelHandler);
    }
}
Also used : ChannelFuture(net.minecraft.util.io.netty.channel.ChannelFuture) Field(java.lang.reflect.Field) Channel(net.minecraft.util.io.netty.channel.Channel) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Channel (net.minecraft.util.io.netty.channel.Channel)16 EpollSocketChannel (io.netty.channel.epoll.EpollSocketChannel)10 NioSocketChannel (net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel)10 Field (java.lang.reflect.Field)4 PlayerChannelHandlerLegacy (io.github.retrooper.packetevents.injector.legacy.PlayerChannelHandlerLegacy)3 ArrayList (java.util.ArrayList)3 ChannelFuture (net.minecraft.util.io.netty.channel.ChannelFuture)3 ListWrapper (io.github.retrooper.packetevents.utils.list.ListWrapper)2 List (java.util.List)2 ChannelHandler (net.minecraft.util.io.netty.channel.ChannelHandler)2 NMSPacket (io.github.retrooper.packetevents.packetwrappers.NMSPacket)1 WrappedPacket (io.github.retrooper.packetevents.packetwrappers.WrappedPacket)1 HashMap (java.util.HashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 ServerListPingEvent (me.devtec.theapi.bukkit.events.ServerListPingEvent)1 PlayerProfile (me.devtec.theapi.bukkit.events.ServerListPingEvent.PlayerProfile)1 EntityPlayer (net.minecraft.server.v1_7_R4.EntityPlayer)1 IChatBaseComponent (net.minecraft.server.v1_7_R4.IChatBaseComponent)1 PacketStatusOutServerInfo (net.minecraft.server.v1_7_R4.PacketStatusOutServerInfo)1 ServerPing (net.minecraft.server.v1_7_R4.ServerPing)1