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
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
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)";
}
}

View File

@ -0,0 +1,102 @@
<?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\data\bedrock;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\SingletonTrait;
use pocketmine\utils\Utils;
use Symfony\Component\Filesystem\Path;
use const pocketmine\BEDROCK_DATA_PATH;
/**
* Tracks Minecraft Bedrock item tags, and the item IDs which belong to them
*
* @internal
*/
final class ItemTagToIdMap{
use SingletonTrait;
private static function make() : self{
$map = json_decode(ErrorToExceptionHandler::trapAndRemoveFalse(fn() => file_get_contents(Path::join(BEDROCK_DATA_PATH, 'item_tags.json'))), true, flags: JSON_THROW_ON_ERROR);
if(!is_array($map)){
throw new AssumptionFailedError("Invalid item tag map, expected array");
}
$cleanMap = [];
foreach($map as $tagName => $ids){
if(!is_string($tagName)){
throw new AssumptionFailedError("Invalid item tag name $tagName, expected string as key");
}
if(!is_array($ids)){
throw new AssumptionFailedError("Invalid item tag $tagName, expected array of IDs as value");
}
$cleanIds = [];
foreach($ids as $id){
if(!is_string($id)){
throw new AssumptionFailedError("Invalid item tag $tagName, expected string as ID, got " . gettype($id));
}
$cleanIds[] = $id;
}
$cleanMap[$tagName] = $cleanIds;
}
return new self($cleanMap);
}
/**
* @var true[][]
* @phpstan-var array<string, array<string, true>>
*/
private array $tagToIdsMap = [];
/**
* @param string[][] $tagToIds
* @phpstan-param array<string, list<string>> $tagToIds
*/
public function __construct(
array $tagToIds
){
foreach(Utils::stringifyKeys($tagToIds) as $tag => $ids){
foreach($ids as $id){
$this->tagToIdsMap[$tag][$id] = true;
}
}
}
/**
* @return string[]
* @phpstan-return list<string>
*/
public function getIdsForTag(string $tag) : array{
return array_keys($this->tagToIdsMap[$tag] ?? []);
}
public function tagContainsId(string $tag, string $id) : bool{
return isset($this->tagToIdsMap[$tag][$id]);
}
public function addIdToTag(string $tag, string $id) : void{
$this->tagToIdsMap[$tag][$id] = true;
}
}

View File

@ -27,6 +27,7 @@ use pocketmine\block\VanillaBlocks;
use pocketmine\crafting\ExactRecipeIngredient;
use pocketmine\crafting\MetaWildcardRecipeIngredient;
use pocketmine\crafting\RecipeIngredient;
use pocketmine\crafting\TagWildcardRecipeIngredient;
use pocketmine\data\bedrock\item\BlockItemIdMap;
use pocketmine\inventory\transaction\action\CreateItemAction;
use pocketmine\inventory\transaction\action\DestroyItemAction;
@ -46,6 +47,7 @@ use pocketmine\network\mcpe\protocol\types\inventory\UIInventorySlotOffset;
use pocketmine\network\mcpe\protocol\types\recipe\IntIdMetaItemDescriptor;
use pocketmine\network\mcpe\protocol\types\recipe\RecipeIngredient as ProtocolRecipeIngredient;
use pocketmine\network\mcpe\protocol\types\recipe\StringIdMetaItemDescriptor;
use pocketmine\network\mcpe\protocol\types\recipe\TagItemDescriptor;
use pocketmine\player\GameMode;
use pocketmine\player\Player;
use pocketmine\utils\AssumptionFailedError;
@ -118,6 +120,7 @@ class TypeConverter{
if($ingredient instanceof MetaWildcardRecipeIngredient){
$id = GlobalItemTypeDictionary::getInstance()->getDictionary()->fromStringId($ingredient->getItemId());
$meta = self::RECIPE_INPUT_WILDCARD_META;
$descriptor = new IntIdMetaItemDescriptor($id, $meta);
}elseif($ingredient instanceof ExactRecipeIngredient){
$item = $ingredient->getItem();
[$id, $meta, $blockRuntimeId] = ItemTranslator::getInstance()->toNetworkId($item);
@ -127,11 +130,14 @@ class TypeConverter{
throw new AssumptionFailedError("Every block state should have an associated meta value");
}
}
$descriptor = new IntIdMetaItemDescriptor($id, $meta);
}elseif($ingredient instanceof TagWildcardRecipeIngredient){
$descriptor = new TagItemDescriptor($ingredient->getTagName());
}else{
throw new \LogicException("Unsupported recipe ingredient type " . get_class($ingredient) . ", only " . ExactRecipeIngredient::class . " and " . MetaWildcardRecipeIngredient::class . " are supported");
}
return new ProtocolRecipeIngredient(new IntIdMetaItemDescriptor($id, $meta), 1);
return new ProtocolRecipeIngredient($descriptor, 1);
}
public function netRecipeIngredientToCore(ProtocolRecipeIngredient $ingredient) : ?RecipeIngredient{
@ -140,6 +146,10 @@ class TypeConverter{
return null;
}
if($descriptor instanceof TagItemDescriptor){
return new TagWildcardRecipeIngredient($descriptor->getTag());
}
if($descriptor instanceof IntIdMetaItemDescriptor){
$stringId = GlobalItemTypeDictionary::getInstance()->getDictionary()->fromIntId($descriptor->getId());
$meta = $descriptor->getMeta();