EnchantCommand: ensure that books are turned into enchanted book items

This commit is contained in:
Dylan K. Taylor 2023-08-15 19:10:48 +01:00
parent 5afbb9d807
commit f516c3c502
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -25,6 +25,7 @@ namespace pocketmine\command\defaults;
use pocketmine\command\CommandSender; use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\item\enchantment\EnchantmentHelper;
use pocketmine\item\enchantment\EnchantmentInstance; use pocketmine\item\enchantment\EnchantmentInstance;
use pocketmine\item\enchantment\StringToEnchantmentParser; use pocketmine\item\enchantment\StringToEnchantmentParser;
use pocketmine\lang\KnownTranslationFactory; use pocketmine\lang\KnownTranslationFactory;
@ -76,8 +77,9 @@ class EnchantCommand extends VanillaCommand{
} }
} }
$item->addEnchantment(new EnchantmentInstance($enchantment, $level)); //this is necessary to deal with enchanted books, which are a different item type than regular books
$player->getInventory()->setItemInHand($item); $enchantedItem = EnchantmentHelper::enchantItem($item, [new EnchantmentInstance($enchantment, $level)]);
$player->getInventory()->setItemInHand($enchantedItem);
self::broadcastCommandMessage($sender, KnownTranslationFactory::commands_enchant_success($player->getName())); self::broadcastCommandMessage($sender, KnownTranslationFactory::commands_enchant_success($player->getName()));
return true; return true;