Cache block entities even without handler. Fix them from being invisible

This commit is contained in:
TheMode 2021-08-21 04:53:43 +02:00
parent 6541951aaa
commit 56f826a41e
2 changed files with 21 additions and 23 deletions

View File

@ -64,7 +64,7 @@ public class DynamicChunk extends Chunk {
final int index = ChunkUtils.getBlockIndex(x, y, z); final int index = ChunkUtils.getBlockIndex(x, y, z);
// Handler // Handler
final BlockHandler handler = block.handler(); final BlockHandler handler = block.handler();
if (handler != null || block.hasNbt()) { if (handler != null || block.hasNbt() || block.registry().isBlockEntity()) {
this.entries.put(index, block); this.entries.put(index, block);
} else { } else {
this.entries.remove(index); this.entries.remove(index);

View File

@ -114,31 +114,29 @@ public class ChunkDataPacket implements ServerPacket {
List<NBTCompound> compounds = new ArrayList<>(); List<NBTCompound> compounds = new ArrayList<>();
for (var entry : entries.entrySet()) { for (var entry : entries.entrySet()) {
final int index = entry.getKey(); 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(); final BlockHandler handler = block.handler();
if (handler == null) if (handler != null) {
continue; final NBTCompound blockNbt = Objects.requireNonNullElseGet(block.nbt(), NBTCompound::new);
final var blockEntityTags = handler.getBlockEntityTags(); for (Tag<?> tag : handler.getBlockEntityTags()) {
if (blockEntityTags.isEmpty()) // Verify if the block should be sent as block entity to client final var value = tag.read(blockNbt);
continue; if (value != null) {
final var blockNbt = Objects.requireNonNullElseGet(block.nbt(), NBTCompound::new); // Tag is present and valid
final var resultNbt = new NBTCompound(); tag.writeUnsafe(resultNbt, value);
for (Tag<?> tag : blockEntityTags) { }
final var value = tag.read(blockNbt);
if (value != null) {
// Tag is present and valid
tag.writeUnsafe(resultNbt, value);
} }
} }
// Add block entity
if (resultNbt.getSize() > 0) { final var blockPosition = ChunkUtils.getBlockPosition(index, chunkX, chunkZ);
final var blockPosition = ChunkUtils.getBlockPosition(index, chunkX, chunkZ); resultNbt.setString("id", blockEntity)
resultNbt.setString("id", handler.getNamespaceId().asString()) .setInt("x", blockPosition.blockX())
.setInt("x", blockPosition.blockX()) .setInt("y", blockPosition.blockY())
.setInt("y", blockPosition.blockY()) .setInt("z", blockPosition.blockZ());
.setInt("z", blockPosition.blockZ()); compounds.add(resultNbt);
compounds.add(resultNbt);
}
} }
writer.writeVarInt(compounds.size()); writer.writeVarInt(compounds.size());
compounds.forEach(nbtCompound -> writer.writeNBT("", nbtCompound)); compounds.forEach(nbtCompound -> writer.writeNBT("", nbtCompound));