x = $this->x; $pk->y = $this->y; $pk->z = $this->z; $pk->namedtag = $this->getSerializedSpawnCompound(); return $pk; } public function spawnTo(Player $player) : bool{ if($this->closed){ return false; } $player->dataPacket($this->createSpawnPacket()); return true; } public function __construct(Level $level, CompoundTag $nbt){ parent::__construct($level, $nbt); $this->spawnToAll(); } public function spawnToAll(){ if($this->closed){ return; } $this->level->broadcastPacketToViewers($this, $this->createSpawnPacket()); } /** * Performs actions needed when the tile is modified, such as clearing caches and respawning the tile to players. * WARNING: This MUST be called to clear spawn-compound and chunk caches when the tile's spawn compound has changed! */ protected function onChanged() : void{ $this->spawnCompoundCache = null; $this->spawnToAll(); $this->level->clearChunkCache($this->getFloorX() >> 4, $this->getFloorZ() >> 4); } /** * Returns encoded NBT (varint, little-endian) used to spawn this tile to clients. Uses cache where possible, * populates cache if it is null. * * @return string encoded NBT */ final public function getSerializedSpawnCompound() : string{ if($this->spawnCompoundCache === null){ if(self::$nbtWriter === null){ self::$nbtWriter = new NetworkLittleEndianNBTStream(); } $this->spawnCompoundCache = self::$nbtWriter->write($this->getSpawnCompound()); } return $this->spawnCompoundCache; } /** * @return CompoundTag */ final public function getSpawnCompound() : CompoundTag{ $nbt = new CompoundTag("", [ new StringTag(self::TAG_ID, static::getSaveId()), new IntTag(self::TAG_X, $this->x), new IntTag(self::TAG_Y, $this->y), new IntTag(self::TAG_Z, $this->z) ]); $this->addAdditionalSpawnData($nbt); return $nbt; } /** * An extension to getSpawnCompound() for * further modifying the generic tile NBT. * * @param CompoundTag $nbt */ abstract protected function addAdditionalSpawnData(CompoundTag $nbt) : void; /** * Called when a player updates a block entity's NBT data * for example when writing on a sign. * * @param CompoundTag $nbt * @param Player $player * * @return bool indication of success, will respawn the tile to the player if false. */ public function updateCompoundTag(CompoundTag $nbt, Player $player) : bool{ return false; } }