Merge branch 'stable' into minor-next

This commit is contained in:
Dylan K. Taylor
2023-06-13 18:06:06 +01:00
12 changed files with 43 additions and 30 deletions

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\convert;
use pocketmine\data\bedrock\item\BlockItemIdMap;
use pocketmine\data\bedrock\item\ItemDeserializer;
use pocketmine\data\bedrock\item\ItemSerializer;
use pocketmine\data\bedrock\item\ItemTypeDeserializeException;
@ -37,18 +38,19 @@ use pocketmine\utils\AssumptionFailedError;
* This class handles translation between network item ID+metadata to PocketMine-MP internal ID+metadata and vice versa.
*/
final class ItemTranslator{
public const NO_BLOCK_RUNTIME_ID = 0;
public const NO_BLOCK_RUNTIME_ID = 0; //this is technically a valid block runtime ID, but is used to represent "no block" (derp mojang)
public function __construct(
private ItemTypeDictionary $itemTypeDictionary,
private BlockStateDictionary $blockStateDictionary,
private ItemSerializer $itemSerializer,
private ItemDeserializer $itemDeserializer
private ItemDeserializer $itemDeserializer,
private BlockItemIdMap $blockItemIdMap
){}
/**
* @return int[]|null
* @phpstan-return array{int, int, int}|null
* @phpstan-return array{int, int, ?int}|null
*/
public function toNetworkIdQuiet(Item $item) : ?array{
try{
@ -60,7 +62,7 @@ final class ItemTranslator{
/**
* @return int[]
* @phpstan-return array{int, int, int}
* @phpstan-return array{int, int, ?int}
*
* @throws ItemTypeSerializeException
*/
@ -78,7 +80,7 @@ final class ItemTranslator{
throw new AssumptionFailedError("Unmapped blockstate returned by blockstate serializer: " . $blockStateData->toNbt());
}
}else{
$blockRuntimeId = self::NO_BLOCK_RUNTIME_ID; //this is technically a valid block runtime ID, but is used to represent "no block" (derp mojang)
$blockRuntimeId = null;
}
return [$numericId, $itemData->getMeta(), $blockRuntimeId];
@ -106,11 +108,13 @@ final class ItemTranslator{
}
$blockStateData = null;
if($networkBlockRuntimeId !== self::NO_BLOCK_RUNTIME_ID){
if($this->blockItemIdMap->lookupBlockId($stringId) !== null){
$blockStateData = $this->blockStateDictionary->generateDataFromStateId($networkBlockRuntimeId);
if($blockStateData === null){
throw new TypeConversionException("Blockstate runtimeID $networkBlockRuntimeId does not correspond to any known blockstate");
}
}elseif($networkBlockRuntimeId !== self::NO_BLOCK_RUNTIME_ID){
throw new TypeConversionException("Item $stringId is not a blockitem, but runtime ID $networkBlockRuntimeId was provided");
}
try{

View File

@ -82,7 +82,8 @@ class TypeConverter{
$this->itemTypeDictionary,
$this->blockTranslator->getBlockStateDictionary(),
GlobalItemDataHandlers::getSerializer(),
GlobalItemDataHandlers::getDeserializer()
GlobalItemDataHandlers::getDeserializer(),
$this->blockItemIdMap
);
$this->skinAdapter = new LegacySkinAdapter();
@ -147,7 +148,7 @@ class TypeConverter{
}elseif($ingredient instanceof ExactRecipeIngredient){
$item = $ingredient->getItem();
[$id, $meta, $blockRuntimeId] = $this->itemTranslator->toNetworkId($item);
if($blockRuntimeId !== ItemTranslator::NO_BLOCK_RUNTIME_ID){
if($blockRuntimeId !== null){
$meta = $this->blockTranslator->getBlockStateDictionary()->getMetaFromStateId($blockRuntimeId);
if($meta === null){
throw new AssumptionFailedError("Every block state should have an associated meta value");
@ -230,7 +231,7 @@ class TypeConverter{
$id,
$meta,
$itemStack->getCount(),
$blockRuntimeId,
$blockRuntimeId ?? ItemTranslator::NO_BLOCK_RUNTIME_ID,
$nbt,
[],
[],