PocketMine-MP 5.19.1 git-5cc1068cd43264d3363295eb8d6901e02f467897
IncompatibleEnchantmentRegistry.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
28use pocketmine\utils\SingletonTrait;
29use function array_intersect_key;
30use function count;
31use function spl_object_id;
32
38 use SingletonTrait;
39
44 private array $incompatibilityMap = [];
45
46 private function __construct(){
47 $this->register(Groups::PROTECTION, [Enchantments::PROTECTION(), Enchantments::FIRE_PROTECTION(), Enchantments::BLAST_PROTECTION(), Enchantments::PROJECTILE_PROTECTION()]);
48 $this->register(Groups::BOW_INFINITE, [Enchantments::INFINITY(), Enchantments::MENDING()]);
49 $this->register(Groups::BLOCK_DROPS, [Enchantments::FORTUNE(), Enchantments::SILK_TOUCH()]);
50 }
51
60 public function register(string $tag, array $enchantments) : void{
61 foreach($enchantments as $enchantment){
62 $this->incompatibilityMap[spl_object_id($enchantment)][$tag] = true;
63 }
64 }
65
71 public function unregister(string $tag, array $enchantments) : void{
72 foreach($enchantments as $enchantment){
73 unset($this->incompatibilityMap[spl_object_id($enchantment)][$tag]);
74 }
75 }
76
80 public function unregisterAll(string $tag) : void{
81 foreach($this->incompatibilityMap as $id => $tags){
82 unset($this->incompatibilityMap[$id][$tag]);
83 }
84 }
85
89 public function areCompatible(Enchantment $first, Enchantment $second) : bool{
90 $firstIncompatibilities = $this->incompatibilityMap[spl_object_id($first)] ?? [];
91 $secondIncompatibilities = $this->incompatibilityMap[spl_object_id($second)] ?? [];
92 return count(array_intersect_key($firstIncompatibilities, $secondIncompatibilities)) === 0;
93 }
94}