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

@ -62,7 +62,7 @@ class GiveCommand extends VanillaCommand{
} }
try{ try{
$item = ItemFactory::fromString($args[1]); $item = ItemFactory::fromStringSingle($args[1]);
}catch(\InvalidArgumentException $e){ }catch(\InvalidArgumentException $e){
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.give.item.notFound", [$args[1]])); $sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.give.item.notFound", [$args[1]]));
return true; return true;

View File

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

View File

@ -354,11 +354,16 @@ class ItemFactory{
if($multiple){ if($multiple){
$blocks = []; $blocks = [];
foreach(explode(",", $str) as $b){ foreach(explode(",", $str) as $b){
$blocks[] = self::fromString($b, false); $blocks[] = self::fromStringSingle($b);
} }
return $blocks; return $blocks;
}else{ }else{
return self::fromStringSingle($str);
}
}
public static function fromStringSingle(string $str) : Item{
$b = explode(":", str_replace([" ", "minecraft:"], ["_", ""], trim($str))); $b = explode(":", str_replace([" ", "minecraft:"], ["_", ""], trim($str)));
if(!isset($b[1])){ if(!isset($b[1])){
$meta = 0; $meta = 0;
@ -378,7 +383,6 @@ class ItemFactory{
return $item; return $item;
} }
}
/** /**
* Returns whether the specified item ID is already registered in the item factory. * Returns whether the specified item ID is already registered in the item factory.

View File

@ -1764,7 +1764,7 @@ class Level implements ChunkManager, Metadatable{
if($tag instanceof ListTag){ if($tag instanceof ListTag){
foreach($tag as $v){ foreach($tag as $v){
if($v instanceof StringTag){ if($v instanceof StringTag){
$entry = ItemFactory::fromString($v->getValue()); $entry = ItemFactory::fromStringSingle($v->getValue());
if($entry->getId() > 0 and $entry->getBlock()->getId() === $target->getId()){ if($entry->getId() > 0 and $entry->getBlock()->getId() === $target->getId()){
$canBreak = true; $canBreak = true;
break; break;
@ -1919,7 +1919,7 @@ class Level implements ChunkManager, Metadatable{
if($tag instanceof ListTag){ if($tag instanceof ListTag){
foreach($tag as $v){ foreach($tag as $v){
if($v instanceof StringTag){ if($v instanceof StringTag){
$entry = ItemFactory::fromString($v->getValue()); $entry = ItemFactory::fromStringSingle($v->getValue());
if($entry->getId() > 0 and $entry->getBlock()->getId() === $blockClicked->getId()){ if($entry->getId() > 0 and $entry->getBlock()->getId() === $blockClicked->getId()){
$canPlace = true; $canPlace = true;
break; break;

View File

@ -120,7 +120,7 @@ class Flat extends Generator{
$cnt = $matches[1] !== "" ? (int) $matches[1] : 1; $cnt = $matches[1] !== "" ? (int) $matches[1] : 1;
try{ try{
$b = ItemFactory::fromString($matches[2])->getBlock(); $b = ItemFactory::fromStringSingle($matches[2])->getBlock();
}catch(\InvalidArgumentException $e){ }catch(\InvalidArgumentException $e){
throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e); throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e);
} }

View File

@ -84,7 +84,7 @@ class ItemTest extends TestCase{
* @param int $meta * @param int $meta
*/ */
public function testFromStringSingle(string $string, int $id, int $meta) : void{ public function testFromStringSingle(string $string, int $id, int $meta) : void{
$item = ItemFactory::fromString($string); $item = ItemFactory::fromStringSingle($string);
self::assertEquals($id, $item->getId()); self::assertEquals($id, $item->getId());
self::assertEquals($meta, $item->getDamage()); self::assertEquals($meta, $item->getDamage());