From 654fc9a2a6dfd1e48a4100dcd06c2ca373520c94 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 10 Jul 2021 20:13:44 +0100 Subject: [PATCH] LegacyStringToItemParser: Throw more specific exceptions --- src/command/defaults/ClearCommand.php | 4 +-- src/command/defaults/GiveCommand.php | 3 +- src/item/Item.php | 2 +- src/item/LegacyStringToItemParser.php | 6 ++-- .../LegacyStringToItemParserException.php | 28 +++++++++++++++++++ src/world/generator/Flat.php | 3 +- 6 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 src/item/LegacyStringToItemParserException.php diff --git a/src/command/defaults/ClearCommand.php b/src/command/defaults/ClearCommand.php index eded7e8fa..96055d86f 100644 --- a/src/command/defaults/ClearCommand.php +++ b/src/command/defaults/ClearCommand.php @@ -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; diff --git a/src/command/defaults/GiveCommand.php b/src/command/defaults/GiveCommand.php index df740c2ac..f28e6fee0 100644 --- a/src/command/defaults/GiveCommand.php +++ b/src/command/defaults/GiveCommand.php @@ -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; } diff --git a/src/item/Item.php b/src/item/Item.php index 103638c15..b28bf9066 100644 --- a/src/item/Item.php +++ b/src/item/Item.php @@ -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(); } diff --git a/src/item/LegacyStringToItemParser.php b/src/item/LegacyStringToItemParser.php index d4d345b57..2d872d93d 100644 --- a/src/item/LegacyStringToItemParser.php +++ b/src/item/LegacyStringToItemParser.php @@ -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; diff --git a/src/item/LegacyStringToItemParserException.php b/src/item/LegacyStringToItemParserException.php new file mode 100644 index 000000000..8b6e7212d --- /dev/null +++ b/src/item/LegacyStringToItemParserException.php @@ -0,0 +1,28 @@ +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){