From 85e1bf1ca409e2dd85f72b41f576a342db1db9cf Mon Sep 17 00:00:00 2001 From: Sodium-Aluminate Date: Mon, 25 Dec 2023 00:03:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E7=90=86=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E5=88=86=E9=92=9F=E4=B8=8D=E6=9B=B4=E6=96=B0=E7=BD=AE=E9=A1=B6?= =?UTF-8?q?=E7=9A=84=E9=A1=BA=E5=BA=8F=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E4=BD=BF=E7=94=A8shutdown=E5=91=BD=E4=BB=A4=E5=8F=8C?= =?UTF-8?q?=E9=87=8D=E6=8F=90=E7=A4=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alpt/vtools/listeners/ServerCloser.java | 52 ++++++++++++------- .../de/strifel/VTools/listeners/TGBridge.java | 2 +- 2 files changed, 34 insertions(+), 20 deletions(-) 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."); }