Keep reference to the framed packet for debugging/transformation purpose

This commit is contained in:
TheMode 2021-08-14 14:39:11 +02:00
parent e96334e315
commit 168672e002
2 changed files with 10 additions and 8 deletions

View File

@ -8,10 +8,11 @@ import net.minestom.server.entity.Player;
import net.minestom.server.entity.pathfinding.PFBlock; import net.minestom.server.entity.pathfinding.PFBlock;
import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler; import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.network.packet.FramedPacket;
import net.minestom.server.network.packet.server.play.ChunkDataPacket; import net.minestom.server.network.packet.server.play.ChunkDataPacket;
import net.minestom.server.network.packet.server.play.UpdateLightPacket; import net.minestom.server.network.packet.server.play.UpdateLightPacket;
import net.minestom.server.network.player.PlayerSocketConnection;
import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.network.player.PlayerSocketConnection;
import net.minestom.server.utils.ArrayUtils; import net.minestom.server.utils.ArrayUtils;
import net.minestom.server.utils.PacketUtils; import net.minestom.server.utils.PacketUtils;
import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.chunk.ChunkUtils;
@ -19,7 +20,6 @@ import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -40,8 +40,8 @@ public class DynamicChunk extends Chunk {
private volatile long lastChangeTime; private volatile long lastChangeTime;
private ByteBuffer cachedChunkBuffer; private FramedPacket cachedChunkBuffer;
private ByteBuffer cachedLightBuffer; private FramedPacket cachedLightBuffer;
private long cachedPacketTime; private long cachedPacketTime;
public DynamicChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ) { public DynamicChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ) {
@ -132,8 +132,8 @@ public class DynamicChunk extends Chunk {
final PlayerConnection connection = player.getPlayerConnection(); final PlayerConnection connection = player.getPlayerConnection();
if (connection instanceof PlayerSocketConnection) { if (connection instanceof PlayerSocketConnection) {
final long lastChange = getLastChangeTime(); final long lastChange = getLastChangeTime();
ByteBuffer chunkPacket = cachedChunkBuffer; var chunkPacket = cachedChunkBuffer;
ByteBuffer lightPacket = cachedLightBuffer; var lightPacket = cachedLightBuffer;
if (lastChange > cachedPacketTime || (chunkPacket == null || lightPacket == null)) { if (lastChange > cachedPacketTime || (chunkPacket == null || lightPacket == null)) {
chunkPacket = PacketUtils.allocateTrimmedPacket(createChunkPacket()); chunkPacket = PacketUtils.allocateTrimmedPacket(createChunkPacket());
lightPacket = PacketUtils.allocateTrimmedPacket(createLightPacket()); lightPacket = PacketUtils.allocateTrimmedPacket(createLightPacket());

View File

@ -197,9 +197,11 @@ public final class PacketUtils {
return createFramedPacket(PACKET_BUFFER.get(), packet, compression); return createFramedPacket(PACKET_BUFFER.get(), packet, compression);
} }
public static ByteBuffer allocateTrimmedPacket(@NotNull ServerPacket packet) { @ApiStatus.Internal
public static FramedPacket allocateTrimmedPacket(@NotNull ServerPacket packet) {
final var temp = PacketUtils.createFramedPacket(packet); final var temp = PacketUtils.createFramedPacket(packet);
return ByteBuffer.allocateDirect(temp.position()).put(temp.flip()).asReadOnlyBuffer(); final var buffer= ByteBuffer.allocateDirect(temp.position()).put(temp.flip()).asReadOnlyBuffer();
return new FramedPacket(packet.getId(), buffer, packet);
} }
@ApiStatus.Internal @ApiStatus.Internal