Skip to content

Commit

Permalink
Fix #2552: Add way to use resource conditions for block loot table da…
Browse files Browse the repository at this point in the history
…tagen (#2823)
  • Loading branch information
Technici4n authored Jan 20, 2023
1 parent 2facd44 commit 06937c4
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.");
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"package": "net.fabricmc.fabric.mixin.datagen",
"compatibilityLevel": "JAVA_16",
"mixins": [
"loot.BlockLootTableGeneratorMixin",
"AbstractTagProviderMixin",
"ModelProviderMixin",
"TagBuilderMixin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,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);
Expand Down

0 comments on commit 06937c4

Please sign in to comment.