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;
}
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;
}
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);
}
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);
}
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);
}
});
}
}
Aggregations