All BlockInventory descendents now have a Position as holder

this allows multiple problems to be solved:
1) Cycle between tile and inventory is now removed.
2) BlockInventory now provides a consistent API for plugins to get the block holding an inventory.
This commit is contained in:
Dylan K. Taylor 2019-08-05 18:50:29 +01:00
parent 358fea9645
commit 9353f616a2
14 changed files with 36 additions and 95 deletions

View File

@ -53,11 +53,11 @@ class BrewingStand extends Spawnable implements Container, Nameable{
public function __construct(World $world, Vector3 $pos){ public function __construct(World $world, Vector3 $pos){
parent::__construct($world, $pos);
$this->inventory = new BrewingStandInventory($this); $this->inventory = new BrewingStandInventory($this);
$this->inventory->addChangeListeners(CallbackInventoryChangeListener::onAnyChange(function(Inventory $unused){ $this->inventory->addChangeListeners(CallbackInventoryChangeListener::onAnyChange(function(Inventory $unused){
$this->world->scheduleDelayedBlockUpdate($this->getBlock()->getPos(), 1); $this->world->scheduleDelayedBlockUpdate($this->getBlock()->getPos(), 1);
})); }));
parent::__construct($world, $pos);
} }
public function readSaveData(CompoundTag $nbt) : void{ public function readSaveData(CompoundTag $nbt) : void{

View File

@ -54,8 +54,8 @@ class Chest extends Spawnable implements Container, Nameable{
private $pairZ; private $pairZ;
public function __construct(World $world, Vector3 $pos){ public function __construct(World $world, Vector3 $pos){
$this->inventory = new ChestInventory($this);
parent::__construct($world, $pos); parent::__construct($world, $pos);
$this->inventory = new ChestInventory($this);
} }
public function readSaveData(CompoundTag $nbt) : void{ public function readSaveData(CompoundTag $nbt) : void{
@ -152,9 +152,9 @@ class Chest extends Spawnable implements Container, Nameable{
$this->doubleInventory = $pair->doubleInventory; $this->doubleInventory = $pair->doubleInventory;
}else{ }else{
if(($pair->x + ($pair->z << 15)) > ($this->x + ($this->z << 15))){ //Order them correctly if(($pair->x + ($pair->z << 15)) > ($this->x + ($this->z << 15))){ //Order them correctly
$this->doubleInventory = $pair->doubleInventory = new DoubleChestInventory($pair, $this); $this->doubleInventory = $pair->doubleInventory = new DoubleChestInventory($pair->inventory, $this->inventory);
}else{ }else{
$this->doubleInventory = $pair->doubleInventory = new DoubleChestInventory($this, $pair); $this->doubleInventory = $pair->doubleInventory = new DoubleChestInventory($this->inventory, $pair->inventory);
} }
} }
} }

View File

@ -56,14 +56,13 @@ class Furnace extends Spawnable implements Container, Nameable{
private $maxFuelTime = 0; private $maxFuelTime = 0;
public function __construct(World $world, Vector3 $pos){ public function __construct(World $world, Vector3 $pos){
parent::__construct($world, $pos);
$this->inventory = new FurnaceInventory($this); $this->inventory = new FurnaceInventory($this);
$this->inventory->addChangeListeners(CallbackInventoryChangeListener::onAnyChange( $this->inventory->addChangeListeners(CallbackInventoryChangeListener::onAnyChange(
function(Inventory $unused) : void{ function(Inventory $unused) : void{
$this->world->scheduleDelayedBlockUpdate($this->asVector3(), 1); $this->world->scheduleDelayedBlockUpdate($this->asVector3(), 1);
}) })
); );
parent::__construct($world, $pos);
} }
public function readSaveData(CompoundTag $nbt) : void{ public function readSaveData(CompoundTag $nbt) : void{

View File

@ -42,8 +42,8 @@ class Hopper extends Spawnable implements Container, Nameable{
private $transferCooldown = 0; private $transferCooldown = 0;
public function __construct(World $world, Vector3 $pos){ public function __construct(World $world, Vector3 $pos){
$this->inventory = new HopperInventory($this);
parent::__construct($world, $pos); parent::__construct($world, $pos);
$this->inventory = new HopperInventory($this);
} }
public function readSaveData(CompoundTag $nbt) : void{ public function readSaveData(CompoundTag $nbt) : void{

View File

@ -50,8 +50,8 @@ abstract class Tile extends Position{
protected $timings; protected $timings;
public function __construct(World $world, Vector3 $pos){ public function __construct(World $world, Vector3 $pos){
$this->timings = Timings::getTileEntityTimings($this);
parent::__construct($pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ(), $world); parent::__construct($pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ(), $world);
$this->timings = Timings::getTileEntityTimings($this);
} }
/** /**

View File

@ -28,19 +28,8 @@ use pocketmine\world\Position;
class AnvilInventory extends BlockInventory{ class AnvilInventory extends BlockInventory{
/** @var Position */ public function __construct(Position $holder){
protected $holder; parent::__construct($holder, 2);
public function __construct(Position $pos){
parent::__construct($pos->asPosition(), 2);
}
/**
* This override is here for documentation and code completion purposes only.
* @return Position
*/
public function getHolder(){
return $this->holder;
} }
public function onClose(Player $who) : void{ public function onClose(Player $who) : void{

View File

@ -23,19 +23,19 @@ declare(strict_types=1);
namespace pocketmine\inventory; namespace pocketmine\inventory;
use pocketmine\math\Vector3; use pocketmine\world\Position;
class BlockInventory extends BaseInventory{ class BlockInventory extends BaseInventory{
/** @var Vector3 */ /** @var Position */
protected $holder; protected $holder;
public function __construct(Vector3 $holder, int $size){ public function __construct(Position $holder, int $size){
$this->holder = $holder; $this->holder = $holder->asPosition();
parent::__construct($size); parent::__construct($size);
} }
/** /**
* @return Vector3 * @return Position
*/ */
public function getHolder(){ public function getHolder(){
return $this->holder; return $this->holder;

View File

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace pocketmine\inventory; namespace pocketmine\inventory;
use pocketmine\math\Vector3; use pocketmine\world\Position;
class BrewingStandInventory extends BlockInventory{ class BrewingStandInventory extends BlockInventory{
public function __construct(Vector3 $holder, int $size = 5){ public function __construct(Position $holder, int $size = 5){
parent::__construct($holder, $size); parent::__construct($holder, $size);
} }
} }

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace pocketmine\inventory; namespace pocketmine\inventory;
use pocketmine\block\tile\Chest;
use pocketmine\network\mcpe\protocol\BlockEventPacket; use pocketmine\network\mcpe\protocol\BlockEventPacket;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\world\Position;
use pocketmine\world\sound\ChestCloseSound; use pocketmine\world\sound\ChestCloseSound;
use pocketmine\world\sound\ChestOpenSound; use pocketmine\world\sound\ChestOpenSound;
use pocketmine\world\sound\Sound; use pocketmine\world\sound\Sound;
@ -33,22 +33,11 @@ use function count;
class ChestInventory extends BlockInventory{ class ChestInventory extends BlockInventory{
/** @var Chest */
protected $holder;
/** /**
* @param Chest $tile * @param Position $holder
*/ */
public function __construct(Chest $tile){ public function __construct(Position $holder){
parent::__construct($tile, 27); parent::__construct($holder, 27);
}
/**
* This override is here for documentation and code completion purposes only.
* @return Chest
*/
public function getHolder(){
return $this->holder;
} }
protected function getOpenSound() : Sound{ protected function getOpenSound() : Sound{

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace pocketmine\inventory; namespace pocketmine\inventory;
use pocketmine\block\tile\Chest;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\world\Position;
use function count; use function count;
class DoubleChestInventory extends ChestInventory implements InventoryHolder{ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
@ -34,9 +34,9 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
/** @var ChestInventory */ /** @var ChestInventory */
private $right; private $right;
public function __construct(Chest $left, Chest $right){ public function __construct(ChestInventory $left, ChestInventory $right){
$this->left = $left->getRealInventory(); $this->left = $left;
$this->right = $right->getRealInventory(); $this->right = $right;
BaseInventory::__construct($this->left->getSize() + $this->right->getSize()); BaseInventory::__construct($this->left->getSize() + $this->right->getSize());
} }
@ -45,7 +45,7 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
} }
/** /**
* @return Chest * @return Position
*/ */
public function getHolder(){ public function getHolder(){
return $this->left->getHolder(); return $this->left->getHolder();

View File

@ -28,19 +28,8 @@ use pocketmine\world\Position;
class EnchantInventory extends BlockInventory{ class EnchantInventory extends BlockInventory{
/** @var Position */ public function __construct(Position $holder){
protected $holder; parent::__construct($holder, 2);
public function __construct(Position $pos){
parent::__construct($pos->asPosition(), 2);
}
/**
* This override is here for documentation and code completion purposes only.
* @return Position
*/
public function getHolder(){
return $this->holder;
} }
public function onClose(Player $who) : void{ public function onClose(Player $who) : void{

View File

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\inventory; namespace pocketmine\inventory;
use pocketmine\block\tile\EnderChest;
use pocketmine\world\Position; use pocketmine\world\Position;
use pocketmine\world\sound\EnderChestCloseSound; use pocketmine\world\sound\EnderChestCloseSound;
use pocketmine\world\sound\EnderChestOpenSound; use pocketmine\world\sound\EnderChestOpenSound;
@ -31,21 +30,15 @@ use pocketmine\world\sound\Sound;
class EnderChestInventory extends ChestInventory{ class EnderChestInventory extends ChestInventory{
/** @var Position */
protected $holder;
public function __construct(){ public function __construct(){
BlockInventory::__construct(new Position(), 27); parent::__construct(new Position(0, 0, 0, null));
} }
/** /**
* Set the holder's position to that of a tile * @param Position $pos
*
* @param EnderChest $enderChest
*/ */
public function setHolderPosition(EnderChest $enderChest) : void{ public function setHolderPosition(Position $pos) : void{
$this->holder->setComponents($enderChest->getFloorX(), $enderChest->getFloorY(), $enderChest->getFloorZ()); $this->holder = $pos->asPosition();
$this->holder->setWorld($enderChest->getWorld());
} }
protected function getOpenSound() : Sound{ protected function getOpenSound() : Sound{
@ -55,12 +48,4 @@ class EnderChestInventory extends ChestInventory{
protected function getCloseSound() : Sound{ protected function getCloseSound() : Sound{
return new EnderChestCloseSound(); return new EnderChestCloseSound();
} }
/**
* This override is here for documentation and code completion purposes only.
* @return Position
*/
public function getHolder(){
return $this->holder;
}
} }

View File

@ -23,23 +23,13 @@ declare(strict_types=1);
namespace pocketmine\inventory; namespace pocketmine\inventory;
use pocketmine\block\tile\Furnace;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\world\Position;
class FurnaceInventory extends BlockInventory{ class FurnaceInventory extends BlockInventory{
/** @var Furnace */
protected $holder;
public function __construct(Furnace $tile){ public function __construct(Position $holder){
parent::__construct($tile, 3); parent::__construct($holder, 3);
}
/**
* This override is here for documentation and code completion purposes only.
* @return Furnace
*/
public function getHolder(){
return $this->holder;
} }
/** /**

View File

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace pocketmine\inventory; namespace pocketmine\inventory;
use pocketmine\math\Vector3; use pocketmine\world\Position;
class HopperInventory extends BlockInventory{ class HopperInventory extends BlockInventory{
public function __construct(Vector3 $holder, int $size = 5){ public function __construct(Position $holder, int $size = 5){
parent::__construct($holder, $size); parent::__construct($holder, $size);
} }
} }