ItemDeserializer: throw a more specific exception on unknown items

This commit is contained in:
Dylan K. Taylor 2022-07-14 19:16:15 +01:00
parent eb8fb63409
commit b4ce5ed515
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 33 additions and 2 deletions

View File

@ -30,6 +30,7 @@ use pocketmine\block\utils\TreeType;
use pocketmine\block\VanillaBlocks as Blocks;
use pocketmine\data\bedrock\block\BlockStateDeserializeException;
use pocketmine\data\bedrock\block\BlockStateDeserializer;
use pocketmine\data\bedrock\block\convert\UnsupportedBlockStateException;
use pocketmine\data\bedrock\CompoundTypeIds;
use pocketmine\data\bedrock\DyeColorIdMap;
use pocketmine\data\bedrock\EntityLegacyIds;
@ -71,6 +72,8 @@ final class ItemDeserializer{
//TODO: this is rough duct tape; we need a better way to deal with this
try{
$block = $this->blockStateDeserializer->deserialize($blockData);
}catch(UnsupportedBlockStateException $e){
throw new UnsupportedItemTypeException($e->getMessage(), 0, $e);
}catch(BlockStateDeserializeException $e){
throw new ItemTypeDeserializeException("Failed to deserialize item data: " . $e->getMessage(), 0, $e);
}
@ -80,7 +83,7 @@ final class ItemDeserializer{
}
$id = $data->getName();
if(!isset($this->deserializers[$id])){
throw new ItemTypeDeserializeException("No deserializer found for ID $id");
throw new UnsupportedItemTypeException("No deserializer found for ID $id");
}
return ($this->deserializers[$id])($data);

View File

@ -23,6 +23,6 @@ declare(strict_types=1);
namespace pocketmine\data\bedrock\item;
final class ItemTypeDeserializeException extends \RuntimeException{
class ItemTypeDeserializeException extends \RuntimeException{
}

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\data\bedrock\item;
final class UnsupportedItemTypeException extends ItemTypeDeserializeException{
}