PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
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
40 protected array $enchantments = [];
41
42 public function hasEnchantments() : bool{
43 return count($this->enchantments) > 0;
44 }
45
46 public function hasEnchantment(Enchantment $enchantment, int $level = -1) : bool{
47 $id = spl_object_id($enchantment);
48 return isset($this->enchantments[$id]) && ($level === -1 || $this->enchantments[$id]->getLevel() === $level);
49 }
50
51 public function getEnchantment(Enchantment $enchantment) : ?EnchantmentInstance{
52 return $this->enchantments[spl_object_id($enchantment)] ?? null;
53 }
54
58 public function removeEnchantment(Enchantment $enchantment, int $level = -1) : self{
59 $instance = $this->getEnchantment($enchantment);
60 if($instance !== null && ($level === -1 || $instance->getLevel() === $level)){
61 unset($this->enchantments[spl_object_id($enchantment)]);
62 }
63
64 return $this;
65 }
66
70 public function removeEnchantments() : self{
71 $this->enchantments = [];
72 return $this;
73 }
74
78 public function addEnchantment(EnchantmentInstance $enchantment) : self{
79 $this->enchantments[spl_object_id($enchantment->getType())] = $enchantment;
80 return $this;
81 }
82
87 public function getEnchantments() : array{
88 return $this->enchantments;
89 }
90
95 public function getEnchantmentLevel(Enchantment $enchantment) : int{
96 return ($instance = $this->getEnchantment($enchantment)) !== null ? $instance->getLevel() : 0;
97 }
98}
getEnchantmentLevel(Enchantment $enchantment)
removeEnchantment(Enchantment $enchantment, int $level=-1)
addEnchantment(EnchantmentInstance $enchantment)