From d1a724c143bb574eb542fb4f43c1ebc1885d464f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 23 Jun 2025 01:24:00 +0300 Subject: [PATCH 1/6] block light --- src/API/LevelAPI.php | 1 + src/Player.php | 1 + src/material/Block.php | 4 + src/material/StaticBlock.php | 5 + src/material/block/attachable/TorchBlock.php | 1 + src/material/block/liquid/LavaBlock.php | 2 + src/material/block/liquid/StillLavaBlock.php | 2 + src/material/block/liquid/StillWaterBlock.php | 1 + src/material/block/liquid/WaterBlock.php | 1 + src/material/block/misc/FireBlock.php | 1 + .../block/misc/NetherReactorBlock.php | 1 + src/material/block/misc/TNTBlock.php | 1 + src/material/block/nonfull/CobwebBlock.php | 1 + src/material/block/ore/CoalOreBlock.php | 1 + src/material/block/ore/DiamondOreBlock.php | 1 + .../block/ore/GlowingRedstoneOreBlock.php | 2 + src/material/block/ore/GoldOreBlock.php | 1 + src/material/block/ore/IronOreBlock.php | 1 + src/material/block/ore/LapisOreBlock.php | 1 + src/material/block/ore/RedstoneOreBlock.php | 1 + .../block/plant/BrownMushroomBlock.php | 1 + src/material/block/solid/BedrockBlock.php | 1 + src/material/block/solid/BookshelfBlock.php | 1 + src/material/block/solid/BricksBlock.php | 1 + .../block/solid/BurningFurnaceBlock.php | 2 + src/material/block/solid/ClayBlock.php | 1 + src/material/block/solid/CoalBlock.php | 1 + src/material/block/solid/CobblestoneBlock.php | 1 + src/material/block/solid/DiamondBlock.php | 1 + src/material/block/solid/DirtBlock.php | 1 + src/material/block/solid/DoubleSlabBlock.php | 1 + .../block/solid/DoubleWoodSlabBlock.php | 1 + src/material/block/solid/FurnaceBlock.php | 2 + .../block/solid/GlowingObsidianBlock.php | 2 + src/material/block/solid/GlowstoneBlock.php | 2 + src/material/block/solid/GoldBlock.php | 1 + src/material/block/solid/GrassBlock.php | 1 + src/material/block/solid/GravelBlock.php | 1 + src/material/block/solid/HayBaleBlock.php | 1 + src/material/block/solid/IceBlock.php | 1 + .../block/solid/InvisibleBedrockBlock.php | 1 + src/material/block/solid/IronBlock.php | 1 + src/material/block/solid/LapisBlock.php | 1 + src/material/block/solid/LeavesBlock.php | 1 + src/material/block/solid/LitPumpkinBlock.php | 2 + src/material/block/solid/MelonBlock.php | 1 + src/material/block/solid/MossStoneBlock.php | 1 + .../block/solid/NetherBricksBlock.php | 1 + src/material/block/solid/NetherrackBlock.php | 1 + src/material/block/solid/ObsidianBlock.php | 1 + src/material/block/solid/PlanksBlock.php | 1 + src/material/block/solid/PumpkinBlock.php | 1 + src/material/block/solid/QuartzBlock.php | 1 + src/material/block/solid/Reserved6Block.php | 1 + src/material/block/solid/SandBlock.php | 1 + src/material/block/solid/SandstoneBlock.php | 1 + src/material/block/solid/SnowBlock.php | 1 + src/material/block/solid/SpongeBlock.php | 1 + src/material/block/solid/StoneBlock.php | 1 + src/material/block/solid/StoneBricksBlock.php | 1 + src/material/block/solid/StonecutterBlock.php | 1 + src/material/block/solid/WoodBlock.php | 1 + src/material/block/solid/WoolBlock.php | 1 + src/material/block/solid/WorkbenchBlock.php | 1 + src/pmf/PMFLevel.php | 54 ++++++- src/world/Level.php | 134 ++++++++++++++++++ 66 files changed, 266 insertions(+), 1 deletion(-) diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index a4d77bd0..b9fcb543 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -18,6 +18,7 @@ class LevelAPI{ $this->server->api->console->register("save-on", "", [$this, "commandHandler"]); $this->server->api->console->register("save-off", "", [$this, "commandHandler"]); $this->server->api->console->register("setwspawn", "Set the spawn position for your current world. ", [$this, "commandHandler"]); + $this->default = $this->server->api->getProperty("level-name"); if($this->loadLevel($this->default) === false){ $this->generateLevel($this->default, $this->server->seed); diff --git a/src/Player.php b/src/Player.php index 24e62794..cf6fc528 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1737,6 +1737,7 @@ class Player{ } public function entityTick(){ + if($this->isSleeping) ++$this->sleepingTime; if($this->server->difficulty == 0 && $this->entity->counter % (20 * 15) == 0){ if($this->entity->health < 20 && $this->entity->health > 0){ diff --git a/src/material/Block.php b/src/material/Block.php index 49e1af41..21527a26 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -160,6 +160,10 @@ abstract class Block extends Position{ public $y = 0; public $z = 0; public $slipperiness; + + public $lightEmission = 0; + public $lightBlock = 0; + public static function interact(Level $level, $x, $y, $z, Player $player){} public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){} diff --git a/src/material/StaticBlock.php b/src/material/StaticBlock.php index 73082ecd..30080e17 100644 --- a/src/material/StaticBlock.php +++ b/src/material/StaticBlock.php @@ -22,10 +22,13 @@ class StaticBlock public static $destroyTime = []; public static $slipperiness = []; public static $boundingBoxes = []; + public static $lightBlock = []; + public static $lightEmission = []; public static $minXs = [], $minYs = [], $minZs = [], $maxXs = [], $maxYs = [], $maxZs = []; public static function init(){ self::$NULL_BOUNDS = new AxisAlignedBB(0, 0, 0, 0, 0, 0); + for($i = 0; $i < 256; ++$i) self::$lightBlock[$i] = self::$lightEmission[$i] = 0; foreach(Block::$class as $nonstaticname){ /**@var Block $b*/ $b = new $nonstaticname(); @@ -43,6 +46,8 @@ class StaticBlock self::$hardness[$b->getID()] = $b->getHardness(); self::$destroyTime[$b->getID()] = $b->breakTime; self::$material[$b->getID()] = $b->material; + self::$lightBlock[$b->getID()] = $b->lightBlock; + self::$lightEmission[$b->getID()] = $b->lightEmission; self::$prealloc[$b->getID()] = $b; $b::$blockID = $b->getID(); FireBlock::setFlammabilityAndCatchingChance($b->getID(), 0, 0); diff --git a/src/material/block/attachable/TorchBlock.php b/src/material/block/attachable/TorchBlock.php index 87d7cc36..bbd181cc 100644 --- a/src/material/block/attachable/TorchBlock.php +++ b/src/material/block/attachable/TorchBlock.php @@ -7,6 +7,7 @@ class TorchBlock extends FlowableBlock implements LightingBlock{ $this->hardness = 0; $this->breakTime = 0; $this->material = Material::$decoration; + $this->lightEmission = 14; } public function getMaxLightValue(){ diff --git a/src/material/block/liquid/LavaBlock.php b/src/material/block/liquid/LavaBlock.php index 3fbe9c65..7167bae4 100644 --- a/src/material/block/liquid/LavaBlock.php +++ b/src/material/block/liquid/LavaBlock.php @@ -8,6 +8,8 @@ class LavaBlock extends LiquidBlockDynamic implements LightingBlock{ $this->hardness = 500; //0 in vanilla $this->breakTime = -1; //in vanilla it is 0, but you cant interact with the block so it is not possible to destroy it $this->material = Material::$lava; + $this->lightEmission = 15; + $this->lightBlock = 255; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/liquid/StillLavaBlock.php b/src/material/block/liquid/StillLavaBlock.php index b4dff404..5fb354ac 100644 --- a/src/material/block/liquid/StillLavaBlock.php +++ b/src/material/block/liquid/StillLavaBlock.php @@ -8,6 +8,8 @@ class StillLavaBlock extends LiquidBlockStatic{ $this->hardness = 500; $this->breakTime = -1; //in vanilla it is 100, but you cant interact with the block so it is not possible to destroy it $this->material = Material::$lava; + $this->lightEmission = 15; + $this->lightBlock = 255; } public static function getTickDelay(){ diff --git a/src/material/block/liquid/StillWaterBlock.php b/src/material/block/liquid/StillWaterBlock.php index 5072cd5b..14682827 100644 --- a/src/material/block/liquid/StillWaterBlock.php +++ b/src/material/block/liquid/StillWaterBlock.php @@ -14,6 +14,7 @@ class StillWaterBlock extends LiquidBlockStatic{ $this->hardness = 500; $this->breakTime = -1; //in vanilla it is 100, but you cant interact with the block so it is not possible to destroy it $this->material = Material::$water; + $this->lightBlock = 3; } public static function getTickDelay(){ diff --git a/src/material/block/liquid/WaterBlock.php b/src/material/block/liquid/WaterBlock.php index 1b090e21..c55bada5 100644 --- a/src/material/block/liquid/WaterBlock.php +++ b/src/material/block/liquid/WaterBlock.php @@ -9,6 +9,7 @@ class WaterBlock extends LiquidBlockDynamic{ $this->hardness = 500; $this->breakTime = -1; //in vanilla it is 100, but you cant interact with the block so it is not possible to destroy it $this->material = Material::$water; + $this->lightBlock = 3; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/misc/FireBlock.php b/src/material/block/misc/FireBlock.php index 9e04dd86..99fc63cc 100644 --- a/src/material/block/misc/FireBlock.php +++ b/src/material/block/misc/FireBlock.php @@ -13,6 +13,7 @@ class FireBlock extends FlowableBlock implements LightingBlock{ $this->hardness = 0; $this->breakTime = 0; $this->material = Material::$fire; + $this->lightEmission = 15; } public static function getAABB(Level $level, $x, $y, $z){ return null; diff --git a/src/material/block/misc/NetherReactorBlock.php b/src/material/block/misc/NetherReactorBlock.php index 1d62a1ff..f7250d94 100644 --- a/src/material/block/misc/NetherReactorBlock.php +++ b/src/material/block/misc/NetherReactorBlock.php @@ -9,6 +9,7 @@ class NetherReactorBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3; $this->material = Material::$metal; + $this->lightBlock = 255; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/misc/TNTBlock.php b/src/material/block/misc/TNTBlock.php index 3afbb6ab..19d847f8 100644 --- a/src/material/block/misc/TNTBlock.php +++ b/src/material/block/misc/TNTBlock.php @@ -8,6 +8,7 @@ class TNTBlock extends SolidBlock{ $this->breakTime = 0; $this->isActivable = true; $this->material = Material::$explosive; + $this->lightBlock = 255; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/nonfull/CobwebBlock.php b/src/material/block/nonfull/CobwebBlock.php index 133bceee..010b1e37 100644 --- a/src/material/block/nonfull/CobwebBlock.php +++ b/src/material/block/nonfull/CobwebBlock.php @@ -9,6 +9,7 @@ class CobwebBlock extends FlowableBlock{ $this->hardness = 30; $this->breakTime = 4; $this->material = Material::$web; + $this->lightBlock = 1; } public static function getAABB(Level $level, $x, $y, $z){ diff --git a/src/material/block/ore/CoalOreBlock.php b/src/material/block/ore/CoalOreBlock.php index 1831be50..5dd90323 100644 --- a/src/material/block/ore/CoalOreBlock.php +++ b/src/material/block/ore/CoalOreBlock.php @@ -7,6 +7,7 @@ class CoalOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3.0; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/DiamondOreBlock.php b/src/material/block/ore/DiamondOreBlock.php index 42d73d60..29929b78 100644 --- a/src/material/block/ore/DiamondOreBlock.php +++ b/src/material/block/ore/DiamondOreBlock.php @@ -7,6 +7,7 @@ class DiamondOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/GlowingRedstoneOreBlock.php b/src/material/block/ore/GlowingRedstoneOreBlock.php index 6a415480..20a92197 100644 --- a/src/material/block/ore/GlowingRedstoneOreBlock.php +++ b/src/material/block/ore/GlowingRedstoneOreBlock.php @@ -7,6 +7,8 @@ class GlowingRedstoneOreBlock extends SolidBlock implements LightingBlock{ $this->hardness = 15; $this->breakTime = 3; $this->material = Material::$stone; + $this->lightBlock = 255; + $this->lightEmission = 9; } public static function onRandomTick(Level $level, $x, $y, $z){ diff --git a/src/material/block/ore/GoldOreBlock.php b/src/material/block/ore/GoldOreBlock.php index f494bc4b..5209b379 100644 --- a/src/material/block/ore/GoldOreBlock.php +++ b/src/material/block/ore/GoldOreBlock.php @@ -7,6 +7,7 @@ class GoldOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3.0; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/IronOreBlock.php b/src/material/block/ore/IronOreBlock.php index 693ca0ca..020dec82 100644 --- a/src/material/block/ore/IronOreBlock.php +++ b/src/material/block/ore/IronOreBlock.php @@ -7,6 +7,7 @@ class IronOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3.0; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/LapisOreBlock.php b/src/material/block/ore/LapisOreBlock.php index 10c61e8a..a60ef015 100644 --- a/src/material/block/ore/LapisOreBlock.php +++ b/src/material/block/ore/LapisOreBlock.php @@ -7,6 +7,7 @@ class LapisOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3.0; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/ore/RedstoneOreBlock.php b/src/material/block/ore/RedstoneOreBlock.php index 962651a5..3f0b260c 100644 --- a/src/material/block/ore/RedstoneOreBlock.php +++ b/src/material/block/ore/RedstoneOreBlock.php @@ -7,6 +7,7 @@ class RedstoneOreBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3; $this->material = Material::$stone; + $this->lightBlock = 255; } public static function interact(Level $level, $x, $y, $z, Player $player){ diff --git a/src/material/block/plant/BrownMushroomBlock.php b/src/material/block/plant/BrownMushroomBlock.php index eb2edf11..e252640c 100644 --- a/src/material/block/plant/BrownMushroomBlock.php +++ b/src/material/block/plant/BrownMushroomBlock.php @@ -7,6 +7,7 @@ class BrownMushroomBlock extends FlowableBlock{ $this->hardness = 0; $this->breakTime = 0; $this->material = Material::$plant; + $this->lightEmission = 1; } public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ diff --git a/src/material/block/solid/BedrockBlock.php b/src/material/block/solid/BedrockBlock.php index 4799553b..70d8035c 100644 --- a/src/material/block/solid/BedrockBlock.php +++ b/src/material/block/solid/BedrockBlock.php @@ -8,6 +8,7 @@ class BedrockBlock extends SolidBlock{ $this->hardness = 18000000; $this->breakTime = -1; $this->material = Material::$stone; + $this->lightBlock = 255; } public function isBreakable(Item $item, Player $player){ diff --git a/src/material/block/solid/BookshelfBlock.php b/src/material/block/solid/BookshelfBlock.php index 80955d9e..fdd93bfc 100644 --- a/src/material/block/solid/BookshelfBlock.php +++ b/src/material/block/solid/BookshelfBlock.php @@ -7,6 +7,7 @@ class BookshelfBlock extends SolidBlock{ $this->hardness = 7.5; $this->breakTime = 1.5; $this->material = Material::$wood; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/BricksBlock.php b/src/material/block/solid/BricksBlock.php index cea18904..f0657c6d 100644 --- a/src/material/block/solid/BricksBlock.php +++ b/src/material/block/solid/BricksBlock.php @@ -7,6 +7,7 @@ class BricksBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/BurningFurnaceBlock.php b/src/material/block/solid/BurningFurnaceBlock.php index 50944933..030b7b63 100644 --- a/src/material/block/solid/BurningFurnaceBlock.php +++ b/src/material/block/solid/BurningFurnaceBlock.php @@ -8,6 +8,8 @@ class BurningFurnaceBlock extends SolidBlock implements LightingBlock{ $this->hardness = 17.5; $this->breakTime = 3.5; $this->material = Material::$stone; + $this->lightBlock = 255; + $this->lightEmission = 13; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/ClayBlock.php b/src/material/block/solid/ClayBlock.php index e1c88681..a059962a 100644 --- a/src/material/block/solid/ClayBlock.php +++ b/src/material/block/solid/ClayBlock.php @@ -7,6 +7,7 @@ class ClayBlock extends SolidBlock{ $this->hardness = 3; $this->breakTime = 0.6; $this->material = Material::$clay; + $this->lightBlock = 255; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/CoalBlock.php b/src/material/block/solid/CoalBlock.php index 6c7a11e2..6a8191ef 100644 --- a/src/material/block/solid/CoalBlock.php +++ b/src/material/block/solid/CoalBlock.php @@ -7,6 +7,7 @@ class CoalBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 5; $this->material = Material::$metal; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/CobblestoneBlock.php b/src/material/block/solid/CobblestoneBlock.php index 96bc7172..7ba03d99 100644 --- a/src/material/block/solid/CobblestoneBlock.php +++ b/src/material/block/solid/CobblestoneBlock.php @@ -7,6 +7,7 @@ class CobblestoneBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/DiamondBlock.php b/src/material/block/solid/DiamondBlock.php index 9fc92a18..eaa0131d 100644 --- a/src/material/block/solid/DiamondBlock.php +++ b/src/material/block/solid/DiamondBlock.php @@ -7,6 +7,7 @@ class DiamondBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 5; $this->material = Material::$metal; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/DirtBlock.php b/src/material/block/solid/DirtBlock.php index fef766f0..87f2eb74 100644 --- a/src/material/block/solid/DirtBlock.php +++ b/src/material/block/solid/DirtBlock.php @@ -8,6 +8,7 @@ class DirtBlock extends SolidBlock{ $this->hardness = 2.5; $this->breakTime = 0.5; $this->material = Material::$dirt; + $this->lightBlock = 255; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/solid/DoubleSlabBlock.php b/src/material/block/solid/DoubleSlabBlock.php index c455099d..0bac086d 100755 --- a/src/material/block/solid/DoubleSlabBlock.php +++ b/src/material/block/solid/DoubleSlabBlock.php @@ -18,6 +18,7 @@ class DoubleSlabBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/DoubleWoodSlabBlock.php b/src/material/block/solid/DoubleWoodSlabBlock.php index 62aa3c40..848c7b87 100644 --- a/src/material/block/solid/DoubleWoodSlabBlock.php +++ b/src/material/block/solid/DoubleWoodSlabBlock.php @@ -14,6 +14,7 @@ class DoubleWoodSlabBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$wood; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/FurnaceBlock.php b/src/material/block/solid/FurnaceBlock.php index 3f8253e2..8fe0cdf9 100644 --- a/src/material/block/solid/FurnaceBlock.php +++ b/src/material/block/solid/FurnaceBlock.php @@ -13,5 +13,7 @@ class FurnaceBlock extends BurningFurnaceBlock{ $this->name = "Furnace"; $this->isActivable = true; $this->material = Material::$stone; + $this->lightBlock = 255; + $this->lightEmission = 0; } } \ No newline at end of file diff --git a/src/material/block/solid/GlowingObsidianBlock.php b/src/material/block/solid/GlowingObsidianBlock.php index cefcb5c5..b2e143db 100644 --- a/src/material/block/solid/GlowingObsidianBlock.php +++ b/src/material/block/solid/GlowingObsidianBlock.php @@ -7,6 +7,8 @@ class GlowingObsidianBlock extends SolidBlock implements LightingBlock{ $this->hardness = 6000; $this->breakTime = 10; $this->material = Material::$stone; + $this->lightBlock = 255; + $this->lightEmission = 13; } public function getMaxLightValue(){ diff --git a/src/material/block/solid/GlowstoneBlock.php b/src/material/block/solid/GlowstoneBlock.php index f746e6c4..e123d491 100644 --- a/src/material/block/solid/GlowstoneBlock.php +++ b/src/material/block/solid/GlowstoneBlock.php @@ -7,6 +7,8 @@ class GlowstoneBlock extends TransparentBlock implements LightingBlock{ $this->hardness = 1.5; $this->breakTime = 0.3; $this->material = Material::$glass; + $this->lightBlock = 255; + $this->lightEmission = 15; } public function getMaxLightValue(){ diff --git a/src/material/block/solid/GoldBlock.php b/src/material/block/solid/GoldBlock.php index 6c623e93..eff8f97d 100644 --- a/src/material/block/solid/GoldBlock.php +++ b/src/material/block/solid/GoldBlock.php @@ -7,6 +7,7 @@ class GoldBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 3; $this->material = Material::$metal; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/GrassBlock.php b/src/material/block/solid/GrassBlock.php index 10945978..f3b49baa 100644 --- a/src/material/block/solid/GrassBlock.php +++ b/src/material/block/solid/GrassBlock.php @@ -8,6 +8,7 @@ class GrassBlock extends SolidBlock{ $this->hardness = 3; $this->breakTime = 0.6; $this->material = Material::$dirt; + $this->lightBlock = 255; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/GravelBlock.php b/src/material/block/solid/GravelBlock.php index 4842e512..2ac020a3 100644 --- a/src/material/block/solid/GravelBlock.php +++ b/src/material/block/solid/GravelBlock.php @@ -7,6 +7,7 @@ class GravelBlock extends FallableBlock{ $this->hardness = 3; $this->breakTime = 0.6; $this->material = Material::$sand; + $this->lightBlock = 255; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/HayBaleBlock.php b/src/material/block/solid/HayBaleBlock.php index eb748749..86dacd51 100644 --- a/src/material/block/solid/HayBaleBlock.php +++ b/src/material/block/solid/HayBaleBlock.php @@ -7,6 +7,7 @@ class HayBaleBlock extends SolidBlock{ $this->hardness = 2.5; $this->breakTime = 0.5; $this->material = Material::$dirt; + $this->lightBlock = 255; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/IceBlock.php b/src/material/block/solid/IceBlock.php index 81d6f82f..52079e10 100644 --- a/src/material/block/solid/IceBlock.php +++ b/src/material/block/solid/IceBlock.php @@ -8,6 +8,7 @@ class IceBlock extends TransparentBlock{ $this->breakTime = 0.5; $this->slipperiness = 0.98; $this->material = Material::$ice; + $this->lightBlock = 3; } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ diff --git a/src/material/block/solid/InvisibleBedrockBlock.php b/src/material/block/solid/InvisibleBedrockBlock.php index 02724952..5ef1f20f 100755 --- a/src/material/block/solid/InvisibleBedrockBlock.php +++ b/src/material/block/solid/InvisibleBedrockBlock.php @@ -8,6 +8,7 @@ class InvisibleBedrockBlock extends SolidBlock{ $this->hardness = 18000000; $this->breakTime = -1; $this->material = Material::$stone; + $this->lightBlock = 255; } public function isBreakable(Item $item, Player $player){ diff --git a/src/material/block/solid/IronBlock.php b/src/material/block/solid/IronBlock.php index 2d14ffc4..1b576ba2 100644 --- a/src/material/block/solid/IronBlock.php +++ b/src/material/block/solid/IronBlock.php @@ -7,6 +7,7 @@ class IronBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 5; $this->material = Material::$metal; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/LapisBlock.php b/src/material/block/solid/LapisBlock.php index 21fe55cb..07346192 100644 --- a/src/material/block/solid/LapisBlock.php +++ b/src/material/block/solid/LapisBlock.php @@ -7,6 +7,7 @@ class LapisBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 3.0; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/LeavesBlock.php b/src/material/block/solid/LeavesBlock.php index 1f6e2480..ee2e6a59 100644 --- a/src/material/block/solid/LeavesBlock.php +++ b/src/material/block/solid/LeavesBlock.php @@ -17,6 +17,7 @@ class LeavesBlock extends TransparentBlock{ $this->hardness = 1; $this->breakTime = 0.2; $this->material = Material::$leaves; + $this->lightBlock = 1; } public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ diff --git a/src/material/block/solid/LitPumpkinBlock.php b/src/material/block/solid/LitPumpkinBlock.php index 642caf8d..2303db16 100644 --- a/src/material/block/solid/LitPumpkinBlock.php +++ b/src/material/block/solid/LitPumpkinBlock.php @@ -15,6 +15,8 @@ class LitPumpkinBlock extends SolidBlock implements LightingBlock{ $this->hardness = 5; $this->breakTime = 1.0; $this->material = Material::$vegetable; + $this->lightBlock = 255; + $this->lightEmission = 15; } public function getMaxLightValue(){ diff --git a/src/material/block/solid/MelonBlock.php b/src/material/block/solid/MelonBlock.php index 690b43c2..71596b2a 100644 --- a/src/material/block/solid/MelonBlock.php +++ b/src/material/block/solid/MelonBlock.php @@ -7,6 +7,7 @@ class MelonBlock extends TransparentBlock{ $this->hardness = 5; $this->breakTime = 1; $this->material = Material::$vegetable; + $this->lightBlock = 255; } public function getDrops(Item $item, Player $player){ return array( diff --git a/src/material/block/solid/MossStoneBlock.php b/src/material/block/solid/MossStoneBlock.php index 181ee302..e1bb1436 100644 --- a/src/material/block/solid/MossStoneBlock.php +++ b/src/material/block/solid/MossStoneBlock.php @@ -7,6 +7,7 @@ class MossStoneBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/NetherBricksBlock.php b/src/material/block/solid/NetherBricksBlock.php index f58002d7..f2eec4a5 100644 --- a/src/material/block/solid/NetherBricksBlock.php +++ b/src/material/block/solid/NetherBricksBlock.php @@ -7,6 +7,7 @@ class NetherBricksBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 2; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/NetherrackBlock.php b/src/material/block/solid/NetherrackBlock.php index 203e7b4f..ff6b510a 100644 --- a/src/material/block/solid/NetherrackBlock.php +++ b/src/material/block/solid/NetherrackBlock.php @@ -7,6 +7,7 @@ class NetherrackBlock extends SolidBlock{ $this->hardness = 2; $this->breakTime = 0.4; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/ObsidianBlock.php b/src/material/block/solid/ObsidianBlock.php index 466201ab..6a7b369d 100644 --- a/src/material/block/solid/ObsidianBlock.php +++ b/src/material/block/solid/ObsidianBlock.php @@ -7,6 +7,7 @@ class ObsidianBlock extends SolidBlock{ $this->hardness = 6000; $this->breakTime = 10; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/PlanksBlock.php b/src/material/block/solid/PlanksBlock.php index c4cc531e..e321f4c7 100644 --- a/src/material/block/solid/PlanksBlock.php +++ b/src/material/block/solid/PlanksBlock.php @@ -14,6 +14,7 @@ class PlanksBlock extends SolidBlock{ $this->hardness = 15; $this->breakTime = 2; $this->material = Material::$wood; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/PumpkinBlock.php b/src/material/block/solid/PumpkinBlock.php index 92002b14..56c66e72 100644 --- a/src/material/block/solid/PumpkinBlock.php +++ b/src/material/block/solid/PumpkinBlock.php @@ -7,6 +7,7 @@ class PumpkinBlock extends SolidBlock{ $this->hardness = 5; $this->breakTime = 1; $this->material = Material::$vegetable; + $this->lightBlock = 255; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/QuartzBlock.php b/src/material/block/solid/QuartzBlock.php index 3640f18b..00fb68b3 100644 --- a/src/material/block/solid/QuartzBlock.php +++ b/src/material/block/solid/QuartzBlock.php @@ -14,6 +14,7 @@ class QuartzBlock extends SolidBlock{ $this->breakTime = 0.8; $this->hardness = 4; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/Reserved6Block.php b/src/material/block/solid/Reserved6Block.php index 3c86f0a0..133f763c 100644 --- a/src/material/block/solid/Reserved6Block.php +++ b/src/material/block/solid/Reserved6Block.php @@ -8,6 +8,7 @@ class Reserved6Block extends SolidBlock{ $this->hardness = 0; $this->breakTime = 0; $this->material = Material::$dirt; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/SandBlock.php b/src/material/block/solid/SandBlock.php index 4c9ee6d1..16623ef1 100644 --- a/src/material/block/solid/SandBlock.php +++ b/src/material/block/solid/SandBlock.php @@ -7,6 +7,7 @@ class SandBlock extends FallableBlock{ $this->hardness = 2.5; $this->breakTime = 0.5; $this->material = Material::$sand; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/SandstoneBlock.php b/src/material/block/solid/SandstoneBlock.php index 1a0d4bab..33a752b5 100644 --- a/src/material/block/solid/SandstoneBlock.php +++ b/src/material/block/solid/SandstoneBlock.php @@ -13,6 +13,7 @@ class SandstoneBlock extends SolidBlock{ $this->hardness = 4; $this->breakTime = 0.8; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/SnowBlock.php b/src/material/block/solid/SnowBlock.php index d41d2148..92addfae 100644 --- a/src/material/block/solid/SnowBlock.php +++ b/src/material/block/solid/SnowBlock.php @@ -7,6 +7,7 @@ class SnowBlock extends SolidBlock{ $this->hardness = 1; $this->breakTime = 0.2; $this->material = Material::$snow; + $this->lightBlock = 255; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/SpongeBlock.php b/src/material/block/solid/SpongeBlock.php index 58702087..ac5275e1 100644 --- a/src/material/block/solid/SpongeBlock.php +++ b/src/material/block/solid/SpongeBlock.php @@ -7,6 +7,7 @@ class SpongeBlock extends SolidBlock{ $this->hardness = 3; $this->breakTime = 0.6; $this->material = Material::$dirt; + $this->lightBlock = 255; } } diff --git a/src/material/block/solid/StoneBlock.php b/src/material/block/solid/StoneBlock.php index 55ee212c..e18b1acb 100644 --- a/src/material/block/solid/StoneBlock.php +++ b/src/material/block/solid/StoneBlock.php @@ -7,6 +7,7 @@ class StoneBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 1.5; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/StoneBricksBlock.php b/src/material/block/solid/StoneBricksBlock.php index 1b856076..cc83158f 100644 --- a/src/material/block/solid/StoneBricksBlock.php +++ b/src/material/block/solid/StoneBricksBlock.php @@ -14,6 +14,7 @@ class StoneBricksBlock extends SolidBlock{ $this->hardness = 30; $this->breakTime = 1.5; $this->material = Material::$stone; + $this->lightBlock = 255; } public function getBreakTime(Item $item, Player $player){ diff --git a/src/material/block/solid/StonecutterBlock.php b/src/material/block/solid/StonecutterBlock.php index fc307d70..63a4d296 100644 --- a/src/material/block/solid/StonecutterBlock.php +++ b/src/material/block/solid/StonecutterBlock.php @@ -8,6 +8,7 @@ class StonecutterBlock extends SolidBlock{ $this->breakTime = 2.5; $this->hardness = 2.5*5; $this->material = Material::$stone; + $this->lightBlock = 255; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/solid/WoodBlock.php b/src/material/block/solid/WoodBlock.php index 04a26316..c9d2fafa 100644 --- a/src/material/block/solid/WoodBlock.php +++ b/src/material/block/solid/WoodBlock.php @@ -19,6 +19,7 @@ class WoodBlock extends SolidBlock{ $this->hardness = 10; $this->breakTime = 2.0; $this->material = Material::$wood; + $this->lightBlock = 255; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/solid/WoolBlock.php b/src/material/block/solid/WoolBlock.php index 52b91735..c0e6e7f2 100644 --- a/src/material/block/solid/WoolBlock.php +++ b/src/material/block/solid/WoolBlock.php @@ -29,6 +29,7 @@ class WoolBlock extends SolidBlock{ $this->hardness = 4; $this->breakTime = 0.8; $this->material = Material::$cloth; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/WorkbenchBlock.php b/src/material/block/solid/WorkbenchBlock.php index 5f80c000..3148a223 100644 --- a/src/material/block/solid/WorkbenchBlock.php +++ b/src/material/block/solid/WorkbenchBlock.php @@ -8,6 +8,7 @@ class WorkbenchBlock extends SolidBlock{ $this->hardness = 12.5; $this->breakTime = 2.5; $this->material = Material::$wood; + $this->lightBlock = 255; } public function onActivate(Item $item, Player $player){ diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index f00388e6..0902c409 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -3,7 +3,6 @@ define("PMF_CURRENT_LEVEL_VERSION", 0x00); class PMFLevel extends PMF{ - public $isLoaded = true; private $levelData = []; public $locationTable = []; @@ -298,6 +297,7 @@ class PMFLevel extends PMF{ for($Y = 0; $Y < $this->levelData["height"]; ++$Y){ $t = 1 << $Y; if(($info[0] & $t) === $t){ + //TODO do something with skylight // 4096 + 2048 + 2048, Block Data, Meta, Light if(strlen($this->chunks[$index][$Y] = gzread($chunk, 8192)) < 8192){ console("[NOTICE] Empty corrupt chunk detected [$X,$Z,:$Y], recovering contents", true, true, 2); @@ -397,6 +397,7 @@ class PMFLevel extends PMF{ }else{ $this->chunks[$index][$Y][$bind] = chr($block); if($block > 0) StaticBlock::getBlock($block)::onPlace($this->level, $x, $y, $z); + $this->level->updateLight(0, $x, $y, $z, $x, $y, $z); } if(!isset($this->chunkChange[$index][$Y])){ @@ -408,6 +409,56 @@ class PMFLevel extends PMF{ return true; } + public function getBlockLight($x, $y, $z){ + if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127) return 0; + $X = $x >> 4; + $Z = $z >> 4; + $Y = $y >> 4; + $index = self::getIndex($X, $Z); + if(!isset($this->chunks[$index]) || $this->chunks[$index] === false || ($this->chunks[$index][$Y] === false)) return 0; + $aX = $x & 0xf; + $aZ = $z & 0xf; + $aY = $y & 0xf; + $m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 24 + ($aX << 5) + ($aZ << 9))]); + return $y & 1 ? $m >> 4 : $m & 0x0F; + } + + public function setBlockLight($x, $y, $z, $value){ + if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127) return false; + $X = $x >> 4; + $Z = $z >> 4; + $Y = $y >> 4; + $value &= 0x0F; + + $index = self::getIndex($X, $Z); + if(!isset($this->chunks[$index]) || $this->chunks[$index] === false){ + if($this->loadChunk($X, $Z) === false) return false; + }elseif($this->chunks[$index][$Y] === false){ + $this->fillMiniChunk($X, $Z, $Y); + } + + $aX = $x & 0xf; + $aZ = $z & 0xf; + $aY = $y & 0xf; + $mindex = (int) (($aY >> 1) + 24 + ($aX << 5) + ($aZ << 9)); + $old_m = ord($this->chunks[$index][$Y][$mindex]); + if(($y & 1) === 0) $m = ($old_m & 0xF0) | $value; + else $m = ($value << 4) | ($old_m & 0x0F); + + if($old_m != $m){ + $this->chunks[$index][$Y][$mindex] = chr($m); + + if(!isset($this->chunkChange[$index][$Y])){ + $this->chunkChange[$index][$Y] = 1; + }else{ + ++$this->chunkChange[$index][$Y]; + } + $this->chunkChange[$index][-1] = true; + return true; + } + return false; + } + public function getBlockDamage($x, $y, $z){ if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){ return 0; @@ -531,6 +582,7 @@ class PMFLevel extends PMF{ $this->chunkChange[$index][-1] = true; if($block > 0) StaticBlock::getBlock($block)::onPlace($this->level, $x, $y, $z); + $this->level->updateLight(0, $x, $y, $z, $x, $y, $z); return true; } return false; diff --git a/src/world/Level.php b/src/world/Level.php index fcd621a1..990d6782 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -520,7 +520,140 @@ class Level{ } $this->addBlockToSendQueue($x, $y, $z, $id, $meta); } + /** + * Contains light updates. Light update is an integer that contains min/max block coords and light layer + * Format: 0011ffeeddccbbaa + * 00 - unused(8th byte) + * 11 - light layer(currently must be 0(block), future: 15(sky)) + * ff, ee, dd - minX, minY, minZ + * cc, bb, aa - maxX, maxY, maxZ + * @var int[] + */ + public $lightUpdates = []; + public $lightUpdateIndx = -1; + public function handleBlockLightUpdate($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + for($x = $minX; $x <= $maxX; ++$x){ + for($z = $minZ; $z <= $maxZ; ++$z){ + for($y = $minY; $y <= $maxY; ++$y){ + $brightness = $this->level->getBlockLight($x, $y, $z); + $blockID = $this->level->getBlockID($x, $y, $z); + $lightBlock = StaticBlock::$lightBlock[$blockID]; + if($lightBlock == 0) $lightBlock = 1; + $lightEmission = StaticBlock::$lightEmission[$blockID]; + $newBrightness = 0; + if($lightBlock <= 14 || $lightEmission != 0){ + $xNegBright = $this->level->getBlockLight($x-1, $y, $z); + $xPosBright = $this->level->getBlockLight($x+1, $y, $z); + $yNegBright = $this->level->getBlockLight($x, $y-1, $z); + $yPosBright = $this->level->getBlockLight($x, $y+1, $z); + $zNegBright = $this->level->getBlockLight($x, $y, $z-1); + $zPosBright = $this->level->getBlockLight($x, $y, $z+1); + + $v15 = $xNegBright; + if($xPosBright > $v15) $v15 = $xPosBright; + if($yNegBright > $v15) $v15 = $yNegBright; + if($yPosBright > $v15) $v15 = $yPosBright; + if($zNegBright > $v15) $v15 = $zNegBright; + if($zPosBright > $v15) $v15 = $zPosBright; + + $newBrightness = $v15 - $lightBlock; + if($newBrightness < 0) $newBrightness = 0; + if($lightEmission > $newBrightness) $newBrightness = $lightEmission; + } + + if($brightness != $newBrightness){ + $this->level->setBlockLight($x, $y, $z, $newBrightness); + $v4 = $newBrightness - 1; + if($v4 < 0) $v4 = 0; + $this->updateLightIfOtherThan(0, $x-1, $y, $z, $v4); + $this->updateLightIfOtherThan(0, $x, $y-1, $z, $v4); + $this->updateLightIfOtherThan(0, $x, $y, $z-1, $v4); + if($x+1 >= $maxX) $this->updateLightIfOtherThan(0, $x+1, $y, $z, $v4); + if($y+1 >= $maxY) $this->updateLightIfOtherThan(0, $x, $y+1, $z, $v4); + if($z+1 >= $maxZ) $this->updateLightIfOtherThan(0, $x, $y, $z+1, $v4); + } + } + } + } + } + public function updateLight($layer, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + /*$max = 5; TODO: expand lightupdate + $sz = $this->lightUpdateIndx; + if($sz < $max) $max = $sz; + for($i = 0; $i < $max; ++$i){ + $update = $this->lightUpdates[$i]; + if($update->layer == $layer && ($r = $update->expand($minX, $minY, $minZ, $maxX, $maxY, $maxZ))){ + --$this->lightUpdatesCount; + } + }*/ + + if($maxX < $minX){ + $s = $minX; + $minX = $maxX; + $maxX = $s; + } + if($maxY < $minY){ + $s = $minY; + $minY = $maxY; + $maxY = $s; + } + if($maxZ < $minZ){ + $s = $minZ; + $minZ = $maxZ; + $maxZ = $s; + } + if($minX < 0) $minX = 0; + if($minX > 255) $minX = 255; + if($minY < 0) $minY = 0; + if($minY > 127) $minY = 127; + if($minZ < 0) $minZ = 0; + if($minZ > 255) $minZ = 255; + + if($maxX < 0) $minX = 0; + if($maxX > 255) $minX = 255; + if($maxY < 0) $minY = 0; + if($maxY > 127) $minY = 127; + if($maxZ < 0) $maxZ = 0; + if($maxZ > 255) $maxZ = 255; + + $update = ($layer << 48) | ($minX << 40) | ($minY << 32) | ($minZ << 24) | ($maxX << 16) | ($maxY << 8) | ($maxZ); + $this->lightUpdates[++$this->lightUpdateIndx] = $update; + } + + public function updateLightIfOtherThan($layer, $x, $y, $z, $level){ + if($y < 0 || $y > 127 || $x < 0 || $x > 255 || $z < 0 || $z > 255) return; + if($layer == 0){ + $blockID = $this->level->getBlockID($x, $y, $z); + $emission = StaticBlock::$lightEmission[$blockID]; + if($emission > $level) $level = $emission; + + if($this->level->getBlockLight($x, $y, $z) != $level){ + $this->updateLight($layer, $x, $y, $z, $x, $y, $z); + } + } + } + public function updateLights(){ + if($this->lightUpdateIndx < 0) return false; + $limit = 500; + do{ + $upd = $this->lightUpdates[$this->lightUpdateIndx]; + unset($this->lightUpdates[$this->lightUpdateIndx]); + --$this->lightUpdateIndx; + $maxZ = $upd & 0xff; + $maxY = ($upd >> 8) & 0xff; + $maxX = ($upd >> 16) & 0xff; + $minZ = ($upd >> 24) & 0xff; + $minY = ($upd >> 32) & 0xff; + $minX = ($upd >> 40) & 0xff; + $layer = $upd >> 48; + if($layer == 0) $this->handleBlockLightUpdate($minX, $minY, $minZ, $maxX, $maxY, $maxZ); + else console("wha"); + if(!--$limit) return true; + }while($this->lightUpdateIndx >= 0); + if(count($this->lightUpdates) > 0) return true; + return false; + } public function onTick(PocketMinecraftServer $server, $currentTime){ if(!$this->stopTime) ++$this->time; for($cX = 0; $cX < 16; ++$cX){ @@ -647,6 +780,7 @@ class Level{ } $this->queuedBlockUpdates = []; + $this->updateLights(); } public function isBoundingBoxOnFire(AxisAlignedBB $bb){ From cd07581d494a15eb16b1f245d157127a465ad475 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 23 Jun 2025 07:02:31 +0300 Subject: [PATCH 2/6] mobs wont spawn if brightness is too high, aditional file with world data for storing additional information --- src/API/ConsoleAPI.php | 6 ++- src/pmf/PMFLevel.php | 102 ++++++++++++++++++++++++++++++++++++++- src/world/Level.php | 73 +++++++++++++++++++++++++--- src/world/MobSpawner.php | 7 ++- 4 files changed, 175 insertions(+), 13 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index c8ac2212..b32e415c 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -210,7 +210,11 @@ class ConsoleAPI{ case "status": $info = $this->server->debugInfo(); if(!($issuer instanceof Player) && $issuer === "console"){ - return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]}"; + $lightUpdates = 0; + foreach($this->server->api->level->levels as $level){ + $lightUpdates += count($level->lightUpdates); + } + return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]} Light updates: {$lightUpdates}"; } return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]})"; case "stop": diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 0902c409..af6e0be3 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -3,13 +3,18 @@ define("PMF_CURRENT_LEVEL_VERSION", 0x00); class PMFLevel extends PMF{ + const NC_DATA_VERSION = 1; public $isLoaded = true; private $levelData = []; + public $hasLight = ""; public $locationTable = []; private $log = 4; //must be 4 or else rip world private $payloadOffset = 0; public $chunks = []; public $chunkChange = []; + /** + * @var Level + */ public $level; public function __construct($file, $blank = false){ if(is_array($blank)){ @@ -81,8 +86,83 @@ class PMFLevel extends PMF{ if($locationTable !== false){ $this->writeLocationTable(); } + + $this->writeNCData(); + } + + public function writeNCData(){ + $dir = dirname($this->file); + try{ + $file = fopen("$dir/ncleveldata.pmf", "wb"); + fwrite($file, chr(self::NC_DATA_VERSION), 1); + fwrite($file, $this->hasLight, 16*16); + }finally{ + fclose($file); + } } + public function readNCData(){ + $this->hasLight = str_repeat("\x00", 16*16); + + $dir = dirname($this->file); + $path = "$dir/ncleveldata.pmf"; + if(!is_file($path)){ + ConsoleAPI::notice("No NC Level data found! Old world?"); + return; + } + try{ + $file = fopen($path, "rb"); + $ncdv = ord(fread($file, 1)); + $v = self::NC_DATA_VERSION; + if($ncdv !== $v){ + ConsoleAPI::notice("NC level data version on server($v) does not match the world($ncdv)! Creating NC level data backup."); + $newpath = "$path.bak.".microtime(true); + $f = copy($path, $newpath); + if($f === false) ConsoleAPI::error("NC level data backup creation failed."); + else ConsoleAPI::notice("NC level data backup created. (path: $newpath)"); + } + $this->hasLight = fread($file, 16*16); + }finally{ + fclose($file); + } + } + + /** + * Checks was skylight generated for chunk + * @param int $X - chunk x + * @param int $Z - chunk z + */ + public function hasSkylight($X, $Z){ + if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false; + $index = self::getIndex($X, $Z); + return (ord($this->hasLight[$index]) & 2) > 0; + } + /** + * Checks was blocklight generated for chunk + * @param int $X - chunk x + * @param int $Z - chunk z + */ + public function hasBlocklight($X, $Z){ + if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false; + $index = self::getIndex($X, $Z); + return (ord($this->hasLight[$index]) & 1) > 0; + } + + public function markHasBlocklight($X, $Z){ + if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false; + $index = self::getIndex($X, $Z); + $o = ord($this->hasLight[$index]); + $this->hasLight[$index] = chr($o | 1); + return true; + } + public function markHasSkylight($X, $Z){ + if($X < 0 || $X > 15 || $Z < 0 || $Z > 15) return false; + $index = self::getIndex($X, $Z); + $o = ord($this->hasLight[$index]); + $this->hasLight[$index] = chr($o | 2); + return true; + } + private function writeLocationTable(){ $cnt = pow($this->levelData["width"], 2); @ftruncate($this->fp, $this->payloadOffset); @@ -159,6 +239,7 @@ class PMFLevel extends PMF{ }else{ $this->payloadOffset = ftell($this->fp); } + $this->readNCData(); return $this->readLocationTable(); } @@ -274,13 +355,12 @@ class PMFLevel extends PMF{ return $this->chunks[$index][$Y]; } + public $checkLight = []; public function loadChunk($X, $Z){ - $index = self::getIndex($X, $Z); if($this->isChunkLoaded($X, $Z)){ return true; - }elseif(!isset($this->locationTable[$index])){ return false; } @@ -307,9 +387,27 @@ class PMFLevel extends PMF{ $this->chunks[$index][$Y] = false; } } + $this->checkLight[$index] = [$X, $Z]; + @gzclose($chunk); return true; } + + public function forceLightUpdatesIfNeeded(){ + foreach($this->checkLight as $index => [$X, $Z]){ + $update = false; + if(!$this->hasBlocklight($X, $Z)){ + ConsoleAPI::notice("Chunk $X $Z (Level: {$this->level->getName()} has no blocklight! Forcing light update(it might take a while)."); + $this->level->updateLight(0, $X*16, 0, $Z*16, $X*16+15, 127, $Z*16+15); + $update = true; + $this->markHasBlocklight($X, $Z); + } + + if($update) while($this->level->updateLights()); + unset($this->checkLight[$index]); + } + + } protected function fillMiniChunk($X, $Z, $Y){ if($this->isChunkLoaded($X, $Z) === false){ diff --git a/src/world/Level.php b/src/world/Level.php index 990d6782..6536dfc6 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -577,18 +577,71 @@ class Level{ } } + /** + * Gets block brightness based on skylight, blocklight and subtracts skyDarken. + * @param int $x + * @param int $y + * @param int $z + */ + public function getRawBrightness(int $x, int $y, int $z){ + //TODO special way to do it for some blocks + + $bl = $this->level->getBlockLight($x, $y, $z); + $l = 0; ///*$this->level->getBrightness(LIGHTLAYER_SKY, $x, $y, $z)*/ (15 - $this->skyDarken); no skylight is implemnted + if($bl > $l) $l = $bl; + if($l < 0) $l = 0; + return $l; + } + + /** + * Schedules light update. Make sure to check that coordinates are in range. + * @param int $layer - light layer(0 - block, 15 - sky(not implemented as of now)). + * @param int $minX - must be in range (0, 255). Must be less than maxX. + * @param int $minY - must be in range (0, 127). Must be less than maxY. + * @param int $minZ - must be in range (0, 255). Must be less than maxZ. + * @param int $maxX - must be in range (0, 255). Must be more than minX. + * @param int $maxY - must be in range (0, 127). Must be more than minY. + * @param int $maxZ - must be in range (0, 255). Must be more than minZ. + */ public function updateLight($layer, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ - /*$max = 5; TODO: expand lightupdate + /*$max = 5; no idea is it needed for block light $sz = $this->lightUpdateIndx; if($sz < $max) $max = $sz; for($i = 0; $i < $max; ++$i){ - $update = $this->lightUpdates[$i]; - if($update->layer == $layer && ($r = $update->expand($minX, $minY, $minZ, $maxX, $maxY, $maxZ))){ - --$this->lightUpdatesCount; + $upd = $this->lightUpdates[$i]; + $llayer = $upd >> 48; + + if($layer == $llayer){ + $maZ = $upd & 0xff; + $maY = ($upd >> 8) & 0xff; + $maX = ($upd >> 16) & 0xff; + $miZ = ($upd >> 24) & 0xff; + $miY = ($upd >> 32) & 0xff; + $miX = ($upd >> 40) & 0xff; + if($miX <= $minX && $miY <= $minY && $miZ <= $minZ && $maX >= $maxX && $maY >= $maxY && $maZ >= $maxZ){ + console("light happen is scheduled to happen already"); + return; + } + if($miX-1 > $minX || $miY-1 > $minY || $miZ-1 > $minZ || $maX+1 < $maxX || $maY+1 < $maxY || $maZ+1 < $maxZ){ + continue; + } + if($miX < $minX) $minX = $miX; + if($miY < $minY) $minY = $miY; + if($miZ < $minZ) $minZ = $miZ; + if($maX > $maxX) $maxX = $maX; + if($maY > $maxY) $maxY = $maY; + if($maZ > $maxZ) $maxZ = $maZ; + + if((($maxZ-$minZ)*($maX-$minY)*($maxX-$minX) - ($maX-$miX)*($maZ-$miZ)*($maY-$miY)) > 2){ + continue; + } + + $indx = $i; + goto update_light_in_array; } }*/ - - if($maxX < $minX){ + $indx = ++$this->lightUpdateIndx; + /*if($maxX < $minX){ $s = $minX; $minX = $maxX; $maxX = $s; @@ -603,6 +656,8 @@ class Level{ $minZ = $maxZ; $maxZ = $s; } + + update_light_in_array: if($minX < 0) $minX = 0; if($minX > 255) $minX = 255; if($minY < 0) $minY = 0; @@ -615,10 +670,10 @@ class Level{ if($maxY < 0) $minY = 0; if($maxY > 127) $minY = 127; if($maxZ < 0) $maxZ = 0; - if($maxZ > 255) $maxZ = 255; + if($maxZ > 255) $maxZ = 255;*/ $update = ($layer << 48) | ($minX << 40) | ($minY << 32) | ($minZ << 24) | ($maxX << 16) | ($maxY << 8) | ($maxZ); - $this->lightUpdates[++$this->lightUpdateIndx] = $update; + $this->lightUpdates[$indx] = $update; } public function updateLightIfOtherThan($layer, $x, $y, $z, $level){ @@ -655,6 +710,8 @@ class Level{ return false; } public function onTick(PocketMinecraftServer $server, $currentTime){ + $this->level->forceLightUpdatesIfNeeded(); + if(!$this->stopTime) ++$this->time; for($cX = 0; $cX < 16; ++$cX){ for($cZ = 0; $cZ < 16; ++$cZ){ diff --git a/src/world/MobSpawner.php b/src/world/MobSpawner.php index 48d63306..ff8de7ae 100644 --- a/src/world/MobSpawner.php +++ b/src/world/MobSpawner.php @@ -88,7 +88,7 @@ class MobSpawner{ $zMob = $z + mt_rand(-3, 3); - $y = $this->getSafeY($xMob, $zMob, $grassOnly, $type >= 32 && $type <= 36 && $type != 35); + $y = $this->getSafeY($xMob, $zMob, $grassOnly, $type >= 32 && $type <= 36 && $type != 35, $type >= 32 && $type <= 36); if(!$y || $y < 0){ continue; } @@ -125,7 +125,7 @@ class MobSpawner{ ]; } - protected function getSafeY($x, $z, $grassOnly = false, $highMob = false){ //first safe block //TODO check boundingbox + protected function getSafeY($x, $z, $grassOnly = false, $highMob = false, $isMonster = false){ //first safe block //TODO check boundingbox $allowed = []; for($y = 0; $y < 128; ++$y){ $b = $this->level->level->getBlockID($x, $y, $z); @@ -136,6 +136,9 @@ class MobSpawner{ (StaticBlock::getIsSolid($b1) && (!$grassOnly || $b1 === GRASS) && (!$highMob || !StaticBlock::getIsSolid($b2) && !StaticBlock::getIsLiquid($b2))) ){ + if($isMonster && ($rb = $this->level->getRawBrightness($x, $y, $z)) > 8){ //dont spawn if too bright + continue; + } $allowed[] = $y; } } From 54ef3882b9531a0144bd946698aa095a78ef7a40 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 24 Jun 2025 13:05:19 +0300 Subject: [PATCH 3/6] small changes --- src/API/ConsoleAPI.php | 2 +- src/world/Level.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index b32e415c..574e42e7 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -214,7 +214,7 @@ class ConsoleAPI{ foreach($this->server->api->level->levels as $level){ $lightUpdates += count($level->lightUpdates); } - return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]} Light updates: {$lightUpdates}"; + return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]}), Entities: {$info["entities"]}, Events: {$info["events"]}, Handlers: {$info["handlers"]}, Actions: {$info["actions"]}, Garbage: {$info["garbage"]}, Light updates: {$lightUpdates}"; } return "TPS: {$info["tps"]}, Memory usage: {$info["memory_usage"]} (Peak {$info["memory_peak_usage"]})"; case "stop": diff --git a/src/world/Level.php b/src/world/Level.php index 6536dfc6..868290c6 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -604,7 +604,7 @@ class Level{ * @param int $maxZ - must be in range (0, 255). Must be more than minZ. */ public function updateLight($layer, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ - /*$max = 5; no idea is it needed for block light + /*$max = 5; //no idea is it needed for block light $sz = $this->lightUpdateIndx; if($sz < $max) $max = $sz; for($i = 0; $i < $max; ++$i){ @@ -619,7 +619,7 @@ class Level{ $miY = ($upd >> 32) & 0xff; $miX = ($upd >> 40) & 0xff; if($miX <= $minX && $miY <= $minY && $miZ <= $minZ && $maX >= $maxX && $maY >= $maxY && $maZ >= $maxZ){ - console("light happen is scheduled to happen already"); + //console("light happen is scheduled to happen already"); return; } if($miX-1 > $minX || $miY-1 > $minY || $miZ-1 > $minZ || $maX+1 < $maxX || $maY+1 < $maxY || $maZ+1 < $maxZ){ From 207dd0ffcdae669c4ba8d500b945c6eec2801d81 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 24 Jun 2025 13:18:09 +0300 Subject: [PATCH 4/6] fixed update blocks not having lightblock value --- src/material/block/solid/InfoUpdate2Block.php | 1 + src/material/block/solid/InfoUpdateBlock.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/material/block/solid/InfoUpdate2Block.php b/src/material/block/solid/InfoUpdate2Block.php index 0bc5cafd..a8a29de6 100755 --- a/src/material/block/solid/InfoUpdate2Block.php +++ b/src/material/block/solid/InfoUpdate2Block.php @@ -8,6 +8,7 @@ class InfoUpdate2Block extends SolidBlock{ $this->hardness = 5; $this->breakTime = 1; $this->material = Material::$dirt; + $this->lightBlock = 255; } } \ No newline at end of file diff --git a/src/material/block/solid/InfoUpdateBlock.php b/src/material/block/solid/InfoUpdateBlock.php index cb7f2b8c..cad299b5 100755 --- a/src/material/block/solid/InfoUpdateBlock.php +++ b/src/material/block/solid/InfoUpdateBlock.php @@ -8,6 +8,7 @@ class InfoUpdateBlock extends SolidBlock{ $this->hardness = 5; $this->breakTime = 1; $this->material = Material::$dirt; + $this->lightBlock = 255; } } \ No newline at end of file From c2faff0f22962e4f5008a87df6b1bb0765883084 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 24 Jun 2025 16:58:28 +0300 Subject: [PATCH 5/6] added ability to disable light updates --- src/PocketMinecraftServer.php | 9 +++++++-- src/pmf/PMFLevel.php | 8 ++++++-- src/world/Level.php | 2 ++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index b779896a..2a0afd75 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -12,7 +12,7 @@ class PocketMinecraftServer{ public $doTick, $levelData, $tiles, $entities, $schedule, $scheduleCnt, $whitelist, $spawn, $difficulty, $stop, $asyncThread; public static $FORCE_20_TPS = false, $KEEP_CHUNKS_LOADED = true, $PACKET_READING_LIMIT = 100; - + public static $ENABLE_LIGHT_UPDATES = true; function __construct($name, $gamemode = SURVIVAL, $seed = false, $port = 19132, $serverip = "0.0.0.0"){ $this->port = (int) $port; $this->doTick = true; @@ -97,7 +97,8 @@ class PocketMinecraftServer{ "keep-items-on-death" => Entity::$keepInventory, "disable-emojis-in-chat" => Player::$disableEmojisInChat, "disable-emojis-on-signs" => Level::$disableEmojisOnSigns, - "allow-dropping-single-items" => Player::$allowDroppingSingleItems + "allow-dropping-single-items" => Player::$allowDroppingSingleItems, + "enable-light-updates" => self::$ENABLE_LIGHT_UPDATES, ]); Player::$disableEmojisInChat = $this->extraprops->get("disable-emojis-in-chat"); Level::$disableEmojisOnSigns = $this->extraprops->get("disable-emojis-on-signs"); @@ -109,6 +110,7 @@ class PocketMinecraftServer{ Living::$entityPushing = $this->extraprops->get("enable-mob-pushing"); self::$FORCE_20_TPS = $this->extraprops->get("force-20-tps"); self::$KEEP_CHUNKS_LOADED = $this->extraprops->get("keep-chunks-loaded"); + self::$ENABLE_LIGHT_UPDATES = $this->extraprops->get("enable-light-updates"); PocketMinecraftServer::$SAVE_PLAYER_DATA = $this->extraprops->get("save-player-data"); MobController::$ADVANCED = $this->extraprops->get("experimental-mob-ai"); Explosion::$enableExplosions = $this->extraprops->get("enable-explosions"); @@ -116,6 +118,9 @@ class PocketMinecraftServer{ if(self::$FORCE_20_TPS){ ConsoleAPI::warn("Forcing 20 tps. This may result in higher CPU usage!"); } + if(!self::$ENABLE_LIGHT_UPDATES){ + ConsoleAPI::warn("Light updates are disabled: this should decrease memory consumption and increase performance, but will prevent all existing light from updating and disable some light-dependant features."); + } if($this->extraprops->get("discord-msg")){ if($this->extraprops->get("discord-webhook-url") !== "none"){ console("[INFO] Discord Logger is enabled."); diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index af6e0be3..2e56276c 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -394,6 +394,10 @@ class PMFLevel extends PMF{ } public function forceLightUpdatesIfNeeded(){ + if(!PocketMinecraftServer::$ENABLE_LIGHT_UPDATES) { + $this->checkLight = []; + return; + } foreach($this->checkLight as $index => [$X, $Z]){ $update = false; if(!$this->hasBlocklight($X, $Z)){ @@ -508,7 +512,7 @@ class PMFLevel extends PMF{ } public function getBlockLight($x, $y, $z){ - if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127) return 0; + if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127 || !PocketMinecraftServer::$ENABLE_LIGHT_UPDATES) return 0; $X = $x >> 4; $Z = $z >> 4; $Y = $y >> 4; @@ -522,7 +526,7 @@ class PMFLevel extends PMF{ } public function setBlockLight($x, $y, $z, $value){ - if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127) return false; + if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127 || !PocketMinecraftServer::$ENABLE_LIGHT_UPDATES) return false; $X = $x >> 4; $Z = $z >> 4; $Y = $y >> 4; diff --git a/src/world/Level.php b/src/world/Level.php index 868290c6..ed779bb2 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -532,6 +532,7 @@ class Level{ public $lightUpdates = []; public $lightUpdateIndx = -1; public function handleBlockLightUpdate($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + if(!PocketMinecraftServer::$ENABLE_LIGHT_UPDATES) return; for($x = $minX; $x <= $maxX; ++$x){ for($z = $minZ; $z <= $maxZ; ++$z){ for($y = $minY; $y <= $maxY; ++$y){ @@ -604,6 +605,7 @@ class Level{ * @param int $maxZ - must be in range (0, 255). Must be more than minZ. */ public function updateLight($layer, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + if(!PocketMinecraftServer::$ENABLE_LIGHT_UPDATES) return; /*$max = 5; //no idea is it needed for block light $sz = $this->lightUpdateIndx; if($sz < $max) $max = $sz; From 23028a7db53818f24aa4c6132f3cfa9d915acbb7 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 24 Jun 2025 17:14:07 +0300 Subject: [PATCH 6/6] /xyz will display block brightness at player's position --- src/API/PlayerAPI.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index f288c661..75dd7055 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -284,8 +284,8 @@ class PlayerAPI{ $xChunk = $x >> 4; $zChunk = $z >> 4; - - return "$username's coordinates: X: $x ($xChunk), Y: $y, Z: $z ($zChunk), world: $level.\nDirection: $direction"; + $brightness = $ent->level->getRawBrightness((int)$x, (int)$y, (int)$z); + return "$username's coordinates: X: $x ($xChunk), Y: $y, Z: $z ($zChunk), world: $level.\nDirection: $direction Brightness: $brightness"; } return $output; }