diff --git a/.gitignore b/.gitignore index 31d2550..46497be 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ build # other eclipse run +run-WebServer runs run-data diff --git a/build.gradle b/build.gradle index 3ea1892..235be5e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,5 @@ +import net.neoforged.moddevgradle.dsl.RunModel + plugins { id 'java-library' id 'maven-publish' @@ -49,13 +51,14 @@ neoForge { runs { client { client() - + gameDirectory = project.file("run-WebServer") // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. systemProperty 'neoforge.enabledGameTestNamespaces', project.mod_id } server { server() + gameDirectory = project.file("run-WebServer") programArgument '--nogui' systemProperty 'neoforge.enabledGameTestNamespaces', project.mod_id } @@ -262,17 +265,4 @@ idea { } tasks.withType(Jar).configureEach { zip64 = true -} - -tasks.withType(com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar).configureEach { - zip64 = true -} - -tasks.register("copyJarToBluemap", Copy) { - dependsOn(tasks.named("jar")) - from(tasks.named("jar").get().archiveFile) - into(layout.projectDirectory.dir("run/config/bluemap/packs")) -} -tasks.named("runServer").configure { - dependsOn("copyJarToBluemap") } \ No newline at end of file diff --git a/src/main/java/eu/cronmoth/createtrainwebapi/ApiServer.java b/src/main/java/eu/cronmoth/createtrainwebapi/ApiServer.java index 1557acd..283af5e 100644 --- a/src/main/java/eu/cronmoth/createtrainwebapi/ApiServer.java +++ b/src/main/java/eu/cronmoth/createtrainwebapi/ApiServer.java @@ -5,11 +5,14 @@ import io.undertow.server.HttpHandler; import io.undertow.server.HttpServerExchange; import io.undertow.server.handlers.PathHandler; +import io.undertow.server.handlers.resource.FileResourceManager; +import io.undertow.server.handlers.resource.ResourceHandler; import io.undertow.server.handlers.sse.ServerSentEventConnectionCallback; import io.undertow.server.handlers.sse.ServerSentEventHandler; import io.undertow.util.Headers; import io.undertow.util.HttpString; +import java.io.File; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -37,39 +40,43 @@ public void start(String host, int port) { ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(4); pathHandler.addExactPath("/trainsLive", - new HttpHandler() { - @Override - public void handleRequest(HttpServerExchange exchange) throws Exception { - exchange.getResponseHeaders().put(new HttpString("Access-Control-Allow-Origin"), "*"); - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/event-stream"); - new ServerSentEventHandler( - new ServerSentEventConnectionCallback() { - @Override - public void connected(io.undertow.server.handlers.sse.ServerSentEventConnection connection, String lastEventId) { - ScheduledFuture future = scheduler.scheduleAtFixedRate(() -> { - if (connection.isOpen()) { - try { - String update = mapper.writeValueAsString(TrackInformation.GetTrainData()); - connection.send(update); - } catch (Exception e) { - e.printStackTrace(); - } - } - }, 0, 200, TimeUnit.MILLISECONDS); - - connection.addCloseTask(conn -> future.cancel(false)); + exchange -> { + exchange.getResponseHeaders().put(new HttpString("Access-Control-Allow-Origin"), "*"); + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/event-stream"); + new ServerSentEventHandler( + (connection, lastEventId) -> { + ScheduledFuture future = scheduler.scheduleAtFixedRate(() -> { + if (connection.isOpen()) { + try { + String update = mapper.writeValueAsString(TrackInformation.GetTrainData()); + connection.send(update); + } catch (Exception e) { + e.printStackTrace(); + } } - } - ).handleRequest(exchange); - } + }, 0, 200, TimeUnit.MILLISECONDS); + + connection.addCloseTask(conn -> future.cancel(false)); + } + ).handleRequest(exchange); } ); + if (new File("bluemap/train_models/").exists()) { + FileResourceManager resourceManager = new FileResourceManager(new File("bluemap/train_models/"), 100); + ResourceHandler resourceHandler = new ResourceHandler(resourceManager) + .setDirectoryListingEnabled(false); + HttpHandler trainModelHandler = exchange -> { + exchange.getResponseHeaders().put(new HttpString("Access-Control-Allow-Origin"), "*"); + resourceHandler.handleRequest(exchange); + }; + + pathHandler.addPrefixPath("/trainModels", trainModelHandler); + } server = Undertow.builder() .addHttpListener(port, host) .setHandler(pathHandler) .build(); - server.start(); } public void stop() { diff --git a/src/main/java/eu/cronmoth/createtrainwebapi/model/TrainCarData.java b/src/main/java/eu/cronmoth/createtrainwebapi/model/TrainCarData.java index fb8effc..ffd640c 100644 --- a/src/main/java/eu/cronmoth/createtrainwebapi/model/TrainCarData.java +++ b/src/main/java/eu/cronmoth/createtrainwebapi/model/TrainCarData.java @@ -1,11 +1,17 @@ package eu.cronmoth.createtrainwebapi.model; import com.simibubi.create.content.trains.entity.Carriage; +import com.simibubi.create.content.trains.entity.CarriageContraption; +import com.simibubi.create.content.trains.entity.CarriageContraptionEntity; import com.simibubi.create.content.trains.graph.TrackNode; +import net.minecraft.nbt.CompoundTag; + +import java.lang.reflect.Field; public class TrainCarData { public int id; public double positionOnTrack; + public String assemblyDirection; //public double length; public NodeData node1; public NodeData node2; @@ -16,5 +22,16 @@ public TrainCarData(Carriage carriage) { //length = carriage.getLeadingPoint().position - positionOnTrack; node1= new NodeData(carriage.getLeadingPoint().node1); node2= new NodeData(carriage.getLeadingPoint().node2); + + try { + Field f = Carriage.class.getDeclaredField("serialisedEntity"); + f.setAccessible(true); + CompoundTag serialisedEntity = (CompoundTag) f.get(carriage); + CompoundTag contraptionTag = serialisedEntity.getCompound("Contraption"); + assemblyDirection = contraptionTag.getString("AssemblyDirection"); + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new RuntimeException(e); + } + } } \ No newline at end of file