Skip to content

Commit

Permalink
Make target path defined in buildscript
Browse files Browse the repository at this point in the history
Add tag providers
Rename Ingredient to DataIngredient
  • Loading branch information
EmmaTheMartian committed Jul 8, 2024
1 parent b98c218 commit ef1aff5
Show file tree
Hide file tree
Showing 44 changed files with 252 additions and 77 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ loom {
register("testData") {
source("test")
property("datagen.run", "datagen_test")
property("datagen.path", project.projectDir.toPath().resolve("src/generated/resources/").toAbsolutePath().toString())
client()
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.daemon=false
loader_version=0.15.6-babric.1

# Mod Properties
mod_version=1.0.0
mod_version=0.0.1
maven_group=emmathemartian
archives_base_name=datagen

Expand Down
9 changes: 4 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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.
1. Include the datagen library. You can do this via Jitpack.
1. Include the datagen library. You can do this via Jitpack:

```groovy
dependencies {
Expand Down Expand Up @@ -49,6 +49,7 @@ before making a release just to be certain that there are no stale files.
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()
}
}
Expand All @@ -59,14 +60,12 @@ before making a release just to be certain that there are no stale files.

```java
public class ModData implements DataEntrypoint {
public static final String MODID = "example_mod";

@Entrypoint.Namespace
private static final Namespace NAMESPACE = Null.get();

@Override
public void run() {
DataGenContext context = new DataGenContext(NAMESPACE, "../src/generated/resources/assets/" + MODID + "/stationapi/");
DataGenContext context = new DataGenContext(NAMESPACE);

context.run(new CraftingRecipeProvider(context) {
@Override
Expand Down Expand Up @@ -100,7 +99,7 @@ before making a release just to be certain that there are no stale files.

## Troubleshooting

### The window does not appear
### The window does not appear or it closes after loading

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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
item.datagen_test.dirty_ruby.name=Dirty Ruby
item.datagen_test.gravelly_ruby.name=Gravelly Ruby
tile.datagen_test.ruby_block.name=Block of Ruby
item.datagen_test.sandy_ruby.name=Sandy Ruby
item.datagen_test.ruby.name=Ruby
item.datagen_test.ruby_rod.name=Ruby Rod

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parent":"minecraft:item/generated","textures":{"layer0":"datagen_test:item/gravelly_ruby"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parent":"minecraft:item/generated","textures":{"layer0":"datagen_test:item/sandy_ruby"}}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"minecraft:crafting_shapeless","ingredients":[{"item":"datagen_test:ruby"},{"item":"minecraft:dirt"},{"item":"minecraft:dirt"}],"result":{"item":"datagen_test:dirty_ruby"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"minecraft:crafting_shapeless","ingredients":[{"item":"datagen_test:ruby"},{"item":"minecraft:gravel"},{"item":"minecraft:gravel"}],"result":{"item":"datagen_test:gravelly_ruby"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"minecraft:crafting_shapeless","ingredients":[{"item":"datagen_test:ruby"},{"item":"minecraft:sand"},{"item":"minecraft:sand"}],"result":{"item":"datagen_test:sandy_ruby"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"minecraft:smelting","ingredient":{"tag":"datagen_test:dirty_ruby_gems"},"result":{"item":"datagen_test:ruby"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"values":["minecraft:diamond_block","datagen_test:ruby_block"]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"values":["datagen_test:dirty_ruby","datagen_test:sandy_ruby","datagen_test:gravelly_ruby"]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"values":["datagen_test:ruby","datagen_test:dirty_ruby"]}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public abstract class AbstractDataProvider {
public final Path path;
public final String name;

public AbstractDataProvider(String path, String name, DataGenContext context) {
this.path = context.root.resolve(path);
public AbstractDataProvider(String path, String name, DataTarget target, DataGenContext context) {
this.path = target.getPath(context).resolve(path);
this.name = name;
}

Expand Down
14 changes: 5 additions & 9 deletions src/main/java/emmathemartian/datagen/DataGenContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@
import java.util.List;
import java.util.function.Function;

public class DataGenContext {
public final Namespace namespace;
public final Path root;

public DataGenContext(Namespace namespace, String rootPath) {
this.namespace = namespace;
this.root = Path.of(rootPath);
}

public record DataGenContext(Namespace namespace) {
public void run(Function<DataGenContext, AbstractDataProvider> provider) {
run(provider.apply(this));
}
Expand All @@ -41,4 +33,8 @@ public void save(Path path, String data) {
public void save(Path path, JsonObject object) {
save(path, new Gson().toJson(object));
}

public Path getTargetPath(String parent) {
return DataGenMod.targetPath.resolve(parent).resolve(namespace.toString()).resolve("stationapi");
}
}
22 changes: 18 additions & 4 deletions src/main/java/emmathemartian/datagen/DataGenMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,41 @@
import emmathemartian.datagen.entrypoint.DataEntrypoint;
import net.fabricmc.loader.api.FabricLoader;
import net.mine_diver.unsafeevents.listener.EventListener;
import net.modificationstation.stationapi.api.event.registry.AfterBlockAndItemRegisterEvent;
import net.mine_diver.unsafeevents.listener.ListenerPriority;
import net.modificationstation.stationapi.api.event.registry.DimensionRegistryEvent;
import net.modificationstation.stationapi.api.mod.entrypoint.Entrypoint;
import net.modificationstation.stationapi.api.util.Null;
import org.apache.logging.log4j.Logger;

@SuppressWarnings("unused")
import java.nio.file.Path;
import java.util.Objects;

public class DataGenMod {
@Entrypoint.Logger
public static final Logger LOGGER = Null.get();

@EventListener
private static void onPostRegistries(AfterBlockAndItemRegisterEvent event) {
public static Path targetPath;

private static Path getTargetPath() {
String strPath = Objects.requireNonNull(System.getProperty("datagen.path"), "System property datagen.path was null. Make sure it is defined in your buildscript.");
return Path.of(strPath);
}

@SuppressWarnings("unused")
@EventListener(priority = ListenerPriority.LOWEST)
private static void onPostRegistries(DimensionRegistryEvent event) {
String property = System.getProperty("datagen.run");
if (property == null)
return;

final String[] toRun = property.split(",");
LOGGER.info("Data generator will be executed for mods: [{}]", String.join(", ", toRun));
LOGGER.info("Data generation target path is `{}`", getTargetPath());
LOGGER.info("Note: The game will be stopped after data generation finishes.");

if (toRun.length > 0) {
targetPath = getTargetPath();

if (!FabricLoader.getInstance().isDevelopmentEnvironment()) {
throw new DataGenException("Data generators should not be executed in production environments. Exiting.");
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/emmathemartian/datagen/DataTarget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package emmathemartian.datagen;

import java.nio.file.Path;

public record DataTarget(String target) {
public static final DataTarget ASSETS = new DataTarget("assets");
public static final DataTarget DATA = new DataTarget("data");

public Path getPath(DataGenContext context) {
return context.getTargetPath(target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import emmathemartian.datagen.IDataBuilder;
import emmathemartian.datagen.util.Ingredient;
import emmathemartian.datagen.util.DataIngredient;
import emmathemartian.datagen.util.ItemStackHelpers;
import net.minecraft.item.ItemStack;
import net.modificationstation.stationapi.api.util.Identifier;
Expand All @@ -14,15 +14,15 @@ public class ShapedRecipeBuilder implements IDataBuilder {
public static final Identifier TYPE_ID = Identifier.of("minecraft:crafting_shaped");

protected List<String> pattern = new ArrayList<>();
protected Map<Character, Ingredient> key = new HashMap<>();
protected Map<Character, DataIngredient> key = new HashMap<>();
protected ItemStack result;

public ShapedRecipeBuilder pattern(String... patterns) {
this.pattern.addAll(Arrays.stream(patterns).toList());
return this;
}

public ShapedRecipeBuilder define(char key, Ingredient ingredient) {
public ShapedRecipeBuilder define(char key, DataIngredient ingredient) {
this.key.put(key, ingredient);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import emmathemartian.datagen.IDataBuilder;
import emmathemartian.datagen.util.Ingredient;
import emmathemartian.datagen.util.DataIngredient;
import emmathemartian.datagen.util.ItemStackHelpers;
import net.minecraft.item.ItemStack;
import net.modificationstation.stationapi.api.util.Identifier;
Expand All @@ -13,10 +13,10 @@
public class ShapelessRecipeBuilder implements IDataBuilder {
public static final Identifier TYPE_ID = Identifier.of("minecraft:crafting_shapeless");

protected List<Ingredient> ingredients = new ArrayList<>();
protected List<DataIngredient> ingredients = new ArrayList<>();
protected ItemStack result;

public ShapelessRecipeBuilder ingredient(Ingredient ingredient) {
public ShapelessRecipeBuilder ingredient(DataIngredient ingredient) {
this.ingredients.add(ingredient);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import com.google.gson.JsonObject;
import emmathemartian.datagen.IDataBuilder;
import emmathemartian.datagen.util.Ingredient;
import emmathemartian.datagen.util.DataIngredient;
import emmathemartian.datagen.util.ItemStackHelpers;
import net.minecraft.item.ItemStack;
import net.modificationstation.stationapi.api.util.Identifier;

public class SmeltingRecipeBuilder implements IDataBuilder {
public static final Identifier TYPE_ID = Identifier.of("minecraft:smelting");

protected Ingredient ingredient;
protected DataIngredient ingredient;
protected ItemStack result;

public SmeltingRecipeBuilder ingredient(Ingredient ingredient) {
public SmeltingRecipeBuilder ingredient(DataIngredient ingredient) {
this.ingredient = ingredient;
return this;
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/emmathemartian/datagen/builder/TagBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package emmathemartian.datagen.builder;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import emmathemartian.datagen.IDataBuilder;
import net.modificationstation.stationapi.api.registry.Registry;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class TagBuilder<T> implements IDataBuilder {
protected final Registry<T> registry;
protected final List<T> entries = new ArrayList<>();

public TagBuilder(Registry<T> registry) {
this.registry = registry;
}

public TagBuilder<T> add(T entry) {
this.entries.add(entry);
return this;
}

@Override
public JsonObject build() {
JsonObject object = new JsonObject();

JsonArray entriesArray = new JsonArray();
entries.forEach(it -> entriesArray.add(Objects.requireNonNull(registry.getId(it)).toString()));
object.add("values", entriesArray);

return object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import emmathemartian.datagen.AbstractDataProvider;
import emmathemartian.datagen.DataGenContext;
import emmathemartian.datagen.DataTarget;

public abstract class AbstractModelProvider extends AbstractDataProvider {
public AbstractModelProvider(String subpath, String name, DataGenContext context) {
super("models/" + subpath, name, context);
super("models/" + subpath, name, DataTarget.ASSETS, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import emmathemartian.datagen.AbstractDataProvider;
import emmathemartian.datagen.DataGenContext;
import emmathemartian.datagen.DataTarget;

public abstract class AbstractRecipeProvider extends AbstractDataProvider {
public AbstractRecipeProvider(String subpath, DataGenContext context) {
super("recipes/" + subpath, "Recipes", context);
super("recipes/" + subpath, "Recipes", DataTarget.DATA, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import emmathemartian.datagen.AbstractDataProvider;
import emmathemartian.datagen.DataGenContext;
import emmathemartian.datagen.DataTarget;
import emmathemartian.datagen.builder.VariantStateBuilder;

public abstract class BlockStateProvider extends AbstractDataProvider {
public BlockStateProvider(DataGenContext context) {
super("blockstates", "Block States", context);
super("blockstates", "Block States", DataTarget.ASSETS, context);
}

protected VariantStateBuilder variant() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package emmathemartian.datagen.provider;

import emmathemartian.datagen.DataGenContext;
import emmathemartian.datagen.builder.TagBuilder;
import net.minecraft.block.Block;
import net.modificationstation.stationapi.api.registry.BlockRegistry;

public abstract class BlockTagProvider extends TagProvider<Block> {
public BlockTagProvider(DataGenContext context) {
super("blocks", "Block Tags", context);
}

@Override
protected TagBuilder<Block> tag() {
return new TagBuilder<>(BlockRegistry.INSTANCE);
}
}
17 changes: 17 additions & 0 deletions src/main/java/emmathemartian/datagen/provider/ItemTagProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package emmathemartian.datagen.provider;

import emmathemartian.datagen.DataGenContext;
import emmathemartian.datagen.builder.TagBuilder;
import net.minecraft.item.Item;
import net.modificationstation.stationapi.api.registry.ItemRegistry;

public abstract class ItemTagProvider extends TagProvider<Item> {
public ItemTagProvider(DataGenContext context) {
super("items", "Item Tags", context);
}

@Override
protected TagBuilder<Item> tag() {
return new TagBuilder<>(ItemRegistry.INSTANCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import emmathemartian.datagen.AbstractDataProvider;
import emmathemartian.datagen.DataGenContext;
import emmathemartian.datagen.DataTarget;

public abstract class LanguageProvider extends AbstractDataProvider {
public LanguageProvider(DataGenContext context) {
super("lang", "Languages", context);
super("lang", "Languages", DataTarget.ASSETS, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import emmathemartian.datagen.DataGenContext;
import emmathemartian.datagen.builder.SmeltingRecipeBuilder;
import emmathemartian.datagen.util.Ingredient;
import emmathemartian.datagen.util.DataIngredient;
import net.minecraft.item.ItemStack;

public abstract class SmeltingRecipeProvider extends AbstractRecipeProvider {
Expand All @@ -14,7 +14,7 @@ protected SmeltingRecipeBuilder smelting() {
return new SmeltingRecipeBuilder();
}

protected SmeltingRecipeBuilder smelting(Ingredient input, ItemStack output) {
protected SmeltingRecipeBuilder smelting(DataIngredient input, ItemStack output) {
return new SmeltingRecipeBuilder()
.ingredient(input)
.result(output);
Expand Down
Loading

0 comments on commit ef1aff5

Please sign in to comment.