Skip to content

Commit

Permalink
Wait for server to start in WorldBuilder.createServer (#4367)
Browse files Browse the repository at this point in the history
* Wait for server to start in WorldBuilder.createServer

* Wait for serverCanAcceptTasks
  • Loading branch information
Earthcomputer authored Jan 14, 2025
1 parent 6aa3fa3 commit 20da279
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import net.minecraft.server.dedicated.MinecraftDedicatedServer;
import net.minecraft.util.Util;

import net.fabricmc.fabric.api.client.gametest.v1.ClientGameTestContext;

public final class DedicatedServerImplUtil {
private static final Logger LOGGER = LoggerFactory.getLogger("fabric-client-gametest-api-v1");
private static final Properties DEFAULT_SERVER_PROPERTIES = Util.make(new Properties(), properties -> {
Expand All @@ -59,19 +61,24 @@ public final class DedicatedServerImplUtil {
private DedicatedServerImplUtil() {
}

public static MinecraftDedicatedServer start(Properties serverProperties) {
public static MinecraftDedicatedServer start(ClientGameTestContext context, Properties serverProperties) {
setupServer(serverProperties);
serverFuture = new CompletableFuture<>();

new Thread(() -> Main.main(new String[0])).start();

MinecraftDedicatedServer server;

try {
return serverFuture.get(10, TimeUnit.SECONDS);
server = serverFuture.get(10, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new RuntimeException(e);
} finally {
serverFuture = null;
}

context.waitFor(client -> ThreadingImpl.isServerRunning && ThreadingImpl.serverCanAcceptTasks);
return server;
}

private static void setupServer(Properties customServerProperties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public TestDedicatedServerContext createServer(Properties serverProperties) {
DedicatedServerImplUtil.saveLevelDataTo = null;
}

MinecraftDedicatedServer server = DedicatedServerImplUtil.start(serverProperties);
MinecraftDedicatedServer server = DedicatedServerImplUtil.start(context, serverProperties);
return new TestDedicatedServerContextImpl(context, server);
}

Expand Down

0 comments on commit 20da279

Please sign in to comment.