Enchantment: Split up enchantment type data and enchantment instance data (#1825)

* Enchantment: Split enchantment type data from instance data
This commit splits enchantments into (effectively) enchantment TYPES vs enchantment INSTANCES.

When applying an enchantment to an item, it only needs to know 2 things:
1. the enchantment ID (identifier) which is used to identify the TYPE
2. the enchantment LEVEL which is used to modify the enchantment's power IN THIS INSTANCE.

Therefore, the LEVEL is not an immutable property. However, all other properties of the currently-named "Enchantment" class are immutable type properties.
Currently, when applying an enchantment to an item, a copy of the enchantment object is created from the registry, and returned. This copies all of the properties contained by the type, which is obviously sub optimal.
This commit is contained in:
Dylan K. Taylor
2017-12-21 12:40:33 +00:00
committed by GitHub
parent 1b4b832c8c
commit 0e538ee51d
4 changed files with 96 additions and 38 deletions

View File

@ -27,6 +27,7 @@ use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\event\TranslationContainer;
use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\enchantment\EnchantmentInstance;
use pocketmine\utils\TextFormat;
class EnchantCommand extends VanillaCommand{
@ -74,9 +75,7 @@ class EnchantCommand extends VanillaCommand{
return true;
}
$enchantment->setLevel((int) ($args[2] ?? 1));
$item->addEnchantment($enchantment);
$item->addEnchantment(new EnchantmentInstance($enchantment, (int) ($args[2] ?? 1)));
$player->getInventory()->setItemInHand($item);