PocketMine-MP 5.16.1 git-585aee9386a787c95e73dd0a05ffca8329606b68
ItemEnchantmentHandlingTrait.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\item;
25
28use function count;
29use function spl_object_id;
30
37 protected array $enchantments = [];
38
39 public function hasEnchantments() : bool{
40 return count($this->enchantments) > 0;
41 }
42
43 public function hasEnchantment(Enchantment $enchantment, int $level = -1) : bool{
44 $id = spl_object_id($enchantment);
45 return isset($this->enchantments[$id]) && ($level === -1 || $this->enchantments[$id]->getLevel() === $level);
46 }
47
48 public function getEnchantment(Enchantment $enchantment) : ?EnchantmentInstance{
49 return $this->enchantments[spl_object_id($enchantment)] ?? null;
50 }
51
55 public function removeEnchantment(Enchantment $enchantment, int $level = -1) : self{
56 $instance = $this->getEnchantment($enchantment);
57 if($instance !== null && ($level === -1 || $instance->getLevel() === $level)){
58 unset($this->enchantments[spl_object_id($enchantment)]);
59 }
60
61 return $this;
62 }
63
67 public function removeEnchantments() : self{
68 $this->enchantments = [];
69 return $this;
70 }
71
75 public function addEnchantment(EnchantmentInstance $enchantment) : self{
76 $this->enchantments[spl_object_id($enchantment->getType())] = $enchantment;
77 return $this;
78 }
79
83 public function getEnchantments() : array{
84 return $this->enchantments;
85 }
86
91 public function getEnchantmentLevel(Enchantment $enchantment) : int{
92 return ($instance = $this->getEnchantment($enchantment)) !== null ? $instance->getLevel() : 0;
93 }
94}
getEnchantmentLevel(Enchantment $enchantment)
removeEnchantment(Enchantment $enchantment, int $level=-1)
addEnchantment(EnchantmentInstance $enchantment)