PocketMine-MP 5.32.2 git-237b304ef9858756b018e44e8f298093f66f823b
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
120 public function getLastInteractedSlot() : ?ChiseledBookshelfSlot{
121 return $this->lastInteractedSlot;
122 }
123
129 public function setLastInteractedSlot(?ChiseledBookshelfSlot $lastInteractedSlot) : self{
130 $this->lastInteractedSlot = $lastInteractedSlot;
131 return $this;
132 }
133
134 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
135 if($face !== $this->facing){
136 return false;
137 }
138
139 $x = Facing::axis($face) === Axis::X ? $clickVector->z : $clickVector->x;
140 $slot = ChiseledBookshelfSlot::fromBlockFaceCoordinates(
141 Facing::isPositive(Facing::rotateY($face, true)) ? 1 - $x : $x,
142 $clickVector->y
143 );
144 $tile = $this->position->getWorld()->getTile($this->position);
145 if(!$tile instanceof TileChiseledBookshelf){
146 return false;
147 }
148
149 $inventory = $tile->getInventory();
150 if(!$inventory->isSlotEmpty($slot->value)){
151 $returnedItems[] = $inventory->getItem($slot->value);
152 $inventory->clear($slot->value);
153 $this->setSlot($slot, false);
154 $this->lastInteractedSlot = $slot;
155 }elseif($item instanceof WritableBookBase || $item instanceof Book || $item instanceof EnchantedBook){
156 //TODO: type tags like blocks would be better for this
157 $inventory->setItem($slot->value, $item->pop());
158 $this->setSlot($slot, true);
159 $this->lastInteractedSlot = $slot;
160 }else{
161 return true;
162 }
163
164 $this->position->getWorld()->setBlock($this->position, $this);
165 return true;
166 }
167
168 public function getDropsForCompatibleTool(Item $item) : array{
169 return [];
170 }
171
172 public function isAffectedBySilkTouch() : bool{
173 return true;
174 }
175}
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)