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.
This commit is contained in:
Dylan K. Taylor 2018-12-28 11:55:52 +00:00
parent cf20f0e23a
commit f64cef7eb6
2 changed files with 24 additions and 37 deletions

View File

@ -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);
}

View File

@ -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;
}
/**