PocketMine-MP 5.27.1 git-9af3cde03fabbe4129c79e46dc87ffa0fff446e6
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;
63
65
67 public static function decodeButton(Button $block, BlockStateReader $in) : Button{
68 return $block
69 ->setFacing($in->readFacingDirection())
70 ->setPressed($in->readBool(BlockStateNames::BUTTON_PRESSED_BIT));
71 }
72
74 public static function decodeCandle(Candle $block, BlockStateReader $in) : Candle{
75 return $block
76 ->setCount($in->readBoundedInt(StateNames::CANDLES, 0, 3) + 1)
77 ->setLit($in->readBool(StateNames::LIT));
78 }
79
87 public static function decodeCrops(Crops $block, BlockStateReader $in) : Crops{
88 return $block->setAge($in->readBoundedInt(BlockStateNames::GROWTH, 0, 7));
89 }
90
93 return $block
94 ->setFacing($in->readCardinalHorizontalFacing())
95 ->setPowered($in->readBool(BlockStateNames::OUTPUT_LIT_BIT))
96 ->setSubtractMode($in->readBool(BlockStateNames::OUTPUT_SUBTRACT_BIT));
97 }
98
105 public static function decodeCopper(CopperMaterial $block, CopperOxidation $oxidation) : CopperMaterial{
106 $block->setOxidation($oxidation);
107 $block->setWaxed(false);
108 return $block;
109 }
110
117 public static function decodeWaxedCopper(CopperMaterial $block, CopperOxidation $oxidation) : CopperMaterial{
118 $block->setOxidation($oxidation);
119 $block->setWaxed(true);
120 return $block;
121 }
122
125 return $block
126 ->setOutputSignalStrength($in->readBoundedInt(BlockStateNames::REDSTONE_SIGNAL, 0, 15));
127 }
128
136 public static function decodeDoor(Door $block, BlockStateReader $in) : Door{
137 //TODO: check if these need any special treatment to get the appropriate data to both halves of the door
138 return $block
139 ->setTop($in->readBool(BlockStateNames::UPPER_BLOCK_BIT))
140 //a door facing "east" is actually facing north - thanks mojang
141 ->setFacing(Facing::rotateY($in->readCardinalHorizontalFacing(), clockwise: false))
142 ->setHingeRight($in->readBool(BlockStateNames::DOOR_HINGE_BIT))
143 ->setOpen($in->readBool(BlockStateNames::OPEN_BIT));
144 }
145
147 public static function decodeDoublePlant(DoublePlant $block, BlockStateReader $in) : DoublePlant{
148 return $block
149 ->setTop($in->readBool(BlockStateNames::UPPER_BLOCK_BIT));
150 }
151
153 public static function decodeFenceGate(FenceGate $block, BlockStateReader $in) : FenceGate{
154 return $block
155 ->setFacing($in->readCardinalHorizontalFacing())
156 ->setInWall($in->readBool(BlockStateNames::IN_WALL_BIT))
157 ->setOpen($in->readBool(BlockStateNames::OPEN_BIT));
158 }
159
162 return $block
163 ->setAxis(match($in->readBoundedInt(BlockStateNames::CORAL_FAN_DIRECTION, 0, 1)){
164 0 => Axis::X,
165 1 => Axis::Z,
166 default => throw new AssumptionFailedError("readBoundedInt() should have prevented this"),
167 });
168 }
169
171 public static function decodeFloorSign(FloorSign $block, BlockStateReader $in) : FloorSign{
172 return $block
173 ->setRotation($in->readBoundedInt(BlockStateNames::GROUND_SIGN_DIRECTION, 0, 15));
174 }
175
176 public static function decodeItemFrame(ItemFrame $block, BlockStateReader $in) : ItemFrame{
177 $in->todo(StateNames::ITEM_FRAME_PHOTO_BIT); //TODO: not sure what the point of this is
178 return $block
179 ->setFacing($in->readFacingDirection())
180 ->setHasMap($in->readBool(StateNames::ITEM_FRAME_MAP_BIT));
181 }
182
184 public static function decodeLeaves(Leaves $block, BlockStateReader $in) : Leaves{
185 return $block
186 ->setNoDecay($in->readBool(StateNames::PERSISTENT_BIT))
187 ->setCheckDecay($in->readBool(StateNames::UPDATE_BIT));
188 }
189
191 public static function decodeLiquid(Liquid $block, BlockStateReader $in, bool $still) : Liquid{
192 $fluidHeightState = $in->readBoundedInt(BlockStateNames::LIQUID_DEPTH, 0, 15);
193 return $block
194 ->setDecay($fluidHeightState & 0x7)
195 ->setFalling(($fluidHeightState & 0x8) !== 0)
196 ->setStill($still);
197 }
198
199 public static function decodeFlowingLiquid(Liquid $block, BlockStateReader $in) : Liquid{
200 return self::decodeLiquid($block, $in, false);
201 }
202
203 public static function decodeStillLiquid(Liquid $block, BlockStateReader $in) : Liquid{
204 return self::decodeLiquid($block, $in, true);
205 }
206
208 public static function decodeLog(Wood $block, bool $stripped, BlockStateReader $in) : Wood{
209 return $block
210 ->setAxis($in->readPillarAxis())
211 ->setStripped($stripped);
212 }
213
215 public static function decodeMushroomBlock(RedMushroomBlock $block, BlockStateReader $in) : Block{
216 switch($type = $in->readBoundedInt(BlockStateNames::HUGE_MUSHROOM_BITS, 0, 15)){
217 case BlockLegacyMetadata::MUSHROOM_BLOCK_ALL_STEM:
218 case BlockLegacyMetadata::MUSHROOM_BLOCK_STEM: throw new BlockStateDeserializeException("This state does not exist");
219 default:
220 //invalid types get left as default
221 $type = MushroomBlockTypeIdMap::getInstance()->fromId($type);
222 return $type !== null ? $block->setMushroomBlockType($type) : $block;
223 }
224 }
225
228 return $block
229 ->setFacing($in->readCardinalHorizontalFacing())
230 ->setDelay($in->readBoundedInt(BlockStateNames::REPEATER_DELAY, 0, 3) + 1);
231 }
232
234 public static function decodeSapling(Sapling $block, BlockStateReader $in) : Sapling{
235 return $block
236 ->setReady($in->readBool(BlockStateNames::AGE_BIT));
237 }
238
241 //TODO: not sure what the deal is here ... seems like a mojang bug / artifact of bad implementation?
242 //best to keep this separate from weighted plates anyway...
243 return $block->setPressed($in->readBoundedInt(BlockStateNames::REDSTONE_SIGNAL, 0, 15) !== 0);
244 }
245
253 public static function decodeSingleSlab(Slab $block, BlockStateReader $in) : Slab{
254 return $block->setSlabType($in->readSlabPosition());
255 }
256
264 public static function decodeDoubleSlab(Slab $block, BlockStateReader $in) : Slab{
265 $in->ignored(StateNames::MC_VERTICAL_HALF);
266 return $block->setSlabType(SlabType::DOUBLE);
267 }
268
276 public static function decodeStairs(Stair $block, BlockStateReader $in) : Stair{
277 return $block
278 ->setUpsideDown($in->readBool(BlockStateNames::UPSIDE_DOWN_BIT))
279 ->setFacing($in->readWeirdoHorizontalFacing());
280 }
281
283 public static function decodeStem(Stem $block, BlockStateReader $in) : Stem{
284 //In PM, we use Facing::UP to indicate that the stem is not attached to a pumpkin/melon, since this makes the
285 //most intuitive sense (the stem is pointing at the sky). However, Bedrock uses the DOWN state for this, which
286 //is absurd, and I refuse to make our API similarly absurd.
287 $facing = $in->readFacingWithoutUp();
288 return self::decodeCrops($block, $in)
289 ->setFacing($facing === Facing::DOWN ? Facing::UP : $facing);
290 }
291
299 public static function decodeTrapdoor(Trapdoor $block, BlockStateReader $in) : Trapdoor{
300 return $block
301 ->setFacing($in->read5MinusHorizontalFacing())
302 ->setTop($in->readBool(BlockStateNames::UPSIDE_DOWN_BIT))
303 ->setOpen($in->readBool(BlockStateNames::OPEN_BIT));
304 }
305
307 public static function decodeWall(Wall $block, BlockStateReader $in) : Wall{
308 $block->setPost($in->readBool(BlockStateNames::WALL_POST_BIT));
309 $block->setConnection(Facing::NORTH, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_NORTH));
310 $block->setConnection(Facing::SOUTH, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_SOUTH));
311 $block->setConnection(Facing::WEST, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_WEST));
312 $block->setConnection(Facing::EAST, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_EAST));
313
314 return $block;
315 }
316
318 public static function decodeWallSign(WallSign $block, BlockStateReader $in) : WallSign{
319 return $block
320 ->setFacing($in->readHorizontalFacing());
321 }
322
323 public static function decodeWeightedPressurePlate(WeightedPressurePlate $block, BlockStateReader $in) : WeightedPressurePlate{
324 return $block
325 ->setOutputSignalStrength($in->readBoundedInt(BlockStateNames::REDSTONE_SIGNAL, 0, 15));
326 }
327}
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 decodeComparator(RedstoneComparator $block, BlockStateReader $in)