From 1f4e6535bb604c790cf5e1567e075649e2065f30 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 22 May 2017 19:20:37 +0100 Subject: [PATCH] Added Bed tile and support for coloured beds --- src/pocketmine/block/Bed.php | 34 +++++++++++++++++++-- src/pocketmine/tile/Bed.php | 58 ++++++++++++++++++++++++++++++++++++ src/pocketmine/tile/Tile.php | 3 +- 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/pocketmine/tile/Bed.php diff --git a/src/pocketmine/block/Bed.php b/src/pocketmine/block/Bed.php index ddb31ae768..c1b59ade43 100644 --- a/src/pocketmine/block/Bed.php +++ b/src/pocketmine/block/Bed.php @@ -27,7 +27,13 @@ use pocketmine\event\TranslationContainer; use pocketmine\item\Item; use pocketmine\level\Level; use pocketmine\math\AxisAlignedBB; +use pocketmine\nbt\tag\ByteTag; +use pocketmine\nbt\tag\CompoundTag; +use pocketmine\nbt\tag\IntTag; +use pocketmine\nbt\tag\StringTag; use pocketmine\Player; +use pocketmine\tile\Bed as TileBed; +use pocketmine\tile\Tile; use pocketmine\utils\TextFormat; class Bed extends Transparent{ @@ -116,6 +122,21 @@ class Bed extends Transparent{ $this->getLevel()->setBlock($block, Block::get($this->id, $meta), true, true); $this->getLevel()->setBlock($next, Block::get($this->id, $meta | 0x08), true, true); + $nbt = new CompoundTag("", [ + new StringTag("id", Tile::BED), + new ByteTag("color", $item->getDamage() & 0x0f), + new IntTag("x", $block->x), + new IntTag("y", $block->y), + new IntTag("z", $block->z), + ]); + + $nbt2 = clone $nbt; + $nbt2["x"] = $next->x; + $nbt2["z"] = $next->z; + + Tile::createTile(Tile::BED, $this->getLevel(), $nbt); + Tile::createTile(Tile::BED, $this->getLevel(), $nbt2); + return true; } } @@ -156,9 +177,16 @@ class Bed extends Transparent{ } public function getDrops(Item $item){ - return [ - [Item::BED, 0, 1], - ]; + $tile = $this->getLevel()->getTile($this); + if($tile instanceof TileBed){ + return [ + [Item::BED, $tile->getColor(), 1] + ]; + }else{ + return [ + [Item::BED, 14, 1] //Red + ]; + } } } diff --git a/src/pocketmine/tile/Bed.php b/src/pocketmine/tile/Bed.php new file mode 100644 index 0000000000..8289e9e5b6 --- /dev/null +++ b/src/pocketmine/tile/Bed.php @@ -0,0 +1,58 @@ +color) or !($nbt->color instanceof ByteTag)){ + $nbt->color = new ByteTag("color", 14); //default to old red + } + parent::__construct($level, $nbt); + } + + public function getColor() : int{ + return $this->namedtag->color->getValue(); + } + + public function setColor(int $color){ + $this->namedtag["color"] = $color & 0x0f; + $this->onChanged(); + } + + public function getSpawnCompound() : CompoundTag{ + return new CompoundTag("", [ + new StringTag("id", Tile::BED), + new IntTag("x", (int) $this->x), + new IntTag("y", (int) $this->y), + new IntTag("z", (int) $this->z), + $this->namedtag->color + ]); + } +} \ No newline at end of file diff --git a/src/pocketmine/tile/Tile.php b/src/pocketmine/tile/Tile.php index 28d6b70ece..f7b0a8d3cd 100644 --- a/src/pocketmine/tile/Tile.php +++ b/src/pocketmine/tile/Tile.php @@ -47,6 +47,7 @@ abstract class Tile extends Position{ const MOB_SPAWNER = "MobSpawner"; const SIGN = "Sign"; const SKULL = "Skull"; + const BED = "Bed"; public static $tileCount = 1; @@ -69,6 +70,7 @@ abstract class Tile extends Position{ public $tickTimer; public static function init(){ + self::registerTile(Bed::class); self::registerTile(Chest::class); self::registerTile(EnchantTable::class); self::registerTile(FlowerPot::class); @@ -113,7 +115,6 @@ abstract class Tile extends Position{ /** * Returns the short save name - * * @return string */ public function getSaveId() : string{