diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/loot/FabricBlockLootTableGenerator.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/loot/FabricBlockLootTableGenerator.java new file mode 100644 index 0000000000..4e3d7c7638 --- /dev/null +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/loot/FabricBlockLootTableGenerator.java @@ -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.api.datagen.v1.loot; + +import com.google.common.base.Preconditions; + +import net.minecraft.data.server.loottable.BlockLootTableGenerator; + +import net.fabricmc.fabric.api.resource.conditions.v1.ConditionJsonProvider; +import net.fabricmc.fabric.impl.datagen.loot.ConditionBlockLootTableGenerator; + +/** + * Fabric-provided extensions for {@link BlockLootTableGenerator}. + * + *

Note: This interface is automatically implemented via Mixin and interface injection. + */ +public interface FabricBlockLootTableGenerator { + /** + * Return a new generator that applies the specified conditions to any loot table it receives, + * and then forwards the loot tables to this generator. + */ + default BlockLootTableGenerator withConditions(ConditionJsonProvider... conditions) { + Preconditions.checkArgument(conditions.length > 0, "Must add at least one condition."); + + return new ConditionBlockLootTableGenerator((BlockLootTableGenerator) this, conditions); + } +} diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricLootTableProvider.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricLootTableProvider.java index 150bb252c0..c9eb1ed91e 100644 --- a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricLootTableProvider.java +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/provider/FabricLootTableProvider.java @@ -39,6 +39,7 @@ import net.minecraft.util.Identifier; import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput; +import net.fabricmc.fabric.api.datagen.v1.loot.FabricBlockLootTableGenerator; import net.fabricmc.fabric.api.resource.conditions.v1.ConditionJsonProvider; import net.fabricmc.fabric.impl.datagen.FabricDataGenHelper; @@ -57,6 +58,8 @@ public interface FabricLootTableProvider extends ConsumerFor block loot tables, use {@link FabricBlockLootTableGenerator#withConditions} instead. */ default BiConsumer withConditions(BiConsumer exporter, ConditionJsonProvider... conditions) { Preconditions.checkArgument(conditions.length > 0, "Must add at least one condition."); diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/impl/datagen/loot/ConditionBlockLootTableGenerator.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/impl/datagen/loot/ConditionBlockLootTableGenerator.java new file mode 100644 index 0000000000..efe070d7ad --- /dev/null +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/impl/datagen/loot/ConditionBlockLootTableGenerator.java @@ -0,0 +1,50 @@ +/* + * 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.impl.datagen.loot; + +import java.util.Collections; + +import net.minecraft.block.Block; +import net.minecraft.data.server.loottable.BlockLootTableGenerator; +import net.minecraft.loot.LootTable; +import net.minecraft.resource.featuretoggle.FeatureFlags; + +import net.fabricmc.fabric.api.resource.conditions.v1.ConditionJsonProvider; +import net.fabricmc.fabric.impl.datagen.FabricDataGenHelper; + +public class ConditionBlockLootTableGenerator extends BlockLootTableGenerator { + private final BlockLootTableGenerator parent; + private final ConditionJsonProvider[] conditions; + + public ConditionBlockLootTableGenerator(BlockLootTableGenerator parent, ConditionJsonProvider[] conditions) { + super(Collections.emptySet(), FeatureFlags.FEATURE_MANAGER.getFeatureSet()); + + this.parent = parent; + this.conditions = conditions; + } + + @Override + public void generate() { + throw new UnsupportedOperationException("generate() should not be called."); + } + + @Override + public void addDrop(Block block, LootTable.Builder lootTable) { + FabricDataGenHelper.addConditions(lootTable, conditions); + this.parent.addDrop(block, lootTable); + } +} diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/mixin/datagen/loot/BlockLootTableGeneratorMixin.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/mixin/datagen/loot/BlockLootTableGeneratorMixin.java new file mode 100644 index 0000000000..4ac0f6e0e1 --- /dev/null +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/mixin/datagen/loot/BlockLootTableGeneratorMixin.java @@ -0,0 +1,27 @@ +/* + * 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.loot; + +import org.spongepowered.asm.mixin.Mixin; + +import net.minecraft.data.server.loottable.BlockLootTableGenerator; + +import net.fabricmc.fabric.api.datagen.v1.loot.FabricBlockLootTableGenerator; + +@Mixin(BlockLootTableGenerator.class) +public class BlockLootTableGeneratorMixin implements FabricBlockLootTableGenerator { +} diff --git a/fabric-data-generation-api-v1/src/main/resources/fabric-data-generation-api-v1.mixins.json b/fabric-data-generation-api-v1/src/main/resources/fabric-data-generation-api-v1.mixins.json index 5784bfad10..55ac256dce 100644 --- a/fabric-data-generation-api-v1/src/main/resources/fabric-data-generation-api-v1.mixins.json +++ b/fabric-data-generation-api-v1/src/main/resources/fabric-data-generation-api-v1.mixins.json @@ -3,6 +3,7 @@ "package": "net.fabricmc.fabric.mixin.datagen", "compatibilityLevel": "JAVA_16", "mixins": [ + "loot.BlockLootTableGeneratorMixin", "AbstractTagProviderMixin", "ModelProviderMixin", "TagBuilderMixin" diff --git a/fabric-data-generation-api-v1/src/main/resources/fabric.mod.json b/fabric-data-generation-api-v1/src/main/resources/fabric.mod.json index de92d96449..c8cf3e1c9b 100644 --- a/fabric-data-generation-api-v1/src/main/resources/fabric.mod.json +++ b/fabric-data-generation-api-v1/src/main/resources/fabric.mod.json @@ -28,6 +28,9 @@ ], "accessWidener" : "fabric-data-generation-api-v1.accesswidener", "custom": { - "fabric-api:module-lifecycle": "stable" + "fabric-api:module-lifecycle": "stable", + "loom:injected_interfaces": { + "net/minecraft/class_7788": ["net/fabricmc/fabric/api/datagen/v1/loot/FabricBlockLootTableGenerator"] + } } } diff --git a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java index ee037af974..96bf9796d5 100644 --- a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java +++ b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java @@ -249,7 +249,8 @@ private TestBlockLootTableProvider(FabricDataOutput output) { @Override public void generate() { - addDrop(SIMPLE_BLOCK); + // Same condition twice to test recursive condition adding + withConditions(ALWAYS_LOADED).withConditions(DefaultResourceConditions.not(NEVER_LOADED)).addDrop(SIMPLE_BLOCK); addDrop(BLOCK_WITHOUT_ITEM, drops(SIMPLE_BLOCK)); excludeFromStrictValidation(BLOCK_WITHOUT_LOOT_TABLE);