LegacyStringToItemParser: Throw more specific exceptions

This commit is contained in:
Dylan K. Taylor
2021-07-10 20:13:44 +01:00
parent 9afc5be0f5
commit 654fc9a2a6
6 changed files with 38 additions and 8 deletions

View File

@@ -680,7 +680,7 @@ class Item implements \JsonSerializable{
}elseif($idTag instanceof StringTag){ //PC item save format
try{
$item = LegacyStringToItemParser::getInstance()->parse($idTag->getValue() . ":$meta");
}catch(\InvalidArgumentException $e){
}catch(LegacyStringToItemParserException $e){
//TODO: improve error handling
return ItemFactory::air();
}

View File

@@ -87,7 +87,7 @@ final class LegacyStringToItemParser{
* - `minecraft:string`
* - `351:4 (lapis lazuli ID:meta)`
*
* @throws \InvalidArgumentException if the given string cannot be parsed as an item identifier
* @throws LegacyStringToItemParserException if the given string cannot be parsed as an item identifier
*/
public function parse(string $input) : Item{
$key = $this->reprocess($input);
@@ -98,7 +98,7 @@ final class LegacyStringToItemParser{
}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");
throw new LegacyStringToItemParserException("Unable to parse \"" . $b[1] . "\" from \"" . $input . "\" as a valid meta value");
}
if(is_numeric($b[0])){
@@ -106,7 +106,7 @@ final class LegacyStringToItemParser{
}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");
throw new LegacyStringToItemParserException("Unable to resolve \"" . $input . "\" to a valid item");
}
return $item;

View File

@@ -0,0 +1,28 @@
<?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;
final class LegacyStringToItemParserException extends \UnexpectedValueException{
}