mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-11 08:19:45 +00:00
crafting: avoid unnecessary recipe mutability
This commit is contained in:
parent
556beacdbf
commit
47b120fa0e
@ -42,13 +42,6 @@ class FurnaceRecipe{
|
||||
$this->ingredient = clone $ingredient;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
*/
|
||||
public function setInput(Item $item) : void{
|
||||
$this->ingredient = clone $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item
|
||||
*/
|
||||
|
@ -88,7 +88,11 @@ class ShapedRecipe implements CraftingRecipe{
|
||||
$this->shape = $shape;
|
||||
|
||||
foreach($ingredients as $char => $i){
|
||||
$this->setIngredient($char, $i);
|
||||
if(strpos(implode($this->shape), $char) === false){
|
||||
throw new \InvalidArgumentException("Symbol '$char' does not appear in the recipe shape");
|
||||
}
|
||||
|
||||
$this->ingredientList[$char] = clone $i;
|
||||
}
|
||||
|
||||
$this->results = array_map(function(Item $item) : Item{ return clone $item; }, $results);
|
||||
@ -118,23 +122,6 @@ class ShapedRecipe implements CraftingRecipe{
|
||||
return $this->getResults();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param Item $item
|
||||
*
|
||||
* @return $this
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function setIngredient(string $key, Item $item){
|
||||
if(strpos(implode($this->shape), $key) === false){
|
||||
throw new \InvalidArgumentException("Symbol '$key' does not appear in the recipe shape");
|
||||
}
|
||||
|
||||
$this->ingredientList[$key] = clone $item;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item[][]
|
||||
*/
|
||||
|
@ -40,7 +40,13 @@ class ShapelessRecipe implements CraftingRecipe{
|
||||
public function __construct(array $ingredients, array $results){
|
||||
foreach($ingredients as $item){
|
||||
//Ensure they get split up properly
|
||||
$this->addIngredient($item);
|
||||
if(count($this->ingredients) + $item->getCount() > 9){
|
||||
throw new \InvalidArgumentException("Shapeless recipes cannot have more than 9 ingredients");
|
||||
}
|
||||
|
||||
while($item->getCount() > 0){
|
||||
$this->ingredients[] = $item->pop();
|
||||
}
|
||||
}
|
||||
|
||||
$this->results = array_map(function(Item $item) : Item{ return clone $item; }, $results);
|
||||
@ -54,44 +60,6 @@ class ShapelessRecipe implements CraftingRecipe{
|
||||
return $this->getResults();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
*
|
||||
* @return ShapelessRecipe
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function addIngredient(Item $item) : ShapelessRecipe{
|
||||
if(count($this->ingredients) + $item->getCount() > 9){
|
||||
throw new \InvalidArgumentException("Shapeless recipes cannot have more than 9 ingredients");
|
||||
}
|
||||
|
||||
while($item->getCount() > 0){
|
||||
$this->ingredients[] = $item->pop();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function removeIngredient(Item $item){
|
||||
foreach($this->ingredients as $index => $ingredient){
|
||||
if($item->getCount() <= 0){
|
||||
break;
|
||||
}
|
||||
if($ingredient->equals($item, !$item->hasAnyDamageValue(), $item->hasNamedTag())){
|
||||
unset($this->ingredients[$index]);
|
||||
$item->pop();
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item[]
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user