| 1 | package net.minecraft.world.level.storage.loot.entries;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.HolderSet;
|
| 9 | import net.minecraft.world.item.ItemStack;
|
| 10 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 11 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 12 | import net.minecraft.world.level.storage.loot.Validatable;
|
| 13 | import net.minecraft.world.level.storage.loot.ValidationContext;
|
| 14 | import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
|
| 15 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 16 |
|
| 17 | public class NestedLootTable extends ExpandableContainerBase {
|
| 18 | public static final MapCodec<NestedLootTable> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 19 | i -> i.group(LootTable.LIST_CODEC.fieldOf("value").forGetter(e -> e.value)).and(expandableFields(i)).apply(i, NestedLootTable::new)
|
| 20 | );
|
| 21 | private final HolderSet<LootTable> value;
|
| 22 |
|
| 23 | private NestedLootTable(
|
| 24 | final HolderSet<LootTable> value,
|
| 25 | final boolean expand,
|
| 26 | final int weight,
|
| 27 | final int quality,
|
| 28 | final Optional<Holder<LootItemCondition>> condition,
|
| 29 | final Optional<Holder<LootItemFunction>> modifier
|
| 30 | ) {
|
| 31 | super(expand, weight, quality, condition, modifier);
|
| 32 | this.value = value;
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public MapCodec<NestedLootTable> codec() {
|
| 37 | return MAP_CODEC;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected boolean addExpandedEntries(final Consumer<LootPoolEntry> output) {
|
| 42 | for (final Holder<LootTable> table : this.value) {
|
| 43 | output.accept(new UniformContainerBase.EntryBase() {
|
| 44 | @Override
|
| 45 | public void createItemStack(final Consumer<ItemStack> output, final LootContext context) {
|
| 46 | table.value().getRandomItemsRaw(context, output);
|
| 47 | }
|
| 48 | });
|
| 49 | }
|
| 50 |
|
| 51 | return true;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected boolean addUnexpandedEntry(final Consumer<LootPoolEntry> output) {
|
| 56 | output.accept(new UniformContainerBase.EntryBase() {
|
| 57 | @Override
|
| 58 | public void createItemStack(final Consumer<ItemStack> output, final LootContext context) {
|
| 59 | NestedLootTable.this.value.forEach(t -> t.value().getRandomItemsRaw(context, output));
|
| 60 | }
|
| 61 | });
|
| 62 | return true;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public void validate(final ValidationContext context) {
|
| 67 | super.validate(context);
|
| 68 | Validatable.validateHolderSet(context, "value", this.value);
|
| 69 | }
|
| 70 |
|
| 71 | public static UniformContainerBase.Builder<?> lootTableReference(final Holder<LootTable> table) {
|
| 72 | return simpleBuilder((weight, quality, conditions, functions) -> new NestedLootTable(HolderSet.direct(table), false, weight, quality, conditions, functions));
|
| 73 | }
|
| 74 |
|
| 75 | public static UniformContainerBase.Builder<?> inlineLootTable(final LootTable table) {
|
| 76 | return lootTableReference(Holder.direct(table));
|
| 77 | }
|
| 78 | }
|
| 79 |
|