A library mod for Station API which allows modders to programmatically generate JSON files.
Currently, the API does not make hashes of files to check for changes. As of now, all files are regenerated every time you run data. Along with this, stale files are not removed. I recommend completely clearing your data generated folder and re-running data before making a release just to be certain that there are no stale files.
The buildscript codeblocks are Groovy, however it should be very similar (if not then exactly the same) in Kotlin DSL.
-
Include the datagen library. You can do this via Jitpack:
dependencies { modImplementation("com.github.emmathemartian:stapi-datagen:version") }
-
Configure source sets to include a new
generated
source set. Make a directory atsrc/generated/
, then add this to your buildscript:sourceSets { main { java { srcDir("src/main/java") } resources { srcDir("src/test/resources") srcDir("src/generated/resources") } } }
-
Create a new run configuration to run the data generator:
loom { ... runs { register("data") { property("datagen.run", "MODID") // replace with your mod's id property("datagen.path", project.projectDir.toPath().resolve("src/generated/resources/").toAbsolutePath().toString()) client() } } }
-
Create an entrypoint for the data generator to find:
public class ModData implements DataEntrypoint { @Entrypoint.Namespace private static final Namespace NAMESPACE = Null.get(); @Override public void run() { DataGenContext context = new DataGenContext(NAMESPACE); context.run(new CraftingRecipeProvider(context) { @Override public void run(DataGenContext context) { shapeless() .ingredient(Ingredient.of(Block.DIRT.asItem())) .ingredient(Ingredient.of(Item.WATER_BUCKET)) .result(new ItemStack(Block.CLAY.asItem())) .save("dirt_to_clay", this, context); } }); } }
// fabric.mod.json { "entrypoints": { "data": [ "yourpackage.ModData" ] } }
-
Reload Gradle and test using
gradlew runData
. You should see the file atsrc/generated/resources/assets/example_mod/stationapi/recipes/crafting/dirt_to_clay.json
.
See
src/test/java/emmathemartian/datagen/test/
for a more comprehensive example.
This is intentional when running the data generator. Due to when data is made and when the mod's JAR is made, you can't use any freshly generated files until the next run.
Read note.
Either you have a literal duplicate file across two source sets, or you are experiencing this bug. If you do not have any duplicate files, but you have duplicate folders, it will be the latter.
Example:
src/main/resouces/assets/
andsrc/generated/resources/assets/
. Theassets
folder is flagged as a duplicate for some reason. Thanks Gradle.
To fix the latter, just put this line in processResources
(or whatever other task is
failing):
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
If you prefer not to hunt down individual tasks to define duplicatesStrategy
in, then
you can add this code to your buildscript, which should configure the duplicates strategy
for most tasks where it is important:
tasks.withType(Jar).configureEach {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
tasks.withType(Copy).configureEach {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}