49 private array $tagMap = [];
51 private function __construct(){
52 $this->
register(Tags::ARMOR, [Tags::HELMET, Tags::CHESTPLATE, Tags::LEGGINGS, Tags::BOOTS]);
53 $this->
register(Tags::SHIELD);
54 $this->
register(Tags::SWORD);
55 $this->
register(Tags::TRIDENT);
56 $this->
register(Tags::BOW);
57 $this->
register(Tags::CROSSBOW);
58 $this->
register(Tags::SHEARS);
59 $this->
register(Tags::FLINT_AND_STEEL);
60 $this->
register(Tags::BLOCK_TOOLS, [Tags::AXE, Tags::PICKAXE, Tags::SHOVEL, Tags::HOE]);
61 $this->
register(Tags::FISHING_ROD);
62 $this->
register(Tags::CARROT_ON_STICK);
63 $this->
register(Tags::COMPASS);
64 $this->
register(Tags::MASK);
65 $this->
register(Tags::ELYTRA);
66 $this->
register(Tags::BRUSH);
67 $this->
register(Tags::WEAPONS, [
81 public function register(
string $tag, array $nestedTags = []) : void{
82 $this->assertNotInternalTag($tag);
84 foreach($nestedTags as $nestedTag){
85 if(!isset($this->tagMap[$nestedTag])){
86 $this->
register($nestedTag);
88 $this->tagMap[$tag][] = $nestedTag;
91 if(!isset($this->tagMap[$tag])){
92 $this->tagMap[$tag] = [];
93 $this->tagMap[Tags::ALL][] = $tag;
97 public function unregister(
string $tag) : void{
98 if(!isset($this->tagMap[$tag])){
101 $this->assertNotInternalTag($tag);
103 unset($this->tagMap[$tag]);
105 foreach(Utils::stringifyKeys($this->tagMap) as $key => $nestedTags){
106 if(($nestedKey = array_search($tag, $nestedTags,
true)) !==
false){
107 unset($nestedTags[$nestedKey]);
108 $this->tagMap[$key] = array_values($nestedTags);
119 $this->assertNotInternalTag($tag);
120 $this->tagMap[$tag] = array_values(array_diff($this->tagMap[$tag], $nestedTags));
129 return $this->tagMap[$tag] ?? [];
137 if(count($firstTags) === 0 || count($secondTags) === 0){
141 $firstLeafTags = $this->getLeafTagsForArray($firstTags);
142 $secondLeafTags = $this->getLeafTagsForArray($secondTags);
144 return count(array_intersect($firstLeafTags, $secondLeafTags)) !== 0;
154 private function getLeafTagsForArray(array $tags) : array{
156 foreach($tags as $tag){
157 $leafTagArrays[] = $this->getLeafTags($tag);
159 return array_unique(array_merge(...$leafTagArrays));
167 private function getLeafTags(
string $tag) : array{
169 $tagsToHandle = [$tag];
171 while(count($tagsToHandle) !== 0){
172 $currentTag = array_shift($tagsToHandle);
173 $nestedTags = $this->getNested($currentTag);
175 if(count($nestedTags) === 0){
176 $result[] = $currentTag;
178 $tagsToHandle = array_merge($tagsToHandle, $nestedTags);
185 private function assertNotInternalTag(
string $tag) : void{
186 if($tag === Tags::ALL){
187 throw new \InvalidArgumentException(
188 "Cannot perform any operations on the internal item enchantment tag '$tag'"