craftingManager = $craftingManager; } /** * @param Item[] $txItems * @param Item[] $recipeItems * * @throws TransactionValidationException */ protected function matchRecipeItems(array $txItems, array $recipeItems, bool $wildcards, int $iterations = 0) : int{ if(count($recipeItems) === 0){ throw new TransactionValidationException("No recipe items given"); } if(count($txItems) === 0){ throw new TransactionValidationException("No transaction items given"); } while(count($recipeItems) > 0){ /** @var Item $recipeItem */ $recipeItem = array_pop($recipeItems); $needCount = $recipeItem->getCount(); foreach($recipeItems as $i => $otherRecipeItem){ if($otherRecipeItem->canStackWith($recipeItem)){ //make sure they have the same wildcards set $needCount += $otherRecipeItem->getCount(); unset($recipeItems[$i]); } } $haveCount = 0; foreach($txItems as $j => $txItem){ if($txItem->equals($recipeItem, !$wildcards || !$recipeItem->hasAnyDamageValue(), !$wildcards || $recipeItem->hasNamedTag())){ $haveCount += $txItem->getCount(); unset($txItems[$j]); } } if($haveCount % $needCount !== 0){ //wrong count for this output, should divide exactly throw new TransactionValidationException("Expected an exact multiple of required $recipeItem (given: $haveCount, needed: $needCount)"); } $multiplier = intdiv($haveCount, $needCount); if($multiplier < 1){ throw new TransactionValidationException("Expected more than zero items matching $recipeItem (given: $haveCount, needed: $needCount)"); } if($iterations === 0){ $iterations = $multiplier; }elseif($multiplier !== $iterations){ //wrong count for this output, should match previous outputs throw new TransactionValidationException("Expected $recipeItem x$iterations, but found x$multiplier"); } } if(count($txItems) > 0){ //all items should be destroyed in this process throw new TransactionValidationException("Expected 0 ingredients left over, have " . count($txItems)); } return $iterations; } public function validate() : void{ $this->squashDuplicateSlotChanges(); if(count($this->actions) < 1){ throw new TransactionValidationException("Transaction must have at least one action to be executable"); } $this->matchItems($this->outputs, $this->inputs); $failed = 0; foreach($this->craftingManager->matchRecipeByOutputs($this->outputs) as $recipe){ try{ //compute number of times recipe was crafted $this->repetitions = $this->matchRecipeItems($this->outputs, $recipe->getResultsFor($this->source->getCraftingGrid()), false); //assert that $repetitions x recipe ingredients should be consumed $this->matchRecipeItems($this->inputs, $recipe->getIngredientList(), true, $this->repetitions); //Success! $this->recipe = $recipe; break; }catch(TransactionValidationException $e){ //failed ++$failed; } } if($this->recipe === null){ throw new TransactionValidationException("Unable to match a recipe to transaction (tried to match against $failed recipes)"); } } protected function callExecuteEvent() : bool{ $ev = new CraftItemEvent($this, $this->recipe, $this->repetitions, $this->inputs, $this->outputs); $ev->call(); return !$ev->isCancelled(); } }