mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 10:22:56 +00:00
Burn meta wildcards from Item, allow more dynamic recipe inputs
this was an obstacle for getting rid of legacy item IDs.
This commit is contained in:
@ -25,12 +25,18 @@ namespace pocketmine\inventory\transaction;
|
||||
|
||||
use pocketmine\crafting\CraftingManager;
|
||||
use pocketmine\crafting\CraftingRecipe;
|
||||
use pocketmine\crafting\RecipeIngredient;
|
||||
use pocketmine\event\inventory\CraftItemEvent;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\player\Player;
|
||||
use pocketmine\utils\Utils;
|
||||
use function array_fill_keys;
|
||||
use function array_keys;
|
||||
use function array_pop;
|
||||
use function count;
|
||||
use function intdiv;
|
||||
use function min;
|
||||
use function uasort;
|
||||
|
||||
/**
|
||||
* This transaction type is specialized for crafting validation. It shares most of the same semantics of the base
|
||||
@ -63,13 +69,110 @@ class CraftingTransaction extends InventoryTransaction{
|
||||
$this->craftingManager = $craftingManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item[] $providedItems
|
||||
* @return Item[]
|
||||
*/
|
||||
private static function packItems(array $providedItems) : array{
|
||||
$packedProvidedItems = [];
|
||||
while(count($providedItems) > 0){
|
||||
$item = array_pop($providedItems);
|
||||
foreach($providedItems as $k => $otherItem){
|
||||
if($item->canStackWith($otherItem)){
|
||||
$item->setCount($item->getCount() + $otherItem->getCount());
|
||||
unset($providedItems[$k]);
|
||||
}
|
||||
}
|
||||
$packedProvidedItems[] = $item;
|
||||
}
|
||||
|
||||
return $packedProvidedItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item[] $providedItems
|
||||
* @param RecipeIngredient[] $recipeIngredients
|
||||
*/
|
||||
public static function matchIngredients(array $providedItems, array $recipeIngredients, int $expectedIterations) : void{
|
||||
if(count($recipeIngredients) === 0){
|
||||
throw new TransactionValidationException("No recipe ingredients given");
|
||||
}
|
||||
if(count($providedItems) === 0){
|
||||
throw new TransactionValidationException("No transaction items given");
|
||||
}
|
||||
|
||||
$packedProvidedItems = self::packItems(Utils::cloneObjectArray($providedItems));
|
||||
$packedProvidedItemMatches = array_fill_keys(array_keys($packedProvidedItems), 0);
|
||||
|
||||
$recipeIngredientMatches = [];
|
||||
|
||||
foreach($recipeIngredients as $ingredientIndex => $recipeIngredient){
|
||||
$acceptedItems = [];
|
||||
foreach($packedProvidedItems as $itemIndex => $packedItem){
|
||||
if($recipeIngredient->accepts($packedItem)){
|
||||
$packedProvidedItemMatches[$itemIndex]++;
|
||||
$acceptedItems[$itemIndex] = $itemIndex;
|
||||
}
|
||||
}
|
||||
|
||||
if(count($acceptedItems) === 0){
|
||||
throw new TransactionValidationException("No provided items satisfy ingredient requirement $recipeIngredient");
|
||||
}
|
||||
|
||||
$recipeIngredientMatches[$ingredientIndex] = $acceptedItems;
|
||||
}
|
||||
|
||||
foreach($packedProvidedItemMatches as $itemIndex => $itemMatchCount){
|
||||
if($itemMatchCount === 0){
|
||||
$item = $packedProvidedItems[$itemIndex];
|
||||
throw new TransactionValidationException("Provided item $item is not accepted by any recipe ingredient");
|
||||
}
|
||||
}
|
||||
|
||||
//Most picky ingredients first - avoid picky ingredient getting their items stolen by wildcard ingredients
|
||||
//TODO: this is still insufficient when multiple wildcard ingredients have overlaps, but we don't (yet) have to
|
||||
//worry about those.
|
||||
uasort($recipeIngredientMatches, fn(array $a, array $b) => count($a) <=> count($b));
|
||||
|
||||
foreach($recipeIngredientMatches as $ingredientIndex => $acceptedItems){
|
||||
$needed = $expectedIterations;
|
||||
|
||||
foreach($packedProvidedItems as $itemIndex => $item){
|
||||
if(!isset($acceptedItems[$itemIndex])){
|
||||
continue;
|
||||
}
|
||||
|
||||
$taken = min($needed, $item->getCount());
|
||||
$needed -= $taken;
|
||||
$item->setCount($item->getCount() - $taken);
|
||||
|
||||
if($item->getCount() === 0){
|
||||
unset($packedProvidedItems[$itemIndex]);
|
||||
}
|
||||
|
||||
if($needed === 0){
|
||||
//validation passed!
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
|
||||
$recipeIngredient = $recipeIngredients[$ingredientIndex];
|
||||
$actualIterations = $expectedIterations - $needed;
|
||||
throw new TransactionValidationException("Not enough items to satisfy recipe ingredient $recipeIngredient for $expectedIterations (only have enough items for $actualIterations iterations)");
|
||||
}
|
||||
|
||||
if(count($packedProvidedItems) > 0){
|
||||
throw new TransactionValidationException("Not all provided items were used");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item[] $txItems
|
||||
* @param Item[] $recipeItems
|
||||
*
|
||||
* @throws TransactionValidationException
|
||||
*/
|
||||
protected function matchRecipeItems(array $txItems, array $recipeItems, bool $wildcards, int $iterations = 0) : int{
|
||||
protected function matchOutputs(array $txItems, array $recipeItems) : int{
|
||||
if(count($recipeItems) === 0){
|
||||
throw new TransactionValidationException("No recipe items given");
|
||||
}
|
||||
@ -77,6 +180,7 @@ class CraftingTransaction extends InventoryTransaction{
|
||||
throw new TransactionValidationException("No transaction items given");
|
||||
}
|
||||
|
||||
$iterations = 0;
|
||||
while(count($recipeItems) > 0){
|
||||
/** @var Item $recipeItem */
|
||||
$recipeItem = array_pop($recipeItems);
|
||||
@ -90,7 +194,7 @@ class CraftingTransaction extends InventoryTransaction{
|
||||
|
||||
$haveCount = 0;
|
||||
foreach($txItems as $j => $txItem){
|
||||
if($txItem->equals($recipeItem, !$wildcards || !$recipeItem->hasAnyDamageValue(), !$wildcards || $recipeItem->hasNamedTag())){
|
||||
if($txItem->canStackWith($recipeItem)){
|
||||
$haveCount += $txItem->getCount();
|
||||
unset($txItems[$j]);
|
||||
}
|
||||
@ -115,7 +219,7 @@ class CraftingTransaction extends InventoryTransaction{
|
||||
|
||||
if(count($txItems) > 0){
|
||||
//all items should be destroyed in this process
|
||||
throw new TransactionValidationException("Expected 0 ingredients left over, have " . count($txItems));
|
||||
throw new TransactionValidationException("Expected 0 items left over, have " . count($txItems));
|
||||
}
|
||||
|
||||
return $iterations;
|
||||
@ -133,9 +237,9 @@ class CraftingTransaction extends InventoryTransaction{
|
||||
foreach($this->craftingManager->matchRecipeByOutputs($this->outputs) as $recipe){
|
||||
try{
|
||||
//compute number of times recipe was crafted
|
||||
$this->repetitions = $this->matchRecipeItems($this->outputs, $recipe->getResultsFor($this->source->getCraftingGrid()), false);
|
||||
$this->repetitions = $this->matchOutputs($this->outputs, $recipe->getResultsFor($this->source->getCraftingGrid()));
|
||||
//assert that $repetitions x recipe ingredients should be consumed
|
||||
$this->matchRecipeItems($this->inputs, $recipe->getIngredientList(), true, $this->repetitions);
|
||||
self::matchIngredients($this->inputs, $recipe->getIngredientList(), $this->repetitions);
|
||||
|
||||
//Success!
|
||||
$this->recipe = $recipe;
|
||||
|
Reference in New Issue
Block a user