PocketMine-MP 5.32.2 git-1ebd7d3960d713d56f77f610fe0c15cdee201069
Loading...
Searching...
No Matches
CommonProperties.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\property;
25
43use pocketmine\block\utils\CopperOxidation;
45use pocketmine\block\utils\CoralType;
46use pocketmine\block\utils\DyeColor;
52use pocketmine\block\utils\SlabType;
59use pocketmine\utils\SingletonTrait;
60
61final class CommonProperties{
62 use SingletonTrait;
63
70
79
82
85
88
91
93 public readonly IntProperty $cropAgeMax7;
96
98 public readonly IntProperty $liquidData;
99
101 public readonly BoolProperty $lit;
102
103 public readonly DummyProperty $dummyCardinalDirection;
104 public readonly DummyProperty $dummyPillarAxis;
105
108
111
116
121 public readonly array $coralIdPrefixes;
126 public readonly array $copperIdPrefixes;
127
132 public readonly array $furnaceIdPrefixes;
133
138 public readonly array $liquidIdPrefixes;
139
144 public readonly array $woodIdPrefixes;
145
150 public readonly array $buttonProperties;
151
156 public readonly array $campfireProperties;
157
162 public readonly array $doorProperties;
163
168 public readonly array $fenceGateProperties;
169
174 public readonly array $itemFrameProperties;
175
180 public readonly array $simplePressurePlateProperties;
181
186 public readonly array $stairProperties;
187
192 public readonly array $stemProperties;
193
198 public readonly array $trapdoorProperties;
199
204 public readonly array $wallProperties;
205
206 private function __construct(){
207 $vm = ValueMappings::getInstance();
208
209 $hfGet = fn(HorizontalFacing $v) => $v->getFacing();
210 $hfSet = fn(HorizontalFacing $v, int $facing) => $v->setFacing($facing);
211 $this->horizontalFacingCardinal = new ValueFromStringProperty(StateNames::MC_CARDINAL_DIRECTION, $vm->cardinalDirection, $hfGet, $hfSet);
212
213 $this->blockFace = new ValueFromStringProperty(
214 StateNames::MC_BLOCK_FACE,
215 $vm->blockFace,
216 fn(AnyFacing $b) => $b->getFacing(),
217 fn(AnyFacing $b, int $v) => $b->setFacing($v)
218 );
219
220 $this->pillarAxis = new ValueFromStringProperty(
221 StateNames::PILLAR_AXIS,
222 $vm->pillarAxis,
223 fn(PillarRotation $b) => $b->getAxis(),
224 fn(PillarRotation $b, int $v) => $b->setAxis($v)
225 );
226
227 $this->torchFacing = new ValueFromStringProperty(
228 StateNames::TORCH_FACING_DIRECTION,
229 $vm->torchFacing,
230 fn(Torch $b) => $b->getFacing(),
231 fn(Torch $b, int $v) => $b->setFacing($v)
232 );
233
234 $this->horizontalFacingSWNE = new ValueFromIntProperty(StateNames::DIRECTION, $vm->horizontalFacingSWNE, $hfGet, $hfSet);
235 $this->horizontalFacingSWNEInverted = new ValueFromIntProperty(StateNames::DIRECTION, $vm->horizontalFacingSWNEInverted, $hfGet, $hfSet);
236 $this->horizontalFacingClassic = new ValueFromIntProperty(StateNames::FACING_DIRECTION, $vm->horizontalFacingClassic, $hfGet, $hfSet);
237
238 $this->anyFacingClassic = new ValueFromIntProperty(
239 StateNames::FACING_DIRECTION,
240 $vm->facing,
241 fn(AnyFacing $b) => $b->getFacing(),
242 fn(AnyFacing $b, int $v) => $b->setFacing($v)
243 );
244
245 $this->multiFacingFlags = new OptionSetFromIntProperty(
246 StateNames::MULTI_FACE_DIRECTION_BITS,
248 Facing::DOWN => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_DOWN,
249 Facing::UP => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_UP,
250 Facing::NORTH => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_NORTH,
251 Facing::SOUTH => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_SOUTH,
252 Facing::WEST => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_WEST,
253 Facing::EAST => BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_EAST
254 ]),
255 fn(MultiFacing $b) => $b->getFaces(),
256 fn(MultiFacing $b, array $v) => $b->setFaces($v)
257 );
258
259 $this->floorSignLikeRotation = new IntProperty(StateNames::GROUND_SIGN_DIRECTION, 0, 15, fn(SignLikeRotation $b) => $b->getRotation(), fn(SignLikeRotation $b, int $v) => $b->setRotation($v));
260
261 $this->analogRedstoneSignal = new IntProperty(StateNames::REDSTONE_SIGNAL, 0, 15, fn(AnalogRedstoneSignalEmitter $b) => $b->getOutputSignalStrength(), fn(AnalogRedstoneSignalEmitter $b, int $v) => $b->setOutputSignalStrength($v));
262
263 $this->cropAgeMax7 = new IntProperty(StateNames::GROWTH, 0, 7, fn(Ageable $b) => $b->getAge(), fn(Ageable $b, int $v) => $b->setAge($v));
264 $this->doublePlantHalf = new BoolProperty(StateNames::UPPER_BLOCK_BIT, fn(DoublePlant $b) => $b->isTop(), fn(DoublePlant $b, bool $v) => $b->setTop($v));
265
266 $fallingFlag = BlockLegacyMetadata::LIQUID_FALLING_FLAG;
267 $this->liquidData = new IntProperty(
268 StateNames::LIQUID_DEPTH,
269 0,
270 15,
271 fn(Liquid $b) => $b->getDecay() | ($b->isFalling() ? $fallingFlag : 0),
272 fn(Liquid $b, int $v) => $b->setDecay($v & ~$fallingFlag)->setFalling(($v & $fallingFlag) !== 0)
273 );
274
275 $this->lit = new BoolProperty(StateNames::LIT, fn(Lightable $b) => $b->isLit(), fn(Lightable $b, bool $v) => $b->setLit($v));
276
277 $this->dummyCardinalDirection = new DummyProperty(StateNames::MC_CARDINAL_DIRECTION, BlockStateStringValues::MC_CARDINAL_DIRECTION_SOUTH);
278 $this->dummyPillarAxis = new DummyProperty(StateNames::PILLAR_AXIS, BlockStateStringValues::PILLAR_AXIS_Y);
279
280 $this->dyeColorIdInfix = new ValueFromStringProperty("color", $vm->dyeColor, fn(Colored $b) => $b->getColor(), fn(Colored $b, DyeColor $v) => $b->setColor($v));
281 $this->litIdInfix = new BoolFromStringProperty("lit", "", "lit_", fn(Lightable $b) => $b->isLit(), fn(Lightable $b, bool $v) => $b->setLit($v));
282
283 $this->slabIdInfix = new BoolFromStringProperty(
284 "double",
285 "",
286 "double_",
287 fn(Slab $b) => $b->getSlabType() === SlabType::DOUBLE,
288
289 //we don't know this is actually a bottom slab yet but we don't have enough information to set the
290 //correct type in this handler
291 //BOTTOM serves as a signal value for the state deserializer to decide whether to ignore the
292 //upper_block_bit property
293 fn(Slab $b, bool $v) => $b->setSlabType($v ? SlabType::DOUBLE : SlabType::BOTTOM)
294 );
295 $this->slabPositionProperty = new BoolFromStringProperty(
296 StateNames::MC_VERTICAL_HALF,
297 BlockStateStringValues::MC_VERTICAL_HALF_BOTTOM,
298 BlockStateStringValues::MC_VERTICAL_HALF_TOP,
299 fn(Slab $b) => $b->getSlabType() === SlabType::TOP,
300
301 //Ignore the value for double slabs (should be set by ID component before this is reached)
302 fn(Slab $b, bool $v) => $b->getSlabType() !== SlabType::DOUBLE ? $b->setSlabType($v ? SlabType::TOP : SlabType::BOTTOM) : null
303 );
304
305 $this->coralIdPrefixes = [
306 "minecraft:",
307 new BoolFromStringProperty("dead", "", "dead_", fn(CoralMaterial $b) => $b->isDead(), fn(CoralMaterial $b, bool $v) => $b->setDead($v)),
308 new ValueFromStringProperty("type", EnumFromRawStateMap::string(CoralType::class, fn(CoralType $case) => match ($case) {
309 CoralType::BRAIN => "brain",
310 CoralType::BUBBLE => "bubble",
311 CoralType::FIRE => "fire",
312 CoralType::HORN => "horn",
313 CoralType::TUBE => "tube"
314 }), fn(CoralMaterial $b) => $b->getCoralType(), fn(CoralMaterial $b, CoralType $v) => $b->setCoralType($v)),
315 ];
316 $this->copperIdPrefixes = [
317 "minecraft:",
318 new BoolFromStringProperty("waxed", "", "waxed_", fn(CopperMaterial $b) => $b->isWaxed(), fn(CopperMaterial $b, bool $v) => $b->setWaxed($v)),
319 new ValueFromStringProperty("oxidation", EnumFromRawStateMap::string(CopperOxidation::class, fn(CopperOxidation $case) => match ($case) {
320 CopperOxidation::NONE => "",
321 CopperOxidation::EXPOSED => "exposed_",
322 CopperOxidation::WEATHERED => "weathered_",
323 CopperOxidation::OXIDIZED => "oxidized_",
324 }), fn(CopperMaterial $b) => $b->getOxidation(), fn(CopperMaterial $b, CopperOxidation $v) => $b->setOxidation($v))
325 ];
326
327 $this->furnaceIdPrefixes = ["minecraft:", $this->litIdInfix];
328
329 $this->liquidIdPrefixes = [
330 "minecraft:",
331 new BoolFromStringProperty("still", "flowing_", "", fn(Liquid $b) => $b->isStill(), fn(Liquid $b, bool $v) => $b->setStill($v))
332 ];
333
334 $this->woodIdPrefixes = [
335 "minecraft:",
336 new BoolFromStringProperty("stripped", "", "stripped_", fn(Wood $b) => $b->isStripped(), fn(Wood $b, bool $v) => $b->setStripped($v)),
337 ];
338
339 $this->buttonProperties = [
341 new BoolProperty(StateNames::BUTTON_PRESSED_BIT, fn(Button $b) => $b->isPressed(), fn(Button $b, bool $v) => $b->setPressed($v)),
342 ];
343
344 $this->campfireProperties = [
346 new BoolProperty(StateNames::EXTINGUISHED, fn(Lightable $b) => $b->isLit(), fn(Lightable $b, bool $v) => $b->setLit($v), inverted: true),
347 ];
348
349 //TODO: check if these need any special treatment to get the appropriate data to both halves of the door
350 $this->doorProperties = [
351 new BoolProperty(StateNames::UPPER_BLOCK_BIT, fn(Door $b) => $b->isTop(), fn(Door $b, bool $v) => $b->setTop($v)),
352 new BoolProperty(StateNames::DOOR_HINGE_BIT, fn(Door $b) => $b->isHingeRight(), fn(Door $b, bool $v) => $b->setHingeRight($v)),
353 new BoolProperty(StateNames::OPEN_BIT, fn(Door $b) => $b->isOpen(), fn(Door $b, bool $v) => $b->setOpen($v)),
355 StateNames::MC_CARDINAL_DIRECTION,
357 //a door facing "east" is actually facing north - thanks mojang
358 Facing::NORTH => BlockStateStringValues::MC_CARDINAL_DIRECTION_EAST,
359 Facing::EAST => BlockStateStringValues::MC_CARDINAL_DIRECTION_SOUTH,
360 Facing::SOUTH => BlockStateStringValues::MC_CARDINAL_DIRECTION_WEST,
361 Facing::WEST => BlockStateStringValues::MC_CARDINAL_DIRECTION_NORTH
362 ]),
363 fn(HorizontalFacing $b) => $b->getFacing(),
364 fn(HorizontalFacing $b, int $v) => $b->setFacing($v)
365 )
366 ];
367
368 $this->fenceGateProperties = [
369 new BoolProperty(StateNames::IN_WALL_BIT, fn(FenceGate $b) => $b->isInWall(), fn(FenceGate $b, bool $v) => $b->setInWall($v)),
370 new BoolProperty(StateNames::OPEN_BIT, fn(FenceGate $b) => $b->isOpen(), fn(FenceGate $b, bool $v) => $b->setOpen($v)),
372 ];
373
374 $this->itemFrameProperties = [
375 new DummyProperty(StateNames::ITEM_FRAME_PHOTO_BIT, false), //TODO: not sure what the point of this is
376 new BoolProperty(StateNames::ITEM_FRAME_MAP_BIT, fn(ItemFrame $b) => $b->hasMap(), fn(ItemFrame $b, bool $v) => $b->setHasMap($v)),
378 ];
379
380 $this->simplePressurePlateProperties = [
381 //TODO: not sure what the deal is here ... seems like a mojang bug / artifact of bad implementation?
382 //best to keep this separate from weighted plates anyway...
383 new IntProperty(
384 StateNames::REDSTONE_SIGNAL,
385 0,
386 15,
387 fn(SimplePressurePlate $b) => $b->isPressed() ? 15 : 0,
388 fn(SimplePressurePlate $b, int $v) => $b->setPressed($v !== 0)
389 )
390 ];
391
392 $this->stairProperties = [
393 new BoolProperty(StateNames::UPSIDE_DOWN_BIT, fn(Stair $b) => $b->isUpsideDown(), fn(Stair $b, bool $v) => $b->setUpsideDown($v)),
394 new ValueFromIntProperty(StateNames::WEIRDO_DIRECTION, $vm->horizontalFacing5Minus, $hfGet, $hfSet),
395 ];
396
397 $this->stemProperties = [
398 new ValueFromIntProperty(StateNames::FACING_DIRECTION, $vm->facingStem, fn(Stem $b) => $b->getFacing(), fn(Stem $b, int $v) => $b->setFacing($v)),
400 ];
401
402 $this->trapdoorProperties = [
403 //this uses the same values as stairs, but the state is named differently
404 new ValueFromIntProperty(StateNames::DIRECTION, $vm->horizontalFacing5Minus, $hfGet, $hfSet),
405
406 new BoolProperty(StateNames::UPSIDE_DOWN_BIT, fn(Trapdoor $b) => $b->isTop(), fn(Trapdoor $b, bool $v) => $b->setTop($v)),
407 new BoolProperty(StateNames::OPEN_BIT, fn(Trapdoor $b) => $b->isOpen(), fn(Trapdoor $b, bool $v) => $b->setOpen($v)),
408 ];
409
410 $wallProperties = [
411 new BoolProperty(StateNames::WALL_POST_BIT, fn(Wall $b) => $b->isPost(), fn(Wall $b, bool $v) => $b->setPost($v)),
412 ];
413 foreach([
414 Facing::NORTH => StateNames::WALL_CONNECTION_TYPE_NORTH,
415 Facing::SOUTH => StateNames::WALL_CONNECTION_TYPE_SOUTH,
416 Facing::WEST => StateNames::WALL_CONNECTION_TYPE_WEST,
417 Facing::EAST => StateNames::WALL_CONNECTION_TYPE_EAST
418 ] as $facing => $stateName){
419 $wallProperties[] = new ValueFromStringProperty(
420 $stateName,
421 EnumFromRawStateMap::string(WallConnectionTypeShim::class, fn(WallConnectionTypeShim $case) => $case->getValue()),
422 fn(Wall $b) => WallConnectionTypeShim::serialize($b->getConnection($facing)),
423 fn(Wall $b, WallConnectionTypeShim $v) => $b->setConnection($facing, $v->deserialize())
424 );
425 }
426 $this->wallProperties = $wallProperties;
427 }
428}
setConnection(int $face, ?WallConnectionType $type)
Definition Wall.php:71
static string(string $class, \Closure $mapper, ?\Closure $aliasMapper=null)
static int(array $serializeMap, array $deserializeAliases=[])
static string(array $serializeMap, array $deserializeAliases=[])