package io.gitlab.jfronny.dynres; import com.mojang.brigadier.Command; import io.gitlab.jfronny.dynres.web.RequestHandler; import io.gitlab.jfronny.dynres.web.bluemapcore.WebServer; import me.sargunvohra.mcmods.autoconfig1u.AutoConfig; import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer; import net.fabricmc.api.DedicatedServerModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.gui.FabricGuiEntry; import net.minecraft.server.command.CommandManager; import net.minecraft.text.LiteralText; import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; @Environment(EnvType.SERVER) public class DynRes implements DedicatedServerModInitializer { static WebServer server; public static File resFile; public static Cfg cfg; public static final String baseLink = "http://127.0.0.1/"; static { AutoConfig.register(Cfg.class, JanksonConfigSerializer::new); cfg = AutoConfig.getConfigHolder(Cfg.class).getConfig(); resFile = new File(FabricLoader.getInstance().getGameDir().toFile(), cfg.resourcesFile); if (!resFile.isFile()) { FabricGuiEntry.displayCriticalError(new FileNotFoundException("The file " + resFile + " does not exist in the game directory but is required"), true); } } @Override public void onInitializeServer() { server = new WebServer(cfg.port, cfg.maxConnections, null, new RequestHandler("resources.zip")); server.start(); ServerLifecycleEvents.SERVER_STOPPED.register(t -> { try { server.close(); server.join(); } catch (Throwable e) { System.err.println("Failed to stop web server"); e.printStackTrace(); } }); CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> { if (dedicated) { dispatcher.register(CommandManager.literal("dynres").requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4)) .executes(context -> { context.getSource().sendFeedback(new LiteralText("DynRes is active. Use dynres restart to reload"), false); return Command.SINGLE_SUCCESS; })); dispatcher.register(CommandManager.literal("dynres").requires((serverCommandSource) -> serverCommandSource.hasPermissionLevel(4)) .then(CommandManager.literal("restart").executes(context -> { try { context.getSource().sendFeedback(new LiteralText("Restarting DynRes"), true); cfg = AutoConfig.getConfigHolder(Cfg.class).getConfig(); server.setPort(cfg.port); server.setMaxConnections(cfg.maxConnections); resFile = new File(FabricLoader.getInstance().getGameDir().toFile(), cfg.resourcesFile); server.close(); server.start(); context.getSource().sendFeedback(new LiteralText("DynRes restarted"), true); } catch (Exception e) { e.printStackTrace(); context.getSource().sendError(new LiteralText(e.getMessage())); } return Command.SINGLE_SUCCESS; }))); } else { System.err.println("DYNRES SHOULD NOT BE RUN ON INTERNAL SERVERS!"); } }); } public static String getPort() { return Integer.toString(server.getPort()); } public static String simplifyElement(String s) { String path = s.toLowerCase(); if (path.startsWith("/")) path = path.substring(1); if (path.endsWith("/")) path = path.substring(0, path.length() - 1); return path; } public static String removePort(String s) { String[] r = s.split(":"); if (r.length > 2) r = Arrays.copyOf(r, 2); return String.join(":", r); } }