Skip to content

Commit

Permalink
fix(docs): Update to use gatherdata methods and event
Browse files Browse the repository at this point in the history
  • Loading branch information
ChampionAsh5357 committed Jan 26, 2025
1 parent c94afc5 commit a15202d
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 200 deletions.
2 changes: 1 addition & 1 deletion docs/advanced/featureflags.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ Feature packs can be generated during regular mod datagen. This is best used in
```java
@SubscribeEvent
public static void gatherData(final GatherDataEvent event) {
public static void gatherData(final GatherDataEvent.Client event) {
DataGenerator generator = event.getGenerator();
// To generate a feature pack, you must first obtain a pack generator instance for the desired pack.
Expand Down
35 changes: 18 additions & 17 deletions docs/concepts/registries.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,23 +312,24 @@ Finally, we use our `RegistrySetBuilder` in an actual data provider, and registe

```java
@SubscribeEvent
public static void onGatherData(GatherDataEvent event) {
CompletableFuture<HolderLookup.Provider> lookupProvider = event.getGenerator().addProvider(
// Only run datapack generation when server data is being generated
event.includeServer(),
// Create the provider
output -> new DatapackBuiltinEntriesProvider(
output,
event.getLookupProvider(),
// Our registry set builder to generate the data from.
new RegistrySetBuilder().add(...),
// A set of mod ids we are generating. Usually only your own mod id.
Set.of("yourmodid")
)
).getRegistryProvider();

// Use the lookup provider generated from your datapack entries as input
// to all other data providers.
public static void onGatherData(GatherDataEvent.Client event) {
// Adds the generated registry objects to the current lookup provider for use
// in other datagen.
this.createDatapackRegistryObjects(
// Our registry set builder to generate the data from.
new RegistrySetBuilder().add(...),
// (Optional) A biconsumer that takes in any conditions to load the object
// associated with the resource key
conditions -> {
conditions.accept(resourceKey, condition);
},
// (Optional) A set of mod ids we are generating the entries for
// By default, supplies the mod id of the current mod container.
Set.of("yourmodid")
);

// You can use the lookup provider with your generated entries by either calling one
// of the `#create*` methods or grabbing the actual lookup via `#getLookupProvider`
// ...
}
```
Expand Down
10 changes: 2 additions & 8 deletions docs/items/armor.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,8 @@ public class MyEquipmentInfoProvider implements DataProvider {

// Listening to the mod event bus
@SubscribeEvent
public static void gatherData(GatherDataEvent event) {
PackOutput output = generator.getPackOutput();

// Other providers here
event.getGenerator().addProvider(
event.includeClient(),
new MyEquipmentInfoProvider(output)
);
public static void gatherData(GatherDataEvent.Client event) {
event.createProvider(MyEquipmentInfoProvider::new);
}
```

Expand Down
4 changes: 2 additions & 2 deletions docs/resources/client/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Language files can be [datagenned][datagen]. To do so, extend the `LanguageProvi
public class MyLanguageProvider extends LanguageProvider {
public MyLanguageProvider(PackOutput output) {
super(
// Provided by the GatherDataEvent.
// Provided by the `GatherDataEvent.Client`.
output,
// Your mod id.
"examplemod",
Expand Down Expand Up @@ -177,7 +177,7 @@ public class MyLanguageProvider extends LanguageProvider {
}
```

Then, register the provider like any other provider in the `GatherDataEvent`.
Then, register the provider like any other provider in the `GatherDataEvent.Client`.

[datacomponent]: ../../items/datacomponents.md
[datagen]: ../index.md#data-generation
Expand Down
11 changes: 2 additions & 9 deletions docs/resources/client/models/datagen.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,8 @@ And like all data providers, don't forget to register your provider to the event

```java
@SubscribeEvent
public static void gatherData(GatherDataEvent event) {
DataGenerator generator = event.getGenerator();
PackOutput output = generator.getPackOutput();

// other providers here
generator.addProvider(
event.includeClient(),
new ExampleModelProvider(output)
);
public static void gatherData(GatherDataEvent.Client event) {
event.createProvider(ExampleModelProvider::new);
}
```

Expand Down
15 changes: 4 additions & 11 deletions docs/resources/client/particles.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Particle definition files can also be [datagenned][datagen] by extending `Partic

```java
public class MyParticleDescriptionProvider extends ParticleDescriptionProvider {
// Get the parameters from GatherDataEvent.
// Get the parameters from `GatherDataEvent.Client`.
public MyParticleDescriptionProvider(PackOutput output) {
super(output);
}
Expand Down Expand Up @@ -166,19 +166,12 @@ public class MyParticleDescriptionProvider extends ParticleDescriptionProvider {
}
```

Don't forget to add the provider to the `GatherDataEvent`:
Don't forget to add the provider to the `GatherDataEvent.Client`:
```java
@SubscribeEvent
public static void gatherData(GatherDataEvent event) {
DataGenerator generator = event.getGenerator();
PackOutput output = generator.getPackOutput();
// other providers here
generator.addProvider(
event.includeClient(),
new MyParticleDescriptionProvider(output)
);
public static void gatherData(GatherDataEvent.Client event) {
event.createProvider(MyParticleDescriptionProvider::new);
}
```
Expand Down
13 changes: 3 additions & 10 deletions docs/resources/client/sounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ Sound files themselves can of course not be [datagenned][datagen], but `sounds.j

```java
public class MySoundDefinitionsProvider extends SoundDefinitionsProvider {
// Parameters can be obtained from GatherDataEvent.
// Parameters can be obtained from `GatherDataEvent.Client`.
public MySoundDefinitionsProvider(PackOutput output) {
// Use your actual mod id instead of "examplemod".
super(output, "examplemod");
Expand Down Expand Up @@ -291,15 +291,8 @@ As with every data provider, don't forget to register the provider to the event:

```java
@SubscribeEvent
public static void gatherData(GatherDataEvent event) {
DataGenerator generator = event.getGenerator();
PackOutput output = generator.getPackOutput();

// other providers here
generator.addProvider(
event.includeClient(),
new MySoundDefinitionsProvider(output)
);
public static void gatherData(GatherDataEvent.Client event) {
event.createProvider(MySoundDefinitionsProvider::new);
}
```

Expand Down
57 changes: 32 additions & 25 deletions docs/resources/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ _See also: [`pack.mcmeta` (Resource Pack)][packmcmetaresourcepack] and [`pack.mc

Data generation, colloquially known as datagen, is a way to programmatically generate JSON resource files, in order to avoid the tedious and error-prone process of writing them by hand. The name is a bit misleading, as it works for assets as well as data.

Datagen is run through the Data run configuration, which is generated for you alongside the Client and Server run configurations. The data run configuration follows the [mod lifecycle][lifecycle] until after the registry events are fired. It then fires the [`GatherDataEvent`][event], in which you can register your to-be-generated objects in the form of data providers, writes said objects to disk, and ends the process.
Datagen is run through the Data run configuration, which is generated for you alongside the Client and Server run configurations. The data run configuration follows the [mod lifecycle][lifecycle] until after the registry events are fired. It then fires one of the [`GatherDataEvent`s][event], in which you can register your to-be-generated objects in the form of data providers, writes said objects to disk, and ends the process.

There are two subtypes which operate on the [**physical side**][physicalside]: `GatherDataEvent.Client` and `GatherDataEvent.Server`. `GatherDataEvent.Client` should contain all providers to generate while `GatherDataEvent.Server` should only contain the providers used to generate datapack entries. As such, all examples shown will use `GatherDataEvent.Client`, as that event is also what is fired by the default `clientData` configuration provided by the MDK.

All data providers extend the `DataProvider` interface and usually require one method to be overridden. The following is a list of noteworthy data generators Minecraft and NeoForge offer (the linked articles add further information, such as helper methods):

Expand Down Expand Up @@ -115,36 +117,43 @@ public class MyRecipeProvider extends RecipeProvider {
@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD, modid = "examplemod")
public class MyDatagenHandler {
@SubscribeEvent
public static void gatherData(GatherDataEvent event) {
// Data generators may require some of these as constructor parameters.
// See below for more details on each of these.
DataGenerator generator = event.getGenerator();
PackOutput output = generator.getPackOutput();
CompletableFuture<HolderLookup.Provider> lookupProvider = event.getLookupProvider();
public static void gatherData(GatherDataEvent.Client event) {
// Data providers should start by calling event.createDatapackRegistryObjects(...)
// to register their datapack registry objects. This allows other providers
// to use these objects during their own data generation.

// From there, providers can generally be registered using event.createProvider(...),
// which acts as a function that provides the PackOutput and optionally the
// CompletableFuture<HolderLookup.Provider>.

// Register the provider.
generator.addProvider(
// A boolean that determines whether the data should actually be generated.
// The event provides methods that determine this:
// event.includeClient(), event.includeServer(),
// event.includeDev() and event.includeReports().
// Since recipes are server data, we only run them in a server datagen.
event.includeServer(),
// Our provider.
new MyRecipeProvider(output, lookupProvider)
);
event.createProvider(MyRecipeProvider::new);
// Other data providers here.

// If you want to create a datapack within the global pack, you can call
// DataGenerator#getBuiltinDatapack. From there, you must use the
// PackGenerator#addProvider method to add any providers to that pack.
DataGenerator.PackGenerator examplePack = event.getGenerator().getBuiltinDatapack(
true, // Should always be true.
"examplemod", // The mod id.
"example_pack" // The name of the pack.
);

examplePack.addProvider(output -> ...);
}
}
```

The event offers some context for you to use:
The event offers some helpers and context for you to use:

- `event.createDatapackRegistryObjects(...)` creates and registers a `DatapackBuiltinEntriesProvider` using the provided `RegistrySetBuilder`. It also forces any future use of the lookup provider to contain your datagenned entries.
- `event.createProvider(...)` registers a provider by providing the `PackOutput` and optionally the `CompletableFuture<HolderLookup.Provider>` as part of a lambda.
- `event.createBlockAndItemTags(...)` registers a `TagsProvider<Block>` and `TagsProvider<Item>` by constructing the `TagsProvider<Item>` using the `TagsProvider<Block>`.
- `event.getGenerator()` returns the `DataGenerator` that you register the providers to.
- `event.getPackOutput()` returns a `PackOutput` that is used by some providers to determine their file output location.
- `event.getResourceManager(PackType)` returns a `ResourceManager` that can be used by providers to check for already existing files.
- `event.getLookupProvider()` returns a `CompletableFuture<HolderLookup.Provider>` that is mainly used by tags and datagen registries to reference other, potentially not yet existing elements.
- `event.includeClient()`, `event.includeServer()`, `event.includeDev()` and `event.includeReports()` are `boolean` methods that allow you to check whether specific command line arguments (see below) are enabled.
- `event.includeDev()` and `event.includeReports()` are `boolean` methods that allow you to check whether specific command line arguments (see below) are enabled.

### Command Line Arguments

Expand All @@ -155,8 +164,6 @@ The data generator can accept several command line arguments:
- `--existing path/to/folder`: Tells the data generator to consider the given folder when checking for existing files. Like with the output, it is recommended to use Gradle's `file(...).getAbsolutePath()`.
- `--existing-mod examplemod`: Tells the data generator to consider the resources in the given mod's JAR file when checking for existing files.
- Generator modes (all of these are boolean arguments and do not need any additional arguments):
- `--includeClient`: Whether to generate client resources (assets). Check at runtime with `GatherDataEvent#includeClient()`.
- `--includeServer`: Whether to generate server resources (data). Check at runtime with `GatherDataEvent#includeServer()`.
- `--includeDev`: Whether to run dev tools. Generally shouldn't be used by mods. Check at runtime with `GatherDataEvent#includeDev()`.
- `--includeReports`: Whether to dump a list of registered objects. Check at runtime with `GatherDataEvent#includeReports()`.
- `--all`: Enable all generator modes.
Expand All @@ -167,7 +174,7 @@ All arguments can be added to the run configurations by adding the following to
runs {
// other run configurations here

data {
clientData {
programArguments.addAll '--arg1', 'value1', '--arg2', 'value2', '--all' // boolean args have no value
}
}
Expand All @@ -179,11 +186,10 @@ For example, to replicate the default arguments, you could specify the following
runs {
// other run configurations here

data {
clientData {
programArguments.addAll '--mod', 'examplemod', // insert your own mod id
'--output', file('src/generated/resources').getAbsolutePath(),
'--includeClient',
'--includeServer'
'--all'
}
}
```
Expand Down Expand Up @@ -221,6 +227,7 @@ runs {
[packmcmetaresourcepack]: https://minecraft.wiki/w/Resource_pack#Contents
[particleprovider]: client/particles.md#datagen
[particles]: client/particles.md
[physicalside]: ../concepts/sides.md#the-physical-side
[predicate]: https://minecraft.wiki/w/Predicate
[recipeprovider]: server/recipes/index.md#data-generation
[recipes]: server/recipes/index.md
Expand Down
46 changes: 19 additions & 27 deletions docs/resources/server/advancements.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,18 @@ public void performExampleAction(ServerPlayer player, additionalContextParameter

Advancements can be [datagenned][datagen] using an `AdvancementProvider`. An `AdvancementProvider` accepts a list of `AdavancementSubProviders`s, which actually generate the advancements using `Advancement.Builder`.

To start, create an instance of `AdvancementProvider` within `GatherDataEvent`:
To start, create an instance of `AdvancementProvider` within one of the `GatherDataEvent`s:

```java
@SubscribeEvent
public static void gatherData(GatherDataEvent event) {
DataGenerator generator = event.getGenerator();
PackOutput output = generator.getPackOutput();
CompletableFuture<HolderLookup.Provider> lookupProvider = event.getLookupProvider();

generator.addProvider(
event.includeServer(),
new AdvancementProvider(
output, lookupProvider,
// Add generators here
List.of(...)
)
);
public static void gatherData(GatherDataEvent.Client event) {
// Call event.createDatapackRegistryObjects(...) first if adding datapack objects

event.createProvider((output, lookupProvider) -> new AdvancementProvider(
output, lookupProvider,
// Add generators here
List.of(...)
));

// Other providers
}
Expand All @@ -197,20 +192,17 @@ public class ExampleClass {
}
}

// In GatherDataEvent
generator.addProvider(
event.includeServer(),
new AdvancementProvider(
output, lookupProvider,
// Add generators here
List.of(
// Add an instance of our generator to the list parameter. This can be done as many times as you want.
// Having multiple generators is purely for organization, all functionality can be achieved with a single generator.
new MyAdvancementGenerator(),
ExampleClass::generateExampleAdvancements
)
// In one of the `GatherDataEvent`s
event.createProvider((output, lookupProvider) -> new AdvancementProvider(
output, lookupProvider,
// Add generators here
List.of(
// Add an instance of our generator to the list parameter. This can be done as many times as you want.
// Having multiple generators is purely for organization, all functionality can be achieved with a single generator.
new MyAdvancementGenerator(),
ExampleClass::generateExampleAdvancements
)
);
));
```

To generate an advancement, you want to use an `Advancement.Builder`:
Expand Down
39 changes: 18 additions & 21 deletions docs/resources/server/damagetypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,32 +95,29 @@ Other damage type-specific behavior, such as invulnerability checks, is often ru

_For more info, see [Data Generation for Datapack Registries][drdatagen]._

Damage type JSON files can be [datagenned][datagen]. Since damage types are a datapack registry, we add a `DatapackBuiltinEntriesProvider` to the `GatherDataEvent` and put our damage types in the `RegistrySetBuilder`:
Damage type JSON files can be [datagenned][datagen]. Since damage types are a datapack registry, we add a `DatapackBuiltinEntriesProvider` via `GatherDataEvent#createDatapackRegistryObjects` and put our damage types in the `RegistrySetBuilder`:

```java
// In your datagen class
@SubscribeEvent
public static void onGatherData(GatherDataEvent event) {
CompletableFuture<HolderLookup.Provider> lookupProvider = event.getGenerator().addProvider(
event.includeServer(),
output -> new DatapackBuiltinEntriesProvider(output, event.getLookupProvider(), new RegistrySetBuilder()
// Add a datapack builtin entry provider for damage types. If this lambda becomes longer,
// this should probably be extracted into a separate method for the sake of readability.
.add(Registries.DAMAGE_TYPE, bootstrap -> {
// Use new DamageType() to create an in-code representation of a damage type.
// The parameters map to the values of the JSON file, in the order seen above.
// All parameters except for the message id and the exhaustion value are optional.
bootstrap.register(EXAMPLE_DAMAGE, new DamageType(EXAMPLE_DAMAGE.location(),
DamageScaling.WHEN_CAUSED_BY_LIVING_NON_PLAYER,
0.1f,
DamageEffects.HURT,
DeathMessageType.DEFAULT))
})
// Add datapack providers for other datapack entries, if applicable.
.add(...),
Set.of(ExampleMod.MOD_ID)
public static void onGatherData(GatherDataEvent.Client event) {
event.createDatapackRegistryObjects(new RegistrySetBuilder()
// Add a datapack builtin entry provider for damage types. If this lambda becomes longer,
// this should probably be extracted into a separate method for the sake of readability.
.add(Registries.DAMAGE_TYPE, bootstrap -> {
// Use new DamageType() to create an in-code representation of a damage type.
// The parameters map to the values of the JSON file, in the order seen above.
// All parameters except for the message id and the exhaustion value are optional.
bootstrap.register(EXAMPLE_DAMAGE, new DamageType(EXAMPLE_DAMAGE.location(),
DamageScaling.WHEN_CAUSED_BY_LIVING_NON_PLAYER,
0.1f,
DamageEffects.HURT,
DeathMessageType.DEFAULT)
)
).getRegistryProvider();
})
// Add datapack providers for other datapack entries, if applicable.
.add(...)
);

// ...
}
Expand Down
Loading

1 comment on commit a15202d

@neoforged-pages-deployments
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploying with Cloudflare Pages

Name Result
Last commit: a15202d4778df6daec7d7069e2e58768263f4a22
Status: ✅ Deploy successful!
Preview URL: https://6c363643.neoforged-docs-previews.pages.dev
PR Preview URL: https://pr-223.neoforged-docs-previews.pages.dev

Please sign in to comment.