$id){ if(!is_int($id)) throw new AssumptionFailedError("Invalid mappings format, expected int values"); $result->addMapping((string) $name, $id); } return $result; } /** * @var int[] * @phpstan-var array */ private $map = []; public function __construct(ItemFactory $itemFactory){ $this->itemFactory = $itemFactory; } public function addMapping(string $alias, int $id) : void{ $this->map[$alias] = $id; } /** * @return int[] * @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"); } if(isset($this->map[strtolower($b[0])])){ $item = $this->itemFactory->get($this->map[strtolower($b[0])], $meta); }else{ throw new LegacyStringToItemParserException("Unable to resolve \"" . $input . "\" to a valid item"); } return $item; } protected function reprocess(string $input) : string{ return str_replace([" ", "minecraft:"], ["_", ""], trim($input)); } }