mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-16 02:38:54 +00:00
Chest block now has responsibility for configuring double chest inventories
it already needs to locate the correct pair anyway to know the left/right for DoubleChestInventoryWindow, so we might as well use this logic for initializing the DoubleChestInventory itself too. The old logic in tile/Chest didn't work correctly.
This commit is contained in:
parent
9c5df90e9b
commit
1d2b52732e
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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";
|
||||
}
|
||||
|
@ -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)){
|
||||
|
@ -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<int, int>
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user