Add PacketUtils#allocateTrimmedPacket

This commit is contained in:
TheMode 2021-08-05 00:08:53 +02:00
parent a2afcdd3a0
commit 58487f4455
2 changed files with 7 additions and 4 deletions

View File

@ -135,10 +135,8 @@ public class DynamicChunk extends Chunk {
ByteBuffer chunkPacket = cachedChunkBuffer;
ByteBuffer lightPacket = cachedLightBuffer;
if (lastChange > cachedPacketTime || (chunkPacket == null || lightPacket == null)) {
final var tempChunk = PacketUtils.createFramedPacket(createChunkPacket());
chunkPacket = ByteBuffer.allocate(tempChunk.position()).put(tempChunk.flip());
final var tempLight = PacketUtils.createFramedPacket(createLightPacket());
lightPacket = ByteBuffer.allocate(tempLight.position()).put(tempLight.flip());
chunkPacket = PacketUtils.allocateTrimmedPacket(createChunkPacket());
lightPacket = PacketUtils.allocateTrimmedPacket(createLightPacket());
this.cachedChunkBuffer = chunkPacket;
this.cachedLightBuffer = lightPacket;
this.cachedPacketTime = lastChange;

View File

@ -180,4 +180,9 @@ public final class PacketUtils {
public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet) {
return createFramedPacket(BUFFER.get().clear(), packet);
}
public static ByteBuffer allocateTrimmedPacket(@NotNull ServerPacket packet) {
final var temp = PacketUtils.createFramedPacket(packet);
return ByteBuffer.allocate(temp.position()).put(temp.flip());
}
}