BaseInventory: Remove getDefaultSize()

it's possible to want to initialize dynamically-sized inventories which don't have a default size.
This commit is contained in:
Dylan K. Taylor 2019-03-22 19:21:41 +00:00
parent 60225a378f
commit 9904810f24
13 changed files with 17 additions and 64 deletions

View File

@ -33,17 +33,13 @@ class AnvilInventory extends ContainerInventory{
protected $holder; protected $holder;
public function __construct(Position $pos){ public function __construct(Position $pos){
parent::__construct($pos->asPosition()); parent::__construct($pos->asPosition(), 2);
} }
public function getNetworkType() : int{ public function getNetworkType() : int{
return WindowTypes::ANVIL; return WindowTypes::ANVIL;
} }
public function getDefaultSize() : int{
return 2; //1 input, 1 material
}
/** /**
* This override is here for documentation and code completion purposes only. * This override is here for documentation and code completion purposes only.
* @return Position * @return Position

View File

@ -43,17 +43,13 @@ class ArmorInventory extends BaseInventory{
public function __construct(Living $holder){ public function __construct(Living $holder){
$this->holder = $holder; $this->holder = $holder;
parent::__construct(); parent::__construct(4);
} }
public function getHolder() : Living{ public function getHolder() : Living{
return $this->holder; return $this->holder;
} }
public function getDefaultSize() : int{
return 4;
}
public function getHelmet() : Item{ public function getHelmet() : Item{
return $this->getItem(self::SLOT_HEAD); return $this->getItem(self::SLOT_HEAD);
} }

View File

@ -49,11 +49,11 @@ abstract class BaseInventory implements Inventory{
protected $slotChangeListener; protected $slotChangeListener;
/** /**
* @param Item[] $items
* @param int $size * @param int $size
* @param Item[] $items
*/ */
public function __construct(array $items = [], ?int $size = null){ public function __construct(int $size, array $items = []){
$this->slots = new \SplFixedArray($size ?? $this->getDefaultSize()); $this->slots = new \SplFixedArray($size);
$this->setContents($items, false); $this->setContents($items, false);
} }
@ -76,8 +76,6 @@ abstract class BaseInventory implements Inventory{
$this->slots->setSize($size); $this->slots->setSize($size);
} }
abstract public function getDefaultSize() : int;
public function getMaxStackSize() : int{ public function getMaxStackSize() : int{
return $this->maxStackSize; return $this->maxStackSize;
} }

View File

