Throw exceptions when failing to deserialize item NBT data, fixed weird crashes when an invalid NBT tag is set on an item

This commit is contained in:
Dylan K. Taylor 2017-03-29 13:35:53 +01:00
parent 1c3d89cfef
commit bc1c75a15a

View File

@ -46,12 +46,22 @@ class Item implements ItemIds, \JsonSerializable{
private static $cachedParser = null;
private static function parseCompoundTag(string $tag) : CompoundTag{
if(strlen($tag) === 0){
throw new \InvalidArgumentException("No NBT data found in supplied string");
}
if(self::$cachedParser === null){
self::$cachedParser = new NBT(NBT::LITTLE_ENDIAN);
}
self::$cachedParser->read($tag);
return self::$cachedParser->getData();
$data = self::$cachedParser->getData();
if(!($data instanceof CompoundTag)){
throw new \InvalidArgumentException("Invalid item NBT string given, it could not be deserialized");
}
return $data;
}
private static function writeCompoundTag(CompoundTag $tag) : string{