PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
tile/Lectern.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block\tile;
25
30use function count;
31
36class Lectern extends Spawnable{
37 public const TAG_HAS_BOOK = "hasBook";
38 public const TAG_PAGE = "page";
39 public const TAG_TOTAL_PAGES = "totalPages";
40 public const TAG_BOOK = "book";
41
42 private int $viewedPage = 0;
43 private ?WritableBookBase $book = null;
44
45 public function readSaveData(CompoundTag $nbt) : void{
46 $this->viewedPage = $nbt->getInt(self::TAG_PAGE, 0);
47 if(($itemTag = $nbt->getCompoundTag(self::TAG_BOOK)) !== null){
48 $book = Item::nbtDeserialize($itemTag);
49 if($book instanceof WritableBookBase && !$book->isNull()){
50 $this->book = $book;
51 }
52 }
53 }
54
55 protected function writeSaveData(CompoundTag $nbt) : void{
56 $nbt->setByte(self::TAG_HAS_BOOK, $this->book !== null ? 1 : 0);
57 $nbt->setInt(self::TAG_PAGE, $this->viewedPage);
58 if($this->book !== null){
59 $nbt->setTag(self::TAG_BOOK, $this->book->nbtSerialize());
60 $nbt->setInt(self::TAG_TOTAL_PAGES, count($this->book->getPages()));
61 }
62 }
63
64 public function getViewedPage() : int{
65 return $this->viewedPage;
66 }
67
68 public function setViewedPage(int $viewedPage) : void{
69 $this->viewedPage = $viewedPage;
70 }
71
72 public function getBook() : ?WritableBookBase{
73 return $this->book !== null ? clone $this->book : null;
74 }
75
76 public function setBook(?WritableBookBase $book) : void{
77 $this->book = $book !== null && !$book->isNull() ? clone $book : null;
78 }
79
80 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
81 $nbt->setByte(self::TAG_HAS_BOOK, $this->book !== null ? 1 : 0);
82 $nbt->setInt(self::TAG_PAGE, $this->viewedPage);
83 if($this->book !== null){
84 $nbt->setTag(self::TAG_BOOK, TypeConverter::getInstance()->getItemTranslator()->toNetworkNbt($this->book));
85 $nbt->setInt(self::TAG_TOTAL_PAGES, count($this->book->getPages()));
86 }
87 }
88}
writeSaveData(CompoundTag $nbt)
addAdditionalSpawnData(CompoundTag $nbt)
static nbtDeserialize(CompoundTag $tag)
Definition: Item.php:740
setInt(string $name, int $value)
setTag(string $name, Tag $tag)