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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations