From 9784f8b6ebed3b4f80d5549e64e01b2f993d4573 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Thu, 7 Sep 2023 12:36:55 -0700 Subject: [PATCH] Skip writing players json when the server is empty and max players hasn't changed --- .../jpenilla/squaremap/common/task/UpdatePlayers.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/xyz/jpenilla/squaremap/common/task/UpdatePlayers.java b/common/src/main/java/xyz/jpenilla/squaremap/common/task/UpdatePlayers.java index acc7b1a6..ef392e6c 100644 --- a/common/src/main/java/xyz/jpenilla/squaremap/common/task/UpdatePlayers.java +++ b/common/src/main/java/xyz/jpenilla/squaremap/common/task/UpdatePlayers.java @@ -32,6 +32,8 @@ public class UpdatePlayers implements Runnable { private final DirectoryProvider directoryProvider; private final ServerAccess serverAccess; private final ConfigManager configManager; + private boolean prevEmpty = false; + private int prevMaxPlayers = Integer.MIN_VALUE; @Inject private UpdatePlayers( @@ -90,9 +92,16 @@ public void run() { }); }); + final int maxPlayers = this.serverAccess.maxPlayers(); + if (players.isEmpty() && this.prevEmpty && maxPlayers == this.prevMaxPlayers) { + return; + } + this.prevEmpty = players.isEmpty(); + this.prevMaxPlayers = maxPlayers; + final Map map = new HashMap<>(); map.put("players", players); - map.put("max", this.serverAccess.maxPlayers()); + map.put("max", maxPlayers); FileUtil.atomicWriteJsonAsync(this.directoryProvider.tilesDirectory().resolve("players.json"), map); }