Search in sources :

Example 1 with PacketBuffer

use of net.minecraft.network.PacketBuffer in project MinecraftForge by MinecraftForge.

the class FMLProxyPacket method copy.

public FMLProxyPacket copy() {
    FMLProxyPacket pkt = new FMLProxyPacket(new PacketBuffer(payload.duplicate()), channel);
    pkt.dispatcher = dispatcher;
    pkt.netHandler = netHandler;
    pkt.target = target;
    return pkt;
}
Also used : PacketBuffer(net.minecraft.network.PacketBuffer)

Example 2 with PacketBuffer

use of net.minecraft.network.PacketBuffer in project MinecraftForge by MinecraftForge.

the class FMLProxyPacket method toS3FPackets.

public List<Packet<INetHandlerPlayClient>> toS3FPackets() throws IOException {
    List<Packet<INetHandlerPlayClient>> ret = Lists.newArrayList();
    byte[] data = payload.array();
    if (data.length < PART_SIZE) {
        ret.add(new SPacketCustomPayload(channel, new PacketBuffer(payload.duplicate())));
    } else {
        //We add a byte header so -1
        int parts = (int) Math.ceil(data.length / (double) (PART_SIZE - 1));
        if (parts > 255) {
            throw new IllegalArgumentException("Payload may not be larger than " + MAX_LENGTH + " bytes");
        }
        PacketBuffer preamble = new PacketBuffer(Unpooled.buffer());
        preamble.writeString(channel);
        preamble.writeByte(parts);
        preamble.writeInt(data.length);
        ret.add(new SPacketCustomPayload("FML|MP", preamble));
        int offset = 0;
        for (int x = 0; x < parts; x++) {
            int length = Math.min(PART_SIZE, data.length - offset + 1);
            byte[] tmp = new byte[length];
            tmp[0] = (byte) (x & 0xFF);
            System.arraycopy(data, offset, tmp, 1, tmp.length - 1);
            offset += tmp.length - 1;
            ret.add(new SPacketCustomPayload("FML|MP", new PacketBuffer(Unpooled.wrappedBuffer(tmp))));
        }
    }
    return ret;
}
Also used : Packet(net.minecraft.network.Packet) SPacketCustomPayload(net.minecraft.network.play.server.SPacketCustomPayload) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 3 with PacketBuffer

use of net.minecraft.network.PacketBuffer in project MinecraftForge by MinecraftForge.

the class ByteBufUtils method writeTag.

/**
     * Write an {@link NBTTagCompound} to the byte buffer. It uses the minecraft encoding.
     *
     * @param to The buffer to write to
     * @param tag The tag to write
     */
public static void writeTag(ByteBuf to, NBTTagCompound tag) {
    PacketBuffer pb = new PacketBuffer(to);
    pb.writeCompoundTag(tag);
}
Also used : PacketBuffer(net.minecraft.network.PacketBuffer)

Example 4 with PacketBuffer

use of net.minecraft.network.PacketBuffer in project MinecraftForge by MinecraftForge.

the class FMLIndexedMessageToMessageCodec method encode.

@Override
protected final void encode(ChannelHandlerContext ctx, A msg, List<Object> out) throws Exception {
    PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
    byte discriminator = types.get(msg.getClass());
    buffer.writeByte(discriminator);
    encodeInto(ctx, msg, buffer);
    FMLProxyPacket proxy = new FMLProxyPacket(buffer, /*.copy()*/
    ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get());
    WeakReference<FMLProxyPacket> ref = ctx.attr(INBOUNDPACKETTRACKER).get().get();
    FMLProxyPacket old = ref == null ? null : ref.get();
    if (old != null) {
        proxy.setDispatcher(old.getDispatcher());
    }
    out.add(proxy);
}
Also used : FMLProxyPacket(net.minecraftforge.fml.common.network.internal.FMLProxyPacket) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 5 with PacketBuffer

use of net.minecraft.network.PacketBuffer in project MinecraftForge by MinecraftForge.

the class PacketLoggingHandler method register.

