Rewritten crafting, fixed #45

This commit is contained in:
Dylan K. Taylor
2017-09-24 14:14:24 +01:00
parent 043ae487de
commit 240cc3043a
14 changed files with 611 additions and 279 deletions

View File

@ -143,4 +143,55 @@ class ShapelessRecipe implements CraftingRecipe{
public function requiresCraftingTable() : bool{
return count($this->ingredients) > 4;
}
/**
* @param Item[][] $input
* @param Item[][] $output
*
* @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();
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;
}
}
}
return count($haveItems) === 0 and count($needItems) === 0;
}
}