Skip to content

Commit

Permalink
fix: graceful shutdown of WebsocketServer on /stop
Browse files Browse the repository at this point in the history
  • Loading branch information
lafkpages committed Jul 5, 2024
1 parent 34e6332 commit 7be7115
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/main/java/com/replmc/ReplCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
Expand Down Expand Up @@ -42,16 +44,22 @@ public void onInitialize() {

websocketServer = new WebsocketServer();

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOGGER.info("Cleanup");
// Gracefully shutdown WebsocketServer on server /stop
ServerLifecycleEvents.SERVER_STOPPING.register((minecraftServer) -> {
LOGGER.info("Server stopping event");

websocketServer.shutdown();
}));
});

// test
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
LOGGER.info("Block interacted by" + player.getName());
BlockState block = world.getBlockState(hitResult.getBlockPos());

LOGGER.info("Block " + block + " interacted by " + player.getName());


return ActionResult.PASS;

});
}

Expand Down

0 comments on commit 7be7115

Please sign in to comment.