Allow using Identifiers in advancement datagen (#5366)

* Allow using Identifiers for advancement datagen

Adds a `save` overload that takes an Identifier, as well as a utility method in the advancement provider to easily add existing vanilla advancements as a parent.

* Fix checkstyle

* Make injected interface class NonExtendable

* Add Javadoc comment to FabricAdvancementBuilder

* Add an example usage to createPlaceholder

* Add ApiStatus.Internal to mixin package

* Remove package info from mixin package

* Enchantment -> Advancement

Co-authored-by: Juuz <6596629+Juuxel@users.noreply.github.com>

* Make advancement builder mixin package private

* Fix weird indenting

* Use snippet javadoc

---------

Co-authored-by: Juuz <6596629+Juuxel@users.noreply.github.com>
This commit is contained in:
Celeste 2026-05-03 15:23:05 +02:00 committed by modmuss50
parent 8e48e038de
commit 633e8f0490
9 changed files with 160 additions and 0 deletions

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.api.datagen.v1.advancement;
import java.util.function.Consumer;
import org.jetbrains.annotations.ApiStatus;
import net.minecraft.advancements.AdvancementHolder;
import net.minecraft.resources.Identifier;
/**
* Advancement builder extensions provided by Fabric.
*/
@ApiStatus.NonExtendable
public interface FabricAdvancementBuilder {
/**
* Builds and saves the advancement.
* @param output The output to save the advancement to
* @param id The id of the advancement
* @return A new holder containing the saved advancement
*/
default AdvancementHolder save(Consumer<AdvancementHolder> output, Identifier id) {
throw new UnsupportedOperationException("Implemented via mixin");
}
}

View file

@ -0,0 +1,20 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@NullMarked
package net.fabricmc.fabric.api.datagen.v1.advancement;
import org.jspecify.annotations.NullMarked;

View file

@ -78,6 +78,21 @@ public abstract class FabricAdvancementProvider implements DataProvider {
};
}
/**
* Creates a reference to an existing advancement.
*
* {@snippet :
* Advancement.Builder builder = ...;
* builder.parent(createPlaceholder(Identifier.withDefaultNamespace("adventure/root")))
* }
*
* @param id The identifier to create a reference for.
* @return A new holder containing the provided id.
*/
public static AdvancementHolder createPlaceholder(Identifier id) {
return Advancement.Builder.advancement().build(id);
}
@Override
public CompletableFuture<?> run(CachedOutput output) {
return this.registryLookup.thenCompose(lookup -> {

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.mixin.datagen.advancement;
import java.util.function.Consumer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.AdvancementHolder;
import net.minecraft.resources.Identifier;
import net.fabricmc.fabric.api.datagen.v1.advancement.FabricAdvancementBuilder;
@Mixin(Advancement.Builder.class)
abstract class AdvancementBuilderMixin implements FabricAdvancementBuilder {
@Shadow
public abstract AdvancementHolder build(Identifier id);
@Override
public AdvancementHolder save(Consumer<AdvancementHolder> output, Identifier id) {
AdvancementHolder advancement = build(id);
output.accept(advancement);
return advancement;
}
}

View file

@ -53,6 +53,7 @@ accessible class net/minecraft/client/data/models/ModelProvider$ItemInfoCollecto
accessible class net/minecraft/client/data/models/ModelProvider$BlockStateGeneratorCollector
accessible field net/minecraft/client/data/models/ModelProvider$BlockStateGeneratorCollector generators Ljava/util/Map;
transitive-inject-interface net/minecraft/advancements/Advancement$Builder net/fabricmc/fabric/api/datagen/v1/advancement/FabricAdvancementBuilder
transitive-inject-interface net/minecraft/data/loot/BlockLootSubProvider net/fabricmc/fabric/api/datagen/v1/loot/FabricBlockLootSubProvider
transitive-inject-interface net/minecraft/data/loot/EntityLootSubProvider net/fabricmc/fabric/api/datagen/v1/loot/FabricEntityLootSubProvider
transitive-inject-interface net/minecraft/data/recipes/RecipeOutput net/fabricmc/fabric/api/datagen/v1/recipe/FabricRecipeOutput

View file

@ -11,6 +11,7 @@
"TagAppenderMixin$TagAppender1Mixin",
"TagBuilderMixin",
"TagsProviderMixin",
"advancement.AdvancementBuilderMixin",
"loot.BlockLootSubProviderAccessor",
"loot.BlockLootSubProviderMixin",
"loot.EntityLootSubProviderAccessor",

View file

@ -0,0 +1,29 @@
{
"parent": "minecraft:adventure/root",
"criteria": {
"killed_something": {
"trigger": "minecraft:player_killed_entity"
}
},
"display": {
"announce_to_chat": false,
"background": "minecraft:textures/gui/advancements/backgrounds/end.png",
"description": {
"translate": "advancements.test.adventure_child.description"
},
"frame": "goal",
"icon": {
"id": "fabric-data-gen-api-v1-testmod:simple_block"
},
"show_toast": false,
"title": {
"translate": "advancements.test.adventure_child.title"
}
},
"requirements": [
[
"killed_something"
]
],
"sends_telemetry_event": true
}

View file

@ -385,6 +385,18 @@ public class DataGeneratorTestEntrypoint implements DataGeneratorEntrypoint {
false, false, false)
.addCriterion("killed_something", KilledTrigger.TriggerInstance.playerKilledEntity())
.save(withConditions(consumer, NEVER_LOADED), MOD_ID + ":test/root_not_loaded");
AdvancementHolder adventureChild = Advancement.Builder.advancement()
.display(SIMPLE_BLOCK,
Component.translatable("advancements.test.adventure_child.title"),
Component.translatable("advancements.test.adventure_child.description"),
Identifier.withDefaultNamespace("textures/gui/advancements/backgrounds/end.png"),
AdvancementType.GOAL,
false, false, false
)
.addCriterion("killed_something", KilledTrigger.TriggerInstance.playerKilledEntity())
.parent(createPlaceholder(Identifier.withDefaultNamespace("adventure/root")))
.save(consumer, Identifier.fromNamespaceAndPath(MOD_ID, "test/adventure_child"));
}
}

View file

@ -48,6 +48,7 @@ accessible class net/minecraft/client/data/models/ModelProvider$ItemInfoCollecto
accessible class net/minecraft/client/data/models/ModelProvider$BlockStateGeneratorCollector
accessible field net/minecraft/client/data/models/ModelProvider$BlockStateGeneratorCollector generators Ljava/util/Map;
transitive-inject-interface net/minecraft/advancements/Advancement$Builder net/fabricmc/fabric/api/datagen/v1/advancement/FabricAdvancementBuilder
transitive-inject-interface net/minecraft/data/loot/BlockLootSubProvider net/fabricmc/fabric/api/datagen/v1/loot/FabricBlockLootSubProvider
transitive-inject-interface net/minecraft/data/loot/EntityLootSubProvider net/fabricmc/fabric/api/datagen/v1/loot/FabricEntityLootSubProvider
transitive-inject-interface net/minecraft/data/recipes/RecipeOutput net/fabricmc/fabric/api/datagen/v1/recipe/FabricRecipeOutput