mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-03 00:29:54 +00:00
138 lines
3.7 KiB
PHP
138 lines
3.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
*
|
|
* ____ _ _ __ __ _ __ __ ____
|
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* @author PocketMine Team
|
|
* @link http://www.pocketmine.net/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace pocketmine\tile;
|
|
|
|
use pocketmine\level\Level;
|
|
use pocketmine\nbt\NetworkLittleEndianNBTStream;
|
|
use pocketmine\nbt\tag\CompoundTag;
|
|
use pocketmine\nbt\tag\IntTag;
|
|
use pocketmine\nbt\tag\StringTag;
|
|
use pocketmine\network\mcpe\protocol\BlockEntityDataPacket;
|
|
use pocketmine\Player;
|
|
|
|
abstract class Spawnable extends Tile{
|
|
/** @var string|null */
|
|
private $spawnCompoundCache = null;
|
|
/** @var NetworkLittleEndianNBTStream|null */
|
|
private static $nbtWriter = null;
|
|
|
|
public function createSpawnPacket() : BlockEntityDataPacket{
|
|
$pk = new BlockEntityDataPacket();
|
|
$pk->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;
|
|
}
|
|
|
|
$pk = $this->createSpawnPacket();
|
|
$this->level->addChunkPacket($this->getFloorX() >> 4, $this->getFloorZ() >> 4, $pk);
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|