From af73c5f2b1dc426fa117efe9ddd76e2dd2f6cde1 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 17 Jul 2019 16:03:08 +0100 Subject: [PATCH] Make EnchantmentInstance immutable, remove enchantment clone from Item it doesn't make sense to set the level of an existing enchantment instance because under the old API it would have no effect anyway (if it was returned from an itemstack) or you had access to the constructor (if applying a new enchantment). Allowing this to be mutable creates needless complexity and performance headaches. --- src/pocketmine/item/Item.php | 2 -- .../item/enchantment/EnchantmentInstance.php | 17 +++-------------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index ab833b6ee..e4da37872 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -45,7 +45,6 @@ use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\TreeRoot; use pocketmine\player\Player; use pocketmine\utils\Binary; -use function array_map; use function base64_decode; use function base64_encode; use function get_class; @@ -848,6 +847,5 @@ class Item implements ItemIds, \JsonSerializable{ } $this->canPlaceOn = $this->canPlaceOn->copy(); $this->canDestroy = $this->canDestroy->copy(); - $this->enchantments = array_map(function(EnchantmentInstance $i){ return clone $i; }, $this->enchantments); } } diff --git a/src/pocketmine/item/enchantment/EnchantmentInstance.php b/src/pocketmine/item/enchantment/EnchantmentInstance.php index 2434ba5b8..4dadc77c1 100644 --- a/src/pocketmine/item/enchantment/EnchantmentInstance.php +++ b/src/pocketmine/item/enchantment/EnchantmentInstance.php @@ -25,8 +25,10 @@ namespace pocketmine\item\enchantment; /** * Container for enchantment data applied to items. + * + * Note: This class is assumed to be immutable. Consider this before making alterations. */ -class EnchantmentInstance{ +final class EnchantmentInstance{ /** @var Enchantment */ private $enchantment; /** @var int */ @@ -66,17 +68,4 @@ class EnchantmentInstance{ public function getLevel() : int{ return $this->level; } - - /** - * Sets the level of the enchantment. - * - * @param int $level - * - * @return $this - */ - public function setLevel(int $level){ - $this->level = $level; - - return $this; - } }