PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
Loading...
Searching...
No Matches
tile/ShulkerBox.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\tile;
25
38
39class ShulkerBox extends Spawnable implements ContainerTile, Nameable{
40 use NameableTrait {
41 addAdditionalSpawnData as addNameSpawnData;
42 }
44
45 public const TAG_FACING = "facing";
46
47 protected Facing $facing = Facing::NORTH;
48
49 protected Inventory $inventory;
50
51 public function __construct(World $world, Vector3 $pos){
52 parent::__construct($world, $pos);
53 $this->inventory = new SimpleInventory(27);
54
55 $this->inventory->getSlotValidators()->add(new CallbackSlotValidator(static function(Inventory $_, Item $item) : ?TransactionValidationException{ //remaining params not needed
56 $blockTypeId = ItemTypeIds::toBlockTypeId($item->getTypeId());
57 if($blockTypeId === BlockTypeIds::SHULKER_BOX || $blockTypeId === BlockTypeIds::DYED_SHULKER_BOX){
58 return new TransactionValidationException("Shulker box inventory cannot contain shulker boxes");
59 }
60
61 return null;
62 }));
63 }
64
65 public function readSaveData(CompoundTag $nbt) : void{
66 $this->loadName($nbt);
67 $this->loadItems($nbt);
68 //TODO: suspicious use of internal Facing value for storage
69 $this->facing = Facing::tryFrom($nbt->getByte(self::TAG_FACING, $this->facing->value)) ?? throw new SavedDataLoadingException("Invalid facing value");
70 }
71
72 protected function writeSaveData(CompoundTag $nbt) : void{
73 $this->saveName($nbt);
74 $this->saveItems($nbt);
75 //TODO: suspicious use of internal Facing value for storage
76 $nbt->setByte(self::TAG_FACING, $this->facing->value);
77 }
78
79 public function copyDataFromItem(Item $item) : void{
80 $this->readSaveData($item->getNamedTag());
81 if($item->hasCustomName()){
82 $this->setName($item->getCustomName());
83 }
84 }
85
86 public function close() : void{
87 if(!$this->closed){
88 $this->inventory->removeAllWindows();
89 parent::close();
90 }
91 }
92
93 protected function onBlockDestroyedHook() : void{
94 //NOOP override of ContainerTrait - shulker boxes retain their contents when destroyed
95 }
96
97 public function getCleanedNBT() : ?CompoundTag{
98 $nbt = parent::getCleanedNBT();
99 if($nbt !== null){
100 $nbt->removeTag(self::TAG_FACING);
101 }
102 return $nbt;
103 }
104
105 public function getFacing() : Facing{
106 return $this->facing;
107 }
108
109 public function setFacing(Facing $facing) : void{
110 $this->facing = $facing;
111 }
112
113 public function getInventory() : Inventory{
114 return $this->inventory;
115 }
116
117 public function getRealInventory() : Inventory{
118 return $this->inventory;
119 }
120
121 public function getDefaultName() : string{
122 return "Shulker Box";
123 }
124
125 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
126 //TODO: suspicious use of internal Facing value for network
127 $nbt->setByte(self::TAG_FACING, $this->facing->value);
128 $this->addNameSpawnData($nbt);
129 }
130}
setByte(string $name, int $value)
copyDataFromItem(Item $item)