PocketMine-MP 5.33.2 git-09cc76ae2b49f1fe3ab0253e6e987fb82bd0a08f
Loading...
Searching...
No Matches
BlockObjectToStateSerializer.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\data\bedrock\block\convert;
25
37use function get_class;
38
47 private array $serializers = [];
48
53 private array $cache = [];
54
55 public function serialize(int $stateId) : BlockStateData{
56 //TODO: singleton usage not ideal
57 //TODO: we may want to deduplicate cache entries to avoid wasting memory
58 return $this->cache[$stateId] ??= $this->serializeBlock(RuntimeBlockStateRegistry::getInstance()->fromStateId($stateId));
59 }
60
61 public function isRegistered(Block $block) : bool{
62 return isset($this->serializers[$block->getTypeId()]);
63 }
64
70 public function map(Block $block, \Closure|Writer|BlockStateData $serializer) : void{
71 if(isset($this->serializers[$block->getTypeId()])){
72 throw new \InvalidArgumentException("Block type ID " . $block->getTypeId() . " (" . $block->getName() . ") already has a serializer registered");
73 }
74 //writer accepted for convenience only
75 $this->serializers[$block->getTypeId()] = $serializer instanceof Writer ? $serializer->getBlockStateData() : $serializer;
76 }
77
81 public function mapSimple(Block $block, string $id) : void{
82 $this->map($block, BlockStateData::current($id, []));
83 }
84
88 public function mapSlab(Slab $block, string $singleId, string $doubleId) : void{
89 $this->map($block, fn(Slab $block) => Helper::encodeSlab($block, $singleId, $doubleId));
90 }
91
95 public function mapStairs(Stair $block, string $id) : void{
96 $this->map($block, fn(Stair $block) => Helper::encodeStairs($block, Writer::create($id)));
97 }
98
102 public function mapLog(Wood $block, string $unstrippedId, string $strippedId) : void{
103 $this->map($block, fn(Wood $block) => Helper::encodeLog($block, $unstrippedId, $strippedId));
104 }
105
112 public function serializeBlock(Block $blockState) : BlockStateData{
113 $typeId = $blockState->getTypeId();
114
115 $locatedSerializer = $this->serializers[$typeId] ?? null;
116 if($locatedSerializer === null){
117 throw new BlockStateSerializeException("No serializer registered for " . get_class($blockState) . " with type ID $typeId");
118 }
119
120 if($locatedSerializer instanceof BlockStateData){ //static data, not dependent on state
121 return $locatedSerializer;
122 }
123
132 $result = $locatedSerializer($blockState);
133
134 return $result instanceof Writer ? $result->getBlockStateData() : $result;
135 }
136}
map(Block $block, \Closure|Writer|BlockStateData $serializer)