mirror of
https://github.com/JonasunderscoreJones/McWebserver.git
synced 2025-10-23 03:19:19 +02:00
some fixes I assume
This commit is contained in:
parent
385879c305
commit
8d9deef4a6
7 changed files with 149 additions and 118 deletions
|
@ -31,6 +31,8 @@ dependencies {
|
|||
// These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time.
|
||||
|
||||
// modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}"
|
||||
|
||||
implementation files('libs/java-curl-1.2.2.jar')
|
||||
}
|
||||
|
||||
processResources {
|
||||
|
|
BIN
libs/java-curl-1.2.2.jar
Normal file
BIN
libs/java-curl-1.2.2.jar
Normal file
Binary file not shown.
|
@ -1,12 +1,15 @@
|
|||
package me.jonasjones.mcwebserver;
|
||||
|
||||
import com.roxstudio.utils.CUrl;
|
||||
import me.jonasjones.mcwebserver.config.ModConfigs;
|
||||
import me.jonasjones.mcwebserver.web.HTTPServer;
|
||||
import me.jonasjones.mcwebserver.web.ServerHandler;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static me.jonasjones.mcwebserver.config.ModConfigs.WEB_PORT;
|
||||
|
||||
public class McWebserver implements ModInitializer {
|
||||
// This logger is used to write text to the console and the log file.
|
||||
|
@ -15,6 +18,9 @@ public class McWebserver implements ModInitializer {
|
|||
public static String MOD_ID = "mcwebserver";
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
||||
public static final Logger VERBOSELOGGER = LoggerFactory.getLogger(MOD_ID + " - VERBOSE LOGGER");
|
||||
private static ServerHandler webserver = new ServerHandler();
|
||||
public static Thread webserverthread = new Thread(webserver);
|
||||
public static boolean mcserveractive = true;
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
|
@ -24,15 +30,30 @@ public class McWebserver implements ModInitializer {
|
|||
|
||||
LOGGER.info("McWebserver initialized!");
|
||||
|
||||
if (ModConfigs.IS_ENABLED) {
|
||||
LOGGER.info("Starting Webserver...");
|
||||
|
||||
webserverthread.start();
|
||||
new Thread(() -> {
|
||||
new HTTPServer(new Socket());
|
||||
HTTPServer.main();
|
||||
}).start();
|
||||
while (true) {
|
||||
if (!mcserveractive) {
|
||||
LOGGER.info("LMFAFMAKONJDGOADJINGOADNGHOADNHGOADNHOADHON");
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(2);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.print("curl 127.0.0.1:" + WEB_PORT);
|
||||
CUrl curl = new CUrl("curl http://localhost:" + WEB_PORT + "/index.html");
|
||||
curl.exec();
|
||||
break;
|
||||
} else {
|
||||
LOGGER.info("Webserver disabled in the config file.");
|
||||
System.out.print(mcserveractive);
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(2);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,17 @@
|
|||
package me.jonasjones.mcwebserver.mixin;
|
||||
|
||||
import me.jonasjones.mcwebserver.McWebserver;
|
||||
import net.minecraft.client.gui.screen.TitleScreen;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(TitleScreen.class)
|
||||
public class InitializeMixin {
|
||||
@Inject(at = @At("HEAD"), method = "init()V")
|
||||
@Mixin(MinecraftServer.class)
|
||||
public class WebserverStopMixin {
|
||||
@Inject(at = @At("HEAD"), method = "shutdown")
|
||||
private void init(CallbackInfo info) {
|
||||
McWebserver.LOGGER.info("This line is printed by an example mod mixin!");
|
||||
McWebserver.LOGGER.info("Stopping Webserver...");
|
||||
McWebserver.mcserveractive = false;
|
||||
}
|
||||
}
|
|
@ -20,6 +20,8 @@ import java.nio.file.Path;
|
|||
import java.time.Instant;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import static me.jonasjones.mcwebserver.McWebserver.mcserveractive;
|
||||
|
||||
public class HTTPServer implements Runnable {
|
||||
static Path WEB_ROOT;
|
||||
static final String DEFAULT_FILE = ModConfigs.WEB_FILE_ROOT;
|
||||
|
@ -62,7 +64,7 @@ public class HTTPServer implements Runnable {
|
|||
McWebserver.LOGGER.info("Listening for connections on port : " + PORT);
|
||||
|
||||
// we listen until user halts server execution
|
||||
while (true) {
|
||||
while (mcserveractive) {
|
||||
HTTPServer myServer = new HTTPServer(serverConnect.accept());
|
||||
|
||||
VerboseLogger.info("Connection opened. (" + Instant.now() + ")");
|
||||
|
@ -70,6 +72,7 @@ public class HTTPServer implements Runnable {
|
|||
// create dedicated thread to manage the client connection
|
||||
Thread thread = new Thread(myServer);
|
||||
thread.start();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,8 +83,11 @@ public class HTTPServer implements Runnable {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
if (mcserveractive) {
|
||||
// we manage our particular client connection
|
||||
BufferedReader in = null; PrintWriter out = null; BufferedOutputStream dataOut = null;
|
||||
BufferedReader in = null;
|
||||
PrintWriter out = null;
|
||||
BufferedOutputStream dataOut = null;
|
||||
String fileRequested = null;
|
||||
|
||||
try {
|
||||
|
@ -178,7 +184,7 @@ public class HTTPServer implements Runnable {
|
|||
VerboseLogger.info("Connection closed.");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] readFileData(Path file) throws IOException {
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
package me.jonasjones.mcwebserver.web;
|
||||
|
||||
|
||||
import me.jonasjones.mcwebserver.McWebserver;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import me.jonasjones.mcwebserver.config.ModConfigs;
|
||||
|
||||
public class ServerHandler extends Thread {
|
||||
static ServerHandler thread = new ServerHandler();
|
||||
public static void startServer() {
|
||||
McWebserver.LOGGER.info("Starting Webserver...");
|
||||
thread.start();
|
||||
}
|
||||
import java.net.Socket;
|
||||
|
||||
public static void stopServer() throws InterruptedException {
|
||||
McWebserver.LOGGER.info("Stopping Webserver...");
|
||||
thread.interrupt();
|
||||
McWebserver.LOGGER.info("Webserver stopped!");
|
||||
}
|
||||
import static me.jonasjones.mcwebserver.McWebserver.LOGGER;
|
||||
|
||||
public class ServerHandler implements Runnable {
|
||||
public static Socket socket = new Socket();
|
||||
|
||||
public void run() {
|
||||
if (ModConfigs.IS_ENABLED) {
|
||||
LOGGER.info("Starting Webserver...");
|
||||
|
||||
new HTTPServer(socket);
|
||||
HTTPServer.main();
|
||||
} else {
|
||||
LOGGER.info("Webserver disabled in the config file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
{
|
||||
"required": true,
|
||||
"minVersion": "0.8",
|
||||
"package": "net.fabricmc.example.mixin",
|
||||
"package": "me.jonasjones.mcwebserver.mixin",
|
||||
"compatibilityLevel": "JAVA_17",
|
||||
"mixins": [
|
||||
"WebserverStopMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue