Item: skip a step when decoding PC itemstacks

This commit is contained in:
Dylan K. Taylor 2020-07-10 21:32:39 +01:00
parent 279abb871d
commit 9b52af62b6
2 changed files with 9 additions and 5 deletions

View File

@ -658,13 +658,12 @@ class Item implements \JsonSerializable{
if($idTag instanceof ShortTag){
$item = ItemFactory::getInstance()->get($idTag->getValue(), $meta, $count);
}elseif($idTag instanceof StringTag){ //PC item save format
try{
$item = LegacyStringToItemParser::getInstance()->parse($idTag->getValue() . ":$meta");
}catch(\InvalidArgumentException $e){
//TODO: improve error handling
//TODO: this isn't a very good mapping source, we need a dedicated mapping for PC
$id = LegacyStringToItemParser::getInstance()->parseId($idTag->getValue());
if($id === null){
return ItemFactory::air();
}
$item->setCount($count);
$item = ItemFactory::getInstance()->get($id, $meta, $count);
}else{
throw new \InvalidArgumentException("Item CompoundTag ID must be an instance of StringTag or ShortTag, " . get_class($idTag) . " given");
}

View File

@ -32,6 +32,7 @@ use function is_int;
use function is_numeric;
use function is_string;
use function json_decode;
use function mb_strtolower;
use function str_replace;
use function strtolower;
use function trim;
@ -78,6 +79,10 @@ final class LegacyStringToItemParser{
$this->map[$alias] = $id;
}
public function parseId(string $input) : ?int{
return $this->map[mb_strtolower($this->reprocess($input))] ?? null;
}
/**
* Tries to parse the specified string into Item types.
*