mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-15 16:05:28 +00:00
204 lines
4.8 KiB
PHP
204 lines
4.8 KiB
PHP
<?php
|
|
|
|
/*
|
|
*
|
|
* ____ _ _ __ __ _ __ __ ____
|
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* @author PocketMine Team
|
|
* @link http://www.pocketmine.net/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace pocketmine\tile;
|
|
|
|
use pocketmine\inventory\ChestInventory;
|
|
use pocketmine\inventory\DoubleChestInventory;
|
|
use pocketmine\inventory\InventoryHolder;
|
|
use pocketmine\nbt\tag\CompoundTag;
|
|
use pocketmine\nbt\tag\IntTag;
|
|
|
|
class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
|
|
use NameableTrait {
|
|
addAdditionalSpawnData as addNameSpawnData;
|
|
}
|
|
use ContainerTrait;
|
|
|
|
public const TAG_PAIRX = "pairx";
|
|
public const TAG_PAIRZ = "pairz";
|
|
public const TAG_PAIR_LEAD = "pairlead";
|
|
|
|
/** @var ChestInventory */
|
|
protected $inventory;
|
|
/** @var DoubleChestInventory */
|
|
protected $doubleInventory = null;
|
|
|
|
/** @var int|null */
|
|
private $pairX;
|
|
/** @var int|null */
|
|
private $pairZ;
|
|
|
|
protected function readSaveData(CompoundTag $nbt) : void{
|
|
if($nbt->hasTag(self::TAG_PAIRX, IntTag::class) and $nbt->hasTag(self::TAG_PAIRZ, IntTag::class)){
|
|
$this->pairX = $nbt->getInt(self::TAG_PAIRX);
|
|
$this->pairZ = $nbt->getInt(self::TAG_PAIRZ);
|
|
}
|
|
$this->loadName($nbt);
|
|
|
|
$this->inventory = new ChestInventory($this);
|
|
$this->loadItems($nbt);
|
|
}
|
|
|
|
protected function writeSaveData(CompoundTag $nbt) : void{
|
|
if($this->isPaired()){
|
|
$nbt->setInt(self::TAG_PAIRX, $this->pairX);
|
|
$nbt->setInt(self::TAG_PAIRZ, $this->pairZ);
|
|
}
|
|
$this->saveName($nbt);
|
|
$this->saveItems($nbt);
|
|
}
|
|
|
|
public function close() : void{
|
|
if(!$this->closed){
|
|
$this->inventory->removeAllViewers(true);
|
|
|
|
if($this->doubleInventory !== null){
|
|
$this->doubleInventory->removeAllViewers(true);
|
|
$this->doubleInventory->invalidate();
|
|
$this->doubleInventory = null;
|
|
}
|
|
|
|
$this->inventory = null;
|
|
|
|
parent::close();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return ChestInventory|DoubleChestInventory
|
|
*/
|
|
public function getInventory(){
|
|
if($this->isPaired() and $this->doubleInventory === null){
|
|
$this->checkPairing();
|
|
}
|
|
return $this->doubleInventory instanceof DoubleChestInventory ? $this->doubleInventory : $this->inventory;
|
|
}
|
|
|
|
/**
|
|
* @return ChestInventory
|
|
*/
|
|
public function getRealInventory(){
|
|
return $this->inventory;
|
|
}
|
|
|
|
protected function checkPairing(){
|
|
if($this->isPaired() and !$this->getLevel()->isChunkLoaded($this->pairX >> 4, $this->pairZ >> 4)){
|
|
//paired to a tile in an unloaded chunk
|
|
$this->doubleInventory = null;
|
|
|
|
}elseif(($pair = $this->getPair()) instanceof Chest){
|
|
if(!$pair->isPaired()){
|
|
$pair->createPair($this);
|
|
$pair->checkPairing();
|
|
}
|
|
if($this->doubleInventory === null){
|
|
if(($pair->x + ($pair->z << 15)) > ($this->x + ($this->z << 15))){ //Order them correctly
|
|
$this->doubleInventory = new DoubleChestInventory($pair, $this);
|
|
}else{
|
|
$this->doubleInventory = new DoubleChestInventory($this, $pair);
|
|
}
|
|
}
|
|
}else{
|
|
$this->doubleInventory = null;
|
|
$this->pairX = $this->pairZ = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getDefaultName() : string{
|
|
return "Chest";
|
|
}
|
|
|
|
public function isPaired(){
|
|
return $this->pairX !== null and $this->pairZ !== null;
|
|
}
|
|
|
|
/**
|
|
* @return Chest|null
|
|
*/
|
|
public function getPair() : ?Chest{
|
|
if($this->isPaired()){
|
|
$tile = $this->getLevel()->getTileAt($this->pairX, $this->y, $this->pairZ);
|
|
if($tile instanceof Chest){
|
|
return $tile;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function pairWith(Chest $tile){
|
|
if($this->isPaired() or $tile->isPaired()){
|
|
return false;
|
|
}
|
|
|
|
$this->createPair($tile);
|
|
|
|
$this->onChanged();
|
|
$tile->onChanged();
|
|
$this->checkPairing();
|
|
|
|
return true;
|
|
}
|
|
|
|
private function createPair(Chest $tile){
|
|
$this->pairX = $tile->x;
|
|
$this->pairZ = $tile->z;
|
|
|
|
$tile->pairX = $this->x;
|
|
$tile->pairZ = $this->z;
|
|
}
|
|
|
|
public function unpair(){
|
|
if(!$this->isPaired()){
|
|
return false;
|
|
}
|
|
|
|
$tile = $this->getPair();
|
|
$this->pairX = $this->pairZ = null;
|
|
|
|
$this->onChanged();
|
|
|
|
if($tile instanceof Chest){
|
|
$tile->pairX = $tile->pairZ = null;
|
|
$tile->checkPairing();
|
|
$tile->onChanged();
|
|
}
|
|
$this->checkPairing();
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
|
|
if($this->isPaired()){
|
|
$nbt->setInt(self::TAG_PAIRX, $this->pairX);
|
|
$nbt->setInt(self::TAG_PAIRZ, $this->pairZ);
|
|
}
|
|
|
|
$this->addNameSpawnData($nbt);
|
|
}
|
|
}
|