Merge branch 'item-stack-request' into item-stack-request-pm5

This commit is contained in:
Dylan K. Taylor
2023-03-14 22:34:47 +00:00
14 changed files with 964 additions and 264 deletions

View File

@ -343,7 +343,7 @@ abstract class BaseInventory implements Inventory{
if($invManager === null){
continue;
}
$invManager->syncSlot($this, $index);
$invManager->onSlotChange($this, $index);
}
}

View File

@ -64,9 +64,11 @@ class CraftingTransaction extends InventoryTransaction{
private CraftingManager $craftingManager;
public function __construct(Player $source, CraftingManager $craftingManager, array $actions = []){
public function __construct(Player $source, CraftingManager $craftingManager, array $actions = [], ?CraftingRecipe $recipe = null, ?int $repetitions = null){
parent::__construct($source, $actions);
$this->craftingManager = $craftingManager;
$this->recipe = $recipe;
$this->repetitions = $repetitions;
}
/**
@ -225,6 +227,18 @@ class CraftingTransaction extends InventoryTransaction{
return $iterations;
}
private function validateRecipe(CraftingRecipe $recipe, ?int $expectedRepetitions) : int{
//compute number of times recipe was crafted
$repetitions = $this->matchOutputs($this->outputs, $recipe->getResultsFor($this->source->getCraftingGrid()));
if($expectedRepetitions !== null && $repetitions !== $expectedRepetitions){
throw new TransactionValidationException("Expected $expectedRepetitions repetitions, got $repetitions");
}
//assert that $repetitions x recipe ingredients should be consumed
self::matchIngredients($this->inputs, $recipe->getIngredientList(), $repetitions);
return $repetitions;
}
public function validate() : void{
$this->squashDuplicateSlotChanges();
if(count($this->actions) < 1){
@ -233,25 +247,29 @@ class CraftingTransaction extends InventoryTransaction{
$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->matchOutputs($this->outputs, $recipe->getResultsFor($this->source->getCraftingGrid()));
//assert that $repetitions x recipe ingredients should be consumed
self::matchIngredients($this->inputs, $recipe->getIngredientList(), $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)");
$failed = 0;
foreach($this->craftingManager->matchRecipeByOutputs($this->outputs) as $recipe){
try{
//compute number of times recipe was crafted
$this->repetitions = $this->matchOutputs($this->outputs, $recipe->getResultsFor($this->source->getCraftingGrid()));
//assert that $repetitions x recipe ingredients should be consumed
self::matchIngredients($this->inputs, $recipe->getIngredientList(), $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)");
}
}else{
$this->repetitions = $this->validateRecipe($this->recipe, $this->repetitions);
}
}