use of io.netty.handler.codec.EncoderException in project Glowstone by GlowstoneMC.
the class CodecsHandler method encode.
@Override
protected void encode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception {
// find codec
Class<? extends Message> clazz = msg.getClass();
CodecRegistration reg = protocol.getCodecRegistration(clazz);
if (reg == null) {
throw new EncoderException("Unknown message type: " + clazz + ".");
}
// write header
ByteBuf headerBuf = ctx.alloc().buffer(8);
ByteBufUtils.writeVarInt(headerBuf, reg.getOpcode());
// write body
ByteBuf messageBuf = ctx.alloc().buffer();
messageBuf = reg.getCodec().encode(messageBuf, msg);
out.add(Unpooled.wrappedBuffer(headerBuf, messageBuf));
}
use of io.netty.handler.codec.EncoderException in project RecurrentComplex by Ivorforce.
the class RCPacketBuffer method readBigTag.
@Nullable
public NBTTagCompound readBigTag() throws IOException {
int i = this.readerIndex();
byte b0 = this.readByte();
if (b0 == 0) {
return null;
} else {
this.readerIndex(i);
try {
return CompressedStreamTools.read(new ByteBufInputStream(this), new NBTSizeTracker(2097152L * 4));
} catch (IOException ioexception) {
throw new EncoderException(ioexception);
}
}
}
use of io.netty.handler.codec.EncoderException in project Galacticraft by micdoodle8.
the class PacketCustom method do_compress.
/**
* Compresses the payload ByteBuf after the type byte
*/
private void do_compress() {
Deflater deflater = new Deflater();
try {
readerIndex(1);
int len = readableBytes();
deflater.setInput(array(), readerIndex(), len);
deflater.finish();
byte[] out = new byte[len];
int clen = deflater.deflate(out);
if (// not worth compressing, gets larger
clen >= len - 5 || !deflater.finished()) {
return;
}
clear();
writeByte(type | 0x80);
writeVarInt(len);
writeByteArray(out);
} catch (Exception e) {
throw new EncoderException(e);
} finally {
readerIndex(0);
deflater.end();
}
}
use of io.netty.handler.codec.EncoderException in project LanternServer by LanternPowered.
the class CodecPlayOutBossBar method encode.
@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutBossBar message) throws CodecException {
ByteBuffer buf = context.byteBufAlloc().buffer();
buf.writeUniqueId(message.getUniqueId());
if (message instanceof MessagePlayOutBossBar.Add) {
MessagePlayOutBossBar.Add message0 = (MessagePlayOutBossBar.Add) message;
buf.writeVarInt(0);
buf.write(Types.LOCALIZED_TEXT, message0.getTitle());
buf.writeFloat(message0.getHealth());
buf.writeVarInt(((LanternBossBarColor) message0.getColor()).getInternalId());
buf.writeVarInt(((LanternBossBarOverlay) message0.getOverlay()).getInternalId());
buf.writeByte(toFlags(message0.isDarkenSky(), message0.isEndMusic()));
} else if (message instanceof MessagePlayOutBossBar.Remove) {
buf.writeVarInt(1);
} else if (message instanceof MessagePlayOutBossBar.UpdatePercent) {
buf.writeVarInt(2);
buf.writeFloat(((MessagePlayOutBossBar.UpdatePercent) message).getPercent());
} else if (message instanceof MessagePlayOutBossBar.UpdateTitle) {
buf.writeVarInt(3);
buf.write(Types.LOCALIZED_TEXT, ((MessagePlayOutBossBar.UpdateTitle) message).getTitle());
} else if (message instanceof MessagePlayOutBossBar.UpdateStyle) {
MessagePlayOutBossBar.UpdateStyle message0 = (MessagePlayOutBossBar.UpdateStyle) message;
buf.writeVarInt(4);
buf.writeVarInt(((LanternBossBarColor) message0.getColor()).getInternalId());
buf.writeVarInt(((LanternBossBarOverlay) message0.getOverlay()).getInternalId());
} else if (message instanceof MessagePlayOutBossBar.UpdateMisc) {
MessagePlayOutBossBar.UpdateMisc message0 = (MessagePlayOutBossBar.UpdateMisc) message;
buf.writeVarInt(5);
buf.writeByte(toFlags(message0.isDarkenSky(), message0.isEndMusic()));
} else {
throw new EncoderException("Unsupported message type: " + message.getClass().getName());
}
return buf;
}
use of io.netty.handler.codec.EncoderException in project LanternServer by LanternPowered.
the class CodecPlayOutEffect method encode.
@Override
public ByteBuffer encode(CodecContext context, Message message) throws CodecException {
final ByteBuffer buf = context.byteBufAlloc().buffer();
if (message instanceof MessagePlayOutEffect) {
final MessagePlayOutEffect message1 = (MessagePlayOutEffect) message;
buf.writeInteger(message1.getType());
buf.write(Types.VECTOR_3_I, message1.getPosition());
buf.writeInteger(message1.getData());
buf.writeBoolean(message1.isBroadcast());
} else if (message instanceof MessagePlayOutRecord) {
final MessagePlayOutRecord message1 = (MessagePlayOutRecord) message;
buf.writeInteger(1010);
buf.write(Types.VECTOR_3_I, message1.getPosition());
buf.writeInteger(message1.getRecord().map(type -> 2256 + ((LanternRecordType) type).getInternalId()).orElse(0));
buf.writeBoolean(false);
} else {
throw new EncoderException("Unsupported message type: " + message.getClass().getName());
}
return buf;
}
Aggregations