added ticking-mode into server.properties, replaced force-20-tps with nodelay mode

This commit is contained in:
GameHerobrine 2025-10-14 12:05:06 +03:00
parent 52d03da938
commit 8362ed4211
3 changed files with 32 additions and 13 deletions

View file

@ -136,7 +136,8 @@ class ServerAPI{
"rcon.password" => substr(base64_encode(Utils::getRandomBytes(20, false)), 3, 10), "rcon.password" => substr(base64_encode(Utils::getRandomBytes(20, false)), 3, 10),
"auto-save" => true, "auto-save" => true,
"enable-mob-ai" => 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: [ ], comments: [
"level-type" => [ "level-type" => [
"Alowed types:", "Alowed types:",
@ -146,6 +147,12 @@ class ServerAPI{
], ],
"abort-reading-after-N-packets" => [ "abort-reading-after-N-packets" => [
"Sets max amount of packets the server can read before forcing tick check." "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); Entity::$allowFly = $this->getProperty("allow-flight", true);
@ -156,6 +163,11 @@ class ServerAPI{
MobSpawner::$MOB_LIMIT = $this->getProperty("mobs-amount", 50); MobSpawner::$MOB_LIMIT = $this->getProperty("mobs-amount", 50);
Entity::$allowedAI = $this->getProperty("enable-mob-ai", true); Entity::$allowedAI = $this->getProperty("enable-mob-ai", true);
PocketMinecraftServer::$PACKET_READING_LIMIT = $this->getProperty("abort-reading-after-N-packets", PocketMinecraftServer::$PACKET_READING_LIMIT); 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 //Load advanced properties
define("DEBUG", $this->getProperty("debug", 1)); define("DEBUG", $this->getProperty("debug", 1));
define("ADVANCED_CACHE", false); define("ADVANCED_CACHE", false);

View file

@ -1,7 +1,9 @@
<?php <?php
class PocketMinecraftServer{ class PocketMinecraftServer{
const TICK_LEGACY = 0;
const TICK_F20TPS = 1;
const TICK_NETWAIT = 2;
public $tCnt, $ticks; public $tCnt, $ticks;
public $extraprops, $serverID, $interface, $database, $version, $invisible, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $eidCnt, $custom, $description, $motd, $port, $saveEnabled; public $extraprops, $serverID, $interface, $database, $version, $invisible, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $eidCnt, $custom, $description, $motd, $port, $saveEnabled;
/** /**
@ -13,10 +15,12 @@ class PocketMinecraftServer{
*/ */
public $clients; public $clients;
private $serverip, $evCnt, $handCnt, $events, $eventsID, $handlers, $serverType, $lastTick, $memoryStats, $async = [], $asyncID = 0; public $serverip, $evCnt, $handCnt, $events, $eventsID, $handlers, $serverType, $lastTick, $memoryStats, $async = [], $asyncID = 0;
public static $TICKING_MODE = PocketMinecraftServer::TICK_LEGACY;
public $doTick, $levelData, $tiles, $entities, $schedule, $scheduleCnt, $whitelist, $spawn, $difficulty, $stop, $asyncThread; 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 $KEEP_CHUNKS_LOADED = true, $PACKET_READING_LIMIT = 100;
public static $BLOCK_BREAKING_PROGRESS = 0.8; public static $BLOCK_BREAKING_PROGRESS = 0.8;
public static $ENABLE_LIGHT_UPDATES = true; public static $ENABLE_LIGHT_UPDATES = true;
function __construct($name, $gamemode = SURVIVAL, $seed = false, $port = 19132, $serverip = "0.0.0.0"){ function __construct($name, $gamemode = SURVIVAL, $seed = false, $port = 19132, $serverip = "0.0.0.0"){
@ -95,7 +99,6 @@ class PocketMinecraftServer{
"discord-bot-name" => "NostalgiaCore Logger", "discord-bot-name" => "NostalgiaCore Logger",
"despawn-mobs" => true, "despawn-mobs" => true,
"mob-despawn-ticks" => 18000, "mob-despawn-ticks" => 18000,
"force-20-tps" => false,
"enable-mob-pushing" => Living::$entityPushing, "enable-mob-pushing" => Living::$entityPushing,
"keep-chunks-loaded" => self::$KEEP_CHUNKS_LOADED, "keep-chunks-loaded" => self::$KEEP_CHUNKS_LOADED,
"use-experimental-hotbar" => Player::$experimentalHotbar, "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 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)" "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" => [ "discord-ru-smiles" => [
"Replaces cyrillic symbols Ы Ь Ъ Ё with emojis when sending a message using discord webhook(as it is done in some custom clients)" "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::$despawnMobs = $this->extraprops->get("despawn-mobs");
Living::$despawnTimer = $this->extraprops->get("mob-despawn-ticks"); Living::$despawnTimer = $this->extraprops->get("mob-despawn-ticks");
Living::$entityPushing = $this->extraprops->get("enable-mob-pushing"); 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::$KEEP_CHUNKS_LOADED = $this->extraprops->get("keep-chunks-loaded");
self::$ENABLE_LIGHT_UPDATES = $this->extraprops->get("enable-light-updates"); self::$ENABLE_LIGHT_UPDATES = $this->extraprops->get("enable-light-updates");
PocketMinecraftServer::$SAVE_PLAYER_DATA = $this->extraprops->get("save-player-data"); PocketMinecraftServer::$SAVE_PLAYER_DATA = $this->extraprops->get("save-player-data");
Explosion::$enableExplosions = $this->extraprops->get("enable-explosions"); Explosion::$enableExplosions = $this->extraprops->get("enable-explosions");
NetherReactorBlock::$enableReactor = $this->extraprops->get("enable-nether-reactor"); 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!"); ConsoleAPI::warn("Forcing 20 tps. This may result in higher CPU usage!");
} }
if(self::$BLOCK_BREAKING_PROGRESS > 1){ if(self::$BLOCK_BREAKING_PROGRESS > 1){
@ -540,7 +540,7 @@ class PocketMinecraftServer{
public function process() public function process()
{ {
$lastLoop = 0; $lastLoop = 0;
if(self::$FORCE_20_TPS){ if(self::$TICKING_MODE != self::TICK_LEGACY){
while($this->stop === false){ while($this->stop === false){
$packetcnt = 0; $packetcnt = 0;
while($packet = $this->interface->readPacket()){ while($packet = $this->interface->readPacket()){

View file

@ -3,7 +3,6 @@
class UDPSocket{ class UDPSocket{
public $connected, $sock, $server, $port; public $connected, $sock, $server, $port;
function __construct($server, $port, $listen = false, $serverip = "0.0.0.0"){ function __construct($server, $port, $listen = false, $serverip = "0.0.0.0"){
$this->server = $server; $this->server = $server;
$this->port = $port; $this->port = $port;
@ -42,6 +41,14 @@ class UDPSocket{
if($this->connected === false){ if($this->connected === false){
return 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); return @socket_recvfrom($this->sock, $buf, 65535, 0, $source, $port);
} }