PocketMine-MP 5.33.2 git-09cc76ae2b49f1fe3ab0253e6e987fb82bd0a08f
Loading...
Searching...
No Matches
ChiseledBookshelf.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\ChiseledBookshelf as TileChiseledBookshelf;
27use pocketmine\block\utils\ChiseledBookshelfSlot;
28use pocketmine\block\utils\FacesOppositePlacingPlayerTrait;
30use pocketmine\block\utils\HorizontalFacingTrait;
40use function spl_object_id;
41
42class ChiseledBookshelf extends Opaque implements HorizontalFacing{
43 use HorizontalFacingTrait;
44 use FacesOppositePlacingPlayerTrait;
45
50 private array $slots = [];
51
52 private ?ChiseledBookshelfSlot $lastInteractedSlot = null;
53
54 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
55 $w->horizontalFacing($this->facing);
56 $w->enumSet($this->slots, ChiseledBookshelfSlot::cases());
57 }
58
59 public function readStateFromWorld() : Block{
60 $tile = $this->position->getWorld()->getTile($this->position);
61 if($tile instanceof TileChiseledBookshelf){
62 $this->lastInteractedSlot = $tile->getLastInteractedSlot();
63 }else{
64 $this->lastInteractedSlot = null;
65 }
66 return $this;
67 }
68
69 public function writeStateToWorld() : void{
70 parent::writeStateToWorld();
71
72 $tile = $this->position->getWorld()->getTile($this->position);
73 if($tile instanceof TileChiseledBookshelf){
74 $tile->setLastInteractedSlot($this->lastInteractedSlot);
75 }
76 }
77
82 public function hasSlot(ChiseledBookshelfSlot $slot) : bool{
83 return isset($this->slots[spl_object_id($slot)]);
84 }
85
96 public function setSlot(ChiseledBookshelfSlot $slot, bool $occupied) : self{
97 if($occupied){
98 $this->slots[spl_object_id($slot)] = $slot;
99 }else{
100 unset($this->slots[spl_object_id($slot)]);
101 }
102 return $this;
103 }
104
113 public function getSlots() : array{
114 return $this->slots;
115 }
116
121 public function setSlots(array $slots) : self{
122 $this->slots = [];
123 foreach($slots as $slot){
124 $this->setSlot($slot, true);
125 }
126 return $this;
127 }
128
132 public function getLastInteractedSlot() : ?ChiseledBookshelfSlot{
133 return $this->lastInteractedSlot;
134 }
135
141 public function setLastInteractedSlot(?ChiseledBookshelfSlot $lastInteractedSlot) : self{
142 $this->lastInteractedSlot = $lastInteractedSlot;
143 return $this;
144 }
145
146 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
147 if($face !== $this->facing){
148 return false;
149 }
150
151 $x = Facing::axis($face) === Axis::X ? $clickVector->z : $clickVector->x;
152 $slot = ChiseledBookshelfSlot::fromBlockFaceCoordinates(
153 Facing::isPositive(Facing::rotateY($face, true)) ? 1 - $x : $x,
154 $clickVector->y
155 );
156 $tile = $this->position->getWorld()->getTile($this->position);
157 if(!$tile instanceof TileChiseledBookshelf){
158 return false;
159 }
160
161 $inventory = $tile->getInventory();
162 if(!$inventory->isSlotEmpty($slot->value)){
163 $returnedItems[] = $inventory->getItem($slot->value);
164 $inventory->clear($slot->value);
165 $this->setSlot($slot, false);
166 $this->lastInteractedSlot = $slot;
167 }elseif($item instanceof WritableBookBase || $item instanceof Book || $item instanceof EnchantedBook){
168 //TODO: type tags like blocks would be better for this
169 $inventory->setItem($slot->value, $item->pop());
170 $this->setSlot($slot, true);
171 $this->lastInteractedSlot = $slot;
172 }else{
173 return true;
174 }
175
176 $this->position->getWorld()->setBlock($this->position, $this);
177 return true;
178 }
179
180 public function getDropsForCompatibleTool(Item $item) : array{
181 return [];
182 }
183
184 public function isAffectedBySilkTouch() : bool{
185 return true;
186 }
187}
setSlot(ChiseledBookshelfSlot $slot, bool $occupied)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
describeBlockOnlyState(RuntimeDataDescriber $w)
hasSlot(ChiseledBookshelfSlot $slot)
setLastInteractedSlot(?ChiseledBookshelfSlot $lastInteractedSlot)
enumSet(array &$set, array $allCases)