diff --git a/src/block/Chest.php b/src/block/Chest.php index e823187c3..8e0616cd4 100644 --- a/src/block/Chest.php +++ b/src/block/Chest.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\block; use pocketmine\block\inventory\ChestInventoryWindow; +use pocketmine\block\inventory\DoubleChestInventory; use pocketmine\block\inventory\DoubleChestInventoryWindow; use pocketmine\block\tile\Chest as TileChest; use pocketmine\block\utils\FacesOppositePlacingPlayerTrait; @@ -87,18 +88,25 @@ class Chest extends Transparent{ return true; } - foreach([false, true] as $clockwise){ - $sideFacing = Facing::rotateY($this->facing, $clockwise); - $side = $this->position->getSide($sideFacing); - $pair = $world->getTile($side); - if($pair instanceof TileChest && $pair->getPair() === $chest){ - [$left, $right] = $clockwise ? [$side, $this->position] : [$this->position, $side]; + $window = null; + if($chest->isPaired()){ + foreach([false, true] as $clockwise){ + $sideFacing = Facing::rotateY($this->facing, $clockwise); + $side = $this->position->getSide($sideFacing); + $pair = $world->getTile($side); + if($pair instanceof TileChest && $pair->getPair() === $chest){ + [$left, $right] = $clockwise ? [$pair, $chest] : [$chest, $pair]; - //TODO: we should probably construct DoubleChestInventory here directly too using the same logic - //right now it uses some weird logic in TileChest which produces incorrect results - //however I'm not sure if this is currently possible - $window = new DoubleChestInventoryWindow($player, $chest->getInventory(), $left, $right); - break; + $doubleInventory = $left->getDoubleInventory() ?? $right->getDoubleInventory(); + if($doubleInventory === null){ + $doubleInventory = new DoubleChestInventory($left->getInventory(), $right->getInventory()); + $left->setDoubleInventory($doubleInventory); + $right->setDoubleInventory($doubleInventory); + } + + $window = new DoubleChestInventoryWindow($player, $doubleInventory, $left->getPosition(), $right->getPosition()); + break; + } } } diff --git a/src/block/tile/Barrel.php b/src/block/tile/Barrel.php index f642d6cac..9b64bbf32 100644 --- a/src/block/tile/Barrel.php +++ b/src/block/tile/Barrel.php @@ -61,10 +61,6 @@ class Barrel extends Spawnable implements Container, Nameable{ return $this->inventory; } - public function getRealInventory() : Inventory{ - return $this->inventory; - } - public function getDefaultName() : string{ return "Barrel"; } diff --git a/src/block/tile/BrewingStand.php b/src/block/tile/BrewingStand.php index 0e4289a84..4a3249350 100644 --- a/src/block/tile/BrewingStand.php +++ b/src/block/tile/BrewingStand.php @@ -117,10 +117,6 @@ class BrewingStand extends Spawnable implements Container, Nameable{ return $this->inventory; } - public function getRealInventory() : Inventory{ - return $this->inventory; - } - private function checkFuel(Item $item) : void{ $ev = new BrewingFuelUseEvent($this); if(!$item->equals(VanillaItems::BLAZE_POWDER(), true, false)){ diff --git a/src/block/tile/Campfire.php b/src/block/tile/Campfire.php index 89e42fa06..a9ad30351 100644 --- a/src/block/tile/Campfire.php +++ b/src/block/tile/Campfire.php @@ -68,10 +68,6 @@ class Campfire extends Spawnable implements Container{ return $this->inventory; } - public function getRealInventory() : CampfireInventory{ - return $this->inventory; - } - /** * @return int[] * @phpstan-return array diff --git a/src/block/tile/Chest.php b/src/block/tile/Chest.php index 35e9425e1..9bc3fdffb 100644 --- a/src/block/tile/Chest.php +++ b/src/block/tile/Chest.php @@ -115,15 +115,14 @@ class Chest extends Spawnable implements Container, Nameable{ $this->containerTraitBlockDestroyedHook(); } - public function getInventory() : Inventory|DoubleChestInventory{ - if($this->isPaired() && $this->doubleInventory === null){ - $this->checkPairing(); - } - return $this->doubleInventory instanceof DoubleChestInventory ? $this->doubleInventory : $this->inventory; + public function getInventory() : Inventory{ + return $this->inventory; } - public function getRealInventory() : Inventory{ - return $this->inventory; + public function getDoubleInventory() : ?DoubleChestInventory{ return $this->doubleInventory; } + + public function setDoubleInventory(?DoubleChestInventory $doubleChestInventory) : void{ + $this->doubleInventory = $doubleChestInventory; } protected function checkPairing() : void{ @@ -134,18 +133,7 @@ class Chest extends Spawnable implements Container, Nameable{ }elseif(($pair = $this->getPair()) instanceof Chest){ if(!$pair->isPaired()){ $pair->createPair($this); - $pair->checkPairing(); - } - if($this->doubleInventory === null){ - if($pair->doubleInventory !== null){ - $this->doubleInventory = $pair->doubleInventory; - }else{ - if(($pair->position->x + ($pair->position->z << 15)) > ($this->position->x + ($this->position->z << 15))){ //Order them correctly - $this->doubleInventory = $pair->doubleInventory = new DoubleChestInventory($pair->inventory, $this->inventory); - }else{ - $this->doubleInventory = $pair->doubleInventory = new DoubleChestInventory($this->inventory, $pair->inventory); - } - } + $this->doubleInventory = $pair->doubleInventory = null; } }else{ $this->doubleInventory = null; diff --git a/src/block/tile/ChiseledBookshelf.php b/src/block/tile/ChiseledBookshelf.php index 06175e27f..da7e42c0d 100644 --- a/src/block/tile/ChiseledBookshelf.php +++ b/src/block/tile/ChiseledBookshelf.php @@ -55,10 +55,6 @@ class ChiseledBookshelf extends Tile implements Container{ return $this->inventory; } - public function getRealInventory() : SimpleInventory{ - return $this->inventory; - } - public function getLastInteractedSlot() : ?ChiseledBookshelfSlot{ return $this->lastInteractedSlot; } @@ -87,7 +83,7 @@ class ChiseledBookshelf extends Tile implements Container{ protected function loadItems(CompoundTag $tag) : void{ if(($inventoryTag = $tag->getTag(Container::TAG_ITEMS)) instanceof ListTag && $inventoryTag->getTagType() === NBT::TAG_Compound){ - $inventory = $this->getRealInventory(); + $inventory = $this->inventory; $listeners = $inventory->getListeners()->toArray(); $inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization @@ -118,7 +114,7 @@ class ChiseledBookshelf extends Tile implements Container{ protected function saveItems(CompoundTag $tag) : void{ $items = []; - foreach($this->getRealInventory()->getContents(true) as $slot => $item){ + foreach($this->inventory->getContents(true) as $slot => $item){ if($item->isNull()){ $items[$slot] = CompoundTag::create() ->setByte(SavedItemStackData::TAG_COUNT, 0) diff --git a/src/block/tile/Container.php b/src/block/tile/Container.php index dd257dd9c..1c4e37fec 100644 --- a/src/block/tile/Container.php +++ b/src/block/tile/Container.php @@ -23,15 +23,12 @@ declare(strict_types=1); namespace pocketmine\block\tile; -use pocketmine\inventory\Inventory; use pocketmine\inventory\InventoryHolder; interface Container extends InventoryHolder{ public const TAG_ITEMS = "Items"; public const TAG_LOCK = "Lock"; - public function getRealInventory() : Inventory; - /** * Returns whether this container can be opened by an item with the given custom name. */ diff --git a/src/block/tile/ContainerTrait.php b/src/block/tile/ContainerTrait.php index fdd050a41..cf69f37b5 100644 --- a/src/block/tile/ContainerTrait.php +++ b/src/block/tile/ContainerTrait.php @@ -25,7 +25,6 @@ namespace pocketmine\block\tile; use pocketmine\data\bedrock\item\SavedItemStackData; use pocketmine\data\SavedDataLoadingException; -use pocketmine\inventory\Inventory; use pocketmine\item\Item; use pocketmine\nbt\NBT; use pocketmine\nbt\tag\CompoundTag; @@ -40,11 +39,9 @@ trait ContainerTrait{ /** @var string|null */ private $lock = null; - abstract public function getRealInventory() : Inventory; - protected function loadItems(CompoundTag $tag) : void{ if(($inventoryTag = $tag->getTag(Container::TAG_ITEMS)) instanceof ListTag && $inventoryTag->getTagType() === NBT::TAG_Compound){ - $inventory = $this->getRealInventory(); + $inventory = $this->getInventory(); $listeners = $inventory->getListeners()->toArray(); $inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization @@ -71,7 +68,7 @@ trait ContainerTrait{ protected function saveItems(CompoundTag $tag) : void{ $items = []; - foreach($this->getRealInventory()->getContents() as $slot => $item){ + foreach($this->getInventory()->getContents() as $slot => $item){ $items[] = $item->nbtSerialize($slot); } @@ -98,7 +95,7 @@ trait ContainerTrait{ * @see Tile::onBlockDestroyedHook() */ protected function onBlockDestroyedHook() : void{ - $inv = $this->getRealInventory(); + $inv = $this->getInventory(); $pos = $this->getPosition(); $world = $pos->getWorld(); diff --git a/src/block/tile/Furnace.php b/src/block/tile/Furnace.php index f744e6b41..eefc8eb24 100644 --- a/src/block/tile/Furnace.php +++ b/src/block/tile/Furnace.php @@ -109,10 +109,6 @@ abstract class Furnace extends Spawnable implements Container, Nameable{ return $this->inventory; } - public function getRealInventory() : Inventory{ - return $this->getInventory(); - } - protected function checkFuel(Item $fuel) : void{ $ev = new FurnaceBurnEvent($this, $fuel, $fuel->getFuelTime()); $ev->call(); diff --git a/src/block/tile/Hopper.php b/src/block/tile/Hopper.php index 8e43fa15b..4a4273323 100644 --- a/src/block/tile/Hopper.php +++ b/src/block/tile/Hopper.php @@ -73,8 +73,4 @@ class Hopper extends Spawnable implements Container, Nameable{ public function getInventory() : Inventory{ return $this->inventory; } - - public function getRealInventory() : Inventory{ - return $this->inventory; - } } diff --git a/src/block/tile/ShulkerBox.php b/src/block/tile/ShulkerBox.php index 1c5b74a08..2e7565915 100644 --- a/src/block/tile/ShulkerBox.php +++ b/src/block/tile/ShulkerBox.php @@ -111,10 +111,6 @@ class ShulkerBox extends Spawnable implements Container, Nameable{ return $this->inventory; } - public function getRealInventory() : Inventory{ - return $this->inventory; - } - public function getDefaultName() : string{ return "Shulker Box"; }