PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
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
130 public static function decodeDoor(Door $block, BlockStateReader $in) : Door{
131 //TODO: check if these need any special treatment to get the appropriate data to both halves of the door
132 return $block
133 ->setTop($in->readBool(BlockStateNames::UPPER_BLOCK_BIT))
134 ->setFacing(Facing::rotateY($in->readLegacyHorizontalFacing(), false))
135 ->setHingeRight($in->readBool(BlockStateNames::DOOR_HINGE_BIT))
136 ->setOpen($in->readBool(BlockStateNames::OPEN_BIT));
137 }
138
140 public static function decodeDoublePlant(DoublePlant $block, BlockStateReader $in) : DoublePlant{
141 return $block
142 ->setTop($in->readBool(BlockStateNames::UPPER_BLOCK_BIT));
143 }
144
146 public static function decodeFenceGate(FenceGate $block, BlockStateReader $in) : FenceGate{
147 return $block
148 ->setFacing($in->readLegacyHorizontalFacing())
149 ->setInWall($in->readBool(BlockStateNames::IN_WALL_BIT))
150 ->setOpen($in->readBool(BlockStateNames::OPEN_BIT));
151 }
152
155 return $block
156 ->setAxis(match($in->readBoundedInt(BlockStateNames::CORAL_FAN_DIRECTION, 0, 1)){
157 0 => Axis::X,
158 1 => Axis::Z,
159 default => throw new AssumptionFailedError("readBoundedInt() should have prevented this"),
160 });
161 }
162
164 public static function decodeFloorSign(FloorSign $block, BlockStateReader $in) : FloorSign{
165 return $block
166 ->setRotation($in->readBoundedInt(BlockStateNames::GROUND_SIGN_DIRECTION, 0, 15));
167 }
168
169 public static function decodeItemFrame(ItemFrame $block, BlockStateReader $in) : ItemFrame{
170 $in->todo(StateNames::ITEM_FRAME_PHOTO_BIT); //TODO: not sure what the point of this is
171 return $block
172 ->setFacing($in->readFacingDirection())
173 ->setHasMap($in->readBool(StateNames::ITEM_FRAME_MAP_BIT));
174 }
175
177 public static function decodeLeaves(Leaves $block, BlockStateReader $in) : Leaves{
178 return $block
179 ->setNoDecay($in->readBool(StateNames::PERSISTENT_BIT))
180 ->setCheckDecay($in->readBool(StateNames::UPDATE_BIT));
181 }
182
184 public static function decodeLiquid(Liquid $block, BlockStateReader $in, bool $still) : Liquid{
185 $fluidHeightState = $in->readBoundedInt(BlockStateNames::LIQUID_DEPTH, 0, 15);
186 return $block
187 ->setDecay($fluidHeightState & 0x7)
188 ->setFalling(($fluidHeightState & 0x8) !== 0)
189 ->setStill($still);
190 }
191
192 public static function decodeFlowingLiquid(Liquid $block, BlockStateReader $in) : Liquid{
193 return self::decodeLiquid($block, $in, false);
194 }
195
196 public static function decodeStillLiquid(Liquid $block, BlockStateReader $in) : Liquid{
197 return self::decodeLiquid($block, $in, true);
198 }
199
201 public static function decodeLog(Wood $block, bool $stripped, BlockStateReader $in) : Wood{
202 return $block
203 ->setAxis($in->readPillarAxis())
204 ->setStripped($stripped);
205 }
206
208 public static function decodeMushroomBlock(RedMushroomBlock $block, BlockStateReader $in) : Block{
209 switch($type = $in->readBoundedInt(BlockStateNames::HUGE_MUSHROOM_BITS, 0, 15)){
210 case BlockLegacyMetadata::MUSHROOM_BLOCK_ALL_STEM:
211 case BlockLegacyMetadata::MUSHROOM_BLOCK_STEM: throw new BlockStateDeserializeException("This state does not exist");
212 default:
213 //invalid types get left as default
214 $type = MushroomBlockTypeIdMap::getInstance()->fromId($type);
215 return $type !== null ? $block->setMushroomBlockType($type) : $block;
216 }
217 }
218
221 return $block
222 ->setFacing($in->readCardinalHorizontalFacing())
223 ->setDelay($in->readBoundedInt(BlockStateNames::REPEATER_DELAY, 0, 3) + 1);
224 }
225
227 public static function decodeSapling(Sapling $block, BlockStateReader $in) : Sapling{
228 return $block
229 ->setReady($in->readBool(BlockStateNames::AGE_BIT));
230 }
231
234 //TODO: not sure what the deal is here ... seems like a mojang bug / artifact of bad implementation?
235 //best to keep this separate from weighted plates anyway...
236 return $block->setPressed($in->readBoundedInt(BlockStateNames::REDSTONE_SIGNAL, 0, 15) !== 0);
237 }
238
240 public static function decodeSingleSlab(Slab $block, BlockStateReader $in) : Slab{
241 return $block->setSlabType($in->readSlabPosition());
242 }
243
245 public static function decodeDoubleSlab(Slab $block, BlockStateReader $in) : Slab{
246 $in->ignored(StateNames::MC_VERTICAL_HALF);
247 return $block->setSlabType(SlabType::DOUBLE);
248 }
249
251 public static function decodeStairs(Stair $block, BlockStateReader $in) : Stair{
252 return $block
253 ->setUpsideDown($in->readBool(BlockStateNames::UPSIDE_DOWN_BIT))
254 ->setFacing($in->readWeirdoHorizontalFacing());
255 }
256
258 public static function decodeStem(Stem $block, BlockStateReader $in) : Stem{
259 //In PM, we use Facing::UP to indicate that the stem is not attached to a pumpkin/melon, since this makes the
260 //most intuitive sense (the stem is pointing at the sky). However, Bedrock uses the DOWN state for this, which
261 //is absurd, and I refuse to make our API similarly absurd.
262 $facing = $in->readFacingWithoutUp();
263 return self::decodeCrops($block, $in)
264 ->setFacing($facing === Facing::DOWN ? Facing::UP : $facing);
265 }
266
268 public static function decodeTrapdoor(Trapdoor $block, BlockStateReader $in) : Trapdoor{
269 return $block
270 ->setFacing($in->read5MinusHorizontalFacing())
271 ->setTop($in->readBool(BlockStateNames::UPSIDE_DOWN_BIT))
272 ->setOpen($in->readBool(BlockStateNames::OPEN_BIT));
273 }
274
276 public static function decodeWall(Wall $block, BlockStateReader $in) : Wall{
277 $block->setPost($in->readBool(BlockStateNames::WALL_POST_BIT));
278 $block->setConnection(Facing::NORTH, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_NORTH));
279 $block->setConnection(Facing::SOUTH, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_SOUTH));
280 $block->setConnection(Facing::WEST, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_WEST));
281 $block->setConnection(Facing::EAST, $in->readWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_EAST));
282
283 return $block;
284 }
285
287 public static function decodeWallSign(WallSign $block, BlockStateReader $in) : WallSign{
288 return $block
289 ->setFacing($in->readHorizontalFacing());
290 }
291
292 public static function decodeWeightedPressurePlate(WeightedPressurePlate $block, BlockStateReader $in) : WeightedPressurePlate{
293 return $block
294 ->setOutputSignalStrength($in->readBoundedInt(BlockStateNames::REDSTONE_SIGNAL, 0, 15));
295 }
296}
setDecay(int $decay)
Definition Liquid.php:68
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)