From 26dea42283afc3b89a64e19e1073d3aa8c2d4418 Mon Sep 17 00:00:00 2001 From: ShockedPlot7560 Date: Wed, 20 Dec 2023 12:03:05 +0100 Subject: [PATCH 1/4] implement blocks tick per world API --- resources/pocketmine.yml | 3 ++ src/YmlServerProperties.php | 1 + .../world/WorldTickedBlocksChangeEvent.php | 44 +++++++++++++++++++ src/world/World.php | 34 +++++++++++++- 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/event/world/WorldTickedBlocksChangeEvent.php diff --git a/resources/pocketmine.yml b/resources/pocketmine.yml index 408b5b95b3d..af3f3e1c0ed 100644 --- a/resources/pocketmine.yml +++ b/resources/pocketmine.yml @@ -127,6 +127,9 @@ chunk-ticking: #Number of blocks inside ticking areas' subchunks that get ticked every tick. Higher values will accelerate events #like tree and plant growth, but at a higher performance cost. blocks-per-subchunk-per-tick: 3 + blocks-per-subchunk-per-world-per-tick: + # List of worlds with custom blocks-per-subchunk-per-tick values + # world: 5 #IDs of blocks not to perform random ticking on. disable-block-ticking: #- grass diff --git a/src/YmlServerProperties.php b/src/YmlServerProperties.php index 9bd203eef85..00577719ea0 100644 --- a/src/YmlServerProperties.php +++ b/src/YmlServerProperties.php @@ -60,6 +60,7 @@ private function __construct(){ public const CHUNK_SENDING_SPAWN_RADIUS = 'chunk-sending.spawn-radius'; public const CHUNK_TICKING = 'chunk-ticking'; public const CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_TICK = 'chunk-ticking.blocks-per-subchunk-per-tick'; + public const CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_WORLD_PER_TICK = 'chunk-ticking.blocks-per-subchunk-per-world-per-tick'; public const CHUNK_TICKING_DISABLE_BLOCK_TICKING = 'chunk-ticking.disable-block-ticking'; public const CHUNK_TICKING_TICK_RADIUS = 'chunk-ticking.tick-radius'; public const CONSOLE = 'console'; diff --git a/src/event/world/WorldTickedBlocksChangeEvent.php b/src/event/world/WorldTickedBlocksChangeEvent.php new file mode 100644 index 00000000000..07c96b0b02e --- /dev/null +++ b/src/event/world/WorldTickedBlocksChangeEvent.php @@ -0,0 +1,44 @@ +oldNumber; + } + + public function getNewNumberOfBlocks() : int{ + return $this->newNumber; + } +} diff --git a/src/world/World.php b/src/world/World.php index 2ff2b5b2ea9..a0198095ba9 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -57,6 +57,7 @@ use pocketmine\event\world\WorldParticleEvent; use pocketmine\event\world\WorldSaveEvent; use pocketmine\event\world\WorldSoundEvent; +use pocketmine\event\world\WorldTickedBlocksChangeEvent; use pocketmine\item\Item; use pocketmine\item\ItemUseResult; use pocketmine\item\LegacyStringToItemParser; @@ -119,6 +120,8 @@ use function get_class; use function gettype; use function is_a; +use function is_array; +use function is_int; use function is_object; use function lcg_value; use function max; @@ -520,7 +523,7 @@ public function __construct( $this->logger->warning("\"chunk-ticking.per-tick\" setting is deprecated, but you've used it to disable chunk ticking. Set \"chunk-ticking.tick-radius\" to 0 in \"pocketmine.yml\" instead."); $this->chunkTickRadius = 0; } - $this->tickedBlocksPerSubchunkPerTick = $cfg->getPropertyInt(YmlServerProperties::CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_TICK, self::DEFAULT_TICKED_BLOCKS_PER_SUBCHUNK_PER_TICK); + $this->initTickedBlocksPerSubchunkPerTick($cfg); $this->maxConcurrentChunkPopulationTasks = $cfg->getPropertyInt(YmlServerProperties::CHUNK_GENERATION_POPULATION_QUEUE_SIZE, 2); $this->initRandomTickBlocksFromConfig($cfg); @@ -3461,4 +3464,33 @@ public function unloadChunks(bool $force = false) : void{ } } } + + private function initTickedBlocksPerSubchunkPerTick(ServerConfigGroup $cfg) : void { + $defaultTick = $cfg->getPropertyInt(YmlServerProperties::CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_TICK, self::DEFAULT_TICKED_BLOCKS_PER_SUBCHUNK_PER_TICK); + $worlds = $cfg->getProperty(YmlServerProperties::CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_WORLD_PER_TICK, []); + if(!is_array($worlds)){ + throw new \InvalidArgumentException("Expected array for " . YmlServerProperties::CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_WORLD_PER_TICK . " but got " . gettype($worlds)); + } + foreach($worlds as $world => $blocksPerTick){ + if($world === $this->getFolderName()){ + if(is_int($blocksPerTick) && $blocksPerTick >= 0){ + $this->setTickedBlocksPerSubchunkPerTick($blocksPerTick); + return; + } + } + } + $this->setTickedBlocksPerSubchunkPerTick($defaultTick); + } + + public function setTickedBlocksPerSubchunkPerTick(int $tickedBlocksPerSubchunkPerTick) : void{ + $old = $this->tickedBlocksPerSubchunkPerTick ?? null; + $this->tickedBlocksPerSubchunkPerTick = $tickedBlocksPerSubchunkPerTick; + if($old !== null){ + (new WorldTickedBlocksChangeEvent($this, $old, $tickedBlocksPerSubchunkPerTick))->call(); + } + } + + public function getTickedBlocksPerSubchunkPerTick() : int{ + return $this->tickedBlocksPerSubchunkPerTick; + } } From 83743f02dff096787668223837094823150c0554 Mon Sep 17 00:00:00 2001 From: ShockedPlot7560 Date: Fri, 29 Mar 2024 22:47:54 +0100 Subject: [PATCH 2/4] redesign the system, moove the property --- resources/pocketmine.yml | 4 +- src/YmlServerProperties.php | 5 ++- .../world/WorldTickedBlocksChangeEvent.php | 44 ------------------- src/world/World.php | 30 +++---------- 4 files changed, 10 insertions(+), 73 deletions(-) delete mode 100644 src/event/world/WorldTickedBlocksChangeEvent.php diff --git a/resources/pocketmine.yml b/resources/pocketmine.yml index af3f3e1c0ed..d6223496458 100644 --- a/resources/pocketmine.yml +++ b/resources/pocketmine.yml @@ -127,9 +127,6 @@ chunk-ticking: #Number of blocks inside ticking areas' subchunks that get ticked every tick. Higher values will accelerate events #like tree and plant growth, but at a higher performance cost. blocks-per-subchunk-per-tick: 3 - blocks-per-subchunk-per-world-per-tick: - # List of worlds with custom blocks-per-subchunk-per-tick values - # world: 5 #IDs of blocks not to perform random ticking on. disable-block-ticking: #- grass @@ -218,6 +215,7 @@ worlds: # seed: 404 # generator: FLAT # preset: 2;bedrock,59xstone,3xdirt,grass;1 + # blocks-per-subchunk-per-tick: xx plugins: #Setting this to true will cause the legacy structure to be used where plugin data is placed inside the --plugins dir. diff --git a/src/YmlServerProperties.php b/src/YmlServerProperties.php index 00577719ea0..a2aa3136a7b 100644 --- a/src/YmlServerProperties.php +++ b/src/YmlServerProperties.php @@ -60,7 +60,6 @@ private function __construct(){ public const CHUNK_SENDING_SPAWN_RADIUS = 'chunk-sending.spawn-radius'; public const CHUNK_TICKING = 'chunk-ticking'; public const CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_TICK = 'chunk-ticking.blocks-per-subchunk-per-tick'; - public const CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_WORLD_PER_TICK = 'chunk-ticking.blocks-per-subchunk-per-world-per-tick'; public const CHUNK_TICKING_DISABLE_BLOCK_TICKING = 'chunk-ticking.disable-block-ticking'; public const CHUNK_TICKING_TICK_RADIUS = 'chunk-ticking.tick-radius'; public const CONSOLE = 'console'; @@ -116,4 +115,8 @@ private function __construct(){ public const TIMINGS = 'timings'; public const TIMINGS_HOST = 'timings.host'; public const WORLDS = 'worlds'; + + public static function WORLD_BLOCKS_PER_SUBCHUNK_PER_TICK(string $worldName) : string{ + return "worlds.$worldName.blocks-per-subchunk-per-tick"; + } } diff --git a/src/event/world/WorldTickedBlocksChangeEvent.php b/src/event/world/WorldTickedBlocksChangeEvent.php deleted file mode 100644 index 07c96b0b02e..00000000000 --- a/src/event/world/WorldTickedBlocksChangeEvent.php +++ /dev/null @@ -1,44 +0,0 @@ -oldNumber; - } - - public function getNewNumberOfBlocks() : int{ - return $this->newNumber; - } -} diff --git a/src/world/World.php b/src/world/World.php index a0198095ba9..1dce773a5b3 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -57,7 +57,6 @@ use pocketmine\event\world\WorldParticleEvent; use pocketmine\event\world\WorldSaveEvent; use pocketmine\event\world\WorldSoundEvent; -use pocketmine\event\world\WorldTickedBlocksChangeEvent; use pocketmine\item\Item; use pocketmine\item\ItemUseResult; use pocketmine\item\LegacyStringToItemParser; @@ -120,8 +119,6 @@ use function get_class; use function gettype; use function is_a; -use function is_array; -use function is_int; use function is_object; use function lcg_value; use function max; @@ -3466,31 +3463,14 @@ public function unloadChunks(bool $force = false) : void{ } private function initTickedBlocksPerSubchunkPerTick(ServerConfigGroup $cfg) : void { - $defaultTick = $cfg->getPropertyInt(YmlServerProperties::CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_TICK, self::DEFAULT_TICKED_BLOCKS_PER_SUBCHUNK_PER_TICK); - $worlds = $cfg->getProperty(YmlServerProperties::CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_WORLD_PER_TICK, []); - if(!is_array($worlds)){ - throw new \InvalidArgumentException("Expected array for " . YmlServerProperties::CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_WORLD_PER_TICK . " but got " . gettype($worlds)); - } - foreach($worlds as $world => $blocksPerTick){ - if($world === $this->getFolderName()){ - if(is_int($blocksPerTick) && $blocksPerTick >= 0){ - $this->setTickedBlocksPerSubchunkPerTick($blocksPerTick); - return; - } - } - } - $this->setTickedBlocksPerSubchunkPerTick($defaultTick); - } - - public function setTickedBlocksPerSubchunkPerTick(int $tickedBlocksPerSubchunkPerTick) : void{ - $old = $this->tickedBlocksPerSubchunkPerTick ?? null; - $this->tickedBlocksPerSubchunkPerTick = $tickedBlocksPerSubchunkPerTick; - if($old !== null){ - (new WorldTickedBlocksChangeEvent($this, $old, $tickedBlocksPerSubchunkPerTick))->call(); + $this->tickedBlocksPerSubchunkPerTick = $cfg->getPropertyInt(YmlServerProperties::CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_TICK, self::DEFAULT_TICKED_BLOCKS_PER_SUBCHUNK_PER_TICK); + $specificTick = $cfg->getPropertyInt(YmlServerProperties::WORLD_BLOCKS_PER_SUBCHUNK_PER_TICK($this->getFolderName()), -1); + if($specificTick >= 0){ + $this->tickedBlocksPerSubchunkPerTick = $specificTick; } } - public function getTickedBlocksPerSubchunkPerTick() : int{ + public function getTickedBlocksPerSubchunkPerTick() : int{ return $this->tickedBlocksPerSubchunkPerTick; } } From d145095ce0ed6a60fd8ef9e54c2f9f2327bc570f Mon Sep 17 00:00:00 2001 From: ShockedPlot7560 Date: Fri, 29 Mar 2024 22:49:22 +0100 Subject: [PATCH 3/4] oops removed the setter --- src/world/World.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/world/World.php b/src/world/World.php index 1dce773a5b3..8d94992c3a4 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -3470,6 +3470,13 @@ private function initTickedBlocksPerSubchunkPerTick(ServerConfigGroup $cfg) : vo } } + public function setTickedBlocksPerSubchunkPerTick(int $tickedBlocksPerSubchunkPerTick) : void{ + if($tickedBlocksPerSubchunkPerTick < 0){ + throw new \InvalidArgumentException("Ticked blocks per subchunk per tick must be non-negative"); + } + $this->tickedBlocksPerSubchunkPerTick = $tickedBlocksPerSubchunkPerTick; + } + public function getTickedBlocksPerSubchunkPerTick() : int{ return $this->tickedBlocksPerSubchunkPerTick; } From 5d032e1bc7c5c766f0621b81f19942051078de83 Mon Sep 17 00:00:00 2001 From: ShockedPlot7560 Date: Fri, 29 Mar 2024 22:58:37 +0100 Subject: [PATCH 4/4] fix code consistency --- src/YmlServerProperties.php | 4 ---- src/world/World.php | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/YmlServerProperties.php b/src/YmlServerProperties.php index a2aa3136a7b..9bd203eef85 100644 --- a/src/YmlServerProperties.php +++ b/src/YmlServerProperties.php @@ -115,8 +115,4 @@ private function __construct(){ public const TIMINGS = 'timings'; public const TIMINGS_HOST = 'timings.host'; public const WORLDS = 'worlds'; - - public static function WORLD_BLOCKS_PER_SUBCHUNK_PER_TICK(string $worldName) : string{ - return "worlds.$worldName.blocks-per-subchunk-per-tick"; - } } diff --git a/src/world/World.php b/src/world/World.php index 8d94992c3a4..1f35c9e47b7 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -3464,7 +3464,7 @@ public function unloadChunks(bool $force = false) : void{ private function initTickedBlocksPerSubchunkPerTick(ServerConfigGroup $cfg) : void { $this->tickedBlocksPerSubchunkPerTick = $cfg->getPropertyInt(YmlServerProperties::CHUNK_TICKING_BLOCKS_PER_SUBCHUNK_PER_TICK, self::DEFAULT_TICKED_BLOCKS_PER_SUBCHUNK_PER_TICK); - $specificTick = $cfg->getPropertyInt(YmlServerProperties::WORLD_BLOCKS_PER_SUBCHUNK_PER_TICK($this->getFolderName()), -1); + $specificTick = $cfg->getPropertyInt("worlds." . $this->getFolderName() . ".blocks-per-subchunk-per-tick", -1); if($specificTick >= 0){ $this->tickedBlocksPerSubchunkPerTick = $specificTick; }