Item-from-string parsing no longer depends on ItemIds

after this is done I'm banning the constant() function.
This commit is contained in:
Dylan K. Taylor
2020-05-13 00:17:15 +01:00
parent ec13aa659a
commit 11ef9fb0c0
9 changed files with 996 additions and 77 deletions

View File

@@ -34,14 +34,7 @@ use pocketmine\entity\Living;
use pocketmine\inventory\ArmorInventory;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\utils\SingletonTrait;
use function constant;
use function defined;
use function explode;
use function is_a;
use function is_numeric;
use function str_replace;
use function strtoupper;
use function trim;
/**
* Manages Item instance creation and registration
@@ -431,37 +424,6 @@ class ItemFactory{
return $item;
}
/**
* Tries to parse the specified string into Item types.
*
* Example accepted formats:
* - `diamond_pickaxe:5`
* - `minecraft:string`
* - `351:4 (lapis lazuli ID:meta)`
*
* @throws \InvalidArgumentException if the given string cannot be parsed as an item identifier
*/
public 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{
throw new \InvalidArgumentException("Unable to parse \"" . $b[1] . "\" from \"" . $str . "\" as a valid meta value");
}
if(is_numeric($b[0])){
$item = $this->get((int) $b[0], $meta);
}elseif(defined(ItemIds::class . "::" . strtoupper($b[0]))){
$item = $this->get(constant(ItemIds::class . "::" . strtoupper($b[0])), $meta);
}else{
throw new \InvalidArgumentException("Unable to resolve \"" . $str . "\" to a valid item");
}
return $item;
}
public static function air() : Item{
return self::$air ?? (self::$air = self::getInstance()->get(ItemIds::AIR, 0, 0));
}