PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
BlockStateSerializerHelper.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
46use pocketmine\block\utils\CopperOxidation;
47use pocketmine\block\utils\SlabType;
57
59 public static function encodeButton(Button $block, Writer $out) : Writer{
60 return $out
61 ->writeFacingDirection($block->getFacing())
62 ->writeBool(BlockStateNames::BUTTON_PRESSED_BIT, $block->isPressed());
63 }
64
65 public static function encodeCandle(Candle $block, Writer $out) : Writer{
66 return $out
67 ->writeBool(StateNames::LIT, $block->isLit())
68 ->writeInt(StateNames::CANDLES, $block->getCount() - 1);
69 }
70
71 public static function encodeChemistryTable(ChemistryTable $block, string $chemistryTableType, Writer $out) : Writer{
72 return $out
73 ->writeString(BlockStateNames::CHEMISTRY_TABLE_TYPE, $chemistryTableType)
74 ->writeLegacyHorizontalFacing(Facing::opposite($block->getFacing()));
75 }
76
77 public static function encodeCrops(Crops $block, Writer $out) : Writer{
78 return $out->writeInt(BlockStateNames::GROWTH, $block->getAge());
79 }
80
81 public static function encodeColoredTorch(Torch $block, bool $highBit, Writer $out) : Writer{
82 return $out
83 ->writeBool(BlockStateNames::COLOR_BIT, $highBit)
84 ->writeTorchFacing($block->getFacing());
85 }
86
87 public static function encodeCauldron(string $liquid, int $fillLevel) : Writer{
88 return Writer::create(Ids::CAULDRON)
89 ->writeString(BlockStateNames::CAULDRON_LIQUID, $liquid)
90 ->writeInt(BlockStateNames::FILL_LEVEL, $fillLevel);
91 }
92
93 public static function selectCopperId(CopperOxidation $oxidation, string $noneId, string $exposedId, string $weatheredId, string $oxidizedId) : string{
94 return match($oxidation){
95 CopperOxidation::NONE => $noneId,
96 CopperOxidation::EXPOSED => $exposedId,
97 CopperOxidation::WEATHERED => $weatheredId,
98 CopperOxidation::OXIDIZED => $oxidizedId,
99 };
100 }
101
102 public static function encodeDoor(Door $block, Writer $out) : Writer{
103 return $out
104 ->writeBool(BlockStateNames::UPPER_BLOCK_BIT, $block->isTop())
105 ->writeLegacyHorizontalFacing(Facing::rotateY($block->getFacing(), true))
106 ->writeBool(BlockStateNames::DOOR_HINGE_BIT, $block->isHingeRight())
107 ->writeBool(BlockStateNames::OPEN_BIT, $block->isOpen());
108 }
109
110 public static function encodeDoublePlant(DoublePlant $block, Writer $out) : Writer{
111 return $out
112 ->writeBool(BlockStateNames::UPPER_BLOCK_BIT, $block->isTop());
113 }
114
115 public static function encodeFenceGate(FenceGate $block, Writer $out) : Writer{
116 return $out
117 ->writeLegacyHorizontalFacing($block->getFacing())
118 ->writeBool(BlockStateNames::IN_WALL_BIT, $block->isInWall())
119 ->writeBool(BlockStateNames::OPEN_BIT, $block->isOpen());
120 }
121
122 public static function encodeFloorSign(FloorSign $block, Writer $out) : Writer{
123 return $out
124 ->writeInt(BlockStateNames::GROUND_SIGN_DIRECTION, $block->getRotation());
125 }
126
127 public static function encodeFurnace(Furnace $block, string $unlitId, string $litId) : Writer{
128 return Writer::create($block->isLit() ? $litId : $unlitId)
129 ->writeCardinalHorizontalFacing($block->getFacing());
130 }
131
132 public static function encodeItemFrame(ItemFrame $block, string $id) : Writer{
133 return Writer::create($id)
134 ->writeBool(StateNames::ITEM_FRAME_MAP_BIT, $block->hasMap())
135 ->writeBool(StateNames::ITEM_FRAME_PHOTO_BIT, false)
136 ->writeFacingDirection($block->getFacing());
137 }
138
139 public static function encodeLeaves(Leaves $block, Writer $out) : Writer{
140 return $out
141 ->writeBool(BlockStateNames::PERSISTENT_BIT, $block->isNoDecay())
142 ->writeBool(BlockStateNames::UPDATE_BIT, $block->isCheckDecay());
143 }
144
145 public static function encodeLiquid(Liquid $block, string $stillId, string $flowingId) : Writer{
146 return Writer::create($block->isStill() ? $stillId : $flowingId)
147 ->writeInt(BlockStateNames::LIQUID_DEPTH, $block->getDecay() | ($block->isFalling() ? 0x8 : 0));
148 }
149
150 public static function encodeLog(Wood $block, string $unstrippedId, string $strippedId) : Writer{
151 $out = $block->isStripped() ?
152 Writer::create($strippedId) :
153 Writer::create($unstrippedId);
154 return $out
155 ->writePillarAxis($block->getAxis());
156 }
157
158 public static function encodeMushroomBlock(RedMushroomBlock $block, Writer $out) : Writer{
159 return $out
160 ->writeInt(BlockStateNames::HUGE_MUSHROOM_BITS, MushroomBlockTypeIdMap::getInstance()->toId($block->getMushroomBlockType()));
161 }
162
163 public static function encodeQuartz(string $type, int $axis) : Writer{
164 return Writer::create(Ids::QUARTZ_BLOCK)
165 ->writeString(BlockStateNames::CHISEL_TYPE, $type)
166 ->writePillarAxis($axis); //this isn't needed for all types, but we have to write it anyway
167 }
168
169 public static function encodeSandstone(string $id, string $type) : Writer{
170 return Writer::create($id)->writeString(BlockStateNames::SAND_STONE_TYPE, $type);
171 }
172
173 public static function encodeSapling(Sapling $block, Writer $out) : Writer{
174 return $out
175 ->writeBool(BlockStateNames::AGE_BIT, $block->isReady());
176 }
177
178 public static function encodeSimplePressurePlate(SimplePressurePlate $block, Writer $out) : Writer{
179 //TODO: not sure what the deal is here ... seems like a mojang bug / artifact of bad implementation?
180 //best to keep this separate from weighted plates anyway...
181 return $out
182 ->writeInt(BlockStateNames::REDSTONE_SIGNAL, $block->isPressed() ? 15 : 0);
183 }
184
185 private static function encodeSingleSlab(Slab $block, string $id) : Writer{
186 return Writer::create($id)
187 ->writeSlabPosition($block->getSlabType());
188 }
189
190 private static function encodeDoubleSlab(Slab $block, string $id) : Writer{
191 return Writer::create($id)
192 //this is (intentionally) also written for double slabs (as zero) to maintain bug parity with MCPE
193 ->writeSlabPosition(SlabType::BOTTOM);
194 }
195
196 public static function encodeSlab(Slab $block, string $singleId, string $doubleId) : Writer{
197 return $block->getSlabType() === SlabType::DOUBLE ?
198 self::encodeDoubleSlab($block, $doubleId) :
199 self::encodeSingleSlab($block, $singleId);
200 }
201
202 public static function encodeStairs(Stair $block, Writer $out) : Writer{
203 return $out
204 ->writeBool(BlockStateNames::UPSIDE_DOWN_BIT, $block->isUpsideDown())
205 ->writeWeirdoHorizontalFacing($block->getFacing());
206 }
207
208 public static function encodeStem(Stem $block, Writer $out) : Writer{
209 //In PM, we use Facing::UP to indicate that the stem is not attached to a pumpkin/melon, since this makes the
210 //most intuitive sense (the stem is pointing at the sky). However, Bedrock uses the DOWN state for this, which
211 //is absurd, and I refuse to make our API similarly absurd.
212 $facing = $block->getFacing();
213 return self::encodeCrops($block, $out)
214 ->writeFacingWithoutUp($facing === Facing::UP ? Facing::DOWN : $facing);
215 }
216
217 public static function encodeStoneBricks(string $type) : Writer{
218 return Writer::create(Ids::STONEBRICK)
219 ->writeString(BlockStateNames::STONE_BRICK_TYPE, $type);
220 }
221
222 private static function encodeStoneSlab(Slab $block, string $singleId, string $doubleId, string $typeKey, string $typeValue) : Writer{
223 return self::encodeSlab($block, $singleId, $doubleId)
224 ->writeString($typeKey, $typeValue);
225 }
226
227 public static function encodeStoneSlab1(Slab $block, string $singleId, string $doubleTypeValue) : Writer{
228 //1.21 made this a mess by flattening single slab IDs but not double ones
229 return $block->getSlabType() === SlabType::DOUBLE ?
230 self::encodeDoubleSlab($block, Ids::DOUBLE_STONE_BLOCK_SLAB)
231 ->writeString(BlockStateNames::STONE_SLAB_TYPE, $doubleTypeValue) :
232 self::encodeSingleSlab($block, $singleId);
233 }
234
235 public static function encodeStoneSlab2(Slab $block, string $typeValue) : Writer{
236 return self::encodeStoneSlab($block, Ids::STONE_BLOCK_SLAB2, Ids::DOUBLE_STONE_BLOCK_SLAB2, BlockStateNames::STONE_SLAB_TYPE_2, $typeValue);
237 }
238
239 public static function encodeStoneSlab3(Slab $block, string $typeValue) : Writer{
240 return self::encodeStoneSlab($block, Ids::STONE_BLOCK_SLAB3, Ids::DOUBLE_STONE_BLOCK_SLAB3, BlockStateNames::STONE_SLAB_TYPE_3, $typeValue);
241 }
242
243 public static function encodeStoneSlab4(Slab $block, string $typeValue) : Writer{
244 return self::encodeStoneSlab($block, Ids::STONE_BLOCK_SLAB4, Ids::DOUBLE_STONE_BLOCK_SLAB4, BlockStateNames::STONE_SLAB_TYPE_4, $typeValue);
245 }
246
247 public static function encodeTrapdoor(Trapdoor $block, Writer $out) : Writer{
248 return $out
249 ->write5MinusHorizontalFacing($block->getFacing())
250 ->writeBool(BlockStateNames::UPSIDE_DOWN_BIT, $block->isTop())
251 ->writeBool(BlockStateNames::OPEN_BIT, $block->isOpen());
252 }
253
254 public static function encodeWall(Wall $block, Writer $out) : Writer{
255 return $out
256 ->writeBool(BlockStateNames::WALL_POST_BIT, $block->isPost())
257 ->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_EAST, $block->getConnection(Facing::EAST))
258 ->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_NORTH, $block->getConnection(Facing::NORTH))
259 ->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_SOUTH, $block->getConnection(Facing::SOUTH))
260 ->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_WEST, $block->getConnection(Facing::WEST));
261 }
262
263 public static function encodeLegacyWall(Wall $block, string $type) : Writer{
264 return self::encodeWall($block, Writer::create(Ids::COBBLESTONE_WALL))
265 ->writeString(BlockStateNames::WALL_BLOCK_TYPE, $type);
266 }
267
268 public static function encodeWallSign(WallSign $block, Writer $out) : Writer{
269 return $out
270 ->writeHorizontalFacing($block->getFacing());
271 }
272}
static opposite(int $direction)
Definition: Facing.php:108
static rotateY(int $direction, bool $clockwise)
Definition: Facing.php:132