PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
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, Writer $out) : Writer{
72 return $out
73 ->writeLegacyHorizontalFacing(Facing::opposite($block->getFacing()));
74 }
75
76 public static function encodeCrops(Crops $block, Writer $out) : Writer{
77 return $out->writeInt(BlockStateNames::GROWTH, $block->getAge());
78 }
79
80 public static function encodeTorch(Torch $block, Writer $out) : Writer{
81 return $out
82 ->writeTorchFacing($block->getFacing());
83 }
84
85 public static function encodeCauldron(string $liquid, int $fillLevel) : Writer{
86 return Writer::create(Ids::CAULDRON)
87 ->writeString(BlockStateNames::CAULDRON_LIQUID, $liquid)
88 ->writeInt(BlockStateNames::FILL_LEVEL, $fillLevel);
89 }
90
91 public static function selectCopperId(CopperOxidation $oxidation, string $noneId, string $exposedId, string $weatheredId, string $oxidizedId) : string{
92 return match($oxidation){
93 CopperOxidation::NONE => $noneId,
94 CopperOxidation::EXPOSED => $exposedId,
95 CopperOxidation::WEATHERED => $weatheredId,
96 CopperOxidation::OXIDIZED => $oxidizedId,
97 };
98 }
99
100 public static function encodeDoor(Door $block, Writer $out) : Writer{
101 return $out
102 ->writeBool(BlockStateNames::UPPER_BLOCK_BIT, $block->isTop())
103 ->writeLegacyHorizontalFacing(Facing::rotateY($block->getFacing(), true))
104 ->writeBool(BlockStateNames::DOOR_HINGE_BIT, $block->isHingeRight())
105 ->writeBool(BlockStateNames::OPEN_BIT, $block->isOpen());
106 }
107
108 public static function encodeDoublePlant(DoublePlant $block, Writer $out) : Writer{
109 return $out
110 ->writeBool(BlockStateNames::UPPER_BLOCK_BIT, $block->isTop());
111 }
112
113 public static function encodeFenceGate(FenceGate $block, Writer $out) : Writer{
114 return $out
115 ->writeLegacyHorizontalFacing($block->getFacing())
116 ->writeBool(BlockStateNames::IN_WALL_BIT, $block->isInWall())
117 ->writeBool(BlockStateNames::OPEN_BIT, $block->isOpen());
118 }
119
120 public static function encodeFloorSign(FloorSign $block, Writer $out) : Writer{
121 return $out
122 ->writeInt(BlockStateNames::GROUND_SIGN_DIRECTION, $block->getRotation());
123 }
124
125 public static function encodeFurnace(Furnace $block, string $unlitId, string $litId) : Writer{
126 return Writer::create($block->isLit() ? $litId : $unlitId)
127 ->writeCardinalHorizontalFacing($block->getFacing());
128 }
129
130 public static function encodeItemFrame(ItemFrame $block, string $id) : Writer{
131 return Writer::create($id)
132 ->writeBool(StateNames::ITEM_FRAME_MAP_BIT, $block->hasMap())
133 ->writeBool(StateNames::ITEM_FRAME_PHOTO_BIT, false)
134 ->writeFacingDirection($block->getFacing());
135 }
136
137 public static function encodeLeaves(Leaves $block, Writer $out) : Writer{
138 return $out
139 ->writeBool(BlockStateNames::PERSISTENT_BIT, $block->isNoDecay())
140 ->writeBool(BlockStateNames::UPDATE_BIT, $block->isCheckDecay());
141 }
142
143 public static function encodeLiquid(Liquid $block, string $stillId, string $flowingId) : Writer{
144 return Writer::create($block->isStill() ? $stillId : $flowingId)
145 ->writeInt(BlockStateNames::LIQUID_DEPTH, $block->getDecay() | ($block->isFalling() ? 0x8 : 0));
146 }
147
148 public static function encodeLog(Wood $block, string $unstrippedId, string $strippedId) : Writer{
149 $out = $block->isStripped() ?
150 Writer::create($strippedId) :
151 Writer::create($unstrippedId);
152 return $out
153 ->writePillarAxis($block->getAxis());
154 }
155
156 public static function encodeMushroomBlock(RedMushroomBlock $block, Writer $out) : Writer{
157 return $out
158 ->writeInt(BlockStateNames::HUGE_MUSHROOM_BITS, MushroomBlockTypeIdMap::getInstance()->toId($block->getMushroomBlockType()));
159 }
160
161 public static function encodeQuartz(int $axis, Writer $out) : Writer{
162 return $out
163 ->writePillarAxis($axis); //this isn't needed for all types, but we have to write it anyway
164 }
165
166 public static function encodeSapling(Sapling $block, Writer $out) : Writer{
167 return $out
168 ->writeBool(BlockStateNames::AGE_BIT, $block->isReady());
169 }
170
171 public static function encodeSimplePressurePlate(SimplePressurePlate $block, Writer $out) : Writer{
172 //TODO: not sure what the deal is here ... seems like a mojang bug / artifact of bad implementation?
173 //best to keep this separate from weighted plates anyway...
174 return $out
175 ->writeInt(BlockStateNames::REDSTONE_SIGNAL, $block->isPressed() ? 15 : 0);
176 }
177
178 private static function encodeSingleSlab(Slab $block, string $id) : Writer{
179 return Writer::create($id)
180 ->writeSlabPosition($block->getSlabType());
181 }
182
183 private static function encodeDoubleSlab(Slab $block, string $id) : Writer{
184 return Writer::create($id)
185 //this is (intentionally) also written for double slabs (as zero) to maintain bug parity with MCPE
186 ->writeSlabPosition(SlabType::BOTTOM);
187 }
188
189 public static function encodeSlab(Slab $block, string $singleId, string $doubleId) : Writer{
190 return $block->getSlabType() === SlabType::DOUBLE ?
191 self::encodeDoubleSlab($block, $doubleId) :
192 self::encodeSingleSlab($block, $singleId);
193 }
194
195 public static function encodeStairs(Stair $block, Writer $out) : Writer{
196 return $out
197 ->writeBool(BlockStateNames::UPSIDE_DOWN_BIT, $block->isUpsideDown())
198 ->writeWeirdoHorizontalFacing($block->getFacing());
199 }
200
201 public static function encodeStem(Stem $block, Writer $out) : Writer{
202 //In PM, we use Facing::UP to indicate that the stem is not attached to a pumpkin/melon, since this makes the
203 //most intuitive sense (the stem is pointing at the sky). However, Bedrock uses the DOWN state for this, which
204 //is absurd, and I refuse to make our API similarly absurd.
205 $facing = $block->getFacing();
206 return self::encodeCrops($block, $out)
207 ->writeFacingWithoutUp($facing === Facing::UP ? Facing::DOWN : $facing);
208 }
209
210 public static function encodeTrapdoor(Trapdoor $block, Writer $out) : Writer{
211 return $out
212 ->write5MinusHorizontalFacing($block->getFacing())
213 ->writeBool(BlockStateNames::UPSIDE_DOWN_BIT, $block->isTop())
214 ->writeBool(BlockStateNames::OPEN_BIT, $block->isOpen());
215 }
216
217 public static function encodeWall(Wall $block, Writer $out) : Writer{
218 return $out
219 ->writeBool(BlockStateNames::WALL_POST_BIT, $block->isPost())
220 ->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_EAST, $block->getConnection(Facing::EAST))
221 ->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_NORTH, $block->getConnection(Facing::NORTH))
222 ->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_SOUTH, $block->getConnection(Facing::SOUTH))
223 ->writeWallConnectionType(BlockStateNames::WALL_CONNECTION_TYPE_WEST, $block->getConnection(Facing::WEST));
224 }
225
226 public static function encodeWallSign(WallSign $block, Writer $out) : Writer{
227 return $out
228 ->writeHorizontalFacing($block->getFacing());
229 }
230}