PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
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 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
52 $w->horizontalFacing($this->facing);
53 $w->enumSet($this->slots, ChiseledBookshelfSlot::cases());
54 }
55
60 public function hasSlot(ChiseledBookshelfSlot $slot) : bool{
61 return isset($this->slots[spl_object_id($slot)]);
62 }
63
74 public function setSlot(ChiseledBookshelfSlot $slot, bool $occupied) : self{
75 if($occupied){
76 $this->slots[spl_object_id($slot)] = $slot;
77 }else{
78 unset($this->slots[spl_object_id($slot)]);
79 }
80 return $this;
81 }
82
91 public function getSlots() : array{
92 return $this->slots;
93 }
94
95 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
96 if($face !== $this->facing){
97 return false;
98 }
99
100 $x = Facing::axis($face) === Axis::X ? $clickVector->z : $clickVector->x;
101 $slot = ChiseledBookshelfSlot::fromBlockFaceCoordinates(
102 Facing::isPositive(Facing::rotateY($face, true)) ? 1 - $x : $x,
103 $clickVector->y
104 );
105 $tile = $this->position->getWorld()->getTile($this->position);
106 if(!$tile instanceof TileChiseledBookshelf){
107 return false;
108 }
109
110 $inventory = $tile->getInventory();
111 if(!$inventory->isSlotEmpty($slot->value)){
112 $returnedItems[] = $inventory->getItem($slot->value);
113 $inventory->clear($slot->value);
114 $this->setSlot($slot, false);
115 }elseif($item instanceof WritableBookBase || $item instanceof Book || $item instanceof EnchantedBook){
116 //TODO: type tags like blocks would be better for this
117 $inventory->setItem($slot->value, $item->pop());
118 $this->setSlot($slot, true);
119 }else{
120 return true;
121 }
122
123 $this->position->getWorld()->setBlock($this->position, $this);
124 return true;
125 }
126
127 public function getDropsForCompatibleTool(Item $item) : array{
128 return [];
129 }
130
131 public function isAffectedBySilkTouch() : bool{
132 return true;
133 }
134}
setSlot(ChiseledBookshelfSlot $slot, bool $occupied)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
describeBlockOnlyState(RuntimeDataDescriber $w)
hasSlot(ChiseledBookshelfSlot $slot)
enumSet(array &$set, array $allCases)