fix some inspections related to crafting recipes

This commit is contained in:
Dylan K. Taylor 2017-07-13 13:59:45 +01:00
parent 7f99d9019a
commit b9355387da
4 changed files with 57 additions and 21 deletions

View File

@ -29,6 +29,7 @@ use pocketmine\utils\UUID;
class FurnaceRecipe implements Recipe{ class FurnaceRecipe implements Recipe{
/** @var UUID|null */
private $id = null; private $id = null;
/** @var Item */ /** @var Item */
@ -46,10 +47,16 @@ class FurnaceRecipe implements Recipe{
$this->ingredient = clone $ingredient; $this->ingredient = clone $ingredient;
} }
/**
* @return UUID|null
*/
public function getId(){ public function getId(){
return $this->id; return $this->id;
} }
/**
* @param UUID $id
*/
public function setId(UUID $id){ public function setId(UUID $id){
if($this->id !== null){ if($this->id !== null){
throw new \InvalidStateException("Id is already set"); throw new \InvalidStateException("Id is already set");
@ -68,14 +75,14 @@ class FurnaceRecipe implements Recipe{
/** /**
* @return Item * @return Item
*/ */
public function getInput(){ public function getInput() : Item{
return clone $this->ingredient; return clone $this->ingredient;
} }
/** /**
* @return Item * @return Item
*/ */
public function getResult(){ public function getResult() : Item{
return clone $this->output; return clone $this->output;
} }

View File

@ -31,14 +31,17 @@ interface Recipe{
/** /**
* @return Item * @return Item
*/ */
public function getResult(); public function getResult() : Item;
public function registerToCraftingManager(); public function registerToCraftingManager();
/** /**
* @return UUID * @return UUID|null
*/ */
public function getId(); public function getId();
/**
* @param UUID $id
*/
public function setId(UUID $id); public function setId(UUID $id);
} }

View File

@ -32,6 +32,7 @@ class ShapedRecipe implements Recipe{
/** @var Item */ /** @var Item */
private $output; private $output;
/** @var UUID|null */
private $id = null; private $id = null;
/** @var string[] */ /** @var string[] */
@ -49,7 +50,7 @@ class ShapedRecipe implements Recipe{
* *
* @throws \Exception * @throws \Exception
*/ */
public function __construct(Item $result, $height, $width){ public function __construct(Item $result, int $height, int $width){
for($h = 0; $h < $height; $h++){ for($h = 0; $h < $height; $h++){
if($width === 0 or $width > 3){ if($width === 0 or $width > 3){
throw new \InvalidStateException("Crafting rows should be 1, 2, 3 wide, not $width"); throw new \InvalidStateException("Crafting rows should be 1, 2, 3 wide, not $width");
@ -60,18 +61,24 @@ class ShapedRecipe implements Recipe{
$this->output = clone $result; $this->output = clone $result;
} }
public function getWidth(){ public function getWidth() : int{
return count($this->ingredients[0]); return count($this->ingredients[0]);
} }
public function getHeight(){ public function getHeight() : int{
return count($this->ingredients); return count($this->ingredients);
} }
public function getResult(){ /**
* @return Item
*/
public function getResult() : Item{
return $this->output; return $this->output;
} }
/**
* @return UUID|null
*/
public function getId(){ public function getId(){
return $this->id; return $this->id;
} }
@ -84,7 +91,14 @@ class ShapedRecipe implements Recipe{
$this->id = $id; $this->id = $id;
} }
public function addIngredient($x, $y, Item $item){ /**
* @param int $x
* @param int $y
* @param Item $item
*
* @return $this
*/
public function addIngredient(int $x, int $y, Item $item){
$this->ingredients[$y][$x] = clone $item; $this->ingredients[$y][$x] = clone $item;
return $this; return $this;
} }
@ -96,7 +110,7 @@ class ShapedRecipe implements Recipe{
* @return $this * @return $this
* @throws \Exception * @throws \Exception
*/ */
public function setIngredient($key, Item $item){ public function setIngredient(string $key, Item $item){
if(!array_key_exists($key, $this->shape)){ if(!array_key_exists($key, $this->shape)){
throw new \Exception("Symbol does not appear in the shape: " . $key); throw new \Exception("Symbol does not appear in the shape: " . $key);
} }
@ -106,7 +120,11 @@ class ShapedRecipe implements Recipe{
return $this; return $this;
} }
protected function fixRecipe($key, $item){ /**
* @param string $key
* @param Item $item
*/
protected function fixRecipe(string $key, Item $item){
foreach($this->shapeItems[$key] as $entry){ foreach($this->shapeItems[$key] as $entry){
$this->ingredients[$entry->y][$entry->x] = clone $item; $this->ingredients[$entry->y][$entry->x] = clone $item;
} }
@ -115,7 +133,7 @@ class ShapedRecipe implements Recipe{
/** /**
* @return Item[][] * @return Item[][]
*/ */
public function getIngredientMap(){ public function getIngredientMap() : array{
$ingredients = []; $ingredients = [];
foreach($this->ingredients as $y => $row){ foreach($this->ingredients as $y => $row){
$ingredients[$y] = []; $ingredients[$y] = [];
@ -132,18 +150,19 @@ class ShapedRecipe implements Recipe{
} }
/** /**
* @param $x * @param int $x
* @param $y * @param int $y
* @return null|Item *
* @return Item
*/ */
public function getIngredient($x, $y){ public function getIngredient(int $x, int $y){
return $this->ingredients[$y][$x] ?? Item::get(Item::AIR); return $this->ingredients[$y][$x] ?? Item::get(Item::AIR);
} }
/** /**
* @return string[] * @return string[]
*/ */
public function getShape(){ public function getShape() : array{
return $this->shape; return $this->shape;
} }

View File

@ -31,6 +31,7 @@ class ShapelessRecipe implements Recipe{
/** @var Item */ /** @var Item */
private $output; private $output;
/** @var UUID|null */
private $id = null; private $id = null;
/** @var Item[] */ /** @var Item[] */
@ -40,10 +41,16 @@ class ShapelessRecipe implements Recipe{
$this->output = clone $result; $this->output = clone $result;
} }
/**
* @return UUID|null
*/
public function getId(){ public function getId(){
return $this->id; return $this->id;
} }
/**
* @param UUID $id
*/
public function setId(UUID $id){ public function setId(UUID $id){
if($this->id !== null){ if($this->id !== null){
throw new \InvalidStateException("Id is already set"); throw new \InvalidStateException("Id is already set");
@ -52,14 +59,14 @@ class ShapelessRecipe implements Recipe{
$this->id = $id; $this->id = $id;
} }
public function getResult(){ public function getResult() : Item{
return clone $this->output; return clone $this->output;
} }
/** /**
* @param Item $item * @param Item $item
* *
* @returns ShapelessRecipe * @return ShapelessRecipe
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
@ -101,7 +108,7 @@ class ShapelessRecipe implements Recipe{
/** /**
* @return Item[] * @return Item[]
*/ */
public function getIngredientList(){ public function getIngredientList() : array{
$ingredients = []; $ingredients = [];
foreach($this->ingredients as $ingredient){ foreach($this->ingredients as $ingredient){
$ingredients[] = clone $ingredient; $ingredients[] = clone $ingredient;
@ -113,7 +120,7 @@ class ShapelessRecipe implements Recipe{
/** /**
* @return int * @return int
*/ */
public function getIngredientCount(){ public function getIngredientCount() : int{
$count = 0; $count = 0;
foreach($this->ingredients as $ingredient){ foreach($this->ingredients as $ingredient){
$count += $ingredient->getCount(); $count += $ingredient->getCount();