PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
ItemEnchantmentTagRegistry.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
27use pocketmine\utils\SingletonTrait;
29use function array_diff;
30use function array_intersect;
31use function array_merge;
32use function array_search;
33use function array_shift;
34use function array_unique;
35use function count;
36
42 use SingletonTrait;
43
48 private array $tagMap = [];
49
50 private function __construct(){
51 $this->register(Tags::ARMOR, [Tags::HELMET, Tags::CHESTPLATE, Tags::LEGGINGS, Tags::BOOTS]);
52 $this->register(Tags::SHIELD);
53 $this->register(Tags::SWORD);
54 $this->register(Tags::TRIDENT);
55 $this->register(Tags::BOW);
56 $this->register(Tags::CROSSBOW);
57 $this->register(Tags::SHEARS);
58 $this->register(Tags::FLINT_AND_STEEL);
59 $this->register(Tags::BLOCK_TOOLS, [Tags::AXE, Tags::PICKAXE, Tags::SHOVEL, Tags::HOE]);
60 $this->register(Tags::FISHING_ROD);
61 $this->register(Tags::CARROT_ON_STICK);
62 $this->register(Tags::COMPASS);
63 $this->register(Tags::MASK);
64 $this->register(Tags::ELYTRA);
65 $this->register(Tags::BRUSH);
66 $this->register(Tags::WEAPONS, [
67 Tags::SWORD,
68 Tags::TRIDENT,
69 Tags::BOW,
70 Tags::CROSSBOW,
71 Tags::BLOCK_TOOLS,
72 ]);
73 }
74
80 public function register(string $tag, array $nestedTags = []) : void{
81 $this->assertNotInternalTag($tag);
82
83 foreach($nestedTags as $nestedTag){
84 if(!isset($this->tagMap[$nestedTag])){
85 $this->register($nestedTag);
86 }
87 $this->tagMap[$tag][] = $nestedTag;
88 }
89
90 if(!isset($this->tagMap[$tag])){
91 $this->tagMap[$tag] = [];
92 $this->tagMap[Tags::ALL][] = $tag;
93 }
94 }
95
96 public function unregister(string $tag) : void{
97 if(!isset($this->tagMap[$tag])){
98 return;
99 }
100 $this->assertNotInternalTag($tag);
101
102 unset($this->tagMap[$tag]);
103
104 foreach(Utils::stringifyKeys($this->tagMap) as $key => $nestedTags){
105 if(($nestedKey = array_search($tag, $nestedTags, true)) !== false){
106 unset($this->tagMap[$key][$nestedKey]);
107 }
108 }
109 }
110
116 public function removeNested(string $tag, array $nestedTags) : void{
117 $this->assertNotInternalTag($tag);
118 $this->tagMap[$tag] = array_diff($this->tagMap[$tag], $nestedTags);
119 }
120
126 public function getNested(string $tag) : array{
127 return $this->tagMap[$tag] ?? [];
128 }
129
134 public function isTagArrayIntersection(array $firstTags, array $secondTags) : bool{
135 if(count($firstTags) === 0 || count($secondTags) === 0){
136 return false;
137 }
138
139 $firstLeafTags = $this->getLeafTagsForArray($firstTags);
140 $secondLeafTags = $this->getLeafTagsForArray($secondTags);
141
142 return count(array_intersect($firstLeafTags, $secondLeafTags)) !== 0;
143 }
144
152 private function getLeafTagsForArray(array $tags) : array{
153 $leafTagArrays = [];
154 foreach($tags as $tag){
155 $leafTagArrays[] = $this->getLeafTags($tag);
156 }
157 return array_unique(array_merge(...$leafTagArrays));
158 }
159
165 private function getLeafTags(string $tag) : array{
166 $result = [];
167 $tagsToHandle = [$tag];
168
169 while(count($tagsToHandle) !== 0){
170 $currentTag = array_shift($tagsToHandle);
171 $nestedTags = $this->getNested($currentTag);
172
173 if(count($nestedTags) === 0){
174 $result[] = $currentTag;
175 }else{
176 $tagsToHandle = array_merge($tagsToHandle, $nestedTags);
177 }
178 }
179
180 return $result;
181 }
182
183 private function assertNotInternalTag(string $tag) : void{
184 if($tag === Tags::ALL){
185 throw new \InvalidArgumentException(
186 "Cannot perform any operations on the internal item enchantment tag '$tag'"
187 );
188 }
189 }
190}
static stringifyKeys(array $array)
Definition: Utils.php:605