CraftingTransaction: Start from pre-computed iteration count for input matching

This will be faster to bail out on failures.
This commit is contained in:
Dylan K. Taylor 2018-04-04 12:15:46 +01:00
parent ef2dd1de92
commit 033b44df5a

View File

@ -44,11 +44,11 @@ class CraftingTransaction extends InventoryTransaction{
* @param Item[] $txItems
* @param Item[] $recipeItems
* @param bool $wildcards
* @param int $iterations
*
* @return int
* @throws TransactionValidationException
*/
protected function matchRecipeItems(array $txItems, array $recipeItems, bool $wildcards) : int{
protected function matchRecipeItems(array $txItems, array $recipeItems, bool $wildcards, int $iterations = 0) : int{
if(empty($recipeItems)){
throw new TransactionValidationException("No recipe items given");
}
@ -56,7 +56,6 @@ class CraftingTransaction extends InventoryTransaction{
throw new TransactionValidationException("No transaction items given");
}
$iterations = 0;
while(!empty($recipeItems)){
/** @var Item $recipeItem */
$recipeItem = array_pop($recipeItems);
@ -120,7 +119,7 @@ class CraftingTransaction extends InventoryTransaction{
try{
$this->repetitions = $this->matchRecipeItems($this->outputs, $this->recipe->getResultsFor($this->source->getCraftingGrid()), false);
if(($inputIterations = $this->matchRecipeItems($this->inputs, $this->recipe->getIngredientList(), true)) !== $this->repetitions){
if(($inputIterations = $this->matchRecipeItems($this->inputs, $this->recipe->getIngredientList(), true, $this->repetitions)) !== $this->repetitions){
throw new TransactionValidationException("Tried to craft recipe $this->repetitions times in batch, but have enough inputs for $inputIterations");
}
}catch(\InvalidStateException $e){