Fix : Add way to use resource conditions for block loot table datagen ()

This commit is contained in:
Technici4n 2023-01-20 18:56:11 +01:00 committed by GitHub
parent 2facd44698
commit 06937c4b07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 128 additions and 2 deletions
fabric-data-generation-api-v1/src

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.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}.
*
* <p>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);
}
}

View file

@ -39,6 +39,7 @@ import net.minecraft.loot.context.LootContextType;
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 Consumer<BiConsumer<Identifier,
/**
* Return a new exporter that applies the specified conditions to any loot table it receives.
*
* <p>For block loot tables, use {@link FabricBlockLootTableGenerator#withConditions} instead.
*/
default BiConsumer<Identifier, LootTable.Builder> withConditions(BiConsumer<Identifier, LootTable.Builder> exporter, ConditionJsonProvider... conditions) {
Preconditions.checkArgument(conditions.length > 0, "Must add at least one condition.");

View file

@ -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);
}
}

View file

@ -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 {
}

View file

@ -3,6 +3,7 @@
"package": "net.fabricmc.fabric.mixin.datagen",
"compatibilityLevel": "JAVA_16",
"mixins": [
"loot.BlockLootTableGeneratorMixin",
"AbstractTagProviderMixin",
"ModelProviderMixin",
"TagBuilderMixin"

View file

@ -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"]
}
}
}

View file

@ -319,7 +319,8 @@ public class DataGeneratorTestEntrypoint implements DataGeneratorEntrypoint {
@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);