diff --git a/src/main/java/com/alpt/vtools/listeners/ServerCloser.java b/src/main/java/com/alpt/vtools/listeners/ServerCloser.java index da897d1..d5d23d5 100644 --- a/src/main/java/com/alpt/vtools/listeners/ServerCloser.java +++ b/src/main/java/com/alpt/vtools/listeners/ServerCloser.java @@ -101,7 +101,7 @@ public class ServerCloser { while (!lock.isEmpty()) { lock.poll().cancel(); } - lock.add(new Counter(INSTANCE, 1, "收到关机命令已经过了 %s 分钟,即将关机。").start()); + lock.add(new Counter(INSTANCE, 1).reason("收到关机命令已经过了 %s 分钟,即将关机。").noLastWarning().start()); } } @@ -110,7 +110,7 @@ public class ServerCloser { while (!lock.isEmpty()) { lock.poll().cancel(); } - lock.add(new Counter(INSTANCE, 60, "虽然关机被取消,但 %s 分钟内依旧没有玩家上线,即将关机。").start()); + lock.add(new Counter(INSTANCE, 60).reason("虽然关机被取消,但 %s 分钟内依旧没有玩家上线,即将关机。").start()); } } @@ -127,15 +127,25 @@ public class ServerCloser { private final int totalMin; private int minLeft; private boolean canceled = false; - private final String reason; + private String reason; + private static final String DEFAULT_REASON = "距离上一个玩家离开已经过了 %s 分钟,即将关机。"; + private boolean lastWarning = true; - protected Counter(ServerCloser instance, int minute) {this(instance, minute, null);} - - protected Counter(ServerCloser instance, int minute, String reason) { + protected Counter(ServerCloser instance, int minute) { this.instance = instance; totalMin = minute; minLeft = minute; - this.reason = Objects.requireNonNullElse(reason, "距离上一个玩家离开已经过了 %s 分钟,即将关机。"); + this.reason = DEFAULT_REASON; + } + + public Counter reason(String reason) { + this.reason = reason; + return this; + } + + public Counter noLastWarning() { + lastWarning = false; + return this; } protected synchronized void cancel() { @@ -159,22 +169,13 @@ public class ServerCloser { while (true) { if (canceled) return; TGBridge.setShuttingDown(minLeft); - try { - this.wait(MINUTE); - } catch (InterruptedException ignored) {} - if (canceled) return; - if (!instance.server.getAllPlayers().isEmpty()) { - instance.plugin.logger.error("ServerCloser: 定时器发现服务器有人。这不应发生,因为定时器本应该被直接打断。"); - TGBridge.error("ServerCloser: #bug @NaAlOH4 定时器发现服务器有人。这不应发生,因为定时器本应该被直接打断。"); - canceled = true; - } - if (canceled) return; - minLeft--; switch (minLeft) { case 1 -> { String msg = "服务器即将在一分钟后关机,使用 /fuck 以取消。"; instance.plugin.logger.info(msg); - TGBridge.log(msg); + if (lastWarning) { + TGBridge.log(msg); + } } case 0 -> { String msg = "ServerCloser: " + reason.formatted(totalMin); @@ -190,6 +191,19 @@ public class ServerCloser { } default -> instance.plugin.logger.info("服务器即将在 {} 分钟后关机", minLeft); } + + try { + this.wait(MINUTE); + } catch (InterruptedException ignored) {} + if (canceled) return; + if (!instance.server.getAllPlayers().isEmpty()) { + instance.plugin.logger.error("ServerCloser: 定时器发现服务器有人。这不应发生,因为定时器本应该被直接打断。"); + TGBridge.error("ServerCloser: #bug @NaAlOH4 定时器发现服务器有人。这不应发生,因为定时器本应该被直接打断。"); + canceled = true; + } + if (canceled) return; + + minLeft--; } } } diff --git a/src/main/java/de/strifel/VTools/listeners/TGBridge.java b/src/main/java/de/strifel/VTools/listeners/TGBridge.java index 34d602d..1ae6c9e 100644 --- a/src/main/java/de/strifel/VTools/listeners/TGBridge.java +++ b/src/main/java/de/strifel/VTools/listeners/TGBridge.java @@ -174,7 +174,7 @@ public class TGBridge { case "/shutdown" -> { if (server.getAllPlayers().isEmpty()) { ServerCloser.INSTANCE.fastShutdown(); - outbound("server will shutdown in 1 minute.\nuse /fuck to cancel."); + outbound("服务器即将在一分钟后关机,使用 /fuck 以取消。"); } else { outbound("still player online, can't shutdown."); }