From 8362ed4211629c7560e1c99404ab9128936a55a9 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 14 Oct 2025 12:05:06 +0300 Subject: [PATCH] added ticking-mode into server.properties, replaced force-20-tps with nodelay mode --- src/API/ServerAPI.php | 14 +++++++++++++- src/PocketMinecraftServer.php | 20 ++++++++++---------- src/network/UDPSocket.php | 11 +++++++++-- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 7cc5cab4..169a3598 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -136,7 +136,8 @@ class ServerAPI{ "rcon.password" => substr(base64_encode(Utils::getRandomBytes(20, false)), 3, 10), "auto-save" => true, "enable-mob-ai" => true, - "abort-reading-after-N-packets" => PocketMinecraftServer::$PACKET_READING_LIMIT + "abort-reading-after-N-packets" => PocketMinecraftServer::$PACKET_READING_LIMIT, + "ticking-mode" => "legacy", ], comments: [ "level-type" => [ "Alowed types:", @@ -146,6 +147,12 @@ class ServerAPI{ ], "abort-reading-after-N-packets" => [ "Sets max amount of packets the server can read before forcing tick check." + ], + "ticking-mode" => [ + "Sets ticking mode. Allowed modes:", + "legacy - sleep while waiting for the next tick. same ticking as in old PocketMine. The server won't use a lot of cpu, but tps may be not stable(causes 16 TPS on Windows)", + "nodelay - unused time is not used for anything.. May cause to use 100% of CPU.", + "netwait - uses unused time to wait for packets. Should always give max tps. Untested on windows." ] ]); Entity::$allowFly = $this->getProperty("allow-flight", true); @@ -156,6 +163,11 @@ class ServerAPI{ MobSpawner::$MOB_LIMIT = $this->getProperty("mobs-amount", 50); Entity::$allowedAI = $this->getProperty("enable-mob-ai", true); PocketMinecraftServer::$PACKET_READING_LIMIT = $this->getProperty("abort-reading-after-N-packets", PocketMinecraftServer::$PACKET_READING_LIMIT); + PocketMinecraftServer::$TICKING_MODE = match(strtolower($this->getProperty("ticking-mode"))){ + default => PocketMinecraftServer::TICK_LEGACY, + "nodelay" => PocketMinecraftServer::TICK_F20TPS, + "netwait" => PocketMinecraftServer::TICK_NETWAIT + }; //Load advanced properties define("DEBUG", $this->getProperty("debug", 1)); define("ADVANCED_CACHE", false); diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index fdbc0ffd..150e3e90 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -1,7 +1,9 @@ "NostalgiaCore Logger", "despawn-mobs" => true, "mob-despawn-ticks" => 18000, - "force-20-tps" => false, "enable-mob-pushing" => Living::$entityPushing, "keep-chunks-loaded" => self::$KEEP_CHUNKS_LOADED, "use-experimental-hotbar" => Player::$experimentalHotbar, @@ -132,9 +135,6 @@ class PocketMinecraftServer{ "If enabled it may cause performance issues when there are a lot of mobs in a small enclosed area", "If disabled it will cause small entity position desync(that should be fixed every time a server sends a new movement packet to the client)" ], - "force-20-tps" => [ - "Forces server to run at 20 tps on some platforms where sleeping for small amount of time is not possible(hi windows & 16 tps)", - ], "discord-ru-smiles" => [ "Replaces cyrillic symbols Ы Ь Ъ Ё with emojis when sending a message using discord webhook(as it is done in some custom clients)" ], @@ -151,13 +151,13 @@ class PocketMinecraftServer{ Living::$despawnMobs = $this->extraprops->get("despawn-mobs"); Living::$despawnTimer = $this->extraprops->get("mob-despawn-ticks"); 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"); Explosion::$enableExplosions = $this->extraprops->get("enable-explosions"); NetherReactorBlock::$enableReactor = $this->extraprops->get("enable-nether-reactor"); - if(self::$FORCE_20_TPS){ + if(self::$TICKING_MODE == self::TICK_F20TPS){ ConsoleAPI::warn("Forcing 20 tps. This may result in higher CPU usage!"); } if(self::$BLOCK_BREAKING_PROGRESS > 1){ @@ -540,7 +540,7 @@ class PocketMinecraftServer{ public function process() { $lastLoop = 0; - if(self::$FORCE_20_TPS){ + if(self::$TICKING_MODE != self::TICK_LEGACY){ while($this->stop === false){ $packetcnt = 0; while($packet = $this->interface->readPacket()){ diff --git a/src/network/UDPSocket.php b/src/network/UDPSocket.php index 15cf0b3c..260a1d4b 100644 --- a/src/network/UDPSocket.php +++ b/src/network/UDPSocket.php @@ -3,7 +3,6 @@ class UDPSocket{ public $connected, $sock, $server, $port; - function __construct($server, $port, $listen = false, $serverip = "0.0.0.0"){ $this->server = $server; $this->port = $port; @@ -42,6 +41,14 @@ class UDPSocket{ if($this->connected === false){ return false; } + if(PocketMinecraftServer::$TICKING_MODE == PocketMinecraftServer::TICK_NETWAIT){ + $read = [$this->sock]; + $next = (ServerAPI::request()->lastTick + 0.05); + $waitTime = $next - microtime(true); + if($waitTime < 0) $waitTime = 0; + $c = socket_select($read, $_, $_, 0, (int)($waitTime*1000000)); + if($c === false || $c <= 0) return false; + } return @socket_recvfrom($this->sock, $buf, 65535, 0, $source, $port); } @@ -52,4 +59,4 @@ class UDPSocket{ return @socket_sendto($this->sock, $data, strlen($data), 0, $dest, $port); } -} \ No newline at end of file +}