Removed InventoryType, added new inventory API methods

This commit is contained in:
Dylan K. Taylor 2017-08-07 11:31:36 +01:00
parent 899e318a88
commit 98e0a2ecba
13 changed files with 115 additions and 264 deletions

View File

@ -47,7 +47,6 @@ use pocketmine\event\Timings;
use pocketmine\event\TimingsHandler;
use pocketmine\event\TranslationContainer;
use pocketmine\inventory\CraftingManager;
use pocketmine\inventory\InventoryType;
use pocketmine\inventory\Recipe;
use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\Item;
@ -1582,7 +1581,6 @@ class Server{
Entity::init();
Tile::init();
InventoryType::init();
Block::init();
Enchantment::init();
Item::init();

View File

@ -24,11 +24,28 @@ declare(strict_types=1);
namespace pocketmine\inventory;
use pocketmine\level\Position;
use pocketmine\network\mcpe\protocol\types\WindowTypes;
use pocketmine\Player;
class AnvilInventory extends ContainerInventory{
/** @var FakeBlockMenu */
protected $holder;
public function __construct(Position $pos){
parent::__construct(new FakeBlockMenu($this, $pos), InventoryType::get(InventoryType::ANVIL));
parent::__construct(new FakeBlockMenu($this, $pos));
}
public function getNetworkType() : int{
return WindowTypes::ANVIL;
}
public function getName() : string{
return "Anvil";
}
public function getDefaultSize() : int{
return 3; //1 input, 1 material, 1 result
}
/**

View File

@ -34,8 +34,6 @@ use pocketmine\Server;
abstract class BaseInventory implements Inventory{
/** @var InventoryType */
protected $type;
/** @var int */
protected $maxStackSize = Inventory::MAX_STACK;
/** @var int */
@ -53,37 +51,31 @@ abstract class BaseInventory implements Inventory{
/**
* @param InventoryHolder $holder
* @param InventoryType $type
* @param Item[] $items
* @param int $overrideSize
* @param string $overrideTitle
* @param int $size
* @param string $title
*/
public function __construct(InventoryHolder $holder, InventoryType $type, array $items = [], $overrideSize = null, $overrideTitle = null){
public function __construct(InventoryHolder $holder, array $items = [], int $size = null, string $title = null){
$this->holder = $holder;
$this->type = $type;
if($overrideSize !== null){
$this->size = (int) $overrideSize;
}else{
$this->size = $this->type->getDefaultSize();
}
if($overrideTitle !== null){
$this->title = $overrideTitle;
}else{
$this->title = $this->type->getDefaultTitle();
}
$this->name = $this->type->getDefaultTitle();
$this->size = $size ?? $this->getDefaultSize();
$this->title = $title ?? $this->getName();
$this->setContents($items);
}
public function __destruct(){
$this->holder = null;
$this->slots = [];
abstract public function getName() : string;
public function getTitle() : string{
return $this->title;
}
/**
* Returns the Minecraft PE inventory type used to show the inventory window to clients.
* @return int
*/
abstract public function getNetworkType() : int;
public function getSize() : int{
return $this->size;
}
@ -92,18 +84,12 @@ abstract class BaseInventory implements Inventory{
$this->size = $size;
}
abstract public function getDefaultSize() : int;
public function getMaxStackSize() : int{
return $this->maxStackSize;
}
public function getName() : string{
return $this->name;
}
public function getTitle() : string{
return $this->title;
}
public function getItem(int $index) : Item{
assert($index >= 0, "Inventory slot should not be negative");
return isset($this->slots[$index]) ? clone $this->slots[$index] : Item::get(Item::AIR, 0, 0);
@ -456,9 +442,4 @@ abstract class BaseInventory implements Inventory{
$player->dataPacket($pk);
}
}
public function getType() : InventoryType{
return $this->type;
}
}

View File

@ -26,12 +26,32 @@ namespace pocketmine\inventory;
use pocketmine\level\Level;
use pocketmine\network\mcpe\protocol\BlockEventPacket;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
use pocketmine\network\mcpe\protocol\types\WindowTypes;
use pocketmine\Player;
use pocketmine\tile\Chest;
class ChestInventory extends ContainerInventory{
/** @var Chest */
protected $holder;
/**
* @param Chest $tile
*/
public function __construct(Chest $tile){
parent::__construct($tile, InventoryType::get(InventoryType::CHEST));
parent::__construct($tile);
}
public function getNetworkType() : int{
return WindowTypes::CONTAINER;
}
public function getName() : string{
return "Chest";
}
public function getDefaultSize() : int{
return 27;
}
/**

View File

@ -33,7 +33,7 @@ abstract class ContainerInventory extends BaseInventory{
parent::onOpen($who);
$pk = new ContainerOpenPacket();
$pk->windowid = $who->getWindowId($this);
$pk->type = $this->getType()->getNetworkType();
$pk->type = $this->getNetworkType();
$holder = $this->getHolder();
if($holder instanceof Vector3){
$pk->x = $holder->getX();

View File

@ -1,62 +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;
/**
* Manages crafting operations
* This class includes future methods for shaped crafting
*
* TODO: add small matrix inventory
*/
class CraftingInventory extends BaseInventory{
/** @var Inventory */
private $resultInventory;
/**
* @param InventoryHolder $holder
* @param Inventory $resultInventory
* @param InventoryType $inventoryType
*
* @throws \Exception
*/
public function __construct(InventoryHolder $holder, Inventory $resultInventory, InventoryType $inventoryType){
if($inventoryType->getDefaultTitle() !== "Crafting"){
throw new \InvalidStateException("Invalid Inventory type, expected CRAFTING or WORKBENCH");
}
$this->resultInventory = $resultInventory;
parent::__construct($holder, $inventoryType);
}
/**
* @return Inventory
*/
public function getResultInventory(){
return $this->resultInventory;
}
public function getSize() : int{
return $this->getResultInventory()->getSize() + parent::getSize();
}
}

View File

@ -39,7 +39,15 @@ 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, InventoryType::get(InventoryType::DOUBLE_CHEST), $items);
BaseInventory::__construct($this, $items);
}
public function getName() : string{
return "Double Chest";
}
public function getDefaultSize() : int{
return $this->left->getDefaultSize() + $this->right->getDefaultSize();
}
public function getInventory(){

View File

@ -24,11 +24,28 @@ declare(strict_types=1);
namespace pocketmine\inventory;
use pocketmine\level\Position;
use pocketmine\network\mcpe\protocol\types\WindowTypes;
use pocketmine\Player;
class EnchantInventory extends ContainerInventory{
/** @var FakeBlockMenu */
protected $holder;
public function __construct(Position $pos){
parent::__construct(new FakeBlockMenu($this, $pos), InventoryType::get(InventoryType::ENCHANT_TABLE));
parent::__construct(new FakeBlockMenu($this, $pos));
}
public function getNetworkType() : int{
return WindowTypes::ENCHANTMENT;
}
public function getName() : string{
return "Enchantment Table";
}
public function getDefaultSize() : int{
return 2; //1 input, 1 lapis
}
/**

View File

@ -24,11 +24,27 @@ declare(strict_types=1);
namespace pocketmine\inventory;
use pocketmine\item\Item;
use pocketmine\network\mcpe\protocol\types\WindowTypes;
use pocketmine\tile\Furnace;
class FurnaceInventory extends ContainerInventory{
/** @var Furnace */
protected $holder;
public function __construct(Furnace $tile){
parent::__construct($tile, InventoryType::get(InventoryType::FURNACE));
parent::__construct($tile);
}
public function getNetworkType() : int{
return WindowTypes::FURNACE;
}
public function getName() : string{
return "Furnace";
}
public function getDefaultSize() : int{
return 3; //1 input, 1 fuel, 1 output
}
/**

View File

@ -193,11 +193,6 @@ interface Inventory{
*/
public function getViewers() : array;
/**
* @return InventoryType
*/
public function getType() : InventoryType;
/**
* @return InventoryHolder
*/

View File

@ -1,111 +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\network\mcpe\protocol\types\WindowTypes;
/**
* Saves all the information regarding default inventory sizes and types
*/
class InventoryType{
//NOTE: Do not confuse these with the network IDs.
const CHEST = 0;
const DOUBLE_CHEST = 1;
const PLAYER = 2;
const FURNACE = 3;
const CRAFTING = 4;
const WORKBENCH = 5;
const STONECUTTER = 6;
const BREWING_STAND = 7;
const ANVIL = 8;
const ENCHANT_TABLE = 9;
private static $default = [];
private $size;
private $title;
private $typeId;
/**
* @param $index
*
* @return InventoryType|null
*/
public static function get($index){
return static::$default[$index] ?? null;
}
public static function init(){
if(count(static::$default) > 0){
return;
}
//TODO: move network stuff out of here
//TODO: move inventory data to json
static::$default = [
static::CHEST => new InventoryType(27, "Chest", WindowTypes::CONTAINER),
static::DOUBLE_CHEST => new InventoryType(27 + 27, "Double Chest", WindowTypes::CONTAINER),
static::PLAYER => new InventoryType(36 + 4, "Player", WindowTypes::INVENTORY), //36 CONTAINER, 4 ARMOR
static::CRAFTING => new InventoryType(5, "Crafting", WindowTypes::INVENTORY), //yes, the use of INVENTORY is intended! 4 CRAFTING slots, 1 RESULT
static::WORKBENCH => new InventoryType(10, "Crafting", WindowTypes::WORKBENCH), //9 CRAFTING slots, 1 RESULT
static::FURNACE => new InventoryType(3, "Furnace", WindowTypes::FURNACE), //2 INPUT, 1 OUTPUT
static::ENCHANT_TABLE => new InventoryType(2, "Enchant", WindowTypes::ENCHANTMENT), //1 INPUT/OUTPUT, 1 LAPIS
static::BREWING_STAND => new InventoryType(4, "Brewing", WindowTypes::BREWING_STAND), //1 INPUT, 3 POTION
static::ANVIL => new InventoryType(3, "Anvil", WindowTypes::ANVIL) //2 INPUT, 1 OUTP
];
}
/**
* @param int $defaultSize
* @param string $defaultTitle
* @param int $typeId
*/
private function __construct($defaultSize, $defaultTitle, $typeId = 0){
$this->size = $defaultSize;
$this->title = $defaultTitle;
$this->typeId = $typeId;
}
/**
* @return int
*/
public function getDefaultSize() : int{
return $this->size;
}
/**
* @return string
*/
public function getDefaultTitle() : string{
return $this->title;
}
/**
* @return int
*/
public function getNetworkType() : int{
return $this->typeId;
}
}

View File

@ -34,6 +34,7 @@ use pocketmine\network\mcpe\protocol\MobArmorEquipmentPacket;
use pocketmine\network\mcpe\protocol\MobEquipmentPacket;
use pocketmine\network\mcpe\protocol\PlayerHotbarPacket;
use pocketmine\network\mcpe\protocol\types\ContainerIds;
use pocketmine\network\mcpe\protocol\types\WindowTypes;
use pocketmine\Player;
use pocketmine\Server;
@ -45,7 +46,19 @@ class PlayerInventory extends BaseInventory{
public function __construct(Human $player){
$this->resetHotbar(false);
parent::__construct($player, InventoryType::get(InventoryType::PLAYER));
parent::__construct($player);
}
public function getNetworkType() : int{
return WindowTypes::INVENTORY;
}
public function getName() : string{
return "Player";
}
public function getDefaultSize() : int{
return 40; //36 inventory, 4 armor
}
public function getSize() : int{

View File

@ -1,41 +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;
/**
* Saves all the information regarding default inventory sizes and types
*/
interface SlotType{
const RESULT = 0;
const CRAFTING = 1; //Not used in Minecraft: PE yet
const ARMOR = 2;
const CONTAINER = 3;
const HOTBAR = 4;
const FUEL = 5;
}