mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 08:17:34 +00:00
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:
parent
3907a2b6ba
commit
f8ce01e2fd
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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());
|
||||
|
Loading…
x
Reference in New Issue
Block a user