PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
Loading...
Searching...
No Matches
tile/Chest.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
33use function abs;
34
35class Chest extends Spawnable implements ContainerTile, Nameable{
36 use NameableTrait {
37 addAdditionalSpawnData as addNameSpawnData;
38 }
40
41 public const TAG_PAIRX = "pairx";
42 public const TAG_PAIRZ = "pairz";
43 public const TAG_PAIR_LEAD = "pairlead";
44
45 protected Inventory $inventory;
46 protected ?CombinedInventoryProxy $doubleInventory = null;
47
52 private ?array $pairXZ = null;
53
54 public function __construct(World $world, Vector3 $pos){
55 parent::__construct($world, $pos);
56 $this->inventory = new SimpleInventory(27);
57 }
58
59 public function readSaveData(CompoundTag $nbt) : void{
60 if(($pairXTag = $nbt->getTag(self::TAG_PAIRX)) instanceof IntTag && ($pairZTag = $nbt->getTag(self::TAG_PAIRZ)) instanceof IntTag){
61 $pairX = $pairXTag->getValue();
62 $pairZ = $pairZTag->getValue();
63 if(
64 ($this->position->x === $pairX && abs($this->position->z - $pairZ) === 1) ||
65 ($this->position->z === $pairZ && abs($this->position->x - $pairX) === 1)
66 ){
67 $this->pairXZ = [$pairX, $pairZ];
68 }else{
69 $this->pairXZ = null;
70 }
71 }
72 $this->loadName($nbt);
73 $this->loadItems($nbt);
74 }
75
76 protected function writeSaveData(CompoundTag $nbt) : void{
77 if($this->pairXZ !== null){
78 [$pairX, $pairZ] = $this->pairXZ;
79 $nbt->setInt(self::TAG_PAIRX, $pairX);
80 $nbt->setInt(self::TAG_PAIRZ, $pairZ);
81 }
82 $this->saveName($nbt);
83 $this->saveItems($nbt);
84 }
85
86 public function getCleanedNBT() : ?CompoundTag{
87 $tag = parent::getCleanedNBT();
88 if($tag !== null){
89 //TODO: replace this with a purpose flag on writeSaveData()
90 $tag->removeTag(self::TAG_PAIRX, self::TAG_PAIRZ);
91 }
92 return $tag;
93 }
94
95 public function close() : void{
96 if(!$this->closed){
97 $this->inventory->removeAllWindows();
98 parent::close();
99 }
100 }
101
102 public function getInventory() : Inventory{
103 return $this->inventory;
104 }
105
106 public function getRealInventory() : Inventory{
107 return $this->inventory;
108 }
109
110 public function getDefaultName() : string{
111 return "Chest";
112 }
113
118 public function getPairXZ() : ?array{
119 return $this->pairXZ;
120 }
121
126 public function setPairXZ(?array $pairXZ) : void{
127 $this->pairXZ = $pairXZ;
128 }
129
130 protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
131 if($this->pairXZ !== null){
132 $nbt->setInt(self::TAG_PAIRX, $this->pairXZ[0]);
133 $nbt->setInt(self::TAG_PAIRZ, $this->pairXZ[1]);
134 }
135
136 $this->addNameSpawnData($nbt);
137 }
138}
writeSaveData(CompoundTag $nbt)
addAdditionalSpawnData(CompoundTag $nbt)
setInt(string $name, int $value)