diff --git a/Common/src/main/java/journeymap_webmap/WebMap.java b/Common/src/main/java/journeymap_webmap/WebMap.java index d9fe41e..a60f6d3 100644 --- a/Common/src/main/java/journeymap_webmap/WebMap.java +++ b/Common/src/main/java/journeymap_webmap/WebMap.java @@ -31,11 +31,25 @@ public class WebMap private Javalin app = null; private static WebMap INSTANCE; + public WebMap(Javalin app) + { + this.app = app; + } + public static WebMap getInstance() { if (INSTANCE == null) { - INSTANCE = new WebMap(); + INSTANCE = new WebMap(null); + } + return INSTANCE; + } + + public static WebMap getInstance(Javalin app) + { + if (INSTANCE == null) + { + INSTANCE = new WebMap(app); } return INSTANCE; } @@ -44,10 +58,18 @@ public void start() { if (!started) { - findPort(true); - initialise(); - started = true; - logger.info("WebMap is now listening on port {}", port); + try + { + logger.info("starting webmap server"); + findPort(true); + initialise(); + started = true; + logger.info("WebMap is now listening on port {}", port); + } + catch (Exception e) + { + logger.error("Failed to start webmap server: ", e); + } } } @@ -55,66 +77,70 @@ private void initialise() { try { - app = Javalin.create(config -> { - String assetsRootProperty = System.getProperty("journeymap.webmap.assets_root", null); - File testFile = new File("../src/main/resources" + FileHandler.ASSETS_WEBMAP); - - if (assetsRootProperty != null) - { - logger.info("Detected 'journeymap.webmap.assets_root' property, serving static files from: " + assetsRootProperty); - config.staticFiles.add(assetsRootProperty, Location.EXTERNAL); - } - else if (testFile.exists()) - { - try - { - String assets = testFile.getCanonicalPath(); - logger.info("Development environment detected, serving static files from the filesystem.: " + assets); - config.staticFiles.add(testFile.getCanonicalPath(), Location.EXTERNAL); - } - catch (IOException e) - { - logger.error("WebMap error finding local assets path", e); - } - } - else - { - File dir = new File(FileHandler.getMinecraftDirectory(), Constants.WEB_DIR); - if (dir.exists()) - { - dir.delete(); - } - if (!dir.exists()) + if (app == null) + { + app = Javalin.create(config -> { + String assetsRootProperty = System.getProperty("journeymap.webmap.assets_root", null); + File testFile = new File("../src/main/resources" + FileHandler.ASSETS_WEBMAP); + + if (assetsRootProperty != null) { - logger.info("Attempting to copy web content to {}", new File(Constants.JOURNEYMAP_DIR, "web")); - boolean created = FileHandler.copyResources(dir, ResourceLocation.fromNamespaceAndPath(MOD_ID, "web"), "", false); - logger.info("Web content copied successfully: {}", created); + logger.info("Detected 'journeymap.webmap.assets_root' property, serving static files from: " + assetsRootProperty); + config.staticFiles.add(assetsRootProperty, Location.EXTERNAL); } - - if (dir.exists()) + else if (testFile.exists()) { - logger.info("Loading web content from local: {}", dir.getPath()); - config.staticFiles.add(dir.getPath(), Location.EXTERNAL); + try + { + String assets = testFile.getCanonicalPath(); + logger.info("Development environment detected, serving static files from the filesystem.: " + assets); + config.staticFiles.add(testFile.getCanonicalPath(), Location.EXTERNAL); + } + catch (IOException e) + { + logger.error("WebMap error finding local assets path", e); + } } else { - logger.info("Loading web content from jar: {}", FileHandler.ASSETS_WEBMAP); - config.staticFiles.add(FileHandler.ASSETS_WEBMAP, Location.CLASSPATH); + File dir = new File(FileHandler.getMinecraftDirectory(), Constants.WEB_DIR); + if (dir.exists()) + { + dir.delete(); + } + if (!dir.exists()) + { + logger.info("Attempting to copy web content to {}", new File(Constants.JOURNEYMAP_DIR, "web")); + boolean created = FileHandler.copyResources(dir, ResourceLocation.fromNamespaceAndPath(MOD_ID, "web"), "", false); + logger.info("Web content copied successfully: {}", created); + } + + if (dir.exists()) + { + logger.info("Loading web content from local: {}", dir.getPath()); + config.staticFiles.add(dir.getPath(), Location.EXTERNAL); + } + else + { + logger.info("Loading web content from jar: {}", FileHandler.ASSETS_WEBMAP); + config.staticFiles.add(FileHandler.ASSETS_WEBMAP, Location.CLASSPATH); + } } - } - }) - .before(ctx -> { - ctx.header("Access-Control-Allow-Origin", "*"); - ctx.header("Cache-Control", "no-cache"); - }) - .get("/waypoint/{id}/icon", Waypoints::iconGet) - .get("/data/{type}", Data::dataGet) - .get("/logs", Log::logGet) - .get("/polygons", Polygons::polygonsGet) - .get("/resources", Resources::resourcesGet) - .get("/skin/{uuid}", Skin::skinGet) - .get("/status", Status::statusGet) - .get("/tiles/tile.png", Tiles::tilesGet); + }) + .before(ctx -> { + ctx.header("Access-Control-Allow-Origin", "*"); + ctx.header("Cache-Control", "no-cache"); + }) + .get("/waypoint/{id}/icon", Waypoints::iconGet) + .get("/data/{type}", Data::dataGet) + .get("/logs", Log::logGet) + .get("/polygons", Polygons::polygonsGet) + .get("/resources", Resources::resourcesGet) + .get("/skin/{uuid}", Skin::skinGet) + .get("/status", Status::statusGet) + .get("/tiles/tile.png", Tiles::tilesGet); + } + app.start(port); } catch (Exception e) diff --git a/Fabric/build.gradle b/Fabric/build.gradle index 8affd24..0d4674a 100644 --- a/Fabric/build.gradle +++ b/Fabric/build.gradle @@ -5,7 +5,7 @@ buildscript { } plugins { - id 'fabric-loom' version '1.7-SNAPSHOT' + id 'fabric-loom' version '1.10-SNAPSHOT' id 'net.darkhax.curseforgegradle' version '1.+' id 'io.github.juuxel.loom-quiltflower' version '1.8.0' } diff --git a/NeoForge/build.gradle b/NeoForge/build.gradle index 75bb3ff..4c209e5 100644 --- a/NeoForge/build.gradle +++ b/NeoForge/build.gradle @@ -35,12 +35,14 @@ configurations.all { dependencies { annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' implementation group: "info.journeymap", name: "journeymap-api-neoforge", version: "${project.journeymap_api_version}-${project.minecraft_version}-SNAPSHOT", changing: true - implementation group: 'io.javalin', name: 'javalin', version: project.javalin_version + + implementation("io.javalin:javalin:${project.javalin_version}") + shade group: 'io.javalin', name: 'javalin', version: project.javalin_version + additionalRuntimeClasspath("io.javalin:javalin:${project.javalin_version}") shade group: 'info.journeymap', name: 'webmap-client', version: project.journeymap_webmap_version, changing: true //external libs shade - shade group: 'io.javalin', name: 'javalin', version: project.javalin_version compileOnly project(":Common") } diff --git a/NeoForge/src/main/java/journeymap_webmap/JourneymapWebmapNeoForge.java b/NeoForge/src/main/java/journeymap_webmap/JourneymapWebmapNeoForge.java index c87655b..317e3dc 100644 --- a/NeoForge/src/main/java/journeymap_webmap/JourneymapWebmapNeoForge.java +++ b/NeoForge/src/main/java/journeymap_webmap/JourneymapWebmapNeoForge.java @@ -1,9 +1,105 @@ package journeymap_webmap; +import io.javalin.Javalin; +import io.javalin.http.staticfiles.Location; +import journeymap.client.io.FileHandler; +import journeymap_webmap.routes.Data; +import journeymap_webmap.routes.Log; +import journeymap_webmap.routes.Polygons; +import journeymap_webmap.routes.Resources; +import journeymap_webmap.routes.Skin; +import journeymap_webmap.routes.Status; +import journeymap_webmap.routes.Tiles; +import journeymap_webmap.routes.Waypoints; +import net.minecraft.resources.ResourceLocation; +import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.common.Mod; +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; + +import static journeymap.common.Journeymap.MOD_ID; @Mod(Constants.MOD_ID) public class JourneymapWebmapNeoForge { + public static final Logger logger = LoggerFactory.getLogger("webmap"); + + public JourneymapWebmapNeoForge(IEventBus eventBus) + { + eventBus.addListener(this::clientSetupEvent); + } + + private void clientSetupEvent(FMLClientSetupEvent event) + { + WebMap.getInstance(create()); + } + + private Javalin create() + { + return Javalin.create(config -> { + String assetsRootProperty = System.getProperty("journeymap.webmap.assets_root", null); + File testFile = new File("../src/main/resources" + FileHandler.ASSETS_WEBMAP); + + if (assetsRootProperty != null) + { + logger.info("Detected 'journeymap.webmap.assets_root' property, serving static files from: " + assetsRootProperty); + config.staticFiles.add(assetsRootProperty, Location.EXTERNAL); + } + else if (testFile.exists()) + { + try + { + String assets = testFile.getCanonicalPath(); + logger.info("Development environment detected, serving static files from the filesystem.: " + assets); + config.staticFiles.add(testFile.getCanonicalPath(), Location.EXTERNAL); + } + catch (IOException e) + { + logger.error("WebMap error finding local assets path", e); + } + } + else + { + File dir = new File(FileHandler.getMinecraftDirectory(), journeymap.client.Constants.WEB_DIR); + if (dir.exists()) + { + dir.delete(); + } + if (!dir.exists()) + { + logger.info("Attempting to copy web content to {}", new File(journeymap.client.Constants.JOURNEYMAP_DIR, "web")); + boolean created = FileHandler.copyResources(dir, ResourceLocation.fromNamespaceAndPath(MOD_ID, "web"), "", false); + logger.info("Web content copied successfully: {}", created); + } + + if (dir.exists()) + { + logger.info("Loading web content from local: {}", dir.getPath()); + config.staticFiles.add(dir.getPath(), Location.EXTERNAL); + } + else + { + logger.info("Loading web content from jar: {}", FileHandler.ASSETS_WEBMAP); + config.staticFiles.add(FileHandler.ASSETS_WEBMAP, Location.CLASSPATH); + } + } + }) + .before(ctx -> { + ctx.header("Access-Control-Allow-Origin", "*"); + ctx.header("Cache-Control", "no-cache"); + }) + .get("/waypoint/{id}/icon", Waypoints::iconGet) + .get("/data/{type}", Data::dataGet) + .get("/logs", Log::logGet) + .get("/polygons", Polygons::polygonsGet) + .get("/resources", Resources::resourcesGet) + .get("/skin/{uuid}", Skin::skinGet) + .get("/status", Status::statusGet) + .get("/tiles/tile.png", Tiles::tilesGet); + } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0d18421..d6e308a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists