Crafting: nuke

This commit brings in a much-needed rewrite of crafting transaction handling.

The following classes have been removed:
- CraftingTransferMaterialAction
- CraftingTakeResultAction

The following classes have significant changes:
- CraftingTransaction
	- All API methods have been removed and are now handled in CraftItemEvent
- CraftItemEvent
	- added the following:
		- getInputs()
		- getOutputs()
		- getRepetitions() (tells how many times a recipe was crafted in this event)
- Recipe interface:
	- Removed getResult() (individual recipes may handle this differently)
- CraftingRecipe interface
	- removed the following:
		- matchItems()
		- getExtraResults()
		- getAllResults()
	- added the following
		- getResults()
		- getIngredientList() : Item[], which must return a 1D array of items that should be consumed (wildcards accepted).
		- matchesCraftingGrid(CraftingGrid)
- ShapedRecipe
	- constructor now accepts string[], Item[], Item[]
- ShapelessRecipe
	- constructor now accepts Item[], Item[]
This commit is contained in:
Dylan K. Taylor
2018-03-26 13:23:28 +01:00
parent bc836aaec1
commit 8572e9e560
14 changed files with 437 additions and 521 deletions

View File

@ -27,17 +27,25 @@ use pocketmine\item\Item;
use pocketmine\utils\UUID;
class ShapelessRecipe implements CraftingRecipe{
/** @var Item */
private $output;
/** @var UUID|null */
private $id = null;
/** @var Item[] */
private $ingredients = [];
/** @var Item[] */
private $results;
public function __construct(Item $result){
$this->output = clone $result;
/**
* @param Item[] $ingredients No more than 9 total. This applies to sum of item stack counts, not count of array.
* @param Item[] $results List of result items created by this recipe.
*/
public function __construct(array $ingredients, array $results){
foreach($ingredients as $item){
//Ensure they get split up properly
$this->addIngredient($item);
}
$this->results = array_map(function(Item $item) : Item{ return clone $item; }, $results);
}
/**
@ -58,16 +66,8 @@ class ShapelessRecipe implements CraftingRecipe{
$this->id = $id;
}
public function getResult() : Item{
return clone $this->output;
}
public function getExtraResults() : array{
return []; //TODO
}
public function getAllResults() : array{
return [$this->getResult()]; //TODO
public function getResults() : array{
return array_map(function(Item $item) : Item{ return clone $item; }, $this->results);
}
/**
@ -78,7 +78,7 @@ class ShapelessRecipe implements CraftingRecipe{
* @throws \InvalidArgumentException
*/
public function addIngredient(Item $item) : ShapelessRecipe{
if(count($this->ingredients) >= 9){
if(count($this->ingredients) + $item->getCount() > 9){
throw new \InvalidArgumentException("Shapeless recipes cannot have more than 9 ingredients");
}
@ -112,12 +112,7 @@ class ShapelessRecipe implements CraftingRecipe{
* @return Item[]
*/
public function getIngredientList() : array{
$ingredients = [];
foreach($this->ingredients as $ingredient){
$ingredients[] = clone $ingredient;
}
return $ingredients;
return array_map(function(Item $item) : Item{ return clone $item; }, $this->ingredients);
}
/**
@ -137,53 +132,25 @@ class ShapelessRecipe implements CraftingRecipe{
}
/**
* @param Item[][] $input
* @param Item[][] $output
* @param CraftingGrid $grid
*
* @return bool
*/
public function matchItems(array $input, array $output) : bool{
/** @var Item[] $haveInputs */
$haveInputs = array_merge(...$input); //we don't care how the items were arranged
$needInputs = $this->getIngredientList();
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();
if(!$this->matchItemList($haveInputs, $needInputs)){
return false;
}
/** @var Item[] $haveOutputs */
$haveOutputs = array_merge(...$output);
$needOutputs = $this->getExtraResults();
if(!$this->matchItemList($haveOutputs, $needOutputs)){
return false;
}
return true;
}
/**
* @param Item[] $haveItems
* @param Item[] $needItems
*
* @return bool
*/
private function matchItemList(array $haveItems, array $needItems) : bool{
foreach($haveItems as $j => $haveItem){
if($haveItem->isNull()){
unset($haveItems[$j]);
continue;
}
foreach($needItems as $i => $needItem){
if($needItem->equals($haveItem, !$needItem->hasAnyDamageValue(), $needItem->hasCompoundTag()) and $needItem->getCount() === $haveItem->getCount()){
unset($haveItems[$j], $needItems[$i]);
break;
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($haveItems) === 0 and count($needItems) === 0;
return empty($input); //crafting grid should be empty apart from the given ingredient stacks
}
}