From 56f826a41e1f1d03b2d59e1e345413de9080704d Mon Sep 17 00:00:00 2001 From: TheMode Date: Sat, 21 Aug 2021 04:53:43 +0200 Subject: [PATCH] Cache block entities even without handler. Fix them from being invisible --- .../server/instance/DynamicChunk.java | 2 +- .../packet/server/play/ChunkDataPacket.java | 42 +++++++++---------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index 7001f3e66..46fd1adb2 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -64,7 +64,7 @@ public class DynamicChunk extends Chunk { final int index = ChunkUtils.getBlockIndex(x, y, z); // Handler final BlockHandler handler = block.handler(); - if (handler != null || block.hasNbt()) { + if (handler != null || block.hasNbt() || block.registry().isBlockEntity()) { this.entries.put(index, block); } else { this.entries.remove(index); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java index 115368986..2d552b831 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java @@ -114,31 +114,29 @@ public class ChunkDataPacket implements ServerPacket { List compounds = new ArrayList<>(); for (var entry : entries.entrySet()) { final int index = entry.getKey(); - final var block = entry.getValue(); + final Block block = entry.getValue(); + final String blockEntity = block.registry().blockEntity(); + if (blockEntity == null) continue; // Only send block entities to client + final NBTCompound resultNbt = new NBTCompound(); + // Append handler tags final BlockHandler handler = block.handler(); - if (handler == null) - continue; - final var blockEntityTags = handler.getBlockEntityTags(); - if (blockEntityTags.isEmpty()) // Verify if the block should be sent as block entity to client - continue; - final var blockNbt = Objects.requireNonNullElseGet(block.nbt(), NBTCompound::new); - final var resultNbt = new NBTCompound(); - for (Tag tag : blockEntityTags) { - final var value = tag.read(blockNbt); - if (value != null) { - // Tag is present and valid - tag.writeUnsafe(resultNbt, value); + if (handler != null) { + final NBTCompound blockNbt = Objects.requireNonNullElseGet(block.nbt(), NBTCompound::new); + for (Tag tag : handler.getBlockEntityTags()) { + final var value = tag.read(blockNbt); + if (value != null) { + // Tag is present and valid + tag.writeUnsafe(resultNbt, value); + } } } - - if (resultNbt.getSize() > 0) { - final var blockPosition = ChunkUtils.getBlockPosition(index, chunkX, chunkZ); - resultNbt.setString("id", handler.getNamespaceId().asString()) - .setInt("x", blockPosition.blockX()) - .setInt("y", blockPosition.blockY()) - .setInt("z", blockPosition.blockZ()); - compounds.add(resultNbt); - } + // Add block entity + final var blockPosition = ChunkUtils.getBlockPosition(index, chunkX, chunkZ); + resultNbt.setString("id", blockEntity) + .setInt("x", blockPosition.blockX()) + .setInt("y", blockPosition.blockY()) + .setInt("z", blockPosition.blockZ()); + compounds.add(resultNbt); } writer.writeVarInt(compounds.size()); compounds.forEach(nbtCompound -> writer.writeNBT("", nbtCompound));