From d654005dae5385bfd65e76d846e06bf4be04abd6 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Thu, 4 Sep 2025 11:58:14 +0100 Subject: [PATCH] Fix server ups active on first start --- exp_server_ups/instance.ts | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/exp_server_ups/instance.ts b/exp_server_ups/instance.ts index b0a97830..1783acc3 100644 --- a/exp_server_ups/instance.ts +++ b/exp_server_ups/instance.ts @@ -6,29 +6,42 @@ export class InstancePlugin extends BaseInstancePlugin { private gameTimes: number[] = []; async onStart() { - this.updateInterval = setInterval(this.updateUps.bind(this), this.instance.config.get("exp_server_ups.update_interval")); + if (!this.instance.config.get("factorio.settings")["auto_pause"]) { + this.setInterval(); + } } onExit() { - if (this.updateInterval) { - clearInterval(this.updateInterval); - } + this.clearInterval(); } async onInstanceConfigFieldChanged(field: string, curr: unknown): Promise { if (field === "exp_server_ups.update_interval") { - this.onExit(); - await this.onStart(); + this.clearInterval(); + this.setInterval(); } else if (field === "exp_server_ups.average_interval") { this.gameTimes.splice(curr as number); } } async onPlayerEvent(event: lib.PlayerEvent): Promise { - if (event.type === "join" && !this.updateInterval) { - await this.onStart(); - } else if (event.type === "leave" && this.instance.playersOnline.size == 0 && this.instance.config.get("factorio.settings")["auto_pause"] as boolean) { - this.onExit(); + if (event.type === "join") { + this.setInterval(); + } else if (event.type === "leave" && this.instance.playersOnline.size == 0 && this.instance.config.get("factorio.settings")["auto_pause"]) { + this.clearInterval(); + } + } + + setInterval() { + if (!this.updateInterval) { + this.updateInterval = setInterval(this.updateUps.bind(this), this.instance.config.get("exp_server_ups.update_interval")); + } + } + + clearInterval() { + if (this.updateInterval) { + clearInterval(this.updateInterval); + this.updateInterval = undefined; } }