74 lines
2.9 KiB
Java
74 lines
2.9 KiB
Java
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 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.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;
|
|
|
|
//TODO host the pack
|
|
@Environment(EnvType.SERVER)
|
|
public class DynRes implements DedicatedServerModInitializer {
|
|
static WebServer server;
|
|
public static final String resFile = "resources.zip";
|
|
public static final String baseLink = "http://127.0.0.1/";
|
|
@Override
|
|
public void onInitializeServer() {
|
|
//TODO allow setting max connections or fixed port
|
|
//TODO allow setting custom resources path
|
|
File f = new File(FabricLoader.getInstance().getGameDir().toFile(), resFile);
|
|
if (!f.isFile()) {
|
|
FabricGuiEntry.displayCriticalError(new FileNotFoundException("The file resources.zip does not exist in the game directory but is required"), true);
|
|
return;
|
|
}
|
|
|
|
server = new WebServer(0, 20, null, new RequestHandler(f, resFile));
|
|
server.start();
|
|
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
|
|
if (dedicated) {
|
|
dispatcher.register(CommandManager.literal("dynres").executes(context -> {
|
|
context.getSource().sendFeedback(new LiteralText("DynRes is active. Use dynres restart if it breaks"), false);
|
|
return Command.SINGLE_SUCCESS;
|
|
}));
|
|
dispatcher.register(CommandManager.literal("dynres").then(CommandManager.literal("restart")).executes(context -> {
|
|
context.getSource().sendFeedback(new LiteralText("Restarting DynRes"), true);
|
|
server.start();
|
|
context.getSource().sendFeedback(new LiteralText("DynRes restarted"), true);
|
|
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);
|
|
}
|
|
}
|