PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
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;
29use pocketmine\block\utils\HorizontalFacingTrait;
39use function spl_object_id;
40
42 use HorizontalFacingTrait;
43 use FacesOppositePlacingPlayerTrait;
44
49 private array $slots = [];
50
51 private ?ChiseledBookshelfSlot $lastInteractedSlot = null;
52
53 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
54 $w->horizontalFacing($this->facing);
55 $w->enumSet($this->slots, ChiseledBookshelfSlot::cases());
56 }
57
58 public function readStateFromWorld() : Block{
59 $tile = $this->position->getWorld()->getTile($this->position);
60 if($tile instanceof TileChiseledBookshelf){
61 $this->lastInteractedSlot = $tile->getLastInteractedSlot();
62 }else{
63 $this->lastInteractedSlot = null;
64 }
65 return $this;
66 }
67
68 public function writeStateToWorld() : void{
69 parent::writeStateToWorld();
70
71 $tile = $this->position->getWorld()->getTile($this->position);
72 if($tile instanceof TileChiseledBookshelf){
73 $tile->setLastInteractedSlot($this->lastInteractedSlot);
74 }
75 }
76
81 public function hasSlot(ChiseledBookshelfSlot $slot) : bool{
82 return isset($this->slots[spl_object_id($slot)]);
83 }
84
95 public function setSlot(ChiseledBookshelfSlot $slot, bool $occupied) : self{
96 if($occupied){
97 $this->slots[spl_object_id($slot)] = $slot;
98 }else{
99 unset($this->slots[spl_object_id($slot)]);
100 }
101 return $this;
102 }
103
112 public function getSlots() : array{
113 return $this->slots;
114 }
115
119 public function getLastInteractedSlot() : ?ChiseledBookshelfSlot{
120 return $this->lastInteractedSlot;
121 }
122
128 public function setLastInteractedSlot(?ChiseledBookshelfSlot $lastInteractedSlot) : self{
129 $this->lastInteractedSlot = $lastInteractedSlot;
130 return $this;
131 }
132
133 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
134 if($face !== $this->facing){
135 return false;
136 }
137
138 $x = Facing::axis($face) === Axis::X ? $clickVector->z : $clickVector->x;
139 $slot = ChiseledBookshelfSlot::fromBlockFaceCoordinates(
140 Facing::isPositive(Facing::rotateY($face, true)) ? 1 - $x : $x,
141 $clickVector->y
142 );
143 $tile = $this->position->getWorld()->getTile($this->position);
144 if(!$tile instanceof TileChiseledBookshelf){
145 return false;
146 }
147
148 $inventory = $tile->getInventory();
149 if(!$inventory->isSlotEmpty($slot->value)){
150 $returnedItems[] = $inventory->getItem($slot->value);
151 $inventory->clear($slot->value);
152 $this->setSlot($slot, false);
153 $this->lastInteractedSlot = $slot;
154 }elseif($item instanceof WritableBookBase || $item instanceof Book || $item instanceof EnchantedBook){
155 //TODO: type tags like blocks would be better for this
156 $inventory->setItem($slot->value, $item->pop());
157 $this->setSlot($slot, true);
158 $this->lastInteractedSlot = $slot;
159 }else{
160 return true;
161 }
162
163 $this->position->getWorld()->setBlock($this->position, $this);
164 return true;
165 }
166
167 public function getDropsForCompatibleTool(Item $item) : array{
168 return [];
169 }
170
171 public function isAffectedBySilkTouch() : bool{
172 return true;
173 }
174}
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)