diff --git a/src/main/java/cn/nukkit/blockentity/BlockEntitySpawnable.java b/src/main/java/cn/nukkit/blockentity/BlockEntitySpawnable.java index 1f9b7ee3ad..7a2d41bf9d 100644 --- a/src/main/java/cn/nukkit/blockentity/BlockEntitySpawnable.java +++ b/src/main/java/cn/nukkit/blockentity/BlockEntitySpawnable.java @@ -21,34 +21,36 @@ public BlockEntitySpawnable(FullChunk chunk, CompoundTag nbt) { public abstract CompoundTag getSpawnCompound(); - public void spawnTo(Player player) { - if (this.closed) { - return; - } - + public BlockEntityDataPacket createSpawnPacket() { CompoundTag tag = this.getSpawnCompound(); BlockEntityDataPacket pk = new BlockEntityDataPacket(); pk.x = (int) this.x; pk.y = (int) this.y; pk.z = (int) this.z; + try { pk.namedTag = NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN, true); } catch (IOException e) { throw new RuntimeException(e); } - player.dataPacket(pk); + + return pk; } - public void spawnToAll() { + public void spawnTo(Player player) { if (this.closed) { return; } - for (Player player : this.getLevel().getChunkPlayers(this.chunk.getX(), this.chunk.getZ()).values()) { - if (player.spawned) { - this.spawnTo(player); - } + player.dataPacket(this.createSpawnPacket()); + } + + public void spawnToAll() { + if (this.closed) { + return; } + + this.level.addChunkPacket(this.chunk.getX(), this.chunk.getZ(), this.createSpawnPacket()); } /**