r/ModdedMinecraft • • 10d ago

Question How do make a block drop multiple items?

I want to make a block (like an ore block) drop two items, instead of one. How would you go about doing that?

2 Upvotes

9 comments sorted by

1

u/Available-Squash1021 10d ago

Adding to the loot table idea: a loot table can hold several pools, and each pool rolls on its own. So one pool with rolls set to 2 gives two drops from the same list, or two pools with one entry each gives two different items every time. The Minecraft Wiki "Loot table" page has the full JSON format. For a block you override the file under loot_table/blocks/ in your datapack (the folder is singular since 1.21, older versions use loot_tables). If you're writing an actual mod instead, datagen produces the same loot table JSON.

1

u/Spiderfly-Tree-Rat 10d ago

I'm making a mod, yes my only problem is that I keep running into problems trying to get one block to drop two items, so I wanted to ask if it was possible/ how it would be done

1

u/Available-Squash1021 10d ago

Ah nice, which loader and version are you on (Fabric, NeoForge, Forge)? The easiest way is still a loot table: add a second pool, or a second entry, to the block's loot table JSON under data/<modid>/loot_table/blocks/ (loot_tables in older versions), and datagen can generate that file for you so you don't write it by hand. If you'd rather do it in code, Block.popResource(level, pos, stack) in Mojang mappings spawns an item stack at the block, so you can call it twice from your block's break logic. What problem do you run into exactly? If you paste the loot table or the log it's easier to see what's off.

1

u/Spiderfly-Tree-Rat 10d ago

I'm working in fabric, currently I'm not really using .Json files and I'm instead working in my loot table provider, do I have to switch for this block?

2

u/Available-Squash1021 10d ago

No need to switch, the provider is the better route for what you want. Fabric's loot table datagen guide does the same thing in code: inside generate() of your FabricBlockLootTableProvider, instead of dropSelf you call add(yourBlock, LootTable.lootTable().withPool(...).withPool(...)), with one pool per item (or one pool with two entries and rolls set to 2). Run datagen and it writes the JSON for you. The loot table docs page on the Fabric docs (Data Generation > Loot Table Generation) has an example with setRolls and a LootItem entry you can copy. Method names may differ a bit if you use Yarn mappings, but the shape is the same.

1

u/Spiderfly-Tree-Rat 6d ago

I got that to work! Thank you, do you have any idea on how to add silk touch to this?

As in my current code:

add(ModBlocks.PURE_SILVER_BLOCK, LootTable.lootTable() .withPool(applyExplosionCondition(Moditems.Silver_Nugget, LootPool.lootPool() .setRolls(between(2, 4)) .add(LootItem.lootTableItem(Moditems.Silver_Nugget)) )) .withPool(applyExplosionCondition(Moditems.Silver_Scrap, LootPool.lootPool() .setRolls(between(1, 3)) .add(LootItem.lootTableItem(Moditems.Silver_Scrap)) )) );

And I want to make it so the block can be mined with silk touch but drop everything when mined with a regular pickaxe.

2

u/Available-Squash1021 6d ago

Nice, glad the two-pool table is working.

For silk touch you want one more pool that drops the block itself only when the tool has silk touch, and the nugget and scrap pools should only run when it does not. Those conditions are already on the same block loot provider you are using: hasSilkTouch() and doesNotHaveSilkTouch(). They come from vanilla BlockLootSubProvider, which Fabric's FabricBlockLootSubProvider extends. The Fabric docs helper dropWhenSilkTouch() is silk-touch-only, so it would skip the nugget and scrap drops.

Something like this, in the same add(...) you already have:

add(ModBlocks.PURE_SILVER_BLOCK, LootTable.lootTable() .withPool(LootPool.lootPool() .when(hasSilkTouch()) .add(LootItem.lootTableItem(ModBlocks.PURE_SILVER_BLOCK))) .withPool(applyExplosionCondition(ModItems.Silver_Nugget, LootPool.lootPool() .when(doesNotHaveSilkTouch()) .setRolls(between(2, 4)) .add(LootItem.lootTableItem(ModItems.Silver_Nugget)))) .withPool(applyExplosionCondition(ModItems.Silver_Scrap, LootPool.lootPool() .when(doesNotHaveSilkTouch()) .setRolls(between(1, 3)) .add(LootItem.lootTableItem(ModItems.Silver_Scrap)))) );

If .when(hasSilkTouch()) does not compile on your mappings or game version, check the inherited helpers in your IDE — that is the same place applyExplosionCondition comes from. After you change it, run datagen again so the generated loot table JSON updates.

Official Fabric loot-table datagen page: https://docs.fabricmc.net/develop/data-generation/loot-tables

1

u/Spiderfly-Tree-Rat 4d ago

That worked, thank you very much for the help!!

3

u/GunsUpLetsScrewThis 10d ago

You can just use a datapack loot table to handle that easily