mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-12 22:45:28 +00:00
LegacyStringToItemParser: Throw more specific exceptions
This commit is contained in:
parent
9afc5be0f5
commit
654fc9a2a6
@ -23,11 +23,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\command\defaults;
|
namespace pocketmine\command\defaults;
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use pocketmine\command\Command;
|
use pocketmine\command\Command;
|
||||||
use pocketmine\command\CommandSender;
|
use pocketmine\command\CommandSender;
|
||||||
use pocketmine\command\utils\InvalidCommandSyntaxException;
|
use pocketmine\command\utils\InvalidCommandSyntaxException;
|
||||||
use pocketmine\item\LegacyStringToItemParser;
|
use pocketmine\item\LegacyStringToItemParser;
|
||||||
|
use pocketmine\item\LegacyStringToItemParserException;
|
||||||
use pocketmine\lang\KnownTranslationKeys;
|
use pocketmine\lang\KnownTranslationKeys;
|
||||||
use pocketmine\lang\TranslationContainer;
|
use pocketmine\lang\TranslationContainer;
|
||||||
use pocketmine\permission\DefaultPermissionNames;
|
use pocketmine\permission\DefaultPermissionNames;
|
||||||
@ -88,7 +88,7 @@ class ClearCommand extends VanillaCommand{
|
|||||||
if(isset($args[2])){
|
if(isset($args[2])){
|
||||||
$item->setCount($maxCount = $this->getInteger($sender, $args[2], 0));
|
$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
|
//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]]));
|
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%" . KnownTranslationKeys::COMMANDS_GIVE_ITEM_NOTFOUND, [$args[1]]));
|
||||||
return true;
|
return true;
|
||||||
|
@ -27,6 +27,7 @@ use pocketmine\command\Command;
|
|||||||
use pocketmine\command\CommandSender;
|
use pocketmine\command\CommandSender;
|
||||||
use pocketmine\command\utils\InvalidCommandSyntaxException;
|
use pocketmine\command\utils\InvalidCommandSyntaxException;
|
||||||
use pocketmine\item\LegacyStringToItemParser;
|
use pocketmine\item\LegacyStringToItemParser;
|
||||||
|
use pocketmine\item\LegacyStringToItemParserException;
|
||||||
use pocketmine\lang\KnownTranslationKeys;
|
use pocketmine\lang\KnownTranslationKeys;
|
||||||
use pocketmine\lang\TranslationContainer;
|
use pocketmine\lang\TranslationContainer;
|
||||||
use pocketmine\nbt\JsonNbtParser;
|
use pocketmine\nbt\JsonNbtParser;
|
||||||
@ -65,7 +66,7 @@ class GiveCommand extends VanillaCommand{
|
|||||||
|
|
||||||
try{
|
try{
|
||||||
$item = LegacyStringToItemParser::getInstance()->parse($args[1]);
|
$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]]));
|
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%" . KnownTranslationKeys::COMMANDS_GIVE_ITEM_NOTFOUND, [$args[1]]));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -680,7 +680,7 @@ class Item implements \JsonSerializable{
|
|||||||
}elseif($idTag instanceof StringTag){ //PC item save format
|
}elseif($idTag instanceof StringTag){ //PC item save format
|
||||||
try{
|
try{
|
||||||
$item = LegacyStringToItemParser::getInstance()->parse($idTag->getValue() . ":$meta");
|
$item = LegacyStringToItemParser::getInstance()->parse($idTag->getValue() . ":$meta");
|
||||||
}catch(\InvalidArgumentException $e){
|
}catch(LegacyStringToItemParserException $e){
|
||||||
//TODO: improve error handling
|
//TODO: improve error handling
|
||||||
return ItemFactory::air();
|
return ItemFactory::air();
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ final class LegacyStringToItemParser{
|
|||||||
* - `minecraft:string`
|
* - `minecraft:string`
|
||||||
* - `351:4 (lapis lazuli ID:meta)`
|
* - `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{
|
public function parse(string $input) : Item{
|
||||||
$key = $this->reprocess($input);
|
$key = $this->reprocess($input);
|
||||||
@ -98,7 +98,7 @@ final class LegacyStringToItemParser{
|
|||||||
}elseif(is_numeric($b[1])){
|
}elseif(is_numeric($b[1])){
|
||||||
$meta = (int) $b[1];
|
$meta = (int) $b[1];
|
||||||
}else{
|
}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])){
|
if(is_numeric($b[0])){
|
||||||
@ -106,7 +106,7 @@ final class LegacyStringToItemParser{
|
|||||||
}elseif(isset($this->map[strtolower($b[0])])){
|
}elseif(isset($this->map[strtolower($b[0])])){
|
||||||
$item = $this->itemFactory->get($this->map[strtolower($b[0])], $meta);
|
$item = $this->itemFactory->get($this->map[strtolower($b[0])], $meta);
|
||||||
}else{
|
}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;
|
return $item;
|
||||||
|
28
src/item/LegacyStringToItemParserException.php
Normal file
28
src/item/LegacyStringToItemParserException.php
Normal 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{
|
||||||
|
|
||||||
|
}
|
@ -25,6 +25,7 @@ namespace pocketmine\world\generator;
|
|||||||
|
|
||||||
use pocketmine\block\VanillaBlocks;
|
use pocketmine\block\VanillaBlocks;
|
||||||
use pocketmine\item\LegacyStringToItemParser;
|
use pocketmine\item\LegacyStringToItemParser;
|
||||||
|
use pocketmine\item\LegacyStringToItemParserException;
|
||||||
use pocketmine\world\ChunkManager;
|
use pocketmine\world\ChunkManager;
|
||||||
use pocketmine\world\format\Chunk;
|
use pocketmine\world\format\Chunk;
|
||||||
use pocketmine\world\generator\object\OreType;
|
use pocketmine\world\generator\object\OreType;
|
||||||
@ -101,7 +102,7 @@ class Flat extends Generator{
|
|||||||
$cnt = $matches[1] !== "" ? (int) $matches[1] : 1;
|
$cnt = $matches[1] !== "" ? (int) $matches[1] : 1;
|
||||||
try{
|
try{
|
||||||
$b = $itemParser->parse($matches[2])->getBlock();
|
$b = $itemParser->parse($matches[2])->getBlock();
|
||||||
}catch(\InvalidArgumentException $e){
|
}catch(LegacyStringToItemParserException $e){
|
||||||
throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e);
|
throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e);
|
||||||
}
|
}
|
||||||
for($cY = $y, $y += $cnt; $cY < $y; ++$cY){
|
for($cY = $y, $y += $cnt; $cY < $y; ++$cY){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user