Item: Remove "ench" tag when all enchantments are removed from an item (#4184)

fixes #4144
This commit is contained in:
TheNewHEROBRINEX 2021-04-18 21:56:07 +02:00 committed by GitHub
parent 6f80b8979d
commit 8d1a1628de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 1 deletions

View File

@ -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{

View File

@ -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

View File

@ -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<array{string,int,int}>