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.
This commit is contained in:
Dylan K. Taylor 2019-07-17 16:03:08 +01:00
parent fec8c75fd8
commit af73c5f2b1
2 changed files with 3 additions and 16 deletions

View File

@ -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);
}
}

View File

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