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
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
6 changed files with 38 additions and 8 deletions

View File

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace pocketmine\command\defaults;
use InvalidArgumentException;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\item\LegacyStringToItemParser;
use pocketmine\item\LegacyStringToItemParserException;
use pocketmine\lang\KnownTranslationKeys;
use pocketmine\lang\TranslationContainer;
use pocketmine\permission\DefaultPermissionNames;
@ -88,7 +88,7 @@ class ClearCommand extends VanillaCommand{
if(isset($args[2])){
$item->setCount($maxCount = $this->getInteger($sender, $args[2], 0));
}
}catch(InvalidArgumentException $e){
}catch(LegacyStringToItemParserException $e){
//vanilla checks this at argument parsing layer, can't come up with a better alternative
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%" . KnownTranslationKeys::COMMANDS_GIVE_ITEM_NOTFOUND, [$args[1]]));
return true;

View File

@ -27,6 +27,7 @@ use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\item\LegacyStringToItemParser;
use pocketmine\item\LegacyStringToItemParserException;
use pocketmine\lang\KnownTranslationKeys;
use pocketmine\lang\TranslationContainer;
use pocketmine\nbt\JsonNbtParser;
@ -65,7 +66,7 @@ class GiveCommand extends VanillaCommand{
try{
$item = LegacyStringToItemParser::getInstance()->parse($args[1]);
}catch(\InvalidArgumentException $e){
}catch(LegacyStringToItemParserException $e){
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%" . KnownTranslationKeys::COMMANDS_GIVE_ITEM_NOTFOUND, [$args[1]]));
return true;
}

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{
}

View File

@ -25,6 +25,7 @@ namespace pocketmine\world\generator;
use pocketmine\block\VanillaBlocks;
use pocketmine\item\LegacyStringToItemParser;
use pocketmine\item\LegacyStringToItemParserException;
use pocketmine\world\ChunkManager;
use pocketmine\world\format\Chunk;
use pocketmine\world\generator\object\OreType;
@ -101,7 +102,7 @@ class Flat extends Generator{
$cnt = $matches[1] !== "" ? (int) $matches[1] : 1;
try{
$b = $itemParser->parse($matches[2])->getBlock();
}catch(\InvalidArgumentException $e){
}catch(LegacyStringToItemParserException $e){
throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e);
}
for($cY = $y, $y += $cnt; $cY < $y; ++$cY){