@ -39,17 +39,13 @@ class ChestInventory extends ContainerInventory{
* @param Chest $tile * @param Chest $tile
*/ */
public function __construct(Chest $tile){ public function __construct(Chest $tile){
parent::__construct($tile); parent::__construct($tile, 27);
} }
public function getNetworkType() : int{ public function getNetworkType() : int{
return WindowTypes::CONTAINER; return WindowTypes::CONTAINER;
} }
public function getDefaultSize() : int{
return 27;
}
/** /**
* This override is here for documentation and code completion purposes only. * This override is here for documentation and code completion purposes only.
* @return Chest * @return Chest

View File

@ -33,9 +33,9 @@ abstract class ContainerInventory extends BaseInventory{
/** @var Vector3 */ /** @var Vector3 */
protected $holder; protected $holder;
public function __construct(Vector3 $holder, array $items = [], ?int $size = null){ public function __construct(Vector3 $holder, int $size, array $items = []){
$this->holder = $holder; $this->holder = $holder;
parent::__construct($items, $size); parent::__construct($size, $items);
} }
protected function onOpen(Player $who) : void{ protected function onOpen(Player $who) : void{

View File

@ -50,17 +50,13 @@ class CraftingGrid extends BaseInventory{
public function __construct(Player $holder, int $gridWidth){ public function __construct(Player $holder, int $gridWidth){
$this->holder = $holder; $this->holder = $holder;
$this->gridWidth = $gridWidth; $this->gridWidth = $gridWidth;
parent::__construct(); parent::__construct($this->getGridWidth() ** 2);
} }
public function getGridWidth() : int{ public function getGridWidth() : int{
return $this->gridWidth; return $this->gridWidth;
} }
public function getDefaultSize() : int{
return $this->getGridWidth() ** 2;
}
public function setSize(int $size) : void{ public function setSize(int $size) : void{
throw new \BadMethodCallException("Cannot change the size of a crafting grid"); throw new \BadMethodCallException("Cannot change the size of a crafting grid");
} }

View File

@ -40,11 +40,7 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
$this->left = $left->getRealInventory(); $this->left = $left->getRealInventory();
$this->right = $right->getRealInventory(); $this->right = $right->getRealInventory();
$items = array_merge($this->left->getContents(true), $this->right->getContents(true)); $items = array_merge($this->left->getContents(true), $this->right->getContents(true));
BaseInventory::__construct($items); BaseInventory::__construct($this->left->getSize() + $this->right->getSize(), $items);
}
public function getDefaultSize() : int{
return $this->left->getDefaultSize() + $this->right->getDefaultSize();
} }
public function getInventory(){ public function getInventory(){

View File

@ -33,17 +33,13 @@ class EnchantInventory extends ContainerInventory{
protected $holder; protected $holder;
public function __construct(Position $pos){ public function __construct(Position $pos){
parent::__construct($pos->asPosition()); parent::__construct($pos->asPosition(), 2);
} }
public function getNetworkType() : int{ public function getNetworkType() : int{
return WindowTypes::ENCHANTMENT; return WindowTypes::ENCHANTMENT;
} }
public function getDefaultSize() : int{
return 2; //1 input, 1 lapis
}
/** /**
* This override is here for documentation and code completion purposes only. * This override is here for documentation and code completion purposes only.
* @return Position * @return Position

View File

@ -34,17 +34,13 @@ class EnderChestInventory extends ChestInventory{
protected $holder; protected $holder;
public function __construct(){ public function __construct(){
ContainerInventory::__construct(new Position()); ContainerInventory::__construct(new Position(), 27);
} }
public function getNetworkType() : int{ public function getNetworkType() : int{
return WindowTypes::CONTAINER; return WindowTypes::CONTAINER;
} }
public function getDefaultSize() : int{
return 27;
}
/** /**
* Set the holder's position to that of a tile * Set the holder's position to that of a tile
* *

View File

@ -32,17 +32,13 @@ class FurnaceInventory extends ContainerInventory{
protected $holder; protected $holder;
public function __construct(Furnace $tile){ public function __construct(Furnace $tile){
parent::__construct($tile); parent::__construct($tile, 3);
} }
public function getNetworkType() : int{ public function getNetworkType() : int{
return WindowTypes::FURNACE; return WindowTypes::FURNACE;
} }
public function getDefaultSize() : int{
return 3; //1 input, 1 fuel, 1 output
}
/** /**
* This override is here for documentation and code completion purposes only. * This override is here for documentation and code completion purposes only.
* @return Furnace * @return Furnace

View File

@ -31,11 +31,7 @@ class PlayerCursorInventory extends BaseInventory{
public function __construct(Player $holder){ public function __construct(Player $holder){
$this->holder = $holder; $this->holder = $holder;
parent::__construct(); parent::__construct(1);
}
public function getDefaultSize() : int{
return 1;
} }
public function setSize(int $size) : void{ public function setSize(int $size) : void{

View File

@ -45,11 +45,7 @@ class PlayerInventory extends BaseInventory{
*/ */
public function __construct(Human $player){ public function __construct(Human $player){
$this->holder = $player; $this->holder = $player;
parent::__construct(); parent::__construct(36);
}
public function getDefaultSize() : int{
return 36;
} }
public function isHotbarSlot(int $slot) : bool{ public function isHotbarSlot(int $slot) : bool{

View File

@ -34,13 +34,8 @@ class BaseInventoryTest extends TestCase{
} }
public function testAddItemDifferentUserData() : void{ public function testAddItemDifferentUserData() : void{
$inv = new class extends BaseInventory{ $inv = new class(1) extends BaseInventory{
public function getDefaultSize() : int{
return 1;
}
public function getName() : string{
return "";
}
}; };
$item1 = ItemFactory::get(Item::ARROW, 0, 1); $item1 = ItemFactory::get(Item::ARROW, 0, 1);
$item2 = ItemFactory::get(Item::ARROW, 0, 1)->setCustomName("TEST"); $item2 = ItemFactory::get(Item::ARROW, 0, 1)->setCustomName("TEST");