|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace imperazim\serializer; |
| 6 | + |
| 7 | +use pocketmine\block\utils\DyeColor; |
| 8 | +use pocketmine\block\utils\ColoredTrait; |
| 9 | +use pocketmine\item\Dye; |
| 10 | +use pocketmine\item\Item; |
| 11 | +use pocketmine\item\ItemBlock; |
| 12 | +use pocketmine\item\StringToItemParser; |
| 13 | +use pocketmine\item\LegacyStringToItemParser; |
| 14 | +use pocketmine\nbt\TreeRoot; |
| 15 | +use pocketmine\nbt\tag\CompoundTag; |
| 16 | +use pocketmine\nbt\LittleEndianNbtSerializer; |
| 17 | + |
| 18 | +use function json_decode; |
| 19 | +use function str_replace; |
| 20 | +use function strtolower; |
| 21 | + |
| 22 | +/** |
| 23 | +* Class ItemSerializable |
| 24 | +* @package imperazim\serializer |
| 25 | +*/ |
| 26 | +final class ItemSerializable { |
| 27 | + |
| 28 | + private static function hasColoredTrait(object $object): bool { |
| 29 | + $class = get_class($object); |
| 30 | + $traits = []; |
| 31 | + do { |
| 32 | + $classTraits = class_uses($class); |
| 33 | + if ($classTraits !== false) { |
| 34 | + $traits = array_merge($traits, $classTraits); |
| 35 | + } |
| 36 | + $class = get_parent_class($class); |
| 37 | + } while ($class !== false); |
| 38 | + return in_array(ColoredTrait::class, $traits, true); |
| 39 | + } |
| 40 | + |
| 41 | + public static function jsonDeserialize(string $data): ?Item { |
| 42 | + try { |
| 43 | + $decodedData = json_decode($data); |
| 44 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 45 | + throw new \RuntimeException("JSON error: " . json_last_error_msg()); |
| 46 | + } |
| 47 | + |
| 48 | + $typeId = $decodedData->typeId ?? null; |
| 49 | + $vanillaName = $decodedData->vanillaName ?? null; |
| 50 | + |
| 51 | + if ($vanillaName !== null) { |
| 52 | + $item = StringToItemParser::getInstance()->parse($vanillaName) |
| 53 | + ?? LegacyStringToItemParser::getInstance()->parse($vanillaName); |
| 54 | + } else { |
| 55 | + throw new \RuntimeException("Neither typeId nor vanillaName found."); |
| 56 | + } |
| 57 | + |
| 58 | + if ($item === null || $item->isNull()) { |
| 59 | + throw new \RuntimeException("Item not found: typeId=$typeId, vanillaName=$vanillaName"); |
| 60 | + } |
| 61 | + |
| 62 | + $item->setCount($decodedData->count ?? 1); |
| 63 | + |
| 64 | + // Handle Dye items color restoration |
| 65 | + if ($item instanceof Dye && isset($decodedData->color)) { |
| 66 | + $colorName = mb_strtoupper($decodedData->color); |
| 67 | + $dyeColor = DyeColor::getAll()[$colorName] ?? null; |
| 68 | + if ($dyeColor !== null) { |
| 69 | + $item->setColor($dyeColor); |
| 70 | + } |
| 71 | + } |
| 72 | + // Handle colored blocks color restoration |
| 73 | + elseif ($item instanceof ItemBlock && self::hasColoredTrait($item->getBlock())) { |
| 74 | + if (isset($decodedData->color)) { |
| 75 | + $colorName = mb_strtoupper($decodedData->color); |
| 76 | + $dyeColor = DyeColor::getAll()[$colorName] ?? null; |
| 77 | + if ($dyeColor !== null) { |
| 78 | + $block = $item->getBlock(); |
| 79 | + if (method_exists($block, 'setColor')) { |
| 80 | + $block->setColor($dyeColor); |
| 81 | + $item = $block->asItem(); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (!empty($decodedData->nbt)) { |
| 88 | + $nbtData = base64_decode($decodedData->nbt, true); |
| 89 | + if ($nbtData === false) { |
| 90 | + throw new \RuntimeException("Invalid base64 NBT"); |
| 91 | + } |
| 92 | + $nbt = (new LittleEndianNbtSerializer())->read($nbtData)->getTag(); |
| 93 | + if (!$nbt instanceof CompoundTag) { |
| 94 | + throw new \RuntimeException("NBT is not CompoundTag"); |
| 95 | + } |
| 96 | + $item->setNamedTag($nbt); |
| 97 | + } |
| 98 | + |
| 99 | + return $item; |
| 100 | + } catch (\RuntimeException $e) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public static function jsonSerialize(Item $item): ?string { |
| 106 | + try { |
| 107 | + $data = [ |
| 108 | + "vanillaName" => strtolower(str_replace(' ', '_', $item->getVanillaName())), |
| 109 | + "typeId" => $item->getTypeId() |
| 110 | + ]; |
| 111 | + |
| 112 | + if ($item->getCount() !== 1) { |
| 113 | + $data["count"] = $item->getCount(); |
| 114 | + } |
| 115 | + |
| 116 | + // Handle Dye items (has color but not ItemBlock) |
| 117 | + if ($item instanceof Dye) { |
| 118 | + $color = $item->getColor(); |
| 119 | + if ($color !== null) { |
| 120 | + $data["color"] = $color->name; |
| 121 | + } |
| 122 | + } |
| 123 | + // Handle colored blocks (wool, concrete, etc) |
| 124 | + elseif ($item instanceof ItemBlock && self::hasColoredTrait($item->getBlock())) { |
| 125 | + $block = $item->getBlock(); |
| 126 | + $color = $block->getColor(); |
| 127 | + if ($color !== null) { |
| 128 | + $data["color"] = $color->name; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if ($item->hasNamedTag()) { |
| 133 | + $nbtSerializer = new LittleEndianNbtSerializer(); |
| 134 | + $data["nbt"] = base64_encode($nbtSerializer->write(new TreeRoot($item->getNamedTag()))); |
| 135 | + } |
| 136 | + |
| 137 | + return json_encode($data); |
| 138 | + } catch (\RuntimeException $e) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments