From 656382ebce13d1a177ef6c96ea63c7567dc0863b Mon Sep 17 00:00:00 2001 From: Katherine Date: Mon, 16 Oct 2023 06:36:34 -0400 Subject: [PATCH] autohost: Stop HttpServer when stopping MinecraftServer (#89) Fixes dedicated server apparently freezing when stopping via /stop command; now exits cleanly Mixin uses the same injection point as Fabric API's ServerLifecycleEvents.SERVER_STOPPED Signed-off-by: unilock --- .../polymer/autohost/api/ResourcePackDataProvider.java | 1 + .../main/java/eu/pb4/polymer/autohost/impl/AutoHost.java | 4 ++++ .../polymer/autohost/impl/providers/EmptyProvider.java | 5 +++++ .../autohost/impl/providers/WebServerProvider.java | 8 +++++++- .../pb4/polymer/autohost/mixin/MinecraftServerMixin.java | 5 +++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/api/ResourcePackDataProvider.java b/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/api/ResourcePackDataProvider.java index f4dd7a16..676affe7 100644 --- a/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/api/ResourcePackDataProvider.java +++ b/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/api/ResourcePackDataProvider.java @@ -15,6 +15,7 @@ public interface ResourcePackDataProvider { JsonElement saveSettings(); void loadSettings(JsonElement settings); void serverStarted(MinecraftServer server); + void serverStopped(MinecraftServer server); static ResourcePackDataProvider getActive() { return AutoHost.provider; diff --git a/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/AutoHost.java b/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/AutoHost.java index 366e8947..e3735c0a 100644 --- a/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/AutoHost.java +++ b/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/AutoHost.java @@ -57,6 +57,10 @@ public static void init(MinecraftServer server) { provider.serverStarted(server); } + public static void end(MinecraftServer server) { + provider.serverStopped(server); + } + public static void generateAndCall(MinecraftServer server, Consumer messageConsumer, Runnable runnable) { Util.getIoWorkerExecutor().execute(() -> { messageConsumer.accept(Text.literal("Starting resource pack generation...")); diff --git a/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/providers/EmptyProvider.java b/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/providers/EmptyProvider.java index 5658db23..3513dca1 100644 --- a/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/providers/EmptyProvider.java +++ b/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/providers/EmptyProvider.java @@ -38,4 +38,9 @@ public void loadSettings(JsonElement settings) { public void serverStarted(MinecraftServer server) { } + + @Override + public void serverStopped(MinecraftServer server) { + + } } diff --git a/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/providers/WebServerProvider.java b/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/providers/WebServerProvider.java index 99840087..46782795 100644 --- a/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/providers/WebServerProvider.java +++ b/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/impl/providers/WebServerProvider.java @@ -23,6 +23,7 @@ public class WebServerProvider implements ResourcePackDataProvider { private Config config; + private HttpServer server; public long size = 0; public String hash = ""; public long lastUpdate = 0; @@ -35,7 +36,7 @@ public class WebServerProvider implements ResourcePackDataProvider { public void serverStarted(MinecraftServer minecraftServer) { try { var address = createBindAddress(minecraftServer, config); - var server = HttpServer.create(address, 0); + server = HttpServer.create(address, 0); server.createContext("/", this::handle); server.setExecutor(Executors.newFixedThreadPool(2)); @@ -65,6 +66,11 @@ public void serverStarted(MinecraftServer minecraftServer) { } } + @Nullable + public void serverStopped(MinecraftServer minecraftServer) { + server.stop(0); + } + private void updateHash() { try { hash = com.google.common.io.Files.asByteSource(PolymerResourcePackUtils.DEFAULT_PATH.toFile()).hash(Hashing.sha1()).toString(); diff --git a/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/mixin/MinecraftServerMixin.java b/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/mixin/MinecraftServerMixin.java index c1a5b240..b25879ed 100644 --- a/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/mixin/MinecraftServerMixin.java +++ b/polymer-autohost/src/main/java/eu/pb4/polymer/autohost/mixin/MinecraftServerMixin.java @@ -13,4 +13,9 @@ public class MinecraftServerMixin { private void polymer_autohost_init(CallbackInfo ci) { AutoHost.init((MinecraftServer) (Object) this); } + + @Inject(method = "shutdown", at = @At("TAIL")) + private void polymer_autohost_end(CallbackInfo ci) { + AutoHost.end((MinecraftServer) (Object) this); + } }