ItemFactory: extract fromStringSingle() from fromString()

on PM4, the multiple functionality is removed, but on PM3 this is a problem for phpstan.
This commit is contained in:
Dylan K. Taylor
2020-03-13 17:54:25 +00:00
parent 3907a2b6ba
commit f8ce01e2fd
6 changed files with 29 additions and 25 deletions

View File

@ -852,7 +852,7 @@ class Item implements ItemIds, \JsonSerializable{
$item = ItemFactory::get($idTag->getValue(), $meta, $count);
}elseif($idTag instanceof StringTag){ //PC item save format
try{
$item = ItemFactory::fromString($idTag->getValue());
$item = ItemFactory::fromStringSingle($idTag->getValue());
}catch(\InvalidArgumentException $e){
//TODO: improve error handling
return ItemFactory::get(Item::AIR, 0, 0);

View File

@ -354,32 +354,36 @@ class ItemFactory{
if($multiple){
$blocks = [];
foreach(explode(",", $str) as $b){
$blocks[] = self::fromString($b, false);
$blocks[] = self::fromStringSingle($b);
}
return $blocks;
}else{
$b = explode(":", str_replace([" ", "minecraft:"], ["_", ""], trim($str)));
if(!isset($b[1])){
$meta = 0;
}elseif(is_numeric($b[1])){
$meta = (int) $b[1];
}else{
throw new \InvalidArgumentException("Unable to parse \"" . $b[1] . "\" from \"" . $str . "\" as a valid meta value");
}
if(is_numeric($b[0])){
$item = self::get((int) $b[0], $meta);
}elseif(defined(ItemIds::class . "::" . strtoupper($b[0]))){
$item = self::get(constant(ItemIds::class . "::" . strtoupper($b[0])), $meta);
}else{
throw new \InvalidArgumentException("Unable to resolve \"" . $str . "\" to a valid item");
}
return $item;
return self::fromStringSingle($str);
}
}
public static function fromStringSingle(string $str) : Item{
$b = explode(":", str_replace([" ", "minecraft:"], ["_", ""], trim($str)));
if(!isset($b[1])){
$meta = 0;
}elseif(is_numeric($b[1])){
$meta = (int) $b[1];
}else{
throw new \InvalidArgumentException("Unable to parse \"" . $b[1] . "\" from \"" . $str . "\" as a valid meta value");
}
if(is_numeric($b[0])){
$item = self::get((int) $b[0], $meta);
}elseif(defined(ItemIds::class . "::" . strtoupper($b[0]))){
$item = self::get(constant(ItemIds::class . "::" . strtoupper($b[0])), $meta);
}else{
throw new \InvalidArgumentException("Unable to resolve \"" . $str . "\" to a valid item");
}
return $item;
}
/**
* Returns whether the specified item ID is already registered in the item factory.
*/