| 1 | package net.minecraft.data.loot;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.Set;
|
| 7 | import java.util.stream.Stream;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.core.HolderGetter;
|
| 10 | import net.minecraft.core.Registry;
|
| 11 | import net.minecraft.core.registries.Registries;
|
| 12 | import net.minecraft.core.registries.SingleRegistryBootstrap;
|
| 13 | import net.minecraft.data.worldgen.BootstrapContext;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.resources.ResourceKey;
|
| 16 | import net.minecraft.util.Util;
|
| 17 | import net.minecraft.util.context.ContextKeySet;
|
| 18 | import net.minecraft.world.RandomSequence;
|
| 19 | import net.minecraft.world.level.levelgen.RandomSupport;
|
| 20 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 21 |
|
| 22 | public class LootTableProvider implements SingleRegistryBootstrap<LootTable> {
|
| 23 | private final Set<ResourceKey<LootTable>> requiredTables;
|
| 24 | private final List<LootTableProvider.SubProviderEntry> subProviders;
|
| 25 |
|
| 26 | public LootTableProvider(final Set<ResourceKey<LootTable>> requiredTables, final List<LootTableProvider.SubProviderEntry> subProviders) {
|
| 27 | this.subProviders = subProviders;
|
| 28 | this.requiredTables = requiredTables;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public void run(final BootstrapContext<LootTable> context) {
|
| 33 | Map<RandomSupport.Seed128bit, Identifier> randomSequenceSeeds = new Object2ObjectOpenHashMap<>();
|
| 34 | HolderGetter<LootTable> lootTables = context.lookup(Registries.LOOT_TABLE);
|
| 35 | this.requiredTables.forEach(lootTables::get);
|
| 36 | this.subProviders.forEach(subProvider -> subProvider.bootstrap().create(new LootTableSubProvider.Context() {
|
| 37 | @Override
|
| 38 | public Holder.Reference<LootTable> accept(final ResourceKey<LootTable> key, final LootTable.Builder lootTable) {
|
| 39 | Identifier sequenceId = LootTableProvider.sequenceIdForLootTable(key);
|
| 40 | Identifier previous = randomSequenceSeeds.put(RandomSequence.seedForKey(sequenceId), sequenceId);
|
| 41 | if (previous != null) {
|
| 42 | Util.logAndPauseIfInIde("Loot table random sequence seed collision on " + previous + " and " + key.identifier());
|
| 43 | }
|
| 44 |
|
| 45 | LootTable table = lootTable.setRandomSequence(sequenceId).setParamSet(subProvider.paramSet).build();
|
| 46 | return context.register(key, table);
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public <S> HolderGetter<S> lookup(final ResourceKey<? extends Registry<? extends S>> key) {
|
| 51 | return context.lookup(key);
|
| 52 | }
|
| 53 |
|
| 54 | @Deprecated
|
| 55 | @Override
|
| 56 | public <S> Stream<Holder.Reference<S>> listContextElements(final ResourceKey<? extends Registry<? extends S>> key) {
|
| 57 | return context.listContextElements(key);
|
| 58 | }
|
| 59 | }).run());
|
| 60 | }
|
| 61 |
|
| 62 | private static Identifier sequenceIdForLootTable(final ResourceKey<LootTable> id) {
|
| 63 | return id.identifier();
|
| 64 | }
|
| 65 |
|
| 66 | public record SubProviderEntry(LootTableSubProvider.Factory bootstrap, ContextKeySet paramSet) {
|
| 67 | }
|
| 68 | }
|
| 69 |
|