22declare(strict_types=1);
24namespace pocketmine\item\enchantment;
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;
48 private array $tagMap = [];
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, [
80 public function register(
string $tag, array $nestedTags = []) :
void{
81 $this->assertNotInternalTag($tag);
83 foreach($nestedTags as $nestedTag){
84 if(!isset($this->tagMap[$nestedTag])){
85 $this->
register($nestedTag);
87 $this->tagMap[$tag][] = $nestedTag;
90 if(!isset($this->tagMap[$tag])){
91 $this->tagMap[$tag] = [];
92 $this->tagMap[Tags::ALL][] = $tag;
96 public function unregister(
string $tag) : void{
97 if(!isset($this->tagMap[$tag])){
100 $this->assertNotInternalTag($tag);
102 unset($this->tagMap[$tag]);
105 if(($nestedKey = array_search($tag, $nestedTags,
true)) !==
false){
106 unset($this->tagMap[$key][$nestedKey]);
117 $this->assertNotInternalTag($tag);
118 $this->tagMap[$tag] = array_diff($this->tagMap[$tag], $nestedTags);
127 return $this->tagMap[$tag] ?? [];
135 if(count($firstTags) === 0 || count($secondTags) === 0){
139 $firstLeafTags = $this->getLeafTagsForArray($firstTags);
140 $secondLeafTags = $this->getLeafTagsForArray($secondTags);
142 return count(array_intersect($firstLeafTags, $secondLeafTags)) !== 0;
152 private function getLeafTagsForArray(array $tags) : array{
154 foreach($tags as $tag){
155 $leafTagArrays[] = $this->getLeafTags($tag);
157 return array_unique(array_merge(...$leafTagArrays));
165 private function getLeafTags(
string $tag) : array{
167 $tagsToHandle = [$tag];
169 while(count($tagsToHandle) !== 0){
170 $currentTag = array_shift($tagsToHandle);
171 $nestedTags = $this->getNested($currentTag);
173 if(count($nestedTags) === 0){
174 $result[] = $currentTag;
176 $tagsToHandle = array_merge($tagsToHandle, $nestedTags);
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'"
isTagArrayIntersection(array $firstTags, array $secondTags)
removeNested(string $tag, array $nestedTags)
static stringifyKeys(array $array)