| 1 | package net.minecraft.world.item.slot;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.List;
|
| 8 | import java.util.function.Function;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.core.HolderSet;
|
| 11 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 12 | import net.minecraft.world.level.storage.loot.Validatable;
|
| 13 | import net.minecraft.world.level.storage.loot.ValidationContext;
|
| 14 |
|
| 15 | public abstract class CompositeSlotSource implements SlotSource {
|
| 16 | protected final HolderSet<SlotSource> terms;
|
| 17 | private final Function<LootContext, SlotCollection> compositeSlotSource;
|
| 18 |
|
| 19 | protected CompositeSlotSource(final HolderSet<SlotSource> terms) {
|
| 20 | this.terms = terms;
|
| 21 | this.compositeSlotSource = group(terms);
|
| 22 | }
|
| 23 |
|
| 24 | private static Function<LootContext, SlotCollection> group(final HolderSet<SlotSource> terms) {
|
| 25 | if (!terms.isBound()) {
|
| 26 | return context -> {
|
| 27 | List<SlotCollection> collections = new ArrayList<>();
|
| 28 |
|
| 29 | for (Holder<SlotSource> term : terms) {
|
| 30 | collections.add(term.value().provide(context));
|
| 31 | }
|
| 32 |
|
| 33 | return SlotCollection.concat(collections);
|
| 34 | };
|
| 35 | }
|
| 36 |
|
| 37 | return switch (terms.size()) {
|
| 38 | case 0 -> var0 -> SlotCollection.EMPTY;
|
| 39 | case 1 -> {
|
| 40 | Holder<SlotSource> term = terms.get(0);
|
| 41 | yield context -> term.value().provide(context);
|
| 42 | }
|
| 43 | case 2 -> {
|
| 44 | Holder<SlotSource> first = terms.get(0);
|
| 45 | Holder<SlotSource> second = terms.get(1);
|
| 46 | yield context -> SlotCollection.concat(first.value().provide(context), second.value().provide(context));
|
| 47 | }
|
| 48 | default -> context -> {
|
| 49 | List<SlotCollection> collections = new ArrayList<>();
|
| 50 |
|
| 51 | for (Holder<SlotSource> term : terms) {
|
| 52 | collections.add(term.value().provide(context));
|
| 53 | }
|
| 54 |
|
| 55 | return SlotCollection.concat(collections);
|
| 56 | };
|
| 57 | };
|
| 58 | }
|
| 59 |
|
| 60 | protected static <T extends CompositeSlotSource> MapCodec<T> createCodec(final Function<HolderSet<SlotSource>, T> factory) {
|
| 61 | return RecordCodecBuilder.mapCodec(i -> i.group(SlotSources.LIST_CODEC.fieldOf("terms").forGetter(t -> t.terms)).apply(i, factory));
|
| 62 | }
|
| 63 |
|
| 64 | protected static <T extends CompositeSlotSource> Codec<T> createInlineCodec(final Function<HolderSet<SlotSource>, T> factory) {
|
| 65 | return SlotSources.LIST_CODEC.xmap(factory, t -> t.terms);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public abstract MapCodec<? extends CompositeSlotSource> codec();
|
| 70 |
|
| 71 | @Override
|
| 72 | public SlotCollection provide(final LootContext context) {
|
| 73 | return this.compositeSlotSource.apply(context);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public void validate(final ValidationContext context) {
|
| 78 | SlotSource.super.validate(context);
|
| 79 | Validatable.validateHolderSet(context, "terms", this.terms);
|
| 80 | }
|
| 81 | }
|
| 82 |
|