Search in sources :

Example 1 with BulkMessage

use of org.lanternpowered.server.network.message.BulkMessage in project LanternServer by LanternPowered.

the class CodecPlayInPlayerVehicleControls method decode.

@Override
public Message decode(CodecContext context, ByteBuffer buf) throws CodecException {
    float sideways = buf.readFloat();
    float forwards = buf.readFloat();
    final byte flags = buf.readByte();
    final boolean jump = (flags & 0x1) != 0;
    final boolean sneak = (flags & 0x2) != 0;
    final List<Message> messages = new ArrayList<>();
    final boolean lastSneak = firstNonNull(context.getChannel().attr(SNEAKING).getAndSet(sneak), false);
    if (lastSneak != sneak) {
        messages.add(new MessagePlayInPlayerSneak(sneak));
    }
    final boolean lastJump = firstNonNull(context.getChannel().attr(JUMPING).getAndSet(jump), false);
    if (lastJump != jump && !firstNonNull(context.getChannel().attr(CodecPlayInPlayerAction.CANCEL_NEXT_JUMP_MESSAGE).getAndSet(false), false)) {
        messages.add(new MessagePlayInPlayerVehicleJump(jump, 0f));
    }
    // The mc client already applies the sneak speed, but we want to choose it
    if (sneak) {
        sideways /= 0.3f;
        forwards /= 0.3f;
    }
    messages.add(new MessagePlayInPlayerMovementInput(forwards, sideways));
    return messages.size() == 1 ? messages.get(0) : new BulkMessage(messages);
}
Also used : MessagePlayInPlayerMovementInput(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerMovementInput) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) Message(org.lanternpowered.server.network.message.Message) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) MessagePlayInPlayerVehicleJump(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerVehicleJump) ArrayList(java.util.ArrayList) MessagePlayInPlayerSneak(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerSneak)

Example 2 with BulkMessage

use of org.lanternpowered.server.network.message.BulkMessage in project LanternServer by LanternPowered.

the class MessageCodecHandler method processMessage.

private void processMessage(Message message, List<Object> output, Protocol protocol, ProtocolState state, CodecContext context) {
    if (message == NullMessage.INSTANCE) {
        return;
    }
    if (message instanceof BulkMessage) {
        ((BulkMessage) message).getMessages().forEach(message1 -> processMessage(message1, output, protocol, state, context));
        return;
    }
    final MessageRegistration messageRegistration = (MessageRegistration) protocol.inbound().findByMessageType(message.getClass()).orElseThrow(() -> new DecoderException("The returned message type is not attached to the used protocol state (" + state.toString() + ")!"));
    final List<Processor> processors = messageRegistration.getProcessors();
    // Only process if there are processors found
    if (!processors.isEmpty()) {
        for (Processor processor : processors) {
            // The processor should handle the output messages
            processor.process(context, message, output);
        }
    } else {
        final Optional<Handler> optHandler = messageRegistration.getHandler();
        if (optHandler.isPresent()) {
            // Add the message to the output
            output.add(new HandlerMessage(message, optHandler.get()));
        } else {
            output.add(message);
        }
    }
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) MessageRegistration(org.lanternpowered.server.network.message.MessageRegistration) Processor(org.lanternpowered.server.network.message.processor.Processor) Handler(org.lanternpowered.server.network.message.handler.Handler) HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage)

Aggregations

BulkMessage (org.lanternpowered.server.network.message.BulkMessage)2 DecoderException (io.netty.handler.codec.DecoderException)1 ArrayList (java.util.ArrayList)1 HandlerMessage (org.lanternpowered.server.network.message.HandlerMessage)1 Message (org.lanternpowered.server.network.message.Message)1 MessageRegistration (org.lanternpowered.server.network.message.MessageRegistration)1 Handler (org.lanternpowered.server.network.message.handler.Handler)1 Processor (org.lanternpowered.server.network.message.processor.Processor)1 MessagePlayInPlayerMovementInput (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerMovementInput)1 MessagePlayInPlayerSneak (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerSneak)1 MessagePlayInPlayerVehicleJump (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerVehicleJump)1