PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
AvailableEnchantmentRegistry.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\enchantment;
25
30use pocketmine\utils\SingletonTrait;
31use function array_filter;
32use function array_values;
33use function count;
34use function spl_object_id;
35
40 use SingletonTrait;
41
43 private array $enchantments = [];
44
46 private array $primaryItemTags = [];
47
49 private array $secondaryItemTags = [];
50
51 private function __construct(){
52 $this->register(Enchantments::PROTECTION(), [Tags::ARMOR], []);
53 $this->register(Enchantments::FIRE_PROTECTION(), [Tags::ARMOR], []);
54 $this->register(Enchantments::FEATHER_FALLING(), [Tags::BOOTS], []);
55 $this->register(Enchantments::BLAST_PROTECTION(), [Tags::ARMOR], []);
56 $this->register(Enchantments::PROJECTILE_PROTECTION(), [Tags::ARMOR], []);
57 $this->register(Enchantments::THORNS(), [Tags::CHESTPLATE], [Tags::HELMET, Tags::LEGGINGS, Tags::BOOTS]);
58 $this->register(Enchantments::RESPIRATION(), [Tags::HELMET], []);
59 $this->register(Enchantments::SHARPNESS(), [Tags::SWORD, Tags::AXE], []);
60 $this->register(Enchantments::KNOCKBACK(), [Tags::SWORD], []);
61 $this->register(Enchantments::FIRE_ASPECT(), [Tags::SWORD], []);
62 $this->register(Enchantments::EFFICIENCY(), [Tags::BLOCK_TOOLS], [Tags::SHEARS]);
63 $this->register(Enchantments::FORTUNE(), [Tags::BLOCK_TOOLS], []);
64 $this->register(Enchantments::SILK_TOUCH(), [Tags::BLOCK_TOOLS], [Tags::SHEARS]);
65 $this->register(
66 Enchantments::UNBREAKING(),
67 [Tags::ARMOR, Tags::WEAPONS, Tags::FISHING_ROD],
68 [Tags::SHEARS, Tags::FLINT_AND_STEEL, Tags::SHIELD, Tags::CARROT_ON_STICK, Tags::ELYTRA, Tags::BRUSH]
69 );
70 $this->register(Enchantments::POWER(), [Tags::BOW], []);
71 $this->register(Enchantments::PUNCH(), [Tags::BOW], []);
72 $this->register(Enchantments::FLAME(), [Tags::BOW], []);
73 $this->register(Enchantments::INFINITY(), [Tags::BOW], []);
74 $this->register(
75 Enchantments::MENDING(),
76 [],
77 [Tags::ARMOR, Tags::WEAPONS, Tags::FISHING_ROD,
78 Tags::SHEARS, Tags::FLINT_AND_STEEL, Tags::SHIELD, Tags::CARROT_ON_STICK, Tags::ELYTRA, Tags::BRUSH]
79 );
80 $this->register(Enchantments::VANISHING(), [], [Tags::ALL]);
81 $this->register(Enchantments::SWIFT_SNEAK(), [], [Tags::LEGGINGS]);
82 }
83
88 public function register(Enchantment $enchantment, array $primaryItemTags, array $secondaryItemTags) : void{
89 $this->enchantments[spl_object_id($enchantment)] = $enchantment;
90 $this->setPrimaryItemTags($enchantment, $primaryItemTags);
91 $this->setSecondaryItemTags($enchantment, $secondaryItemTags);
92 }
93
94 public function unregister(Enchantment $enchantment) : void{
95 unset($this->enchantments[spl_object_id($enchantment)]);
96 unset($this->primaryItemTags[spl_object_id($enchantment)]);
97 unset($this->secondaryItemTags[spl_object_id($enchantment)]);
98 }
99
100 public function unregisterAll() : void{
101 $this->enchantments = [];
102 $this->primaryItemTags = [];
103 $this->secondaryItemTags = [];
104 }
105
106 public function isRegistered(Enchantment $enchantment) : bool{
107 return isset($this->enchantments[spl_object_id($enchantment)]);
108 }
109
119 public function getPrimaryItemTags(Enchantment $enchantment) : array{
120 return $this->primaryItemTags[spl_object_id($enchantment)] ?? [];
121 }
122
126 public function setPrimaryItemTags(Enchantment $enchantment, array $tags) : void{
127 if(!$this->isRegistered($enchantment)){
128 throw new \LogicException("Cannot set primary item tags for non-registered enchantment");
129 }
130 $this->primaryItemTags[spl_object_id($enchantment)] = array_values($tags);
131 }
132
142 public function getSecondaryItemTags(Enchantment $enchantment) : array{
143 return $this->secondaryItemTags[spl_object_id($enchantment)] ?? [];
144 }
145
149 public function setSecondaryItemTags(Enchantment $enchantment, array $tags) : void{
150 if(!$this->isRegistered($enchantment)){
151 throw new \LogicException("Cannot set secondary item tags for non-registered enchantment");
152 }
153 $this->secondaryItemTags[spl_object_id($enchantment)] = array_values($tags);
154 }
155
161 public function getPrimaryEnchantmentsForItem(Item $item) : array{
162 $itemTags = $item->getEnchantmentTags();
163 if(count($itemTags) === 0 || $item->hasEnchantments()){
164 return [];
165 }
166
167 return array_filter(
168 $this->enchantments,
169 fn(Enchantment $e) => TagRegistry::getInstance()->isTagArrayIntersection($this->getPrimaryItemTags($e), $itemTags)
170 );
171 }
172
181 public function getAllEnchantmentsForItem(Item $item) : array{
182 if(count($item->getEnchantmentTags()) === 0){
183 return [];
184 }
185
186 return array_filter(
187 $this->enchantments,
188 fn(Enchantment $enchantment) => $this->isAvailableForItem($enchantment, $item)
189 );
190 }
191
197 public function isAvailableForItem(Enchantment $enchantment, Item $item) : bool{
198 $itemTags = $item->getEnchantmentTags();
199 $tagRegistry = TagRegistry::getInstance();
200
201 return $tagRegistry->isTagArrayIntersection($this->getPrimaryItemTags($enchantment), $itemTags) ||
202 $tagRegistry->isTagArrayIntersection($this->getSecondaryItemTags($enchantment), $itemTags);
203 }
204
208 public function getAll() : array{
209 return $this->enchantments;
210 }
211}