PocketMine-MP 5.27.2 git-d86943fa8c6384be3e2c1901ebf94f584b27e784
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;
32use function array_filter;
33use function array_values;
34use function count;
35use function spl_object_id;
36
41 use SingletonTrait;
42
44 private array $enchantments = [];
45
47 private array $primaryItemTags = [];
48
50 private array $secondaryItemTags = [];
51
52 private function __construct(){
53 $this->register(Enchantments::PROTECTION(), [Tags::ARMOR], []);
54 $this->register(Enchantments::FIRE_PROTECTION(), [Tags::ARMOR], []);
55 $this->register(Enchantments::FEATHER_FALLING(), [Tags::BOOTS], []);
56 $this->register(Enchantments::BLAST_PROTECTION(), [Tags::ARMOR], []);
57 $this->register(Enchantments::PROJECTILE_PROTECTION(), [Tags::ARMOR], []);
58 $this->register(Enchantments::THORNS(), [Tags::CHESTPLATE], [Tags::HELMET, Tags::LEGGINGS, Tags::BOOTS]);
59 $this->register(Enchantments::RESPIRATION(), [Tags::HELMET], []);
60 $this->register(Enchantments::AQUA_AFFINITY(), [Tags::HELMET], []);
61 $this->register(Enchantments::FROST_WALKER(), [/* no primary items */], [Tags::BOOTS]);
62 $this->register(Enchantments::SHARPNESS(), [Tags::SWORD, Tags::AXE], []);
63 $this->register(Enchantments::KNOCKBACK(), [Tags::SWORD], []);
64 $this->register(Enchantments::FIRE_ASPECT(), [Tags::SWORD], []);
65 $this->register(Enchantments::EFFICIENCY(), [Tags::BLOCK_TOOLS], [Tags::SHEARS]);
66 $this->register(Enchantments::FORTUNE(), [Tags::BLOCK_TOOLS], []);
67 $this->register(Enchantments::SILK_TOUCH(), [Tags::BLOCK_TOOLS], [Tags::SHEARS]);
68 $this->register(
69 Enchantments::UNBREAKING(),
70 [Tags::ARMOR, Tags::WEAPONS, Tags::FISHING_ROD],
71 [Tags::SHEARS, Tags::FLINT_AND_STEEL, Tags::SHIELD, Tags::CARROT_ON_STICK, Tags::ELYTRA, Tags::BRUSH]
72 );
73 $this->register(Enchantments::POWER(), [Tags::BOW], []);
74 $this->register(Enchantments::PUNCH(), [Tags::BOW], []);
75 $this->register(Enchantments::FLAME(), [Tags::BOW], []);
76 $this->register(Enchantments::INFINITY(), [Tags::BOW], []);
77 $this->register(
78 Enchantments::MENDING(),
79 [],
80 [Tags::ARMOR, Tags::WEAPONS, Tags::FISHING_ROD,
81 Tags::SHEARS, Tags::FLINT_AND_STEEL, Tags::SHIELD, Tags::CARROT_ON_STICK, Tags::ELYTRA, Tags::BRUSH]
82 );
83 $this->register(Enchantments::VANISHING(), [], [Tags::ALL]);
84 $this->register(Enchantments::SWIFT_SNEAK(), [], [Tags::LEGGINGS]);
85 }
86
91 public function register(Enchantment $enchantment, array $primaryItemTags, array $secondaryItemTags) : void{
92 $this->enchantments[spl_object_id($enchantment)] = $enchantment;
93 $this->setPrimaryItemTags($enchantment, $primaryItemTags);
94 $this->setSecondaryItemTags($enchantment, $secondaryItemTags);
95 }
96
97 public function unregister(Enchantment $enchantment) : void{
98 unset($this->enchantments[spl_object_id($enchantment)]);
99 unset($this->primaryItemTags[spl_object_id($enchantment)]);
100 unset($this->secondaryItemTags[spl_object_id($enchantment)]);
101 }
102
103 public function unregisterAll() : void{
104 $this->enchantments = [];
105 $this->primaryItemTags = [];
106 $this->secondaryItemTags = [];
107 }
108
109 public function isRegistered(Enchantment $enchantment) : bool{
110 return isset($this->enchantments[spl_object_id($enchantment)]);
111 }
112
122 public function getPrimaryItemTags(Enchantment $enchantment) : array{
123 return $this->primaryItemTags[spl_object_id($enchantment)] ?? [];
124 }
125
129 public function setPrimaryItemTags(Enchantment $enchantment, array $tags) : void{
130 if(!$this->isRegistered($enchantment)){
131 throw new \LogicException("Cannot set primary item tags for non-registered enchantment");
132 }
133 Utils::validateArrayValueType($tags, fn(string $v) => 1);
134 $this->primaryItemTags[spl_object_id($enchantment)] = array_values($tags);
135 }
136
146 public function getSecondaryItemTags(Enchantment $enchantment) : array{
147 return $this->secondaryItemTags[spl_object_id($enchantment)] ?? [];
148 }
149
153 public function setSecondaryItemTags(Enchantment $enchantment, array $tags) : void{
154 if(!$this->isRegistered($enchantment)){
155 throw new \LogicException("Cannot set secondary item tags for non-registered enchantment");
156 }
157 Utils::validateArrayValueType($tags, fn(string $v) => 1);
158 $this->secondaryItemTags[spl_object_id($enchantment)] = array_values($tags);
159 }
160
166 public function getPrimaryEnchantmentsForItem(Item $item) : array{
167 $itemTags = $item->getEnchantmentTags();
168 if(count($itemTags) === 0 || $item->hasEnchantments()){
169 return [];
170 }
171
172 return array_filter(
173 $this->enchantments,
174 fn(Enchantment $e) => TagRegistry::getInstance()->isTagArrayIntersection($this->getPrimaryItemTags($e), $itemTags)
175 );
176 }
177
186 public function getAllEnchantmentsForItem(Item $item) : array{
187 if(count($item->getEnchantmentTags()) === 0){
188 return [];
189 }
190
191 return array_filter(
192 $this->enchantments,
193 fn(Enchantment $enchantment) => $this->isAvailableForItem($enchantment, $item)
194 );
195 }
196
202 public function isAvailableForItem(Enchantment $enchantment, Item $item) : bool{
203 $itemTags = $item->getEnchantmentTags();
204 $tagRegistry = TagRegistry::getInstance();
205
206 return $tagRegistry->isTagArrayIntersection($this->getPrimaryItemTags($enchantment), $itemTags) ||
207 $tagRegistry->isTagArrayIntersection($this->getSecondaryItemTags($enchantment), $itemTags);
208 }
209
213 public function getAll() : array{
214 return $this->enchantments;
215 }
216}