Skip to content

Commit

Permalink
autohost: Stop HttpServer when stopping MinecraftServer (#89)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
unilock authored Oct 16, 2023
1 parent 768af0c commit 656382e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Text> messageConsumer, Runnable runnable) {
Util.getIoWorkerExecutor().execute(() -> {
messageConsumer.accept(Text.literal("Starting resource pack generation..."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public void loadSettings(JsonElement settings) {
public void serverStarted(MinecraftServer server) {

}

@Override
public void serverStopped(MinecraftServer server) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 656382e

Please sign in to comment.