Some cleanup to how tiles are created

This commit is contained in:
Dylan K. Taylor
2017-10-11 18:07:20 +01:00
parent be2d134994
commit b7a9e10d49
17 changed files with 155 additions and 205 deletions

View File

@ -24,9 +24,12 @@ declare(strict_types=1);
namespace pocketmine\tile;
use pocketmine\item\Item;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\Player;
class Bed extends Spawnable{
@ -49,4 +52,8 @@ class Bed extends Spawnable{
public function addAdditionalSpawnData(CompoundTag $nbt){
$nbt->color = $this->namedtag->color;
}
protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{
$nbt->color = new ByteTag("color", $item !== null ? $item->getDamage() : 14); //default red
}
}

View File

@ -35,6 +35,7 @@ use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\Player;
class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
@ -287,4 +288,12 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
$nbt->CustomName = $this->namedtag->CustomName;
}
}
protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{
$nbt->Items = new ListTag("Items", [], NBT::TAG_Compound);
if($item !== null and $item->hasCustomName()){
$nbt->CustomName = new StringTag("CustomName", $item->getCustomName());
}
}
}

View File

@ -23,8 +23,11 @@ declare(strict_types=1);
namespace pocketmine\tile;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\Player;
class EnchantTable extends Spawnable implements Nameable{
@ -51,4 +54,10 @@ class EnchantTable extends Spawnable implements Nameable{
$nbt->CustomName = $this->namedtag->CustomName;
}
}
protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{
if($item !== null and $item->hasCustomName()){
$nbt->CustomName = new StringTag("CustomName", $item->getCustomName());
}
}
}

View File

@ -26,9 +26,11 @@ namespace pocketmine\tile;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ShortTag;
use pocketmine\Player;
class FlowerPot extends Spawnable{
@ -87,4 +89,9 @@ class FlowerPot extends Spawnable{
$nbt->item = $this->namedtag->item;
$nbt->mData = $this->namedtag->mData;
}
protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{
$nbt->item = new ShortTag("item", 0);
$nbt->mData = new IntTag("mData", 0);
}
}

View File

@ -33,12 +33,14 @@ use pocketmine\inventory\InventoryHolder;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\network\mcpe\protocol\ContainerSetDataPacket;
use pocketmine\Player;
class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
/** @var FurnaceInventory */
@ -294,4 +296,12 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
$nbt->CustomName = $this->namedtag->CustomName;
}
}
protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{
$nbt->Items = new ListTag("Items", [], NBT::TAG_Compound);
if($item !== null and $item->hasCustomName()){
$nbt->CustomName = new StringTag("CustomName", $item->getCustomName());
}
}
}

View File

@ -26,9 +26,11 @@ namespace pocketmine\tile;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\FloatTag;
use pocketmine\Player;
class ItemFrame extends Spawnable{
@ -92,4 +94,9 @@ class ItemFrame extends Spawnable{
}
}
protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{
$nbt->ItemDropChance = new FloatTag("ItemDropChance", 1.0);
$nbt->ItemRotation = new ByteTag("ItemRotation", 0);
}
}

View File

@ -24,7 +24,9 @@ declare(strict_types=1);
namespace pocketmine\tile;
use pocketmine\event\block\SignChangeEvent;
use pocketmine\item\Item;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\Player;
@ -158,10 +160,22 @@ class Sign extends Spawnable{
if(!$ev->isCancelled()){
$this->setText(...$ev->getLines());
return true;
}else{
return false;
}
}
protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{
for($i = 1; $i <= 4; ++$i){
$key = "Text$i";
$nbt->$key = new StringTag($key, "");
}
if($player !== null){
$nbt->Creator = new StringTag("Creator", $player->getRawUniqueId());
}
}
}

View File

@ -23,9 +23,12 @@ declare(strict_types=1);
namespace pocketmine\tile;
use pocketmine\item\Item;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\Player;
class Skull extends Spawnable{
const TYPE_SKELETON = 0;
@ -58,4 +61,14 @@ class Skull extends Spawnable{
$nbt->SkullType = $this->namedtag->SkullType;
$nbt->Rot = $this->namedtag->Rot;
}
protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{
$nbt->SkullType = new ByteTag("SkullType", $item !== null ? $item->getDamage() : self::TYPE_SKELETON);
$rot = 0;
if($face === Vector3::SIDE_UP and $player !== null){
$rot = floor(($player->yaw * 16 / 360) + 0.5) & 0x0F;
}
$nbt->Rot = new ByteTag("Rot", $rot);
}
}

View File

@ -24,15 +24,22 @@ declare(strict_types=1);
/**
* All the Tile classes and related classes
*/
namespace pocketmine\tile;
use pocketmine\block\Block;
use pocketmine\event\Timings;
use pocketmine\event\TimingsHandler;
use pocketmine\item\Item;
use pocketmine\level\format\Chunk;
use pocketmine\level\Level;
use pocketmine\level\Position;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\NamedTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\Player;
use pocketmine\Server;
abstract class Tile extends Position{
@ -121,7 +128,7 @@ abstract class Tile extends Position{
* Returns the short save name
* @return string
*/
public function getSaveId() : string{
public static function getSaveId() : string{
return self::$shortNames[static::class];
}
@ -150,7 +157,7 @@ abstract class Tile extends Position{
}
public function saveNBT(){
$this->namedtag->id->setValue($this->getSaveId());
$this->namedtag->id->setValue(static::getSaveId());
$this->namedtag->x->setValue($this->x);
$this->namedtag->y->setValue($this->y);
$this->namedtag->z->setValue($this->z);
@ -167,6 +174,53 @@ abstract class Tile extends Position{
}
}
/**
* Creates and returns a CompoundTag containing the necessary information to spawn a tile of this type.
*
* @param Vector3 $pos
* @param int|null $face
* @param Item|null $item
* @param Player|null $player
*
* @return CompoundTag
*/
public static function createNBT(Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : CompoundTag{
$nbt = new CompoundTag("", [
new StringTag("id", static::getSaveId()),
new IntTag("x", (int) $pos->x),
new IntTag("y", (int) $pos->y),
new IntTag("z", (int) $pos->z)
]);
static::createAdditionalNBT($nbt, $pos, $face, $item, $player);
if($item !== null){
if($item->hasCustomBlockData()){
foreach($item->getCustomBlockData() as $customBlockDataTag){
if(!($customBlockDataTag instanceof NamedTag)){
continue;
}
$nbt->{$customBlockDataTag->getName()} = $customBlockDataTag;
}
}
}
return $nbt;
}
/**
* Called by createNBT() to allow descendent classes to add their own base NBT using the parameters provided.
*
* @param CompoundTag $nbt
* @param Vector3 $pos
* @param int|null $face
* @param Item|null $item
* @param Player|null $player
*/
protected static function createAdditionalNBT(CompoundTag $nbt, Vector3 $pos, ?int $face = null, ?Item $item = null, ?Player $player = null) : void{
}
/**
* @return Block
*/