PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
ItemTranslator.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\convert;
25
36
40final class ItemTranslator{
41 public const NO_BLOCK_RUNTIME_ID = 0; //this is technically a valid block runtime ID, but is used to represent "no block" (derp mojang)
42
43 public function __construct(
44 private ItemTypeDictionary $itemTypeDictionary,
45 private BlockStateDictionary $blockStateDictionary,
46 private ItemSerializer $itemSerializer,
47 private ItemDeserializer $itemDeserializer,
48 private BlockItemIdMap $blockItemIdMap
49 ){}
50
55 public function toNetworkIdQuiet(Item $item) : ?array{
56 try{
57 return $this->toNetworkId($item);
59 return null;
60 }
61 }
62
69 public function toNetworkId(Item $item) : array{
70 //TODO: we should probably come up with a cache for this
71
72 $itemData = $this->itemSerializer->serializeType($item);
73
74 $numericId = $this->itemTypeDictionary->fromStringId($itemData->getName());
75 $blockStateData = $itemData->getBlock();
76
77 if($blockStateData !== null){
78 $blockRuntimeId = $this->blockStateDictionary->lookupStateIdFromData($blockStateData);
79 if($blockRuntimeId === null){
80 throw new AssumptionFailedError("Unmapped blockstate returned by blockstate serializer: " . $blockStateData->toNbt());
81 }
82 }else{
83 $blockRuntimeId = null;
84 }
85
86 return [$numericId, $itemData->getMeta(), $blockRuntimeId];
87 }
88
92 public function toNetworkNbt(Item $item) : CompoundTag{
93 //TODO: this relies on the assumption that network item NBT is the same as disk item NBT, which may not always
94 //be true - if we stick on an older world version while updating network version, this could be a problem (and
95 //may be a problem for multi version implementations)
96 return $this->itemSerializer->serializeStack($item)->toNbt();
97 }
98
102 public function fromNetworkId(int $networkId, int $networkMeta, int $networkBlockRuntimeId) : Item{
103 try{
104 $stringId = $this->itemTypeDictionary->fromIntId($networkId);
105 }catch(\InvalidArgumentException $e){
106 //TODO: a quiet version of fromIntId() would be better than catching InvalidArgumentException
107 throw TypeConversionException::wrap($e, "Invalid network itemstack ID $networkId");
108 }
109
110 $blockStateData = null;
111 if($this->blockItemIdMap->lookupBlockId($stringId) !== null){
112 $blockStateData = $this->blockStateDictionary->generateDataFromStateId($networkBlockRuntimeId);
113 if($blockStateData === null){
114 throw new TypeConversionException("Blockstate runtimeID $networkBlockRuntimeId does not correspond to any known blockstate");
115 }
116 }elseif($networkBlockRuntimeId !== self::NO_BLOCK_RUNTIME_ID){
117 throw new TypeConversionException("Item $stringId is not a blockitem, but runtime ID $networkBlockRuntimeId was provided");
118 }
119
120 try{
121 return $this->itemDeserializer->deserializeType(new SavedItemData($stringId, $networkMeta, $blockStateData));
122 }catch(ItemTypeDeserializeException $e){
123 throw TypeConversionException::wrap($e, "Invalid network itemstack data");
124 }
125 }
126}
fromNetworkId(int $networkId, int $networkMeta, int $networkBlockRuntimeId)