WebServer.getInstance to get the current web server

This commit is contained in:
JFronny 2021-11-10 17:04:41 +01:00
parent 8096596683
commit 39555b3270
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
2 changed files with 16 additions and 10 deletions

View File

@ -1,5 +1,7 @@
package io.gitlab.jfronny.libjf.web.api; package io.gitlab.jfronny.libjf.web.api;
import io.gitlab.jfronny.libjf.web.impl.JfWeb;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
@ -14,4 +16,8 @@ public interface WebServer {
void stop(); void stop();
void restart(); void restart();
boolean isActive(); boolean isActive();
static WebServer getInstance() {
return JfWeb.SERVER;
}
} }

View File

@ -16,25 +16,25 @@ import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
public class JfWeb implements ClientModInitializer, DedicatedServerModInitializer, ModInitializer { public class JfWeb implements ClientModInitializer, DedicatedServerModInitializer, ModInitializer {
public final WebServer server; public static final WebServer SERVER;
public JfWeb() { static {
JfWebConfig.ensureValidPort(); JfWebConfig.ensureValidPort();
server = new JfWebServer(JfWebConfig.port, JfWebConfig.maxConnections); SERVER = new JfWebServer(JfWebConfig.port, JfWebConfig.maxConnections);
} }
@Override @Override
public void onInitializeClient() { public void onInitializeClient() {
if (isEnabled()) { if (isEnabled()) {
ClientLifecycleEvents.CLIENT_STARTED.register(client -> server.restart()); ClientLifecycleEvents.CLIENT_STARTED.register(client -> SERVER.restart());
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> server.stop()); ClientLifecycleEvents.CLIENT_STOPPING.register(client -> SERVER.stop());
} }
} }
@Override @Override
public void onInitializeServer() { public void onInitializeServer() {
if (isEnabled()) { if (isEnabled()) {
ServerLifecycleEvents.SERVER_STARTED.register(client -> server.restart()); ServerLifecycleEvents.SERVER_STARTED.register(client -> SERVER.restart());
ServerLifecycleEvents.SERVER_STOPPED.register(client -> server.stop()); ServerLifecycleEvents.SERVER_STOPPED.register(client -> SERVER.stop());
} }
} }
@ -46,7 +46,7 @@ public class JfWeb implements ClientModInitializer, DedicatedServerModInitialize
LiteralArgumentBuilder<ServerCommandSource> web = CommandManager.literal("web"); LiteralArgumentBuilder<ServerCommandSource> web = CommandManager.literal("web");
base.then(web); base.then(web);
web.executes(context -> { web.executes(context -> {
if (server.isActive()) { if (SERVER.isActive()) {
context.getSource().sendFeedback(new LiteralText("LibWeb is active. Use libweb restart to reload"), false); context.getSource().sendFeedback(new LiteralText("LibWeb is active. Use libweb restart to reload"), false);
} }
else { else {
@ -57,7 +57,7 @@ public class JfWeb implements ClientModInitializer, DedicatedServerModInitialize
web.then(CommandManager.literal("restart").executes(context -> { web.then(CommandManager.literal("restart").executes(context -> {
try { try {
context.getSource().sendFeedback(new LiteralText("Restarting LibWeb"), true); context.getSource().sendFeedback(new LiteralText("Restarting LibWeb"), true);
server.restart(); SERVER.restart();
} }
catch (Exception e) { catch (Exception e) {
LibJf.LOGGER.error("Failed to run restart command", e); LibJf.LOGGER.error("Failed to run restart command", e);
@ -68,7 +68,7 @@ public class JfWeb implements ClientModInitializer, DedicatedServerModInitialize
dispatcher.register(base); dispatcher.register(base);
}); });
} }
Runtime.getRuntime().addShutdownHook(new Thread(server::stop)); Runtime.getRuntime().addShutdownHook(new Thread(SERVER::stop));
} }
private boolean isEnabled() { private boolean isEnabled() {