Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ build
# other
eclipse
run
run-WebServer
runs
run-data

Expand Down
18 changes: 4 additions & 14 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import net.neoforged.moddevgradle.dsl.RunModel

plugins {
id 'java-library'
id 'maven-publish'
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}
57 changes: 32 additions & 25 deletions src/main/java/eu/cronmoth/createtrainwebapi/ApiServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

}
}