diff --git a/src/block/BaseBanner.php b/src/block/BaseBanner.php index fd2b225bf..d5f859f7c 100644 --- a/src/block/BaseBanner.php +++ b/src/block/BaseBanner.php @@ -53,13 +53,15 @@ abstract class BaseBanner extends Transparent{ parent::__construct($idInfo, $name, $breakInfo); } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $tile = $this->position->getWorld()->getTile($this->position); if($tile instanceof TileBanner){ $this->color = $tile->getBaseColor(); $this->setPatterns($tile->getPatterns()); } + + return $this; } public function writeStateToWorld() : void{ diff --git a/src/block/BaseSign.php b/src/block/BaseSign.php index 3061030a8..7ce7a829f 100644 --- a/src/block/BaseSign.php +++ b/src/block/BaseSign.php @@ -58,13 +58,15 @@ abstract class BaseSign extends Transparent{ $this->asItemCallback = $asItemCallback; } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $tile = $this->position->getWorld()->getTile($this->position); if($tile instanceof TileSign){ $this->text = $tile->getText(); $this->editorEntityRuntimeId = $tile->getEditorEntityRuntimeId(); } + + return $this; } public function writeStateToWorld() : void{ diff --git a/src/block/Bed.php b/src/block/Bed.php index 4bf4dac23..258f9ce06 100644 --- a/src/block/Bed.php +++ b/src/block/Bed.php @@ -68,13 +68,15 @@ class Bed extends Transparent{ $w->writeBool($this->head); } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); //read extra state information from the tile - this is an ugly hack $tile = $this->position->getWorld()->getTile($this->position); if($tile instanceof TileBed){ $this->color = $tile->getColor(); } + + return $this; } public function writeStateToWorld() : void{ diff --git a/src/block/Block.php b/src/block/Block.php index d521cb522..383b28797 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -189,9 +189,14 @@ class Block{ * * Clears any cached precomputed objects, such as bounding boxes. Remove any outdated precomputed things such as * AABBs and force recalculation. + * + * A replacement block may be returned. This is useful if the block type changed due to reading of world data (e.g. + * data from a block entity). */ - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ $this->collisionBoxes = null; + + return $this; } public function writeStateToWorld() : void{ diff --git a/src/block/Door.php b/src/block/Door.php index d5497bb7c..9f99de849 100644 --- a/src/block/Door.php +++ b/src/block/Door.php @@ -58,7 +58,7 @@ class Door extends Transparent{ $w->writeBool($this->open); } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); //copy door properties from other half @@ -71,6 +71,8 @@ class Door extends Transparent{ $this->hingeRight = $other->hingeRight; } } + + return $this; } public function isTop() : bool{ return $this->top; } diff --git a/src/block/Fence.php b/src/block/Fence.php index 08c9b5eac..7180b38ea 100644 --- a/src/block/Fence.php +++ b/src/block/Fence.php @@ -37,7 +37,7 @@ class Fence extends Transparent{ return 0.25; } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); foreach(Facing::HORIZONTAL as $facing){ @@ -48,6 +48,8 @@ class Fence extends Transparent{ unset($this->connections[$facing]); } } + + return $this; } /** diff --git a/src/block/FlowerPot.php b/src/block/FlowerPot.php index 6099539a0..f2cafe15e 100644 --- a/src/block/FlowerPot.php +++ b/src/block/FlowerPot.php @@ -36,7 +36,7 @@ class FlowerPot extends Flowable{ protected ?Block $plant = null; - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $tile = $this->position->getWorld()->getTile($this->position); if($tile instanceof TileFlowerPot){ @@ -44,6 +44,8 @@ class FlowerPot extends Flowable{ }else{ $this->setPlant(null); } + + return $this; } public function writeStateToWorld() : void{ diff --git a/src/block/ItemFrame.php b/src/block/ItemFrame.php index ec6d77880..8785fbd54 100644 --- a/src/block/ItemFrame.php +++ b/src/block/ItemFrame.php @@ -71,7 +71,7 @@ class ItemFrame extends Flowable{ $w->writeBool($this->hasMap); } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $tile = $this->position->getWorld()->getTile($this->position); if($tile instanceof TileItemFrame){ @@ -82,6 +82,8 @@ class ItemFrame extends Flowable{ $this->itemRotation = $tile->getItemRotation() % self::ROTATIONS; $this->itemDropChance = $tile->getItemDropChance(); } + + return $this; } public function writeStateToWorld() : void{ diff --git a/src/block/Jukebox.php b/src/block/Jukebox.php index 3d5f340b3..811148f74 100644 --- a/src/block/Jukebox.php +++ b/src/block/Jukebox.php @@ -96,12 +96,14 @@ class Jukebox extends Opaque{ return $drops; } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $jukebox = $this->position->getWorld()->getTile($this->position); if($jukebox instanceof JukeboxTile){ $this->record = $jukebox->getRecord(); } + + return $this; } public function writeStateToWorld() : void{ diff --git a/src/block/Lectern.php b/src/block/Lectern.php index cb5a9d9fe..23c971ae2 100644 --- a/src/block/Lectern.php +++ b/src/block/Lectern.php @@ -59,13 +59,15 @@ class Lectern extends Transparent{ $w->writeBool($this->producingSignal); } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $tile = $this->position->getWorld()->getTile($this->position); if($tile instanceof TileLectern){ $this->viewedPage = $tile->getViewedPage(); $this->book = $tile->getBook(); } + + return $this; } public function writeStateToWorld() : void{ diff --git a/src/block/Liquid.php b/src/block/Liquid.php index 81f34e6f6..e1bafbcbc 100644 --- a/src/block/Liquid.php +++ b/src/block/Liquid.php @@ -160,9 +160,11 @@ abstract class Liquid extends Transparent{ return $block->falling ? 0 : $block->decay; } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $this->flowVector = null; + + return $this; } public function getFlowVector() : Vector3{ diff --git a/src/block/Note.php b/src/block/Note.php index 8417ff8e8..b67891b87 100644 --- a/src/block/Note.php +++ b/src/block/Note.php @@ -32,7 +32,7 @@ class Note extends Opaque{ private int $pitch = self::MIN_PITCH; - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $tile = $this->position->getWorld()->getTile($this->position); if($tile instanceof TileNote){ @@ -40,6 +40,8 @@ class Note extends Opaque{ }else{ $this->pitch = self::MIN_PITCH; } + + return $this; } public function writeStateToWorld() : void{ diff --git a/src/block/RedstoneComparator.php b/src/block/RedstoneComparator.php index cd2004460..aeaaeb543 100644 --- a/src/block/RedstoneComparator.php +++ b/src/block/RedstoneComparator.php @@ -60,12 +60,14 @@ class RedstoneComparator extends Flowable{ $w->writeBool($this->powered); } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $tile = $this->position->getWorld()->getTile($this->position); if($tile instanceof Comparator){ $this->signalStrength = $tile->getSignalStrength(); } + + return $this; } public function writeStateToWorld() : void{ diff --git a/src/block/RedstoneWire.php b/src/block/RedstoneWire.php index 7c770e9c4..022672b5d 100644 --- a/src/block/RedstoneWire.php +++ b/src/block/RedstoneWire.php @@ -41,9 +41,11 @@ class RedstoneWire extends Flowable{ return false; } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); //TODO: check connections to nearby redstone components + + return $this; } public function onNearbyBlockChange() : void{ diff --git a/src/block/ShulkerBox.php b/src/block/ShulkerBox.php index 1cc994ef2..b5b2223d8 100644 --- a/src/block/ShulkerBox.php +++ b/src/block/ShulkerBox.php @@ -53,12 +53,14 @@ class ShulkerBox extends Opaque{ } } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $shulker = $this->position->getWorld()->getTile($this->position); if($shulker instanceof TileShulkerBox){ $this->facing = $shulker->getFacing(); } + + return $this; } public function getMaxStackSize() : int{ diff --git a/src/block/Skull.php b/src/block/Skull.php index 4dbdfb823..33da1da9b 100644 --- a/src/block/Skull.php +++ b/src/block/Skull.php @@ -77,13 +77,15 @@ class Skull extends Flowable{ $w->writeFacing($this->facing); } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $tile = $this->position->getWorld()->getTile($this->position); if($tile instanceof TileSkull){ $this->skullType = $tile->getSkullType(); $this->rotation = $tile->getRotation(); } + + return $this; } public function writeStateToWorld() : void{ diff --git a/src/block/Stair.php b/src/block/Stair.php index 67a3d7b40..5a585277b 100644 --- a/src/block/Stair.php +++ b/src/block/Stair.php @@ -59,7 +59,7 @@ class Stair extends Transparent{ $w->writeBool($this->upsideDown); } - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); $clockwise = Facing::rotateY($this->facing, true); @@ -70,6 +70,8 @@ class Stair extends Transparent{ }else{ $this->shape = StairShape::STRAIGHT(); } + + return $this; } public function isUpsideDown() : bool{ return $this->upsideDown; } diff --git a/src/block/Thin.php b/src/block/Thin.php index ad5824494..28afef3da 100644 --- a/src/block/Thin.php +++ b/src/block/Thin.php @@ -33,7 +33,7 @@ class Thin extends Transparent{ /** @var bool[] facing => dummy */ protected array $connections = []; - public function readStateFromWorld() : void{ + public function readStateFromWorld() : Block{ parent::readStateFromWorld(); foreach(Facing::HORIZONTAL as $facing){ @@ -44,6 +44,8 @@ class Thin extends Transparent{ unset($this->connections[$facing]); } } + + return $this; } protected function recalculateCollisionBoxes() : array{ diff --git a/src/world/World.php b/src/world/World.php index 3f905e962..7d5366b0b 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -824,7 +824,11 @@ class World implements ChunkManager{ } $block = $this->getBlockAt($x, $y, $z); - $block->readStateFromWorld(); //for blocks like fences, force recalculation of connected AABBs + $replacement = $block->readStateFromWorld(); //for blocks like fences, force recalculation of connected AABBs + if($replacement !== $block){ + $replacement->position($this, $x, $y, $z); + $block = $replacement; + } $ev = new BlockUpdateEvent($block); $ev->call(); @@ -1548,7 +1552,11 @@ class World implements ChunkManager{ $addToCache = false; }else{ $dynamicStateRead = true; - $block->readStateFromWorld(); + $replacement = $block->readStateFromWorld(); + if($replacement !== $block){ + $replacement->position($this, $x, $y, $z); + $block = $replacement; + } $dynamicStateRead = false; }