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