|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace imperazim\enchantment; |
| 6 | + |
| 7 | +use ReflectionClass; |
| 8 | +use ReflectionProperty; |
| 9 | +use pocketmine\data\bedrock\EnchantmentIdMap; |
| 10 | +use pocketmine\item\enchantment\StringToEnchantmentParser; |
| 11 | +use pocketmine\utils\StringToTParser; |
| 12 | + |
| 13 | +/** |
| 14 | + * Static facade for registering and managing custom enchantments. |
| 15 | + */ |
| 16 | +final class EnchantmentFactory { |
| 17 | + |
| 18 | + /** @var array<int, CustomEnchantment> id => enchantment */ |
| 19 | + private static array $enchants = []; |
| 20 | + |
| 21 | + /** |
| 22 | + * Register a custom enchantment with the given numeric ID. |
| 23 | + */ |
| 24 | + public static function register(int $id, CustomEnchantment $enchant): void { |
| 25 | + EnchantmentIdMap::getInstance()->register($id, $enchant); |
| 26 | + self::$enchants[$id] = $enchant; |
| 27 | + |
| 28 | + $parser = StringToEnchantmentParser::getInstance(); |
| 29 | + $parser->register($enchant->name, fn() => $enchant); |
| 30 | + $displayName = $enchant->getDisplayName(); |
| 31 | + if ($displayName !== '' && $displayName !== $enchant->name) { |
| 32 | + $parser->register($displayName, fn() => $enchant); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Unregister a custom enchantment by ID or instance. |
| 38 | + */ |
| 39 | + public static function unregister(int|CustomEnchantment $target): void { |
| 40 | + if ($target instanceof CustomEnchantment) { |
| 41 | + $id = array_search($target, self::$enchants, true); |
| 42 | + if ($id === false) { |
| 43 | + return; |
| 44 | + } |
| 45 | + } else { |
| 46 | + $id = $target; |
| 47 | + } |
| 48 | + |
| 49 | + if (!isset(self::$enchants[$id])) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $enchant = self::$enchants[$id]; |
| 54 | + |
| 55 | + // Unregister from StringToEnchantmentParser |
| 56 | + self::unregisterName($enchant); |
| 57 | + |
| 58 | + // Unregister from EnchantmentIdMap |
| 59 | + self::unregisterId($id); |
| 60 | + |
| 61 | + unset(self::$enchants[$id]); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Get a custom enchantment by its numeric ID. |
| 66 | + */ |
| 67 | + public static function get(int $id): ?CustomEnchantment { |
| 68 | + return self::$enchants[$id] ?? null; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get a custom enchantment by its name. |
| 73 | + */ |
| 74 | + public static function getByName(string $name): ?CustomEnchantment { |
| 75 | + $result = StringToEnchantmentParser::getInstance()->parse($name); |
| 76 | + return $result instanceof CustomEnchantment ? $result : null; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Get all registered custom enchantments. |
| 81 | + * @return array<int, CustomEnchantment> |
| 82 | + */ |
| 83 | + public static function getAll(): array { |
| 84 | + return self::$enchants; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Remove enchantment names from StringToEnchantmentParser via reflection. |
| 89 | + */ |
| 90 | + private static function unregisterName(CustomEnchantment $enchant): void { |
| 91 | + $parser = StringToEnchantmentParser::getInstance(); |
| 92 | + $callbackMap = self::getPrivateProperty($parser, 'callbackMap'); |
| 93 | + |
| 94 | + $normalize = fn(string $s): string => strtolower(str_replace([' ', 'minecraft:'], ['_', ''], trim($s))); |
| 95 | + |
| 96 | + unset($callbackMap[$normalize($enchant->name)]); |
| 97 | + $displayName = $enchant->getDisplayName(); |
| 98 | + if ($displayName !== '' && $displayName !== $enchant->name) { |
| 99 | + unset($callbackMap[$normalize($displayName)]); |
| 100 | + } |
| 101 | + |
| 102 | + self::setPrivateProperty($parser, 'callbackMap', $callbackMap); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Remove enchantment ID mapping from EnchantmentIdMap via reflection. |
| 107 | + */ |
| 108 | + private static function unregisterId(int $id): void { |
| 109 | + $idMap = EnchantmentIdMap::getInstance(); |
| 110 | + |
| 111 | + $enchant = $idMap->fromId($id); |
| 112 | + if ($enchant !== null) { |
| 113 | + $enumToId = self::getPrivateProperty($idMap, 'enumToId'); |
| 114 | + unset($enumToId[spl_object_id($enchant)]); |
| 115 | + self::setPrivateProperty($idMap, 'enumToId', $enumToId); |
| 116 | + } |
| 117 | + |
| 118 | + $idToEnum = self::getPrivateProperty($idMap, 'idToEnum'); |
| 119 | + unset($idToEnum[$id]); |
| 120 | + self::setPrivateProperty($idMap, 'idToEnum', $idToEnum); |
| 121 | + } |
| 122 | + |
| 123 | + private static function getPrivateProperty(object $object, string $property): mixed { |
| 124 | + $reflection = new ReflectionProperty($object, $property); |
| 125 | + $reflection->setAccessible(true); |
| 126 | + return $reflection->getValue($object); |
| 127 | + } |
| 128 | + |
| 129 | + private static function setPrivateProperty(object $object, string $property, mixed $value): void { |
| 130 | + $reflection = new ReflectionProperty($object, $property); |
| 131 | + $reflection->setAccessible(true); |
| 132 | + $reflection->setValue($object, $value); |
| 133 | + } |
| 134 | +} |
0 commit comments