PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
LegacyStringToItemParser.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\item;
25
31use pocketmine\utils\SingletonTrait;
34use Symfony\Component\Filesystem\Path;
35use function explode;
36use function is_array;
37use function is_numeric;
38use function is_string;
39use function json_decode;
40use function str_replace;
41use function strtolower;
42use function trim;
43
58 use SingletonTrait;
59
60 private static function make() : self{
61 $result = new self(
62 GlobalItemDataHandlers::getUpgrader(),
63 GlobalItemDataHandlers::getDeserializer()
64 );
65
66 $mappingsRaw = Filesystem::fileGetContents(Path::join(\pocketmine\RESOURCE_PATH, 'item_from_string_bc_map.json'));
67
68 $mappings = json_decode($mappingsRaw, true);
69 if(!is_array($mappings)) throw new AssumptionFailedError("Invalid mappings format, expected array");
70
71 foreach(Utils::promoteKeys($mappings) as $name => $id){
72 if(!is_string($id)) throw new AssumptionFailedError("Invalid mappings format, expected string values");
73 $result->addMapping((string) $name, $id);
74 }
75
76 return $result;
77 }
78
83 private array $map = [];
84
85 public function __construct(
86 private ItemDataUpgrader $itemDataUpgrader,
87 private ItemDeserializer $itemDeserializer
88 ){}
89
90 public function addMapping(string $alias, string $id) : void{
91 $this->map[$alias] = $id;
92 }
93
98 public function getMappings() : array{
99 return $this->map;
100 }
101
112 public function parse(string $input) : Item{
113 $key = $this->reprocess($input);
114 $b = explode(":", $key);
115
116 if(!isset($b[1])){
117 $meta = 0;
118 }elseif(is_numeric($b[1])){
119 $meta = (int) $b[1];
120 }else{
121 throw new LegacyStringToItemParserException("Unable to parse \"" . $b[1] . "\" from \"" . $input . "\" as a valid meta value");
122 }
123
124 $lower = strtolower($b[0]);
125 if($lower === "0" || $lower === "air"){
126 //item deserializer doesn't recognize air items since they aren't supposed to exist
127 return VanillaItems::AIR();
128 }
129
130 $legacyId = $this->map[$lower] ?? null;
131 if($legacyId === null){
132 throw new LegacyStringToItemParserException("Unable to resolve \"" . $input . "\" to a valid item");
133 }
134 $itemData = $this->itemDataUpgrader->upgradeItemTypeDataString($legacyId, $meta, 1, null);
135
136 try{
137 return $this->itemDeserializer->deserializeStack($itemData);
138 }catch(ItemTypeDeserializeException $e){
139 throw new LegacyStringToItemParserException($e->getMessage(), 0, $e);
140 }
141 }
142
143 protected function reprocess(string $input) : string{
144 return str_replace([" ", "minecraft:"], ["_", ""], trim($input));
145 }
146}