mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-19 04:15:04 +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:
@@ -26,7 +26,7 @@ namespace pocketmine\command\defaults;
|
||||
use pocketmine\command\Command;
|
||||
use pocketmine\command\CommandSender;
|
||||
use pocketmine\command\utils\InvalidCommandSyntaxException;
|
||||
use pocketmine\item\ItemFactory;
|
||||
use pocketmine\item\LegacyStringToItemParser;
|
||||
use pocketmine\lang\TranslationContainer;
|
||||
use pocketmine\nbt\JsonNbtParser;
|
||||
use pocketmine\nbt\NbtDataException;
|
||||
@@ -62,7 +62,7 @@ class GiveCommand extends VanillaCommand{
|
||||
}
|
||||
|
||||
try{
|
||||
$item = ItemFactory::getInstance()->fromString($args[1]);
|
||||
$item = LegacyStringToItemParser::getInstance()->parse($args[1]);
|
||||
}catch(\InvalidArgumentException $e){
|
||||
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.give.item.notFound", [$args[1]]));
|
||||
return true;
|
||||
|
@@ -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));
|
||||
}
|
||||
}
|
@@ -49,6 +49,7 @@ use pocketmine\event\world\WorldSaveEvent;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\item\ItemFactory;
|
||||
use pocketmine\item\ItemUseResult;
|
||||
use pocketmine\item\LegacyStringToItemParser;
|
||||
use pocketmine\math\AxisAlignedBB;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\network\mcpe\protocol\BlockActorDataPacket;
|
||||
@@ -1449,9 +1450,9 @@ class World implements ChunkManager{
|
||||
|
||||
if($player->isAdventure(true) and !$ev->isCancelled()){
|
||||
$canBreak = false;
|
||||
$itemFactory = ItemFactory::getInstance();
|
||||
$itemParser = LegacyStringToItemParser::getInstance();
|
||||
foreach($item->getCanDestroy() as $v){
|
||||
$entry = $itemFactory->fromString($v);
|
||||
$entry = $itemParser->parse($v);
|
||||
if($entry->getBlock()->isSameType($target)){
|
||||
$canBreak = true;
|
||||
break;
|
||||
@@ -1583,9 +1584,9 @@ class World implements ChunkManager{
|
||||
$ev = new BlockPlaceEvent($player, $hand, $blockReplace, $blockClicked, $item);
|
||||
if($player->isAdventure(true) and !$ev->isCancelled()){
|
||||
$canPlace = false;
|
||||
$itemFactory = ItemFactory::getInstance();
|
||||
$itemParser = LegacyStringToItemParser::getInstance();
|
||||
foreach($item->getCanPlaceOn() as $v){
|
||||
$entry = $itemFactory->fromString($v);
|
||||
$entry = $itemParser->parse($v);
|
||||
if($entry->getBlock()->isSameType($blockClicked)){
|
||||
$canPlace = true;
|
||||
break;
|
||||
|
@@ -24,7 +24,7 @@ declare(strict_types=1);
|
||||
namespace pocketmine\world\generator;
|
||||
|
||||
use pocketmine\block\VanillaBlocks;
|
||||
use pocketmine\item\ItemFactory;
|
||||
use pocketmine\item\LegacyStringToItemParser;
|
||||
use pocketmine\world\ChunkManager;
|
||||
use pocketmine\world\format\Chunk;
|
||||
use pocketmine\world\generator\object\OreType;
|
||||
@@ -99,7 +99,7 @@ class Flat extends Generator{
|
||||
$result = [];
|
||||
$split = array_map('\trim', explode(',', $layers));
|
||||
$y = 0;
|
||||
$itemFactory = ItemFactory::getInstance();
|
||||
$itemParser = LegacyStringToItemParser::getInstance();
|
||||
foreach($split as $line){
|
||||
preg_match('#^(?:(\d+)[x|*])?(.+)$#', $line, $matches);
|
||||
if(count($matches) !== 3){
|
||||
@@ -108,7 +108,7 @@ class Flat extends Generator{
|
||||
|
||||
$cnt = $matches[1] !== "" ? (int) $matches[1] : 1;
|
||||
try{
|
||||
$b = $itemFactory->fromString($matches[2])->getBlock();
|
||||
$b = $itemParser->parse($matches[2])->getBlock();
|
||||
}catch(\InvalidArgumentException $e){
|
||||
throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e);
|
||||
}
|
||||
|
Reference in New Issue
Block a user