From 8d1a1628de2e670197e7e0815ca7c9bf3a32f909 Mon Sep 17 00:00:00 2001 From: TheNewHEROBRINEX Date: Sun, 18 Apr 2021 21:56:07 +0200 Subject: [PATCH] Item: Remove "ench" tag when all enchantments are removed from an item (#4184) fixes #4144 --- src/pocketmine/item/Item.php | 6 +++++- tests/phpstan/configs/l8-baseline.neon | 5 +++++ tests/phpunit/item/ItemTest.php | 13 +++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index b062d6bb2..4a3625228 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -339,7 +339,11 @@ class Item implements ItemIds, \JsonSerializable{ } } - $this->setNamedTagEntry($ench); + if($ench->getCount() > 0){ + $this->setNamedTagEntry($ench); + }else{ + $this->removeNamedTagEntry(self::TAG_ENCH); + } } public function removeEnchantments() : void{ diff --git a/tests/phpstan/configs/l8-baseline.neon b/tests/phpstan/configs/l8-baseline.neon index 7a1973f82..2a32ddbf2 100644 --- a/tests/phpstan/configs/l8-baseline.neon +++ b/tests/phpstan/configs/l8-baseline.neon @@ -1605,6 +1605,11 @@ parameters: count: 1 path: ../../../src/pocketmine/utils/Utils.php + - + message: "#^Parameter \\#1 \\$enchantment of class pocketmine\\\\item\\\\enchantment\\\\EnchantmentInstance constructor expects pocketmine\\\\item\\\\enchantment\\\\Enchantment, pocketmine\\\\item\\\\enchantment\\\\Enchantment\\|null given\\.$#" + count: 1 + path: ../../phpunit/item/ItemTest.php + - message: "#^Cannot call method cancel\\(\\) on pocketmine\\\\scheduler\\\\TaskHandler\\|null\\.$#" count: 1 diff --git a/tests/phpunit/item/ItemTest.php b/tests/phpunit/item/ItemTest.php index 57a94ea6e..cea63730c 100644 --- a/tests/phpunit/item/ItemTest.php +++ b/tests/phpunit/item/ItemTest.php @@ -25,12 +25,15 @@ namespace pocketmine\item; use PHPUnit\Framework\TestCase; use pocketmine\block\BlockFactory; +use pocketmine\item\enchantment\Enchantment; +use pocketmine\item\enchantment\EnchantmentInstance; class ItemTest extends TestCase{ public function setUp() : void{ BlockFactory::init(); ItemFactory::init(); + Enchantment::init(); } /** @@ -61,6 +64,16 @@ class ItemTest extends TestCase{ } } + /** + * Tests that when all enchantments are removed from an item, the "ench" tag is removed as well + */ + public function testEnchantmentRemoval() : void{ + $item = ItemFactory::get(Item::DIAMOND_SWORD); + $item->addEnchantment(new EnchantmentInstance(Enchantment::getEnchantment(Enchantment::SHARPNESS))); + $item->removeEnchantment(Enchantment::SHARPNESS); + self::assertNull($item->getNamedTag()->getTag(Item::TAG_ENCH)); + } + /** * @return mixed[][] * @phpstan-return list