22declare(strict_types=1);
24namespace pocketmine\data\bedrock\block\convert;
46use pocketmine\block\utils\CopperOxidation;
47use pocketmine\block\utils\SlabType;
59 public static function encodeButton(
Button $block, Writer $out) : Writer{
61 ->writeFacingDirection($block->getFacing())
62 ->writeBool(BlockStateNames::BUTTON_PRESSED_BIT, $block->isPressed());
65 public static function encodeCandle(
Candle $block, Writer $out) : Writer{
67 ->writeBool(StateNames::LIT, $block->isLit())
68 ->writeInt(StateNames::CANDLES, $block->getCount() - 1);
71 public static function encodeChemistryTable(
ChemistryTable $block,
string $chemistryTableType, Writer $out) : Writer{
73 ->writeString(BlockStateNames::CHEMISTRY_TABLE_TYPE, $chemistryTableType)
77 public static function encodeCrops(
Crops $block, Writer $out) : Writer{
78 return $out->writeInt(BlockStateNames::GROWTH, $block->getAge());
81 public static function encodeColoredTorch(
Torch $block,
bool $highBit, Writer $out) : Writer{
83 ->writeBool(BlockStateNames::COLOR_BIT, $highBit)
84 ->writeTorchFacing($block->getFacing());
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);
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,
102 public static function encodeDoor(
Door $block, Writer $out) : Writer{
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());
110 public static function encodeDoublePlant(
DoublePlant $block, Writer $out) : Writer{
112 ->writeBool(BlockStateNames::UPPER_BLOCK_BIT, $block->isTop());
115 public static function encodeFenceGate(
FenceGate $block, Writer $out) : Writer{
117 ->writeLegacyHorizontalFacing($block->getFacing())
118 ->writeBool(BlockStateNames::IN_WALL_BIT, $block->isInWall())
119 ->writeBool(BlockStateNames::OPEN_BIT, $block->isOpen());
122 public static function encodeFloorSign(
FloorSign $block, Writer $out) : Writer{
124 ->writeInt(BlockStateNames::GROUND_SIGN_DIRECTION, $block->getRotation());
127 public static function encodeFurnace(
Furnace $block,
string $unlitId,
string $litId) : Writer{
128 return Writer::create($block->isLit() ? $litId : $unlitId)
129 ->writeCardinalHorizontalFacing($block->getFacing());
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());
139 public static function encodeLeaves(
Leaves $block, Writer $out) : Writer{
141 ->writeBool(BlockStateNames::PERSISTENT_BIT, $block->isNoDecay())
142 ->writeBool(BlockStateNames::UPDATE_BIT, $block->isCheckDecay());
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));
150 public static function encodeLog(
Wood $block,
string $unstrippedId,
string $strippedId) : Writer{
151 $out = $block->isStripped() ?
152 Writer::create($strippedId) :
153 Writer::create($unstrippedId);
155 ->writePillarAxis($block->getAxis());
158 public static function encodeMushroomBlock(
RedMushroomBlock $block, Writer $out) : Writer{
160 ->writeInt(BlockStateNames::HUGE_MUSHROOM_BITS, MushroomBlockTypeIdMap::getInstance()->toId($block->getMushroomBlockType()));
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);
169 public static function encodeSandstone(
string $id,
string $type) : Writer{
170 return Writer::create($id)->writeString(BlockStateNames::SAND_STONE_TYPE, $type);
173 public static function encodeSapling(
Sapling $block, Writer $out) : Writer{
175 ->writeBool(BlockStateNames::AGE_BIT, $block->isReady());
178 public static function encodeSimplePressurePlate(
SimplePressurePlate $block, Writer $out) : Writer{
182 ->writeInt(BlockStateNames::REDSTONE_SIGNAL, $block->isPressed() ? 15 : 0);
185 private static function encodeSingleSlab(
Slab $block,
string $id) : Writer{
186 return Writer::create($id)
187 ->writeSlabPosition($block->getSlabType());
190 private static function encodeDoubleSlab(
Slab $block,
string $id) : Writer{
191 return Writer::create($id)
193 ->writeSlabPosition(SlabType::BOTTOM);
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);
202 public static function encodeStairs(
Stair $block, Writer $out) : Writer{
204 ->writeBool(BlockStateNames::UPSIDE_DOWN_BIT, $block->isUpsideDown())
205 ->writeWeirdoHorizontalFacing($block->getFacing());
208 public static function encodeStem(
Stem $block, Writer $out) : Writer{
212 $facing = $block->getFacing();
213 return self::encodeCrops($block, $out)
214 ->writeFacingWithoutUp($facing === Facing::UP ? Facing::DOWN : $facing);
217 public static function encodeStoneBricks(
string $type) : Writer{
218 return Writer::create(Ids::STONEBRICK)
219 ->writeString(BlockStateNames::STONE_BRICK_TYPE, $type);
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);
227 public static function encodeStoneSlab1(
Slab $block,
string $singleId,
string $doubleTypeValue) : Writer{
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);
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);
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);
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);
247 public static function encodeTrapdoor(
Trapdoor $block, Writer $out) : Writer{
249 ->write5MinusHorizontalFacing($block->getFacing())
250 ->writeBool(BlockStateNames::UPSIDE_DOWN_BIT, $block->isTop())
251 ->writeBool(BlockStateNames::OPEN_BIT, $block->isOpen());
254 public static function encodeWall(
Wall $block, Writer $out) : Writer{
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));
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);
268 public static function encodeWallSign(
WallSign $block, Writer $out) : Writer{
270 ->writeHorizontalFacing($block->getFacing());
static opposite(int $direction)
static rotateY(int $direction, bool $clockwise)