[Feature] Interface to automatically add Block Loot tables in the Datagen pass
This commit is contained in:
parent
0becc27cf0
commit
0d4461d84f
3 changed files with 72 additions and 0 deletions
|
@ -0,0 +1,40 @@
|
|||
package org.betterx.bclib.api.v3.datagen;
|
||||
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.storage.loot.LootTable;
|
||||
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
||||
|
||||
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
|
||||
import net.fabricmc.fabric.api.datagen.v1.provider.SimpleFabricLootTableProvider;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class BlockLootTableProvider extends SimpleFabricLootTableProvider {
|
||||
protected final List<String> modIDs;
|
||||
|
||||
public BlockLootTableProvider(
|
||||
FabricDataOutput output,
|
||||
List<String> modIDs
|
||||
) {
|
||||
super(output, LootContextParamSets.BLOCK);
|
||||
this.modIDs = modIDs;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void generate(BiConsumer<ResourceLocation, LootTable.Builder> biConsumer) {
|
||||
for (Block block : BuiltInRegistries.BLOCK) {
|
||||
if (block instanceof LootDropProvider dropper) {
|
||||
ResourceLocation id = BuiltInRegistries.BLOCK.getKey(block);
|
||||
if (id != null && modIDs.contains(id.getNamespace())) {
|
||||
LootTable.Builder builder = LootTable.lootTable();
|
||||
dropper.getDroppedItemsBCL(builder);
|
||||
biConsumer.accept(id.withPrefix("blocks/"), builder);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package org.betterx.bclib.api.v3.datagen;
|
||||
|
||||
import org.betterx.bclib.behaviours.interfaces.BehaviourExplosionResistant;
|
||||
|
||||
import net.minecraft.world.level.ItemLike;
|
||||
import net.minecraft.world.level.storage.loot.LootPool;
|
||||
import net.minecraft.world.level.storage.loot.LootTable;
|
||||
import net.minecraft.world.level.storage.loot.entries.LootItem;
|
||||
import net.minecraft.world.level.storage.loot.predicates.ExplosionCondition;
|
||||
import net.minecraft.world.level.storage.loot.providers.number.ConstantValue;
|
||||
|
||||
public interface DropSelfLootProvider<B extends ItemLike> extends LootDropProvider {
|
||||
@Override
|
||||
default void getDroppedItemsBCL(LootTable.Builder builder) {
|
||||
var pool = LootPool.lootPool()
|
||||
.setRolls(ConstantValue.exactly(1.0f))
|
||||
.add(LootItem.lootTableItem((B) this));
|
||||
|
||||
if (this instanceof BehaviourExplosionResistant) {
|
||||
pool = pool.when(ExplosionCondition.survivesExplosion());
|
||||
}
|
||||
|
||||
builder.withPool(pool);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package org.betterx.bclib.api.v3.datagen;
|
||||
|
||||
import net.minecraft.world.level.storage.loot.LootTable;
|
||||
|
||||
public interface LootDropProvider {
|
||||
void getDroppedItemsBCL(LootTable.Builder builder);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue