PocketMine-MP 5.33.2 git-09cc76ae2b49f1fe3ab0253e6e987fb82bd0a08f
Loading...
Searching...
No Matches
BlockStateWriter.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
26use pocketmine\block\utils\BellAttachmentType;
27use pocketmine\block\utils\SlabType;
28use pocketmine\block\utils\WallConnectionType;
44
45final class BlockStateWriter{
46
51 private array $states = [];
52
53 public function __construct(
54 private string $id
55 ){}
56
57 public static function create(string $id) : self{
58 return new self($id);
59 }
60
62 public function writeBool(string $name, bool $value) : self{
63 $this->states[$name] = new ByteTag($value ? 1 : 0);
64 return $this;
65 }
66
68 public function writeInt(string $name, int $value) : self{
69 $this->states[$name] = new IntTag($value);
70 return $this;
71 }
72
74 public function writeString(string $name, string $value) : self{
75 $this->states[$name] = new StringTag($value);
76 return $this;
77 }
78
84 public function mapIntToString(string $name, IntFromRawStateMap $map, int $value) : self{
85 $raw = $map->valueToRaw($value);
86 $this->writeString($name, $raw);
87 return $this;
88 }
89
95 public function mapIntToInt(string $name, IntFromRawStateMap $map, int $value) : self{
96 $raw = $map->valueToRaw($value);
97 $this->writeInt($name, $raw);
98 return $this;
99 }
100
105 public function writeFacingDirection(int $value) : self{
106 return $this->mapIntToInt(BlockStateNames::FACING_DIRECTION, ValueMappings::getInstance()->facing, $value);
107 }
108
113 public function writeBlockFace(int $value) : self{
114 $this->mapIntToString(BlockStateNames::MC_BLOCK_FACE, ValueMappings::getInstance()->blockFace, $value);
115 return $this;
116 }
117
124 public function writeFacingFlags(array $faces) : self{
125 $result = 0;
126 foreach($faces as $face){
127 $result |= match($face){
128 Facing::DOWN => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_DOWN,
129 Facing::UP => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_UP,
130 Facing::NORTH => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_NORTH,
131 Facing::SOUTH => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_SOUTH,
132 Facing::WEST => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_WEST,
133 Facing::EAST => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_EAST,
134 default => throw new AssumptionFailedError("Unhandled face $face")
135 };
136 }
137
138 return $this->writeInt(BlockStateNames::MULTI_FACE_DIRECTION_BITS, $result);
139 }
140
145 public function writeEndRodFacingDirection(int $value) : self{
146 //end rods are stupid in bedrock and have everything except up/down the wrong way round
147 return $this->writeFacingDirection(Facing::axis($value) !== Axis::Y ? Facing::opposite($value) : $value);
148 }
149
154 public function writeHorizontalFacing(int $value) : self{
155 return $this->mapIntToInt(BlockStateNames::FACING_DIRECTION, ValueMappings::getInstance()->horizontalFacingClassic, $value);
156 }
157
162 public function writeWeirdoHorizontalFacing(int $value) : self{
163 return $this->mapIntToInt(BlockStateNames::WEIRDO_DIRECTION, ValueMappings::getInstance()->horizontalFacing5Minus, $value);
164 }
165
170 public function writeLegacyHorizontalFacing(int $value) : self{
171 return $this->mapIntToInt(BlockStateNames::DIRECTION, ValueMappings::getInstance()->horizontalFacingSWNE, $value);
172 }
173
179 public function write5MinusHorizontalFacing(int $value) : self{
180 return $this->mapIntToInt(BlockStateNames::DIRECTION, ValueMappings::getInstance()->horizontalFacing5Minus, $value);
181 }
182
188 public function writeCardinalHorizontalFacing(int $value) : self{
189 return $this->mapIntToString(BlockStateNames::MC_CARDINAL_DIRECTION, ValueMappings::getInstance()->cardinalDirection, $value);
190 }
191
196 public function writeCoralFacing(int $value) : self{
197 return $this->mapIntToInt(BlockStateNames::CORAL_DIRECTION, ValueMappings::getInstance()->horizontalFacingCoral, $value);
198 }
199
204 public function writeFacingWithoutDown(int $value) : self{
205 if($value === Facing::DOWN){
206 throw new BlockStateSerializeException("Invalid facing DOWN");
207 }
208 $this->writeFacingDirection($value);
209 return $this;
210 }
211
216 public function writeFacingWithoutUp(int $value) : self{
217 if($value === Facing::UP){
218 throw new BlockStateSerializeException("Invalid facing UP");
219 }
220 $this->writeFacingDirection($value);
221 return $this;
222 }
223
228 public function writePillarAxis(int $axis) : self{
229 $this->mapIntToString(BlockStateNames::PILLAR_AXIS, ValueMappings::getInstance()->pillarAxis, $axis);
230 return $this;
231 }
232
237 public function writeSlabPosition(SlabType $slabType) : self{
238 $this->writeString(BlockStateNames::MC_VERTICAL_HALF, match($slabType){
239 SlabType::TOP => StringValues::MC_VERTICAL_HALF_TOP,
240 SlabType::BOTTOM => StringValues::MC_VERTICAL_HALF_BOTTOM,
241 default => throw new BlockStateSerializeException("Invalid slab type " . $slabType->name)
242 });
243 return $this;
244 }
245
250 public function writeTorchFacing(int $facing) : self{
251 $this->mapIntToString(BlockStateNames::TORCH_FACING_DIRECTION, ValueMappings::getInstance()->torchFacing, $facing);
252 return $this;
253 }
254
259 public function writeBellAttachmentType(BellAttachmentType $attachmentType) : self{
260 return $this->writeUnitEnum(BlockStateNames::ATTACHMENT, ValueMappings::getInstance()->bellAttachmentType, $attachmentType);
261 }
262
267 public function writeWallConnectionType(string $name, ?WallConnectionType $wallConnectionType) : self{
268 $this->writeString($name, match($wallConnectionType){
269 null => StringValues::WALL_CONNECTION_TYPE_EAST_NONE,
270 WallConnectionType::SHORT => StringValues::WALL_CONNECTION_TYPE_EAST_SHORT,
271 WallConnectionType::TALL => StringValues::WALL_CONNECTION_TYPE_EAST_TALL,
272 });
273 return $this;
274 }
275
284 public function writeUnitEnum(string $name, EnumFromRawStateMap $map, \UnitEnum $case) : self{
285 $value = $map->valueToRaw($case);
286 $this->writeString($name, $value);
287
288 return $this;
289 }
290
291 public function getBlockStateData() : BlockStateData{
292 return BlockStateData::current($this->id, $this->states);
293 }
294}
writeUnitEnum(string $name, EnumFromRawStateMap $map, \UnitEnum $case)
mapIntToInt(string $name, IntFromRawStateMap $map, int $value)
writeBellAttachmentType(BellAttachmentType $attachmentType)
mapIntToString(string $name, IntFromRawStateMap $map, int $value)
writeWallConnectionType(string $name, ?WallConnectionType $wallConnectionType)