use of net.minecraft.network.Packet 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.Packet in project MinecraftForge by MinecraftForge.
the class FMLEmbeddedChannel method generatePacketFrom.
/**
* Utility method to generate a regular packet from a custom packet. Basically, it writes the packet through the
* outbound side which should have a message to message codec present (such as {@link FMLIndexedMessageToMessageCodec},
* transforming from mod packets to standard {@link FMLProxyPacket}s.
*
* This is mostly useful in cases where vanilla expects a packet, such as the TileEntity getDescriptionPacket.
*
* @param object The inbound packet
* @return A Packet suitable for passing to vanilla network code.
*/
public Packet<?> generatePacketFrom(Object object) {
OutboundTarget outboundTarget = attr(FMLOutboundHandler.FML_MESSAGETARGET).getAndSet(OutboundTarget.NOWHERE);
writeOutbound(object);
Packet<?> pkt = (Packet<?>) outboundMessages().poll();
attr(FMLOutboundHandler.FML_MESSAGETARGET).set(outboundTarget);
return pkt;
}
use of net.minecraft.network.Packet 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