From 5d56030afa19ce7f7f6e72b2a23fdb35d9b242e0 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 20 Sep 2018 18:53:41 +0100 Subject: [PATCH] Item: make nbtDeserialize() return AIR when reading an unknown PC item This is scummy, but it's better than crashing the whole server just because a chest contained an unknown item. --- src/pocketmine/entity/object/ItemEntity.php | 3 +++ src/pocketmine/item/Item.php | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/pocketmine/entity/object/ItemEntity.php b/src/pocketmine/entity/object/ItemEntity.php index f41b73813..b50e2b362 100644 --- a/src/pocketmine/entity/object/ItemEntity.php +++ b/src/pocketmine/entity/object/ItemEntity.php @@ -73,6 +73,9 @@ class ItemEntity extends Entity{ } $this->item = Item::nbtDeserialize($itemTag); + if($this->item->isNull()){ + throw new \UnexpectedValueException("Item for " . get_class($this) . " is invalid"); + } $this->server->getPluginManager()->callEvent(new ItemSpawnEvent($this)); diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index 6f33ba580..974ce0b67 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -953,7 +953,12 @@ class Item implements ItemIds, \JsonSerializable{ if($idTag instanceof ShortTag){ $item = ItemFactory::get(Binary::unsignShort($idTag->getValue()), $meta, $count); }elseif($idTag instanceof StringTag){ //PC item save format - $item = ItemFactory::fromString($idTag->getValue()); + try{ + $item = ItemFactory::fromString($idTag->getValue()); + }catch(\InvalidArgumentException $e){ + //TODO: improve error handling + return ItemFactory::get(Item::AIR, 0, 0); + } $item->setDamage($meta); $item->setCount($count); }else{