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

@ -199,4 +199,63 @@ class ShapedRecipe implements CraftingRecipe{
public function requiresCraftingTable() : bool{
return $this->getHeight() > 2 or $this->getWidth() > 2;
}
/**
* @param Item[][] $input
* @param Item[][] $output
*
* @return bool
*/
public function matchItems(array $input, array $output) : bool{
$map = $this->getIngredientMap();
//match the given items to the requested items
for($y = 0, $y2 = $this->getHeight(); $y < $y2; ++$y){
for($x = 0, $x2 = $this->getWidth(); $x < $x2; ++$x){
$given = $input[$y][$x] ?? null;
$required = $map[$y][$x];
if($given === null or !$required->equals($given, !$required->hasAnyDamageValue(), $required->hasCompoundTag()) or $required->getCount() !== $given->getCount()){
return false;
}
unset($input[$y][$x]);
}
}
//we shouldn't need to check if there's anything left in the map, the last block should take care of that
//however, we DO need to check if there are too many items in the grid outside of the recipe
/**
* @var Item[] $row
*/
foreach($input as $y => $row){
foreach($row as $x => $needItem){
if(!$needItem->isNull()){
return false; //too many input ingredients
}
}
}
//and then, finally, check that the output items are good:
/** @var Item[] $haveItems */
$haveItems = array_merge(...$output);
$needItems = $this->getExtraResults();
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;
}
}