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