$id){ if(!is_string($id)) throw new AssumptionFailedError("Invalid mappings format, expected string values"); $result->addMapping((string) $name, $id); } return $result; } /** * @var string[] * @phpstan-var array */ private array $map = []; public function __construct( private ItemDataUpgrader $itemDataUpgrader, private ItemDeserializer $itemDeserializer ){} public function addMapping(string $alias, string $id) : void{ $this->map[$alias] = $id; } /** * @return string[] * @phpstan-return array */ public function getMappings() : array{ return $this->map; } /** * Tries to parse the specified string into Item types. * * Example accepted formats: * - `diamond_pickaxe:5` * - `minecraft:string` * - `351:4 (lapis lazuli ID:meta)` * * @throws LegacyStringToItemParserException if the given string cannot be parsed as an item identifier */ public function parse(string $input) : Item{ $key = $this->reprocess($input); $b = explode(":", $key); if(!isset($b[1])){ $meta = 0; }elseif(is_numeric($b[1])){ $meta = (int) $b[1]; }else{ throw new LegacyStringToItemParserException("Unable to parse \"" . $b[1] . "\" from \"" . $input . "\" as a valid meta value"); } $lower = strtolower($b[0]); if($lower === "0" || $lower === "air"){ //item deserializer doesn't recognize air items since they aren't supposed to exist return VanillaItems::AIR(); } $legacyId = $this->map[$lower] ?? null; if($legacyId === null){ throw new LegacyStringToItemParserException("Unable to resolve \"" . $input . "\" to a valid item"); } $itemData = $this->itemDataUpgrader->upgradeItemTypeDataString($legacyId, $meta, 1, null); try{ return $this->itemDeserializer->deserializeStack($itemData); }catch(ItemTypeDeserializeException $e){ throw new LegacyStringToItemParserException($e->getMessage(), 0, $e); } } protected function reprocess(string $input) : string{ return str_replace([" ", "minecraft:"], ["_", ""], trim($input)); } }