Skip to content
Draft
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
66 changes: 66 additions & 0 deletions libraries/datagen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Data Generator API

The Data Generator API is a library that provides a simple and intuitive way to generate data files for Minecraft mods.
It allows mod developers to easily create block models, item models, and other data files without having to manually write JSON files.

## Registering Data Generators
### Entrypoint

The Data Generator API provides an entrypoint that allows mod developers to register their data generators.
This entrypoint is used to register the data generators that will be used to generate the data files for the mod.

An example is shown below.

`fabric.mod.json`
```json
{
...
"entrypoints": {
...
"datagen": [
"com.example.ExampleDataGenerator"
],
...
},
...
}
```

```java
package com.example;

import net.ornithemc.osl.datagen.api.DataGeneratorInitializer;

public class ExampleDataGenerator implements DataGeneratorInitializer {
@Override
public void onDatagenInit(ModDataGenerator dataGenerator) {
PackGenerator pack = dataGenerator.createPack();
pack.addProvider(ExampleModelProvider::new);
}
}
```

### Mod Data Providers

Mod data providers are used to provide data to the pack generator.
There are a few builtin providers, such as the model provider, and language provider.

An example for the model provider is shown below.

```java
package com.example;

public class ExampleModelProvider extends ModModelProvider {
public ExampleModelProvider(PackGenerator generator, ModContainer mod) {
super(generator, mod);
}

public void generateModels(ModelGenerator generator) {
BlockModelTemplates templates = generator.blockTemplates();
templates.simpleBlock(Blocks.OAK_PLANKS, block("oak_planks"));
}
}
```

Vanilla DataProviders can also be extended to provide data such as tags.
In the extended constructor, replace DataGenerator with PackGenerator and upcast it back to DataGenerator.
6 changes: 6 additions & 0 deletions libraries/datagen/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
setUpLibrary(project)

dependencies {
implementation 'com.google.code.gson:gson:2.8.0'
implementation 'com.google.guava:guava:17.0'
}
1 change: 1 addition & 0 deletions libraries/datagen/datagen-mc18w43a-mc1.14.4/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpModule(project)
5 changes: 5 additions & 0 deletions libraries/datagen/datagen-mc18w43a-mc1.14.4/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#min_mc_version = 1.13-pre6
min_mc_version = 18w43a
max_mc_version = 1.14.4

minecraft_version = 1.14.4
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package net.ornithemc.osl.datagen.api.model;

import com.google.gson.JsonObject;
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;

import java.util.HashMap;
import java.util.Map;

/**
* Represents a block or item model.
*/
public class ModelBuilder {
private NamespacedIdentifier parent;
private Map<String, NamespacedIdentifier> textures = new HashMap<>();

/**
* Creates a new model builder.
*
* @return the model builder
*/
public static ModelBuilder create() {
return new ModelBuilder();
}

/**
* Creates a new model builder with the given parent.
*
* @param parent the parent model
* @return the model builder
*/
public static ModelBuilder create(NamespacedIdentifier parent) {
return new ModelBuilder().parent(parent);
}

/**
* Sets the parent model.
*
* @param parent the parent model
* @return the model builder
*/
public ModelBuilder parent(NamespacedIdentifier parent) {
this.parent = parent;
return this;
}

/**
* Sets the texture path for the given name.
*
* @param name the name of the texture
* @param texture the texture path
* @return the model builder
*/
public ModelBuilder texture(String name, NamespacedIdentifier texture) {
this.textures.put(name, texture);
return this;
}

public JsonObject build() {
JsonObject model = new JsonObject();

if (parent != null) {
model.addProperty("parent", parent.toString());
}

if (!textures.isEmpty()) {
JsonObject texturesJson = new JsonObject();
for (Map.Entry<String, NamespacedIdentifier> entry : textures.entrySet()) {
texturesJson.addProperty(entry.getKey(), entry.getValue().toString());
}
model.add("textures", texturesJson);
}

return model;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package net.ornithemc.osl.datagen.api.model;

import net.minecraft.block.Block;
import net.minecraft.item.ItemLike;
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
import net.ornithemc.osl.datagen.api.model.block.BlockModelDefinitionBuilder;
import net.ornithemc.osl.datagen.api.model.block.BlockModelTemplates;

/**
* Provides methods for generating model resources.
*/
public interface ModelGenerator {
/**
* Generates a blockstate file for the given block.
*
* @param block the block to generate the blockstate for
* @param builder the builder to use for generating the blockstate
*/
void blockstate(Block block, BlockModelDefinitionBuilder builder);

/**
* Generates an item model file for the given item or block item.
*
* @param item the item to generate the item model for
* @param builder the builder to use for generating the item model
*/
void itemModel(ItemLike item, ModelBuilder builder);

/**
* Generates a block model file for the given identifier.
*
* @param identifier the identifier to generate the block model for
* @param builder the builder to use for generating the block model
* @return the generated block model identifier
*/
NamespacedIdentifier blockModel(NamespacedIdentifier identifier, ModelBuilder builder);

/**
* Generates a block model file for the given block.
*
* @param block the block to generate the block model for
* @param builder the builder to use for generating the block model
* @return the generated block model identifier
*/
NamespacedIdentifier blockModel(Block block, ModelBuilder builder);

/**
* Generates a block model file for the given block with the given suffix.
*
* @param block the block to generate the block model for
* @param suffix the suffix to append to the block model identifier
* @param builder the builder to use for generating the block model
* @return the generated block model identifier
*/
NamespacedIdentifier blockModel(Block block, String suffix, ModelBuilder builder);

/**
* Returns the block model templates.
*
* @return the block model templates
*/
BlockModelTemplates blockTemplates();
}
Loading
Loading