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