PocketMine-MP 5.31.1 git-c65f740ce5006911c3bac72aa5e6c3707846bd6e
Loading...
Searching...
No Matches
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;
25
26use pocketmine\block\tile\Lectern as TileLectern;
27use pocketmine\block\utils\FacesOppositePlacingPlayerTrait;
29use pocketmine\block\utils\SupportType;
38use function count;
39
40class Lectern extends Transparent implements HorizontalFacing{
41 use FacesOppositePlacingPlayerTrait;
42
43 protected int $viewedPage = 0;
44 protected ?WritableBookBase $book = null;
45
46 protected bool $producingSignal = false;
47
48 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
49 $w->horizontalFacing($this->facing);
50 $w->bool($this->producingSignal);
51 }
52
53 public function readStateFromWorld() : Block{
54 parent::readStateFromWorld();
55 $tile = $this->position->getWorld()->getTile($this->position);
56 if($tile instanceof TileLectern){
57 $this->viewedPage = $tile->getViewedPage();
58 $this->book = $tile->getBook();
59 }
60
61 return $this;
62 }
63
64 public function writeStateToWorld() : void{
65 parent::writeStateToWorld();
66 $tile = $this->position->getWorld()->getTile($this->position);
67 if($tile instanceof TileLectern){
68 $tile->setViewedPage($this->viewedPage);
69 $tile->setBook($this->book);
70 }
71 }
72
73 public function getFlammability() : int{
74 return 30;
75 }
76
77 public function getDrops(Item $item) : array{
78 $drops = parent::getDrops($item);
79 if($this->book !== null){
80 $drops[] = clone $this->book;
81 }
82
83 return $drops;
84 }
85
86 protected function recalculateCollisionBoxes() : array{
87 return [AxisAlignedBB::one()->trim(Facing::UP, 0.1)];
88 }
89
90 public function getSupportType(int $facing) : SupportType{
91 return SupportType::NONE;
92 }
93
94 public function isProducingSignal() : bool{ return $this->producingSignal; }
95
97 public function setProducingSignal(bool $producingSignal) : self{
98 $this->producingSignal = $producingSignal;
99 return $this;
100 }
101
102 public function getViewedPage() : int{
103 return $this->viewedPage;
104 }
105
107 public function setViewedPage(int $viewedPage) : self{
108 $this->viewedPage = $viewedPage;
109 return $this;
110 }
111
112 public function getBook() : ?WritableBookBase{
113 return $this->book !== null ? clone $this->book : null;
114 }
115
117 public function setBook(?WritableBookBase $book) : self{
118 $this->book = $book !== null && !$book->isNull() ? (clone $book)->setCount(1) : null;
119 $this->viewedPage = 0;
120 return $this;
121 }
122
123 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
124 if($this->book === null && $item instanceof WritableBookBase){
125 $world = $this->position->getWorld();
126 $world->setBlock($this->position, $this->setBook($item));
127 $world->addSound($this->position, new LecternPlaceBookSound());
128 $item->pop();
129 }
130 return true;
131 }
132
133 public function onAttack(Item $item, int $face, ?Player $player = null) : bool{
134 if($this->book !== null){
135 $world = $this->position->getWorld();
136 $world->dropItem($this->position->up(), $this->book);
137 $world->setBlock($this->position, $this->setBook(null));
138 }
139 return false;
140 }
141
142 public function onPageTurn(int $newPage) : bool{
143 if($newPage === $this->viewedPage){
144 return true;
145 }
146 if($this->book === null || $newPage >= count($this->book->getPages()) || $newPage < 0){
147 return false;
148 }
149
150 $this->viewedPage = $newPage;
151 $world = $this->position->getWorld();
152 if(!$this->producingSignal){
153 $this->producingSignal = true;
154 $world->scheduleDelayedBlockUpdate($this->position, 1);
155 }
156
157 $world->setBlock($this->position, $this);
158
159 return true;
160 }
161
162 public function onScheduledUpdate() : void{
163 if($this->producingSignal){
164 $this->producingSignal = false;
165 $this->position->getWorld()->setBlock($this->position, $this);
166 }
167 }
168}
setViewedPage(int $viewedPage)
Definition Lectern.php:107
setProducingSignal(bool $producingSignal)
Definition Lectern.php:97
onAttack(Item $item, int $face, ?Player $player=null)
Definition Lectern.php:133
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition Lectern.php:48
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition Lectern.php:123
setBook(?WritableBookBase $book)
Definition Lectern.php:117
getSupportType(int $facing)
Definition Lectern.php:90
pop(int $count=1)
Definition Item.php:433