addIngredient($item); } $this->results = array_map(function(Item $item) : Item{ return clone $item; }, $results); } /** * @return Item[] */ public function getResults() : array{ return array_map(function(Item $item) : Item{ return clone $item; }, $this->results); } public function getResultsFor(CraftingGrid $grid) : array{ return $this->getResults(); } /** * @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; } /** * @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->hasCompoundTag())){ unset($this->ingredients[$index]); $item->pop(); } } return $this; } /** * @return Item[] */ public function getIngredientList() : array{ return array_map(function(Item $item) : Item{ return clone $item; }, $this->ingredients); } public function getIngredientCount() : int{ $count = 0; foreach($this->ingredients as $ingredient){ $count += $ingredient->getCount(); } return $count; } /** * @deprecated */ public function registerToCraftingManager(CraftingManager $manager) : void{ $manager->registerShapelessRecipe($this); } public function matchesCraftingGrid(CraftingGrid $grid) : bool{ //don't pack the ingredients - shapeless recipes require that each ingredient be in a separate slot $input = $grid->getContents(); foreach($this->ingredients as $needItem){ foreach($input as $j => $haveItem){ if($haveItem->equals($needItem, !$needItem->hasAnyDamageValue(), $needItem->hasCompoundTag()) and $haveItem->getCount() >= $needItem->getCount()){ unset($input[$j]); continue 2; } } return false; //failed to match the needed item to a given item } return count($input) === 0; //crafting grid should be empty apart from the given ingredient stacks } }