From f64cef7eb689823244832ff071bb10b73f6fd101 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 28 Dec 2018 11:55:52 +0000 Subject: [PATCH] ItemFactory: Get rid of $multiple crap this is required in a specialized format, which doesn't make any sense. Plugins with multiple packed item formats should parse them themselves. --- src/pocketmine/item/Item.php | 10 +++--- src/pocketmine/item/ItemFactory.php | 51 +++++++++++------------------ 2 files changed, 24 insertions(+), 37 deletions(-) diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index e3e30d537..82c04b35d 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -93,17 +93,17 @@ class Item implements ItemIds, \JsonSerializable{ } /** - * Tries to parse the specified string into Item ID/meta identifiers, and returns Item instances it created. + * Tries to parse the specified string into Item types. * * This function redirects to {@link ItemFactory#fromString}. * * @param string $str - * @param bool $multiple * - * @return Item[]|Item + * @return Item + * @throws \InvalidArgumentException */ - public static function fromString(string $str, bool $multiple = false){ - return ItemFactory::fromString($str, $multiple); + public static function fromString(string $str) : Item{ + return ItemFactory::fromString($str); } diff --git a/src/pocketmine/item/ItemFactory.php b/src/pocketmine/item/ItemFactory.php index 2c2622951..575d6b789 100644 --- a/src/pocketmine/item/ItemFactory.php +++ b/src/pocketmine/item/ItemFactory.php @@ -360,51 +360,38 @@ class ItemFactory{ } /** - * Tries to parse the specified string into Item ID/meta identifiers, and returns Item instances it created. + * Tries to parse the specified string into Item types. * * Example accepted formats: * - `diamond_pickaxe:5` * - `minecraft:string` * - `351:4 (lapis lazuli ID:meta)` * - * If multiple item instances are to be created, their identifiers must be comma-separated, for example: - * `diamond_pickaxe,wooden_shovel:18,iron_ingot` - * * @param string $str - * @param bool $multiple * - * @return Item[]|Item + * @return Item * * @throws \InvalidArgumentException if the given string cannot be parsed as an item identifier */ - public static function fromString(string $str, bool $multiple = false){ - if($multiple){ - $blocks = []; - foreach(explode(",", $str) as $b){ - $blocks[] = self::fromString($b, false); - } - - return $blocks; + public static function fromString(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{ - $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; + 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; } /**