CraftingRecipe: added methods to allow recipes to derive outputs from inputs

this will be needed for special recipes like shulker-box and banner recipes.
This commit is contained in:
Dylan K. Taylor 2018-03-28 19:54:52 +01:00
parent 8572e9e560
commit a1090623a2
5 changed files with 24 additions and 8 deletions

View File

@ -220,7 +220,10 @@ class CraftingManager{
* @param ShapedRecipe $recipe
*/
public function registerShapedRecipe(ShapedRecipe $recipe) : void{
$recipe->setId($uuid = UUID::fromData((string) ++self::$RECIPE_COUNT, json_encode(self::pack($recipe->getResults()))));
$this->recipes[$uuid->toBinary()] = $recipe;
$this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
$this->craftingDataCache = null;
}
@ -228,7 +231,10 @@ class CraftingManager{
* @param ShapelessRecipe $recipe
*/
public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
$recipe->setId($uuid = UUID::fromData((string) ++self::$RECIPE_COUNT, json_encode(self::pack($recipe->getResults()))));
$this->recipes[$uuid->toBinary()] = $recipe;
$this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
$this->craftingDataCache = null;
}
@ -284,11 +290,6 @@ class CraftingManager{
* @param Recipe $recipe
*/
public function registerRecipe(Recipe $recipe) : void{
if($recipe instanceof CraftingRecipe){
$recipe->setId($uuid = UUID::fromData((string) ++self::$RECIPE_COUNT, json_encode(self::pack($recipe->getResults()))));
$this->recipes[$uuid->toBinary()] = $recipe;
}
$recipe->registerToCraftingManager($this);
}

View File

@ -46,11 +46,13 @@ interface CraftingRecipe extends Recipe{
public function getIngredientList() : array;
/**
* Returns a list of items created by crafting this recipe.
* Returns a list of results this recipe will produce when the inputs in the given crafting grid are consumed.
*
* @param CraftingGrid $grid
*
* @return Item[]
*/
public function getResults() : array;
public function getResultsFor(CraftingGrid $grid) : array;
/**
* Returns whether the given crafting grid meets the requirements to craft this recipe.

View File

@ -107,6 +107,15 @@ class ShapedRecipe implements CraftingRecipe{
return array_map(function(Item $item) : Item{ return clone $item; }, $this->results);
}
/**
* @param CraftingGrid $grid
*
* @return Item[]
*/
public function getResultsFor(CraftingGrid $grid) : array{
return $this->getResults();
}
/**
* @return UUID|null
*/

View File

@ -70,6 +70,10 @@ class ShapelessRecipe implements CraftingRecipe{
return array_map(function(Item $item) : Item{ return clone $item; }, $this->results);
}
public function getResultsFor(CraftingGrid $grid) : array{
return $this->getResults();
}
/**
* @param Item $item
*

View File

@ -121,7 +121,7 @@ class CraftingTransaction extends InventoryTransaction{
}
try{
$this->repetitions = $this->matchRecipeItems($this->outputs, $this->recipe->getResults(), false);
$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){
throw new \InvalidStateException("Tried to craft recipe $this->repetitions times in batch, but have enough inputs for $inputIterations");