Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autohost: Stop HttpServer when stopping MinecraftServer #89

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading