PocketMine-MP 5.33.2 git-636b96a9a5c35292f076e2f2e9c5723611d28d7b
Loading...
Searching...
No Matches
BlockStateDeserializerHelper.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
49use pocketmine\block\utils\CopperOxidation;
50use pocketmine\block\utils\SlabType;
62
67
69 public static function decodeButton(Button $block, BlockStateReader $in) : Button{
70 return $block
71 ->setFacing($in->readFacingDirection())
72 ->setPressed($in->readBool(BlockStateNames::BUTTON_PRESSED_BIT));
73 }
74
83 public static function decodeCandle(Candle $block, BlockStateReader $in) : Candle{
84 return $block
85 ->setCount($in->readBoundedInt(StateNames::CANDLES, 0, 3) + 1)
86 ->setLit($in->readBool(StateNames::LIT));
87 }
88
96 public static function decodeCrops(Crops $block, BlockStateReader $in) : Crops{
97 return $block->setAge($in->readBoundedInt(BlockStateNames::GROWTH, 0, 7));
98 }
99
102 return $block
103 ->setFacing($in->readCardinalHorizontalFacing())
104 ->setPowered($in->readBool(BlockStateNames::OUTPUT_LIT_BIT))
105 ->setSubtractMode($in->readBool(BlockStateNames::OUTPUT_SUBTRACT_BIT));
106 }
107
115 public static function decodeCopper(CopperMaterial $block, CopperOxidation $oxidation) : CopperMaterial{
116 $block->setOxidation($oxidation);
117 $block->setWaxed(false);
118 return $block;
119 }
120
128 public static function decodeWaxedCopper(CopperMaterial $block, CopperOxidation $oxidation) : CopperMaterial{
129 $block->setOxidation($oxidation);
130 $block->setWaxed(true);
131 return $block;
132 }
133
136 return $block
137 ->setOutputSignalStrength($in->readBoundedInt(BlockStateNames::REDSTONE_SIGNAL, 0, 15));
138 }
139
148 public static function decodeDoor(Door $block, BlockStateReader $in) : Door{
149 //TODO: check if these need any special treatment to get the appropriate data to both halves of the door
150 return $block
151 ->setTop($in->readBool(BlockStateNames::UPPER_BLOCK_BIT))
152 //a door facing "east" is actually facing north - thanks mojang
153 ->setFacing(Facing::rotateY($in->readCardinalHorizontalFacing(), clockwise: false))
154 ->setHingeRight($in->readBool(BlockStateNames::DOOR_HINGE_BIT))
155 ->setOpen($in->readBool(BlockStateNames::OPEN_BIT));
156 }
157
159 public static function decodeDoublePlant(DoublePlant $block, BlockStateReader $in) : DoublePlant{
160 return $block
161 ->setTop($in->readBool(BlockStateNames::UPPER_BLOCK_BIT));
162 }
163
168 public static function decodeFenceGate(FenceGate $block, BlockStateReader $in) : FenceGate{
169 return $block
170 ->setFacing($in->readCardinalHorizontalFacing())
171 ->setInWall($in->readBool(BlockStateNames::IN_WALL_BIT))
172 ->setOpen($in->readBool(BlockStateNames::OPEN_BIT));
173 }
174
180 return $block
181 ->setAxis($in->mapIntFromInt(BlockStateNames::CORAL_FAN_DIRECTION, ValueMappings::getInstance()->coralAxis));
182 }
183
188 public static function decodeFloorSign(FloorSign $block, BlockStateReader $in) : FloorSign{
189 return $block
190 ->setRotation($in->readBoundedInt(BlockStateNames::GROUND_SIGN_DIRECTION, 0, 15));
191 }
192
193 public static function decodeItemFrame(ItemFrame $block, BlockStateReader $in) : ItemFrame{
194 $in->todo(StateNames::ITEM_FRAME_PHOTO_BIT); //TODO: not sure what the point of this is
195 return $block
197 ->setHasMap($in->readBool(StateNames::ITEM_FRAME_MAP_BIT));
198 }
199
204 public static function decodeLeaves(Leaves $block, BlockStateReader $in) : Leaves{
205 return $block
206 ->setNoDecay($in->readBool(StateNames::PERSISTENT_BIT))
207 ->setCheckDecay($in->readBool(StateNames::UPDATE_BIT));
208 }
209
211 public static function decodeLiquid(Liquid $block, BlockStateReader $in, bool $still) : Liquid{
212 $fluidHeightState = $in->readBoundedInt(BlockStateNames::LIQUID_DEPTH, 0, 15);
213 return $block
214 ->setDecay($fluidHeightState & 0x7)
215 ->setFalling(($fluidHeightState & 0x8) !== 0)
216 ->setStill($still);
217 }
218
219 public static function decodeFlowingLiquid(Liquid $block, BlockStateReader $in) : Liquid{
220 return self::decodeLiquid($block, $in, false);
221 }
222
223 public static function decodeStillLiquid(Liquid $block, BlockStateReader $in) : Liquid{
224 return self::decodeLiquid($block, $in, true);
225 }
226
228 public static function decodeLog(Wood $block, bool $stripped, BlockStateReader $in) : Wood{
229 return $block
230 ->setAxis($in->readPillarAxis())
231 ->setStripped($stripped);
232 }
233
235 public static function decodeMushroomBlock(RedMushroomBlock $block, BlockStateReader $in) : Block{
236 switch($type = $in->readBoundedInt(BlockStateNames::HUGE_MUSHROOM_BITS, 0, 15)){
237 case BlockLegacyMetadata::MUSHROOM_BLOCK_ALL_STEM:
238 case BlockLegacyMetadata::MUSHROOM_BLOCK_STEM: throw new BlockStateDeserializeException("This state does not exist");
239 default:
240 //invalid types get left as default
241 $type = MushroomBlockTypeIdMap::getInstance()->fromId($type);
242 return $type !== null ? $block->setMushroomBlockType($type) : $block;
243 }
244 }
245
248 return $block
249 ->setFacing($in->readCardinalHorizontalFacing())
250 ->setDelay($in->readBoundedInt(BlockStateNames::REPEATER_DELAY, 0, 3) + 1);
251 }
252
257 public static function decodeSapling(Sapling $block, BlockStateReader $in) : Sapling{
258 return $block
259 ->setReady($in->readBool(BlockStateNames::AGE_BIT));
260 }
261
264 //TODO: not sure what the deal is here ... seems like a mojang bug / artifact of bad implementation?
265 //best to keep this separate from weighted plates anyway...
266 return $block->setPressed($in->readBoundedInt(BlockStateNames::REDSTONE_SIGNAL, 0, 15) !== 0);
267 }
268
276 public static function decodeSingleSlab(Slab $block, BlockStateReader $in) : Slab{
277 return $block->setSlabType($in->readSlabPosition());
278 }
279
287 public static function decodeDoubleSlab(Slab $block, BlockStateReader $in) : Slab{
288 $in->ignored(StateNames::MC_VERTICAL_HALF);
289 return $block->setSlabType(SlabType::DOUBLE);
290 }
291
300 public static function decodeStairs(Stair $block, BlockStateReader $in) : Stair{
301 return $block
302 ->setUpsideDown($in->readBool(BlockStateNames::UPSIDE_DOWN_BIT))
303 ->setFacing($in->readWeirdoHorizontalFacing());
304 }
305
307 public static function decodeStem(Stem $block, BlockStateReader $in) : Stem{
308 //In PM, we use Facing::UP to indicate that the stem is not attached to a pumpkin/melon, since this makes the
309 //most intuitive sense (the stem is pointing at the sky). However, Bedrock uses the DOWN state for this, which
310 //is absurd, and I refuse to make our API similarly absurd.
311 $facing = $in->readFacingWithoutUp();
312 return self::decodeCrops($block, $in)
313 ->setFacing($facing === Facing::DOWN ? Facing::UP : $facing);
314 }
315
324 public static function decodeTrapdoor(Trapdoor $block, BlockStateReader $in) : Trapdoor{
325 return $block
326 ->setFacing($in->read5MinusHorizontalFacing())
327 ->setTop($in->readBool(BlockStateNames::UPSIDE_DOWN_BIT))
328 ->setOpen($in->readBool(BlockStateNames::OPEN_BIT));
329 }
330
332 public static function decodeWall(Wall $block, BlockStateReader $in) : Wall{
333 $block->setPost($in->readBool(BlockStateNames::WALL_POST_BIT));
334 $block->setConnection(Facing::NORTH, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_NORTH));
335 $block->setConnection(Facing::SOUTH, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_SOUTH));
336 $block->setConnection(Facing::WEST, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_WEST));
337 $block->setConnection(Facing::EAST, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_EAST));
338
339 return $block;
340 }
341
346 public static function decodeWallSign(WallSign $block, BlockStateReader $in) : WallSign{
347 return $block
348 ->setFacing($in->readHorizontalFacing());
349 }
350
356 return $block
357 ->setOutputSignalStrength($in->readBoundedInt(BlockStateNames::REDSTONE_SIGNAL, 0, 15));
358 }
359}
setDecay(int $decay)
Definition Liquid.php:67
setMushroomBlockType(MushroomBlockType $mushroomBlockType)
setSlabType(SlabType $slabType)
Definition Slab.php:61
setConnection(int $face, ?WallConnectionType $type)
Definition Wall.php:71
static decodeRepeater(RedstoneRepeater $block, BlockStateReader $in)
static decodeCopper(CopperMaterial $block, CopperOxidation $oxidation)
static decodeMushroomBlock(RedMushroomBlock $block, BlockStateReader $in)
static decodeLiquid(Liquid $block, BlockStateReader $in, bool $still)
static decodeLog(Wood $block, bool $stripped, BlockStateReader $in)
static decodeDaylightSensor(DaylightSensor $block, BlockStateReader $in)
static decodeWaxedCopper(CopperMaterial $block, CopperOxidation $oxidation)
static decodeSimplePressurePlate(SimplePressurePlate $block, BlockStateReader $in)
static decodeWeightedPressurePlate(WeightedPressurePlate $block, BlockStateReader $in)
static decodeComparator(RedstoneComparator $block, BlockStateReader $in)