PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
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::AQUA_AFFINITY(), [Tags::HELMET], []);
60 $this->register(Enchantments::FROST_WALKER(), [/* no primary items */], [Tags::BOOTS]);
61 $this->register(Enchantments::SHARPNESS(), [Tags::SWORD, Tags::AXE], []);
62 $this->register(Enchantments::KNOCKBACK(), [Tags::SWORD], []);
63 $this->register(Enchantments::FIRE_ASPECT(), [Tags::SWORD], []);
64 $this->register(Enchantments::EFFICIENCY(), [Tags::BLOCK_TOOLS], [Tags::SHEARS]);
65 $this->register(Enchantments::FORTUNE(), [Tags::BLOCK_TOOLS], []);
66 $this->register(Enchantments::SILK_TOUCH(), [Tags::BLOCK_TOOLS], [Tags::SHEARS]);
67 $this->register(
68 Enchantments::UNBREAKING(),
69 [Tags::ARMOR, Tags::WEAPONS, Tags::FISHING_ROD],
70 [Tags::SHEARS, Tags::FLINT_AND_STEEL, Tags::SHIELD, Tags::CARROT_ON_STICK, Tags::ELYTRA, Tags::BRUSH]
71 );
72 $this->register(Enchantments::POWER(), [Tags::BOW], []);
73 $this->register(Enchantments::PUNCH(), [Tags::BOW], []);
74 $this->register(Enchantments::FLAME(), [Tags::BOW], []);
75 $this->register(Enchantments::INFINITY(), [Tags::BOW], []);
76 $this->register(
77 Enchantments::MENDING(),
78 [],
79 [Tags::ARMOR, Tags::WEAPONS, Tags::FISHING_ROD,
80 Tags::SHEARS, Tags::FLINT_AND_STEEL, Tags::SHIELD, Tags::CARROT_ON_STICK, Tags::ELYTRA, Tags::BRUSH]
81 );
82 $this->register(Enchantments::VANISHING(), [], [Tags::ALL]);
83 $this->register(Enchantments::SWIFT_SNEAK(), [], [Tags::LEGGINGS]);
84 }
85
90 public function register(Enchantment $enchantment, array $primaryItemTags, array $secondaryItemTags) : void{
91 $this->enchantments[spl_object_id($enchantment)] = $enchantment;
92 $this->setPrimaryItemTags($enchantment, $primaryItemTags);
93 $this->setSecondaryItemTags($enchantment, $secondaryItemTags);
94 }
95
96 public function unregister(Enchantment $enchantment) : void{
97 unset($this->enchantments[spl_object_id($enchantment)]);
98 unset($this->primaryItemTags[spl_object_id($enchantment)]);
99 unset($this->secondaryItemTags[spl_object_id($enchantment)]);
100 }
101
102 public function unregisterAll() : void{
103 $this->enchantments = [];
104 $this->primaryItemTags = [];
105 $this->secondaryItemTags = [];
106 }
107
108 public function isRegistered(Enchantment $enchantment) : bool{
109 return isset($this->enchantments[spl_object_id($enchantment)]);
110 }
111
121 public function getPrimaryItemTags(Enchantment $enchantment) : array{
122 return $this->primaryItemTags[spl_object_id($enchantment)] ?? [];
123 }
124
128 public function setPrimaryItemTags(Enchantment $enchantment, array $tags) : void{
129 if(!$this->isRegistered($enchantment)){
130 throw new \LogicException("Cannot set primary item tags for non-registered enchantment");
131 }
132 $this->primaryItemTags[spl_object_id($enchantment)] = array_values($tags);
133 }
134
144 public function getSecondaryItemTags(Enchantment $enchantment) : array{
145 return $this->secondaryItemTags[spl_object_id($enchantment)] ?? [];
146 }
147
151 public function setSecondaryItemTags(Enchantment $enchantment, array $tags) : void{
152 if(!$this->isRegistered($enchantment)){
153 throw new \LogicException("Cannot set secondary item tags for non-registered enchantment");
154 }
155 $this->secondaryItemTags[spl_object_id($enchantment)] = array_values($tags);
156 }
157
163 public function getPrimaryEnchantmentsForItem(Item $item) : array{
164 $itemTags = $item->getEnchantmentTags();
165 if(count($itemTags) === 0 || $item->hasEnchantments()){
166 return [];
167 }
168
169 return array_filter(
170 $this->enchantments,
171 fn(Enchantment $e) => TagRegistry::getInstance()->isTagArrayIntersection($this->getPrimaryItemTags($e), $itemTags)
172 );
173 }
174
183 public function getAllEnchantmentsForItem(Item $item) : array{
184 if(count($item->getEnchantmentTags()) === 0){
185 return [];
186 }
187
188 return array_filter(
189 $this->enchantments,
190 fn(Enchantment $enchantment) => $this->isAvailableForItem($enchantment, $item)
191 );
192 }
193
199 public function isAvailableForItem(Enchantment $enchantment, Item $item) : bool{
200 $itemTags = $item->getEnchantmentTags();
201 $tagRegistry = TagRegistry::getInstance();
202
203 return $tagRegistry->isTagArrayIntersection($this->getPrimaryItemTags($enchantment), $itemTags) ||
204 $tagRegistry->isTagArrayIntersection($this->getSecondaryItemTags($enchantment), $itemTags);
205 }
206
210 public function getAll() : array{
211 return $this->enchantments;
212 }
213}