mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 18:32:55 +00:00
Rewritten crafting, fixed #45
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user