Search in sources :

Example 31 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class AbstractCodecPlayInOutCustomPayload method decode.

@Override
public Message decode(CodecContext context, ByteBuffer buf) throws CodecException {
    final String channel = buf.readLimitedString(LanternChannelRegistrar.MAX_NAME_LENGTH);
    final int length = buf.available();
    if (length > Short.MAX_VALUE) {
        throw new DecoderException("CustomPayload messages may not be longer then " + Short.MAX_VALUE + " bytes");
    }
    final ByteBuffer content = buf.slice();
    final Message message = decode0(context, content, channel);
    if (content.available() > 0) {
        Lantern.getLogger().warn("Trailing bytes {}b after decoding with custom payload message codec {} with channel {}!\n{}", content.available(), getClass().getName(), channel, message);
    }
    // Skip all the bytes, we already processed them
    buf.setReadIndex(buf.readerIndex() + buf.available());
    return message;
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Example 32 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class CodecPlayOutBlockChange method encode.

@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutBlockChange message) throws CodecException {
    ByteBuffer buf = context.byteBufAlloc().buffer();
    buf.write(Types.VECTOR_3_I, message.getPosition());
    buf.writeVarInt(message.getBlockState());
    return buf;
}
Also used : ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Example 33 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer 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;
}
Also used : MessagePlayOutBossBar(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutBossBar) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) EncoderException(io.netty.handler.codec.EncoderException)

Example 34 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class CodecPlayOutChangeGameState method encode.

@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutChangeGameState message) throws CodecException {
    ByteBuffer buf = context.byteBufAlloc().buffer(LENGTH);
    buf.writeByte((byte) message.getType());
    buf.writeFloat(message.getValue());
    return buf;
}
Also used : ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Example 35 with ByteBuffer

use of org.lanternpowered.server.network.buffer.ByteBuffer in project LanternServer by LanternPowered.

the class CodecPlayOutChunkData method encode.

@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutChunkData message) throws CodecException {
    final MessagePlayOutChunkData.Section[] sections = message.getSections();
    final byte[] biomes = message.getBiomes();
    final int x = message.getX();
    final int z = message.getZ();
    final ByteBuffer buf = context.byteBufAlloc().buffer();
    buf.writeInteger(message.getX());
    buf.writeInteger(message.getZ());
    buf.writeBoolean(biomes != null);
    int sectionBitmask = 0;
    final ByteBuffer dataBuf = context.byteBufAlloc().buffer();
    ByteBuffer tileEntitiesBuf = null;
    int tileEntitiesCount = 0;
    for (int i = 0; i < sections.length; i++) {
        if (sections[i] == null) {
            continue;
        }
        sectionBitmask |= 1 << i;
        final MessagePlayOutChunkData.Section section = sections[i];
        final VariableValueArray types = section.getTypes();
        dataBuf.writeByte((byte) types.getBitsPerValue());
        final int[] palette = section.getPalette();
        if (palette != null) {
            dataBuf.writeVarInt(palette.length);
            for (int value : palette) {
                dataBuf.writeVarInt(value);
            }
        } else {
            // Using global palette
            dataBuf.writeVarInt(0);
        }
        final long[] backing = types.getBacking();
        dataBuf.writeVarInt(backing.length);
        final byte[] blockLight = section.getBlockLight();
        final byte[] skyLight = section.getSkyLight();
        dataBuf.ensureWritable(backing.length * 8 + blockLight.length + (skyLight != null ? skyLight.length : 0));
        for (long value : backing) {
            dataBuf.writeLong(value);
        }
        dataBuf.writeBytes(blockLight);
        if (skyLight != null) {
            dataBuf.writeBytes(skyLight);
        }
        final Short2ObjectMap<DataView> tileEntities = section.getTileEntities();
        if (!tileEntities.isEmpty() && tileEntitiesBuf == null) {
            tileEntitiesBuf = context.byteBufAlloc().buffer();
        }
        for (Short2ObjectMap.Entry<DataView> tileEntityEntry : tileEntities.short2ObjectEntrySet()) {
            tileEntitiesCount++;
            final int index = tileEntityEntry.getShortKey() & 0xffff;
            final DataView dataView = tileEntityEntry.getValue();
            dataView.set(X, x * 16 + (index & 0xf));
            dataView.set(Y, i << 4 | index >> 8);
            dataView.set(Z, z * 16 + ((index >> 4) & 0xf));
            // noinspection ConstantConditions
            tileEntitiesBuf.writeDataView(dataView);
        }
    }
    if (biomes != null) {
        dataBuf.writeBytes(biomes);
    }
    buf.writeVarInt(sectionBitmask);
    buf.writeVarInt(dataBuf.writerIndex());
    try {
        buf.writeBytes(dataBuf);
    } finally {
        dataBuf.release();
    }
    buf.writeVarInt(tileEntitiesCount);
    if (tileEntitiesBuf != null) {
        try {
            buf.writeBytes(tileEntitiesBuf);
        } finally {
            tileEntitiesBuf.release();
        }
    }
    return buf;
}
Also used : VariableValueArray(org.lanternpowered.server.util.collect.array.VariableValueArray) MessagePlayOutChunkData(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutChunkData) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) Short2ObjectMap(it.unimi.dsi.fastutil.shorts.Short2ObjectMap) DataView(org.spongepowered.api.data.DataView)

Aggregations

ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)75 EncoderException (io.netty.handler.codec.EncoderException)9 Vector3d (com.flowpowered.math.vector.Vector3d)7 Message (org.lanternpowered.server.network.message.Message)5 Map (java.util.Map)4 MessagePlayInOutChannelPayload (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutChannelPayload)4 AbstractParameterList (org.lanternpowered.server.network.entity.parameter.AbstractParameterList)3 NullMessage (org.lanternpowered.server.network.message.NullMessage)3 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 Vector3i (com.flowpowered.math.vector.Vector3i)2 CodecException (io.netty.handler.codec.CodecException)2 LanternPotionEffectType (org.lanternpowered.server.effect.potion.LanternPotionEffectType)2 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)2 NetworkSession (org.lanternpowered.server.network.NetworkSession)2 LanternByteBuffer (org.lanternpowered.server.network.buffer.LanternByteBuffer)2 BulkMessage (org.lanternpowered.server.network.message.BulkMessage)2 CodecRegistration (org.lanternpowered.server.network.message.CodecRegistration)2 HandlerMessage (org.lanternpowered.server.network.message.HandlerMessage)2 Codec (org.lanternpowered.server.network.message.codec.Codec)2 RawItemStack (org.lanternpowered.server.network.objects.RawItemStack)2