PocketMine-MP 5.15.1 git-ed158f8a1b0cfe334ac5f45febc0f633602014f2
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
32
33class ShulkerBox extends Spawnable implements Container, Nameable{
34 use NameableTrait {
35 addAdditionalSpawnData as addNameSpawnData;
36 }
38
39 public const TAG_FACING = "facing";
40
41 protected int $facing = Facing::NORTH;
42
43 protected ShulkerBoxInventory $inventory;
44
45 public function __construct(World $world, Vector3 $pos){
46 parent::__construct($world, $pos);
47 $this->inventory = new ShulkerBoxInventory($this->position);
48 }
49
50 public function readSaveData(CompoundTag $nbt) : void{
51 $this->loadName($nbt);
52 $this->loadItems($nbt);
53 $this->facing = $nbt->getByte(self::TAG_FACING, $this->facing);
54 }
55
56 protected function writeSaveData(CompoundTag $nbt) : void{
57 $this->saveName($nbt);
58 $this->saveItems($nbt);
59 $nbt->setByte(self::TAG_FACING, $this->facing);
60 }
61
62 public function copyDataFromItem(Item $item) : void{
63 $this->readSaveData($item->getNamedTag());
64 if($item->hasCustomName()){
65 $this->setName($item->getCustomName());
66 }
67 }
68
69 public function close() : void{
70 if(!$this->closed){
71 $this->inventory->removeAllViewers();
72 parent::close();
73 }
74 }
75
76 protected function onBlockDestroyedHook() : void{
77 //NOOP override of ContainerTrait - shulker boxes retain their contents when destroyed
78 }
79
80 public function getCleanedNBT() : ?CompoundTag{
81 $nbt = parent::getCleanedNBT();
82 if($nbt !== null){
83 $nbt->removeTag(self::TAG_FACING);
84 }
85 return $nbt;
86 }
87
88 public function getFacing() : int{
89 return $this->facing;
90 }
91
92 public function setFacing(int $facing) : void{
93 $this->facing = $facing;
94 }
95
96 public function getInventory() : ShulkerBoxInventory{
97 return $this->inventory;
98 }
99
100 public function getRealInventory() : ShulkerBoxInventory{
101 return $this->inventory;
102 }
103
104 public function getDefaultName() : string{
105 return "Shulker Box";
106 }
107
108 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
109 $nbt->setByte(self::TAG_FACING, $this->facing);
110 $this->addNameSpawnData($nbt);
111 }
112}
addAdditionalSpawnData(CompoundTag $nbt)
setByte(string $name, int $value)
removeTag(string ... $names)
copyDataFromItem(Item $item)