public static void register(NetworkManager manager) {
    ChannelPipeline pipeline = manager.channel().pipeline();
    final EnumPacketDirection direction = manager.getDirection();
    if (manager.isLocalChannel()) {
        pipeline.addBefore("packet_handler", "splitter", new SimpleChannelInboundHandler<Packet<?>>() {

            String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: C->S" : "CLIENT: S->C");

            @Override
            protected void channelRead0(ChannelHandlerContext ctx, Packet<?> msg) throws Exception {
                PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
                msg.writePacketData(buf);
                FMLLog.log(Level.DEBUG, "%s %s:\n%s", prefix, msg.getClass().getSimpleName(), ByteBufUtils.getContentDump(buf));
                ctx.fireChannelRead(msg);
            }
        });
        pipeline.addBefore("splitter", "prepender", new ChannelOutboundHandlerAdapter() {

            String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: S->C" : "CLIENT: C->S");

            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                if (msg instanceof Packet<?>) {
                    PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
                    ((Packet<?>) msg).writePacketData(buf);
                    FMLLog.log(Level.DEBUG, "%s %s:\n%s", prefix, msg.getClass().getSimpleName(), ByteBufUtils.getContentDump(buf));
                }
                ctx.write(msg, promise);
            }
        });
    } else {
        pipeline.replace("splitter", "splitter", new NettyVarint21FrameDecoder() {

            String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: C->S" : "CLIENT: S->C");

            @Override
            protected void decode(ChannelHandlerContext context, ByteBuf input, List<Object> output) throws Exception {
                super.decode(context, input, output);
                Iterator<Object> itr = output.iterator();
                while (itr.hasNext()) {
                    ByteBuf pkt = (ByteBuf) itr.next();
                    pkt.markReaderIndex();
                    FMLLog.log(Level.DEBUG, "%s:\n%s", prefix, ByteBufUtils.getContentDump(pkt));
                    pkt.resetReaderIndex();
                }
            }
        });
        pipeline.replace("prepender", "prepender", new NettyVarint21FrameEncoder() {

            String prefix = (direction == EnumPacketDirection.SERVERBOUND ? "SERVER: S->C" : "CLIENT: C->S");

            @Override
            protected void encode(ChannelHandlerContext context, ByteBuf input, ByteBuf output) throws Exception {
                input.markReaderIndex();
                FMLLog.log(Level.DEBUG, "%s:\n%s", prefix, ByteBufUtils.getContentDump(input));
                input.resetReaderIndex();
                super.encode(context, input, output);
            }
        });
    }
}
Also used : Packet(net.minecraft.network.Packet) NettyVarint21FrameDecoder(net.minecraft.network.NettyVarint21FrameDecoder) EnumPacketDirection(net.minecraft.network.EnumPacketDirection) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) ChannelPipeline(io.netty.channel.ChannelPipeline) Iterator(java.util.Iterator) NettyVarint21FrameEncoder(net.minecraft.network.NettyVarint21FrameEncoder) PacketBuffer(net.minecraft.network.PacketBuffer)

Aggregations

PacketBuffer (net.minecraft.network.PacketBuffer)103 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)18 FMLProxyPacket (net.minecraftforge.fml.common.network.internal.FMLProxyPacket)14 IOException (java.io.IOException)13 ByteBuf (io.netty.buffer.ByteBuf)11 SPacketCustomPayload (net.minecraft.network.play.server.SPacketCustomPayload)7 BlockPos (net.minecraft.util.math.BlockPos)7 HashMap (java.util.HashMap)4 DimensionInformation (mcjty.rftoolsdim.dimensions.DimensionInformation)4 DimensionDescriptor (mcjty.rftoolsdim.dimensions.description.DimensionDescriptor)4 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 NetHandlerPlayServer (net.minecraft.network.NetHandlerPlayServer)3 Test (org.junit.Test)3 PacketBufferBC (buildcraft.lib.net.PacketBufferBC)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 PacketCustomTileData (gregtech.api.net.PacketCustomTileData)2 PacketUIWidgetUpdate (gregtech.api.net.PacketUIWidgetUpdate)2 AbstractData (hellfirepvp.fracture.common.data.AbstractData)2