diff --git a/src/pocketmine/command/defaults/GiveCommand.php b/src/pocketmine/command/defaults/GiveCommand.php index 8e2138aa0..fc9a96b93 100644 --- a/src/pocketmine/command/defaults/GiveCommand.php +++ b/src/pocketmine/command/defaults/GiveCommand.php @@ -62,7 +62,7 @@ class GiveCommand extends VanillaCommand{ } try{ - $item = ItemFactory::fromString($args[1]); + $item = ItemFactory::fromStringSingle($args[1]); }catch(\InvalidArgumentException $e){ $sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.give.item.notFound", [$args[1]])); return true; diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index 5edd234e8..83be88cf9 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -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); diff --git a/src/pocketmine/item/ItemFactory.php b/src/pocketmine/item/ItemFactory.php index cf1c63774..2d537231f 100644 --- a/src/pocketmine/item/ItemFactory.php +++ b/src/pocketmine/item/ItemFactory.php @@ -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. */ diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index c883a222f..1a8b6b35e 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -1764,7 +1764,7 @@ class Level implements ChunkManager, Metadatable{ if($tag instanceof ListTag){ foreach($tag as $v){ if($v instanceof StringTag){ - $entry = ItemFactory::fromString($v->getValue()); + $entry = ItemFactory::fromStringSingle($v->getValue()); if($entry->getId() > 0 and $entry->getBlock()->getId() === $target->getId()){ $canBreak = true; break; @@ -1919,7 +1919,7 @@ class Level implements ChunkManager, Metadatable{ if($tag instanceof ListTag){ foreach($tag as $v){ if($v instanceof StringTag){ - $entry = ItemFactory::fromString($v->getValue()); + $entry = ItemFactory::fromStringSingle($v->getValue()); if($entry->getId() > 0 and $entry->getBlock()->getId() === $blockClicked->getId()){ $canPlace = true; break; diff --git a/src/pocketmine/level/generator/Flat.php b/src/pocketmine/level/generator/Flat.php index 24c4888d3..681b4ad0a 100644 --- a/src/pocketmine/level/generator/Flat.php +++ b/src/pocketmine/level/generator/Flat.php @@ -120,7 +120,7 @@ class Flat extends Generator{ $cnt = $matches[1] !== "" ? (int) $matches[1] : 1; try{ - $b = ItemFactory::fromString($matches[2])->getBlock(); + $b = ItemFactory::fromStringSingle($matches[2])->getBlock(); }catch(\InvalidArgumentException $e){ throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e); } diff --git a/tests/phpunit/item/ItemTest.php b/tests/phpunit/item/ItemTest.php index d84449269..817840f6c 100644 --- a/tests/phpunit/item/ItemTest.php +++ b/tests/phpunit/item/ItemTest.php @@ -84,7 +84,7 @@ class ItemTest extends TestCase{ * @param int $meta */ 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($meta, $item->getDamage());