Inventory: Removed need for Inventory to have an InventoryHolder

Inventory holders are now freed from BaseInventory. They are now declared by subclasses by convention, but are not required in most cases.

Ideally, this would be followed by the removal of the need for inventories to know their holders at all. They should just be simple containers of items.

This fixes #1560 by removing FakeBlockMenu.
This commit is contained in:
Dylan K. Taylor 2018-01-22 19:42:48 +00:00
parent e5ca22a9a6
commit 2fb580db26
11 changed files with 50 additions and 72 deletions

View File

@ -29,11 +29,11 @@ use pocketmine\Player;
class AnvilInventory extends ContainerInventory{
/** @var FakeBlockMenu */
/** @var Position */
protected $holder;
public function __construct(Position $pos){
parent::__construct(new FakeBlockMenu($this, $pos));
parent::__construct($pos->asPosition());
}
public function getNetworkType() : int{
@ -50,7 +50,7 @@ class AnvilInventory extends ContainerInventory{
/**
* This override is here for documentation and code completion purposes only.
* @return FakeBlockMenu
* @return Position
*/
public function getHolder(){
return $this->holder;

View File

@ -45,18 +45,13 @@ abstract class BaseInventory implements Inventory{
protected $slots = [];
/** @var Player[] */
protected $viewers = [];
/** @var InventoryHolder */
protected $holder;
/**
* @param InventoryHolder $holder
* @param Item[] $items
* @param int $size
* @param string $title
* @param Item[] $items
* @param int $size
* @param string $title
*/
public function __construct(InventoryHolder $holder, array $items = [], int $size = null, string $title = null){
$this->holder = $holder;
public function __construct(array $items = [], int $size = null, string $title = null){
$this->slots = new \SplFixedArray($size ?? $this->getDefaultSize());
$this->title = $title ?? $this->getName();
@ -379,10 +374,6 @@ abstract class BaseInventory implements Inventory{
}
}
public function getHolder(){
return $this->holder;
}
public function setMaxStackSize(int $size) : void{
$this->maxStackSize = $size;
}

View File

@ -30,6 +30,14 @@ use pocketmine\network\mcpe\protocol\ContainerOpenPacket;
use pocketmine\Player;
abstract class ContainerInventory extends BaseInventory{
/** @var Vector3 */
protected $holder;
public function __construct(Vector3 $holder, array $items = [], int $size = null, string $title = null){
$this->holder = $holder;
parent::__construct($items, $size, $title);
}
public function onOpen(Player $who) : void{
parent::onOpen($who);
$pk = new ContainerOpenPacket();
@ -65,4 +73,11 @@ abstract class ContainerInventory extends BaseInventory{
* @return int
*/
abstract public function getNetworkType() : int;
/**
* @return Vector3
*/
public function getHolder(){
return $this->holder;
}
}

View File

@ -26,9 +26,12 @@ namespace pocketmine\inventory;
use pocketmine\Player;
class CraftingGrid extends BaseInventory{
/** @var Player */
protected $holder;
public function __construct(Player $holder){
parent::__construct($holder);
$this->holder = $holder;
parent::__construct();
}
public function getGridWidth() : int{
@ -54,4 +57,11 @@ class CraftingGrid extends BaseInventory{
public function sendContents($target) : void{
//no way to do this
}
/**
* @return Player
*/
public function getHolder(){
return $this->holder;
}
}

View File

@ -37,7 +37,7 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
$this->left = $left->getRealInventory();
$this->right = $right->getRealInventory();
$items = array_merge($this->left->getContents(), $this->right->getContents());
BaseInventory::__construct($this, $items);
BaseInventory::__construct($items);
}
public function getName() : string{

View File

@ -29,11 +29,11 @@ use pocketmine\Player;
class EnchantInventory extends ContainerInventory{
/** @var FakeBlockMenu */
/** @var Position */
protected $holder;
public function __construct(Position $pos){
parent::__construct(new FakeBlockMenu($this, $pos));
parent::__construct($pos->asPosition());
}
public function getNetworkType() : int{
@ -50,7 +50,7 @@ class EnchantInventory extends ContainerInventory{
/**
* This override is here for documentation and code completion purposes only.
* @return FakeBlockMenu
* @return Position
*/
public function getHolder(){
return $this->holder;

View File

@ -24,16 +24,17 @@ declare(strict_types=1);
namespace pocketmine\inventory;
use pocketmine\entity\Human;
use pocketmine\level\Position;
use pocketmine\network\mcpe\protocol\types\WindowTypes;
use pocketmine\tile\EnderChest;
class EnderChestInventory extends ChestInventory{
/** @var FakeBlockMenu */
/** @var Position */
protected $holder;
public function __construct(Human $owner){
ContainerInventory::__construct(new FakeBlockMenu($this, $owner->getPosition()));
ContainerInventory::__construct(new Position());
}
public function getNetworkType() : int{
@ -60,7 +61,7 @@ class EnderChestInventory extends ChestInventory{
/**
* This override is here for documentation and code completion purposes only.
* @return FakeBlockMenu
* @return Position
*/
public function getHolder(){
return $this->holder;

View File

@ -32,6 +32,11 @@ abstract class EntityInventory extends BaseInventory{
/** @var Entity */
protected $holder;
public function __construct(Entity $holder, array $items = [], int $size = null, string $title = null){
$this->holder = $holder;
parent::__construct($items, $size, $title);
}
protected function doSetItemEvents(int $index, Item $newItem) : ?Item{
Server::getInstance()->getPluginManager()->callEvent($ev = new EntityInventoryChangeEvent($this->getHolder(), $this->getItem($index), $newItem, $index));
if($ev->isCancelled()){
@ -42,9 +47,9 @@ abstract class EntityInventory extends BaseInventory{
}
/**
* @return Entity|InventoryHolder
* @return Entity
*/
public function getHolder(){
return parent::getHolder();
return $this->holder;
}
}

View File

@ -1,40 +0,0 @@
<?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\inventory;
use pocketmine\level\Position;
class FakeBlockMenu extends Position implements InventoryHolder{
private $inventory;
public function __construct(Inventory $inventory, Position $pos){
$this->inventory = $inventory;
parent::__construct($pos->x, $pos->y, $pos->z, $pos->level);
}
public function getInventory(){
return $this->inventory;
}
}

View File

@ -201,11 +201,6 @@ interface Inventory{
*/
public function getViewers() : array;
/**
* @return InventoryHolder
*/
public function getHolder();
/**
* @param Player $who
*/

View File

@ -30,7 +30,8 @@ class PlayerCursorInventory extends BaseInventory{
protected $holder;
public function __construct(Player $holder){
parent::__construct($holder);
$this->holder = $holder;
parent::__construct();
}
public function getName() : string{