and more typehints

This commit is contained in:
Dylan K. Taylor
2017-07-14 10:56:51 +01:00
parent b9355387da
commit c3b8be3f60
119 changed files with 598 additions and 541 deletions

View File

@ -84,32 +84,32 @@ abstract class BaseInventory implements Inventory{
$this->slots = [];
}
public function getSize(){
public function getSize() : int{
return $this->size;
}
public function setSize($size){
$this->size = (int) $size;
public function setSize(int $size){
$this->size = $size;
}
public function getMaxStackSize(){
public function getMaxStackSize() : int{
return $this->maxStackSize;
}
public function getName(){
public function getName() : string{
return $this->name;
}
public function getTitle(){
public function getTitle() : string{
return $this->title;
}
public function getItem($index){
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);
}
public function getContents(){
public function getContents() : array{
return $this->slots;
}
@ -134,7 +134,7 @@ abstract class BaseInventory implements Inventory{
}
}
public function setItem($index, Item $item){
public function setItem(int $index, Item $item) : bool{
$item = clone $item;
if($index < 0 or $index >= $this->size){
return false;
@ -159,7 +159,7 @@ abstract class BaseInventory implements Inventory{
return true;
}
public function contains(Item $item){
public function contains(Item $item) : bool{
$count = max(1, $item->getCount());
$checkDamage = !$item->hasAnyDamageValue();
$checkTags = $item->hasCompoundTag();
@ -175,7 +175,7 @@ abstract class BaseInventory implements Inventory{
return false;
}
public function all(Item $item){
public function all(Item $item) : array{
$slots = [];
$checkDamage = !$item->hasAnyDamageValue();
$checkTags = $item->hasCompoundTag();
@ -199,7 +199,7 @@ abstract class BaseInventory implements Inventory{
}
}
public function first(Item $item){
public function first(Item $item) : int{
$count = max(1, $item->getCount());
$checkDamage = !$item->hasAnyDamageValue();
$checkTags = $item->hasCompoundTag();
@ -213,7 +213,7 @@ abstract class BaseInventory implements Inventory{
return -1;
}
public function firstEmpty(){
public function firstEmpty() : int{
for($i = 0; $i < $this->size; ++$i){
if($this->getItem($i)->getId() === Item::AIR){
return $i;
@ -223,7 +223,7 @@ abstract class BaseInventory implements Inventory{
return -1;
}
public function canAddItem(Item $item){
public function canAddItem(Item $item) : bool{
$item = clone $item;
$checkDamage = !$item->hasAnyDamageValue();
$checkTags = $item->hasCompoundTag();
@ -245,7 +245,7 @@ abstract class BaseInventory implements Inventory{
return false;
}
public function addItem(Item ...$slots){
public function addItem(Item ...$slots) : array{
/** @var Item[] $itemSlots */
/** @var Item[] $slots */
$itemSlots = [];
@ -302,7 +302,7 @@ abstract class BaseInventory implements Inventory{
return $itemSlots;
}
public function removeItem(Item ...$slots){
public function removeItem(Item ...$slots) : array{
/** @var Item[] $itemSlots */
/** @var Item[] $slots */
$itemSlots = [];
@ -338,7 +338,7 @@ abstract class BaseInventory implements Inventory{
return $itemSlots;
}
public function clear($index){
public function clear(int $index) : bool{
if(isset($this->slots[$index])){
$item = Item::get(Item::AIR, 0, 0);
$old = $this->slots[$index];
@ -372,7 +372,7 @@ abstract class BaseInventory implements Inventory{
/**
* @return Player[]
*/
public function getViewers(){
public function getViewers() : array{
return $this->viewers;
}
@ -380,11 +380,11 @@ abstract class BaseInventory implements Inventory{
return $this->holder;
}
public function setMaxStackSize($size){
$this->maxStackSize = (int) $size;
public function setMaxStackSize(int $size){
$this->maxStackSize = $size;
}
public function open(Player $who){
public function open(Player $who) : bool{
$who->getServer()->getPluginManager()->callEvent($ev = new InventoryOpenEvent($this, $who));
if($ev->isCancelled()){
return false;
@ -459,7 +459,7 @@ abstract class BaseInventory implements Inventory{
}
}
public function getType(){
public function getType() : InventoryType{
return $this->type;
}

View File

@ -43,31 +43,31 @@ class BaseTransaction implements Transaction{
* @param Item $sourceItem
* @param Item $targetItem
*/
public function __construct(Inventory $inventory, $slot, Item $sourceItem, Item $targetItem){
public function __construct(Inventory $inventory, int $slot, Item $sourceItem, Item $targetItem){
$this->inventory = $inventory;
$this->slot = (int) $slot;
$this->slot = $slot;
$this->sourceItem = clone $sourceItem;
$this->targetItem = clone $targetItem;
$this->creationTime = microtime(true);
}
public function getCreationTime(){
public function getCreationTime() : float{
return $this->creationTime;
}
public function getInventory(){
public function getInventory() : Inventory{
return $this->inventory;
}
public function getSlot(){
public function getSlot() : int{
return $this->slot;
}
public function getSourceItem(){
public function getSourceItem() : Item{
return clone $this->sourceItem;
}
public function getTargetItem(){
public function getTargetItem() : Item{
return clone $this->targetItem;
}
}

View File

@ -56,7 +56,7 @@ class CraftingInventory extends BaseInventory{
return $this->resultInventory;
}
public function getSize(){
public function getSize() : int{
return $this->getResultInventory()->getSize() + parent::getSize();
}
}

View File

@ -161,14 +161,14 @@ class CraftingManager{
/**
* @return Recipe[]
*/
public function getRecipes(){
public function getRecipes() : array{
return $this->recipes;
}
/**
* @return FurnaceRecipe[]
*/
public function getFurnaceRecipes(){
public function getFurnaceRecipes() : array{
return $this->furnaceRecipes;
}
@ -239,7 +239,7 @@ class CraftingManager{
* @param ShapelessRecipe $recipe
* @return bool
*/
public function matchRecipe(ShapelessRecipe $recipe){
public function matchRecipe(ShapelessRecipe $recipe) : bool{
if(!isset($this->recipeLookup[$idx = $recipe->getResult()->getId() . ":" . $recipe->getResult()->getDamage()])){
return false;
}

View File

@ -50,19 +50,19 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
return $this->left->getHolder();
}
public function getItem($index){
public function getItem(int $index) : Item{
return $index < $this->left->getSize() ? $this->left->getItem($index) : $this->right->getItem($index - $this->right->getSize());
}
public function setItem($index, Item $item){
public function setItem(int $index, Item $item) : bool{
return $index < $this->left->getSize() ? $this->left->setItem($index, $item) : $this->right->setItem($index - $this->right->getSize(), $item);
}
public function clear($index){
public function clear(int $index) : bool{
return $index < $this->left->getSize() ? $this->left->clear($index) : $this->right->clear($index - $this->right->getSize());
}
public function getContents(){
public function getContents() : array{
$contents = [];
for($i = 0; $i < $this->getSize(); ++$i){
$contents[$i] = $this->getItem($i);
@ -129,14 +129,14 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
/**
* @return ChestInventory
*/
public function getLeftSide(){
public function getLeftSide() : ChestInventory{
return $this->left;
}
/**
* @return ChestInventory
*/
public function getRightSide(){
public function getRightSide() : ChestInventory{
return $this->right;
}
}

View File

@ -41,21 +41,21 @@ class FurnaceInventory extends ContainerInventory{
/**
* @return Item
*/
public function getResult(){
public function getResult() : Item{
return $this->getItem(2);
}
/**
* @return Item
*/
public function getFuel(){
public function getFuel() : Item{
return $this->getItem(1);
}
/**
* @return Item
*/
public function getSmelting(){
public function getSmelting() : Item{
return $this->getItem(0);
}
@ -64,7 +64,7 @@ class FurnaceInventory extends ContainerInventory{
*
* @return bool
*/
public function setResult(Item $item){
public function setResult(Item $item) : bool{
return $this->setItem(2, $item);
}
@ -73,7 +73,7 @@ class FurnaceInventory extends ContainerInventory{
*
* @return bool
*/
public function setFuel(Item $item){
public function setFuel(Item $item) : bool{
return $this->setItem(1, $item);
}
@ -82,7 +82,7 @@ class FurnaceInventory extends ContainerInventory{
*
* @return bool
*/
public function setSmelting(Item $item){
public function setSmelting(Item $item) : bool{
return $this->setItem(0, $item);
}

View File

@ -32,37 +32,48 @@ use pocketmine\Player;
interface Inventory{
const MAX_STACK = 64;
public function getSize();
/**
* @return int
*/
public function getSize() : int;
public function getMaxStackSize();
/**
* @return int
*/
public function getMaxStackSize() : int;
/**
* @param int $size
*/
public function setMaxStackSize($size);
public function setMaxStackSize(int $size);
public function getName();
/**
* @return string
*/
public function getName() : string;
public function getTitle();
/**
* @return string
*/
public function getTitle() : string;
/**
* @param int $index
*
* @return Item
*/
public function getItem($index);
public function getItem(int $index) : Item;
/**
* Puts an Item in a slot.
* If a plugin refuses the update or $index is invalid, it'll return false
* If a source Player is specified, it won't send a Inventory update to it
*
* @param int $index
* @param Item $item
*
* @return bool
*/
public function setItem($index, Item $item);
public function setItem(int $index, Item $item) : bool;
/**
* Stores the given Items in the inventory. This will try to fill
@ -74,7 +85,7 @@ interface Inventory{
*
* @return Item[]
*/
public function addItem(Item ...$slots);
public function addItem(Item ...$slots) : array;
/**
* Checks if a given Item can be added to the inventory
@ -83,7 +94,7 @@ interface Inventory{
*
* @return bool
*/
public function canAddItem(Item $item);
public function canAddItem(Item $item) : bool;
/**
* Removes the given Item from the inventory.
@ -93,12 +104,12 @@ interface Inventory{
*
* @return Item[]
*/
public function removeItem(Item ...$slots);
public function removeItem(Item ...$slots) : array;
/**
* @return Item[]
*/
public function getContents();
public function getContents() : array;
/**
* @param Item[] $items
@ -124,7 +135,7 @@ interface Inventory{
*
* @return bool
*/
public function contains(Item $item);
public function contains(Item $item) : bool;
/**
* Will return all the Items that has the same id and metadata (if not null).
@ -134,7 +145,7 @@ interface Inventory{
*
* @return Item[]
*/
public function all(Item $item);
public function all(Item $item) : array;
/**
* Will return the first slot has the same id and metadata (if not null) as the Item.
@ -144,14 +155,14 @@ interface Inventory{
*
* @return int
*/
public function first(Item $item);
public function first(Item $item) : int;
/**
* Returns the first empty slot, or -1 if not found
*
* @return int
*/
public function firstEmpty();
public function firstEmpty() : int;
/**
* Will remove all the Items that has the same id and metadata (if not null)
@ -167,7 +178,7 @@ interface Inventory{
*
* @return bool
*/
public function clear($index);
public function clear(int $index) : bool;
/**
* Clears all the slots
@ -180,12 +191,12 @@ interface Inventory{
*
* @return Player[]
*/
public function getViewers();
public function getViewers() : array;
/**
* @return InventoryType
*/
public function getType();
public function getType() : InventoryType;
/**
* @return InventoryHolder
@ -204,7 +215,7 @@ interface Inventory{
*
* @return bool
*/
public function open(Player $who);
public function open(Player $who) : bool;
public function close(Player $who);

View File

@ -91,21 +91,21 @@ class InventoryType{
/**
* @return int
*/
public function getDefaultSize(){
public function getDefaultSize() : int{
return $this->size;
}
/**
* @return string
*/
public function getDefaultTitle(){
public function getDefaultTitle() : string{
return $this->title;
}
/**
* @return int
*/
public function getNetworkType(){
public function getNetworkType() : int{
return $this->typeId;
}
}

View File

@ -46,11 +46,11 @@ class PlayerInventory extends BaseInventory{
parent::__construct($player, InventoryType::get(InventoryType::PLAYER));
}
public function getSize(){
public function getSize() : int{
return parent::getSize() - 4; //Remove armor slots
}
public function setSize($size){
public function setSize(int $size){
parent::setSize($size + 4);
$this->sendContents($this->getViewers());
}
@ -315,7 +315,7 @@ class PlayerInventory extends BaseInventory{
return $this->setItem($this->getSize() + 3, $boots);
}
public function setItem($index, Item $item){
public function setItem(int $index, Item $item) : bool{
if($index < 0 or $index >= $this->size){
return false;
}elseif($item->getId() === 0 or $item->getCount() <= 0){
@ -346,7 +346,7 @@ class PlayerInventory extends BaseInventory{
return true;
}
public function clear($index){
public function clear(int $index) : bool{
if(isset($this->slots[$index])){
$item = Item::get(Item::AIR, 0, 0);
$old = $this->slots[$index];

View File

@ -155,7 +155,7 @@ class ShapedRecipe implements Recipe{
*
* @return Item
*/
public function getIngredient(int $x, int $y){
public function getIngredient(int $x, int $y) : Item{
return $this->ingredients[$y][$x] ?? Item::get(Item::AIR);
}

View File

@ -70,7 +70,7 @@ class ShapelessRecipe implements Recipe{
*
* @throws \InvalidArgumentException
*/
public function addIngredient(Item $item){
public function addIngredient(Item $item) : ShapelessRecipe{
if(count($this->ingredients) >= 9){
throw new \InvalidArgumentException("Shapeless recipes cannot have more than 9 ingredients");
}

View File

@ -54,19 +54,25 @@ class SimpleTransactionGroup implements TransactionGroup{
/**
* @return Player
*/
public function getSource(){
public function getSource() : Player{
return $this->source;
}
public function getCreationTime(){
public function getCreationTime() : float{
return $this->creationTime;
}
public function getInventories(){
/**
* @return Inventory[]
*/
public function getInventories() : array{
return $this->inventories;
}
public function getTransactions(){
/**
* @return Transaction[]
*/
public function getTransactions() : array{
return $this->transactions;
}
@ -93,7 +99,7 @@ class SimpleTransactionGroup implements TransactionGroup{
*
* @return bool
*/
protected function matchItems(array &$needItems, array &$haveItems){
protected function matchItems(array &$needItems, array &$haveItems) : bool{
foreach($this->transactions as $key => $ts){
if($ts->getTargetItem()->getId() !== Item::AIR){
$needItems[] = $ts->getTargetItem();
@ -128,7 +134,7 @@ class SimpleTransactionGroup implements TransactionGroup{
return true;
}
public function canExecute(){
public function canExecute() : bool{
$haveItems = [];
$needItems = [];
@ -149,7 +155,10 @@ class SimpleTransactionGroup implements TransactionGroup{
return false;
}
public function execute(){
/**
* @return bool
*/
public function execute() : bool{
if($this->hasExecuted() or !$this->canExecute()){
return false;
}
@ -175,7 +184,7 @@ class SimpleTransactionGroup implements TransactionGroup{
return true;
}
public function hasExecuted(){
public function hasExecuted() : bool{
return $this->hasExecuted;
}
}

View File

@ -30,25 +30,25 @@ interface Transaction{
/**
* @return Inventory
*/
public function getInventory();
public function getInventory() : Inventory;
/**
* @return int
*/
public function getSlot();
public function getSlot() : int;
/**
* @return Item
*/
public function getSourceItem();
public function getSourceItem() : Item;
/**
* @return Item
*/
public function getTargetItem();
public function getTargetItem() : Item;
/**
* @return float
*/
public function getCreationTime();
public function getCreationTime() : float;
}

View File

@ -28,17 +28,17 @@ interface TransactionGroup{
/**
* @return float
*/
public function getCreationTime();
public function getCreationTime() : float;
/**
* @return Transaction[]
*/
public function getTransactions();
public function getTransactions() : array;
/**
* @return Inventory[]
*/
public function getInventories();
public function getInventories() : array;
/**
* @param Transaction $transaction
@ -48,16 +48,16 @@ interface TransactionGroup{
/**
* @return bool
*/
public function canExecute();
public function canExecute() : bool;
/**
* @return bool
*/
public function execute();
public function execute() : bool;
/**
* @return bool
*/
public function hasExecuted();
public function hasExecuted() : bool;
}