Added internal support for tag recipe ingredients

This commit is contained in:
Dylan K. Taylor
2022-12-02 14:03:58 +00:00
parent bc8def3be1
commit ca3b5c38b7
4 changed files with 171 additions and 4 deletions

View File

@ -52,14 +52,14 @@ use function json_decode;
final class CraftingManagerFromDataHelper{
private static function deserializeIngredient(RecipeIngredientData $data) : ?RecipeIngredient{
if(!isset($data->name)){
return null; //TODO: not yet implemented
}
if(isset($data->count) && $data->count !== 1){
//every case we've seen so far where this isn't the case, it's been a bug and the count was ignored anyway
//e.g. gold blocks crafted from 9 ingots, but each input item individually had a count of 9
throw new SavedDataLoadingException("Recipe inputs should have a count of exactly 1");
}
if(isset($data->tag)){
return new TagWildcardRecipeIngredient($data->tag);
}
$meta = $data->meta ?? null;
if($meta === RecipeIngredientData::WILDCARD_META_VALUE){

View File

@ -0,0 +1,55 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\crafting;
use pocketmine\data\bedrock\ItemTagToIdMap;
use pocketmine\item\Item;
use pocketmine\world\format\io\GlobalItemDataHandlers;
/**
* Recipe ingredient that matches items whose ID falls within a specific set. This is used for magic meta value
* wildcards and also for ingredients which use item tags (since tags implicitly rely on ID only).
*
* @internal
*/
final class TagWildcardRecipeIngredient implements RecipeIngredient{
public function __construct(
private string $tagName
){}
public function getTagName() : string{ return $this->tagName; }
public function accepts(Item $item) : bool{
if($item->getCount() < 1){
return false;
}
return ItemTagToIdMap::getInstance()->tagContainsId($this->tagName, GlobalItemDataHandlers::getSerializer()->serializeType($item)->getName());
}
public function __toString() : string{
return "TagWildcardRecipeIngredient($this->tagName)";
}
}