use of io.netty.handler.codec.EncoderException in project LanternServer by LanternPowered.
the class CodecPlayOutEntityEquipment method encode.
@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutEntityEquipment message) throws CodecException {
final ByteBuffer buf = context.byteBufAlloc().buffer();
buf.writeVarInt(message.getEntityId());
buf.writeVarInt(message.getSlotIndex());
final Object item = message.getItem();
if (item instanceof ItemStack) {
buf.write(Types.ITEM_STACK, (ItemStack) item);
} else if (item instanceof RawItemStack || item == null) {
buf.write(Types.RAW_ITEM_STACK, (RawItemStack) item);
} else {
throw new EncoderException("Invalid item type:" + item.getClass().getName());
}
return buf;
}
use of io.netty.handler.codec.EncoderException in project LanternServer by LanternPowered.
the class CodecPlayInOutCustomPayload method encode0.
@Override
protected MessageResult encode0(CodecContext context, Message message) throws CodecException {
if (message instanceof MessagePlayInOutBrand) {
return new MessageResult("MC|Brand", context.byteBufAlloc().buffer().writeString(((MessagePlayInOutBrand) message).getBrand()));
} else if (message instanceof MessagePlayOutOpenBook) {
final ByteBuffer buf = context.byteBufAlloc().buffer();
buf.writeVarInt(((MessagePlayOutOpenBook) message).getHandType() == HandTypes.MAIN_HAND ? 0 : 1);
return new MessageResult("MC|BOpen", buf);
} else if (message instanceof MessagePlayOutStopSounds) {
final MessagePlayOutStopSounds message0 = (MessagePlayOutStopSounds) message;
final ByteBuffer buf = context.byteBufAlloc().buffer();
buf.writeString(message0.getCategory() == null ? "" : message0.getCategory().getName());
buf.writeString(message0.getSound() == null ? "" : message0.getSound());
return new MessageResult("MC|StopSound", buf);
}
throw new EncoderException("Unsupported message type: " + message);
}
use of io.netty.handler.codec.EncoderException in project LanternServer by LanternPowered.
the class CodecPlayOutWorldBorder method encode.
@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutWorldBorder message) throws CodecException {
ByteBuffer buf = context.byteBufAlloc().buffer();
if (message instanceof MessagePlayOutWorldBorder.Initialize) {
MessagePlayOutWorldBorder.Initialize message1 = (MessagePlayOutWorldBorder.Initialize) message;
buf.writeVarInt(3);
buf.writeDouble(message1.getCenterX());
buf.writeDouble(message1.getCenterZ());
buf.writeDouble(message1.getOldDiameter());
buf.writeDouble(message1.getNewDiameter());
buf.writeVarLong(message1.getLerpTime());
buf.writeVarInt(message1.getWorldSize());
buf.writeVarInt(message1.getWarningTime());
buf.writeVarInt(message1.getWarningDistance());
} else if (message instanceof MessagePlayOutWorldBorder.UpdateCenter) {
MessagePlayOutWorldBorder.UpdateCenter message1 = (MessagePlayOutWorldBorder.UpdateCenter) message;
buf.writeVarInt(2);
buf.writeDouble(message1.getX());
buf.writeDouble(message1.getZ());
} else if (message instanceof MessagePlayOutWorldBorder.UpdateLerpedDiameter) {
MessagePlayOutWorldBorder.UpdateLerpedDiameter message1 = (MessagePlayOutWorldBorder.UpdateLerpedDiameter) message;
buf.writeVarInt(1);
buf.writeDouble(message1.getOldDiameter());
buf.writeDouble(message1.getNewDiameter());
buf.writeVarLong(message1.getLerpTime());
} else if (message instanceof MessagePlayOutWorldBorder.UpdateDiameter) {
buf.writeVarInt(0);
buf.writeDouble(((MessagePlayOutWorldBorder.UpdateDiameter) message).getDiameter());
} else if (message instanceof MessagePlayOutWorldBorder.UpdateWarningDistance) {
buf.writeVarInt(5);
buf.writeVarInt(((MessagePlayOutWorldBorder.UpdateWarningDistance) message).getDistance());
} else if (message instanceof MessagePlayOutWorldBorder.UpdateWarningTime) {
buf.writeVarInt(4);
buf.writeVarInt(((MessagePlayOutWorldBorder.UpdateWarningTime) message).getTime());
} 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 MessageProcessorHandler method acceptOutboundMessage.
@Override
public boolean acceptOutboundMessage(Object msg) throws Exception {
final Message message = (Message) msg;
final Protocol protocol = this.codecContext.getSession().getProtocol();
final MessageRegistration registration = protocol.outbound().findByMessageType(message.getClass()).orElse(null);
if (registration == null) {
throw new EncoderException("Message type (" + message.getClass().getName() + ") is not registered in state " + this.codecContext.getSession().getProtocolState().name() + "!");
}
final List<Processor> processors = ((MessageRegistration) protocol.outbound().findByMessageType(message.getClass()).get()).getProcessors();
// Only process if there are processors found
if (!processors.isEmpty()) {
final List<Object> messages = new ArrayList<>();
for (Processor processor : processors) {
// The processor should handle the output messages
processor.process(this.codecContext, message, messages);
}
if (message instanceof ReferenceCounted && !messages.contains(message)) {
((ReferenceCounted) message).release();
}
if (!messages.isEmpty()) {
this.messages.set(messages);
}
return true;
}
return false;
}
use of io.netty.handler.codec.EncoderException in project LanternServer by LanternPowered.
the class CodecPlayInOutCustomPayload method encode0.
@Override
protected MessageResult encode0(CodecContext context, Message message) throws CodecException {
if (message instanceof MessageForgeHandshakeInOutAck) {
return new MessageResult("FML|HS", context.byteBufAlloc().buffer(2).writeByte((byte) FML_HANDSHAKE_ACK).writeByte((byte) ((ForgeServerHandshakePhase) ((MessageForgeHandshakeInOutAck) message).getPhase()).ordinal()));
} else if (message instanceof MessageForgeHandshakeInOutHello) {
return new MessageResult("FML|HS", context.byteBufAlloc().buffer(2).writeByte((byte) FML_HANDSHAKE_SERVER_HELLO).writeByte((byte) FORGE_PROTOCOL));
} else if (message instanceof MessageForgeHandshakeInOutModList) {
Map<String, String> entries = ((MessageForgeHandshakeInOutModList) message).getEntries();
ByteBuffer buf = context.byteBufAlloc().buffer();
buf.writeByte((byte) FML_HANDSHAKE_MOD_LIST);
buf.writeVarInt(entries.size());
for (Map.Entry<String, String> en : entries.entrySet()) {
buf.writeString(en.getKey());
buf.writeString(en.getValue());
}
return new MessageResult("FML|HS", buf);
} else if (message instanceof MessageForgeHandshakeOutReset) {
return new MessageResult("FML|HS", context.byteBufAlloc().buffer(1).writeByte((byte) FML_HANDSHAKE_RESET));
}
throw new EncoderException("Unsupported message type: " + message);
}
Aggregations