From 918748b3731fcf43d43c5531a354674eaa0a3752 Mon Sep 17 00:00:00 2001 From: Martin Prokoph Date: Sat, 6 Sep 2025 21:27:15 +0200 Subject: [PATCH] feat: support running the game from jar file --- build.gradle.kts | 5 +++++ .../eu/midnightdust/yaytris/Translation.java | 12 ++++++++++-- .../midnightdust/yaytris/util/NightJson.java | 19 +++++++++++++------ .../yaytris/util/sound/SoundUtil.java | 2 +- .../main/resources/translation}/de_de.json5 | 0 .../main/resources/translation}/en_gb.json5 | 0 6 files changed, 29 insertions(+), 9 deletions(-) rename {translation => src/main/resources/translation}/de_de.json5 (100%) rename {translation => src/main/resources/translation}/en_gb.json5 (100%) diff --git a/build.gradle.kts b/build.gradle.kts index 6dec271..67258cf 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,6 +14,11 @@ dependencies { testImplementation("org.junit.jupiter:junit-jupiter") } +tasks.jar { + manifest.attributes["Main-Class"] = "eu.midnightdust.yaytris.Tetris" +} + + tasks.test { useJUnitPlatform() } \ No newline at end of file diff --git a/src/main/java/eu/midnightdust/yaytris/Translation.java b/src/main/java/eu/midnightdust/yaytris/Translation.java index 307e6be..3ebdb30 100644 --- a/src/main/java/eu/midnightdust/yaytris/Translation.java +++ b/src/main/java/eu/midnightdust/yaytris/Translation.java @@ -2,6 +2,8 @@ package eu.midnightdust.yaytris; import eu.midnightdust.yaytris.util.NightJson; +import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; @@ -15,8 +17,14 @@ public class Translation { * @see NightJson */ public static void load(String locale) { - NightJson json = new NightJson(Translation.class, String.format("translation/%s.json5", locale)); - json.readJson(); + String jsonString; // This workaround is needed to support reading translations from json files embedded in the .jar file + try (InputStream stream = Translation.class.getResourceAsStream(String.format("/translation/%s.json5", locale))) { + jsonString = stream != null ? new String(stream.readAllBytes()) : ""; + } catch (IOException e) { + throw new RuntimeException(e); + } + NightJson json = new NightJson(Translation.class, "translation"); + json.readJsonFromString(jsonString); } /** diff --git a/src/main/java/eu/midnightdust/yaytris/util/NightJson.java b/src/main/java/eu/midnightdust/yaytris/util/NightJson.java index 44894c4..360e04a 100644 --- a/src/main/java/eu/midnightdust/yaytris/util/NightJson.java +++ b/src/main/java/eu/midnightdust/yaytris/util/NightJson.java @@ -103,10 +103,6 @@ public class NightJson { return String.format(type == String.class || type.isEnum() ? "\"%s\"" : "%s", value); } - /** - * Read the json file from disk and overwrite the json class's field values. - */ - @SuppressWarnings("unchecked") public void readJson() { if (fileLocation == null) return; try { @@ -115,9 +111,20 @@ public class NightJson { writeJson(); return; } + readJsonFromString(Files.readString(file.toPath())); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + /** + * Read the json file from disk and overwrite the json class's field values. + */ + @SuppressWarnings("unchecked") + public void readJsonFromString(String jsonString) { + try { Map asMap = jsonToMap( - Files.readString(file.toPath()).replaceAll("(//)+.*\n", ""), // Replace comment lines (Json5) + jsonString.replaceAll("(//)+.*\n", ""), // Replace comment lines (Json5) (key) -> getField(key).isPresent() ? getField(key).get().getType() : String.class); // Determine data type for (String key : asMap.keySet()) { @@ -130,7 +137,7 @@ public class NightJson { ((Map)jsonMap.get(null)).put(key, value); } } - } catch (IOException | IllegalAccessException | NoSuchElementException | ClassCastException e) { + } catch (IllegalAccessException | NoSuchElementException | ClassCastException e) { System.out.println("Oh no! An Error occurred whilst reading the JSON file :("); //noinspection CallToPrintStackTrace e.printStackTrace(); diff --git a/src/main/java/eu/midnightdust/yaytris/util/sound/SoundUtil.java b/src/main/java/eu/midnightdust/yaytris/util/sound/SoundUtil.java index ad8f066..13ae023 100644 --- a/src/main/java/eu/midnightdust/yaytris/util/sound/SoundUtil.java +++ b/src/main/java/eu/midnightdust/yaytris/util/sound/SoundUtil.java @@ -41,7 +41,7 @@ public class SoundUtil { * @param fileLocation the URI of the desired audio file (should be a .wav file) */ public static void playSoundClip(String fileLocation) { - try (AudioInputStream stream = AudioSystem.getAudioInputStream(getResource(fileLocation))) { // FIXME: Support audio files from JAR. File streams won't work here for some reason. + try (AudioInputStream stream = AudioSystem.getAudioInputStream(getResource(fileLocation))) { AudioFormat format = stream.getFormat(); DataLine.Info info = new DataLine.Info(Clip.class, format); diff --git a/translation/de_de.json5 b/src/main/resources/translation/de_de.json5 similarity index 100% rename from translation/de_de.json5 rename to src/main/resources/translation/de_de.json5 diff --git a/translation/en_gb.json5 b/src/main/resources/translation/en_gb.json5 similarity index 100% rename from translation/en_gb.json5 rename to src/main/resources/translation/en_gb.json5