mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-18 20:14:31 +00:00
Item-from-string parsing no longer depends on ItemIds
after this is done I'm banning the constant() function.
This commit is contained in:
@@ -667,7 +667,7 @@ class Item implements \JsonSerializable{
|
||||
$item = ItemFactory::getInstance()->get($idTag->getValue(), $meta, $count);
|
||||
}elseif($idTag instanceof StringTag){ //PC item save format
|
||||
try{
|
||||
$item = ItemFactory::getInstance()->fromString($idTag->getValue() . ":$meta");
|
||||
$item = LegacyStringToItemParser::getInstance()->parse($idTag->getValue() . ":$meta");
|
||||
}catch(\InvalidArgumentException $e){
|
||||
//TODO: improve error handling
|
||||
return ItemFactory::air();
|
||||
|
@@ -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));
|
||||
}
|
||||
|
117
src/item/LegacyStringToItemParser.php
Normal file
117
src/item/LegacyStringToItemParser.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\item;
|
||||
|
||||
use pocketmine\utils\AssumptionFailedError;
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
use function explode;
|
||||
use function file_get_contents;
|
||||
use function is_array;
|
||||
use function is_int;
|
||||
use function is_numeric;
|
||||
use function is_string;
|
||||
use function json_decode;
|
||||
use function str_replace;
|
||||
use function strtolower;
|
||||
use function trim;
|
||||
|
||||
/**
|
||||
* This class fills in as a substitute for all the stuff that used to make ItemFactory::fromString()
|
||||
* work. Since legacy item IDs are on their way out, we can't keep using their constants as stringy
|
||||
* IDs (especially not considering the unnoticed BC-break potential posed by such a stupid idea).
|
||||
*/
|
||||
final class LegacyStringToItemParser{
|
||||
use SingletonTrait;
|
||||
|
||||
/** @var ItemFactory */
|
||||
private $itemFactory;
|
||||
|
||||
private static function make() : self{
|
||||
$result = new self(ItemFactory::getInstance());
|
||||
|
||||
$mappingsRaw = @file_get_contents(\pocketmine\RESOURCE_PATH . '/item_from_string_bc_map.json');
|
||||
if($mappingsRaw === false) throw new AssumptionFailedError("Missing required resource file");
|
||||
|
||||
$mappings = json_decode($mappingsRaw, true);
|
||||
if(!is_array($mappings)) throw new AssumptionFailedError("Invalid mappings format, expected array");
|
||||
|
||||
foreach($mappings as $name => $id){
|
||||
if(!is_string($name) or !is_int($id)) throw new AssumptionFailedError("Invalid mappings format, expected string keys and int values");
|
||||
$result->addMapping($name, $id);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<string, int>
|
||||
*/
|
||||
private $map = [];
|
||||
|
||||
public function __construct(ItemFactory $itemFactory){
|
||||
$this->itemFactory = $itemFactory;
|
||||
}
|
||||
|
||||
public function addMapping(string $alias, int $id) : void{
|
||||
$this->map[$alias] = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 parse(string $input) : Item{
|
||||
$key = $this->reprocess($input);
|
||||
$b = explode(":", $key);
|
||||
|
||||
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 \"" . $input . "\" as a valid meta value");
|
||||
}
|
||||
|
||||
if(is_numeric($b[0])){
|
||||
$item = $this->itemFactory->get((int) $b[0], $meta);
|
||||
}elseif(isset($this->map[strtolower($b[0])])){
|
||||
$item = $this->itemFactory->get($this->map[strtolower($b[0])], $meta);
|
||||
}else{
|
||||
throw new \InvalidArgumentException("Unable to resolve \"" . $input . "\" to a valid item");
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
protected function reprocess(string $input) : string{
|
||||
return str_replace([" ", "minecraft:"], ["_", ""], trim($input));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user