| 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.world.item.ItemStack;
|
| 9 | import net.minecraft.world.item.slot.SlotSource;
|
| 10 | import net.minecraft.world.item.slot.SlotSources;
|
| 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 | import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
|
| 15 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 16 |
|
| 17 | public class SlotLoot extends SingleEntryContainerBase {
|
| 18 | public static final MapCodec<SlotLoot> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 19 | i -> i.group(SlotSources.CODEC.fieldOf("slot_source").forGetter(t -> t.slotSource)).and(uniformFields(i)).apply(i, SlotLoot::new)
|
| 20 | );
|
| 21 | private final Holder<SlotSource> slotSource;
|
| 22 |
|
| 23 | private SlotLoot(
|
| 24 | final Holder<SlotSource> slotSource,
|
| 25 | final int weight,
|
| 26 | final int quality,
|
| 27 | final Optional<Holder<LootItemCondition>> condition,
|
| 28 | final Optional<Holder<LootItemFunction>> modifier
|
| 29 | ) {
|
| 30 | super(weight, quality, condition, modifier);
|
| 31 | this.slotSource = slotSource;
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public MapCodec<SlotLoot> codec() {
|
| 36 | return MAP_CODEC;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public void createItemStack(final Consumer<ItemStack> output, final LootContext context) {
|
| 41 | this.slotSource.value().provide(context).itemCopies().filter(stack -> !stack.isEmpty()).forEach(output);
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void validate(final ValidationContext context) {
|
| 46 | super.validate(context);
|
| 47 | Validatable.validateHolder(context, "slot_source", this.slotSource);
|
| 48 | }
|
| 49 | }
|
| 50 |
|