43 public const TAG_PAIRX =
"pairx";
44 public const TAG_PAIRZ =
"pairz";
45 public const TAG_PAIR_LEAD =
"pairlead";
50 private ?
int $pairX =
null;
51 private ?
int $pairZ =
null;
54 parent::__construct($world, $pos);
58 public function readSaveData(
CompoundTag $nbt) :
void{
59 if(($pairXTag = $nbt->
getTag(self::TAG_PAIRX)) instanceof
IntTag && ($pairZTag = $nbt->
getTag(self::TAG_PAIRZ)) instanceof
IntTag){
60 $pairX = $pairXTag->getValue();
61 $pairZ = $pairZTag->getValue();
63 ($this->position->x === $pairX && abs($this->position->z - $pairZ) === 1) ||
64 ($this->position->z === $pairZ && abs($this->position->x - $pairX) === 1)
66 $this->pairX = $pairX;
67 $this->pairZ = $pairZ;
69 $this->pairX = $this->pairZ =
null;
72 $this->loadName($nbt);
73 $this->loadItems($nbt);
77 if($this->isPaired()){
78 $nbt->
setInt(self::TAG_PAIRX, $this->pairX);
79 $nbt->
setInt(self::TAG_PAIRZ, $this->pairZ);
81 $this->saveName($nbt);
82 $this->saveItems($nbt);
86 $tag = parent::getCleanedNBT();
89 $tag->removeTag(self::TAG_PAIRX, self::TAG_PAIRZ);
94 public function close() : void{
96 $this->inventory->removeAllViewers();
98 if($this->doubleInventory !==
null){
99 if($this->isPaired() && $this->position->getWorld()->isChunkLoaded($this->pairX >> Chunk::COORD_BIT_SIZE, $this->pairZ >> Chunk::COORD_BIT_SIZE)){
100 $this->doubleInventory->removeAllViewers();
101 if(($pair = $this->getPair()) !==
null){
102 $pair->doubleInventory =
null;
105 $this->doubleInventory =
null;
114 $this->containerTraitBlockDestroyedHook();
118 if($this->isPaired() && $this->doubleInventory === null){
119 $this->checkPairing();
121 return $this->doubleInventory instanceof DoubleChestInventory ? $this->doubleInventory : $this->inventory;
124 public function getRealInventory() : ChestInventory{
125 return $this->inventory;
128 protected function checkPairing() : void{
129 if($this->isPaired() && !$this->position->getWorld()->isInLoadedTerrain(new Vector3($this->pairX, $this->position->y, $this->pairZ))){
131 $this->doubleInventory =
null;
133 }elseif(($pair = $this->getPair()) instanceof Chest){
134 if(!$pair->isPaired()){
135 $pair->createPair($this);
136 $pair->checkPairing();
138 if($this->doubleInventory ===
null){
139 if($pair->doubleInventory !==
null){
140 $this->doubleInventory = $pair->doubleInventory;
142 if(($pair->position->x + ($pair->position->z << 15)) > ($this->position->x + ($this->position->z << 15))){
143 $this->doubleInventory = $pair->doubleInventory =
new DoubleChestInventory($pair->inventory, $this->inventory);
145 $this->doubleInventory = $pair->doubleInventory =
new DoubleChestInventory($this->inventory, $pair->inventory);
150 $this->doubleInventory =
null;
151 $this->pairX = $this->pairZ =
null;
155 public function getDefaultName() : string{
159 public function isPaired() : bool{
160 return $this->pairX !== null && $this->pairZ !== null;
163 public function getPair() : ?Chest{
164 if($this->isPaired()){
165 $tile = $this->position->getWorld()->getTileAt($this->pairX, $this->position->y, $this->pairZ);
166 if($tile instanceof Chest){
174 public function pairWith(Chest $tile) : bool{
175 if($this->isPaired() || $tile->isPaired()){
179 $this->createPair($tile);
181 $this->clearSpawnCompoundCache();
182 $tile->clearSpawnCompoundCache();
183 $this->checkPairing();
188 private function createPair(Chest $tile) : void{
190 $this->pairZ = $tile->getPosition()->z;
196 public function unpair() : bool{
197 if(!$this->isPaired()){
201 $tile = $this->getPair();
202 $this->pairX = $this->pairZ =
null;
204 $this->clearSpawnCompoundCache();
206 if($tile instanceof Chest){
207 $tile->pairX = $tile->pairZ =
null;
208 $tile->checkPairing();
209 $tile->clearSpawnCompoundCache();
211 $this->checkPairing();
217 if($this->isPaired()){
218 $nbt->
setInt(self::TAG_PAIRX, $this->pairX);
219 $nbt->
setInt(self::TAG_PAIRZ, $this->pairZ);
222 $this->addNameSpawnData($nbt);