| 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.Set;
|
| 7 | import net.minecraft.util.context.ContextKey;
|
| 8 | import net.minecraft.world.entity.SlotProvider;
|
| 9 | import net.minecraft.world.inventory.SlotRange;
|
| 10 | import net.minecraft.world.inventory.SlotRanges;
|
| 11 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 12 | import net.minecraft.world.level.storage.loot.LootContextArg;
|
| 13 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 14 |
|
| 15 | public class RangeSlotSource implements SlotSource {
|
| 16 | private static final LootContextArg<Object> CONTAINER_ARG = LootContextArg.of(LootContextParams.CONTAINER);
|
| 17 | private static final Codec<LootContextArg<Object>> SOURCE_CODEC = LootContextArg.createArgCodec(
|
| 18 | builder -> builder.anyOf(LootContext.EntityTarget.values()).anyOf(LootContext.BlockEntityTarget.values()).or("container", CONTAINER_ARG)
|
| 19 | );
|
| 20 | public static final MapCodec<RangeSlotSource> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 21 | i -> i.group(SOURCE_CODEC.optionalFieldOf("source", CONTAINER_ARG).forGetter(t -> t.source), SlotRanges.CODEC.fieldOf("slots").forGetter(t -> t.slotRange))
|
| 22 | .apply(i, RangeSlotSource::new)
|
| 23 | );
|
| 24 | private final LootContextArg<Object> source;
|
| 25 | private final SlotRange slotRange;
|
| 26 |
|
| 27 | private RangeSlotSource(final LootContextArg<Object> source, final SlotRange slotRange) {
|
| 28 | this.source = source;
|
| 29 | this.slotRange = slotRange;
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public MapCodec<RangeSlotSource> codec() {
|
| 34 | return MAP_CODEC;
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public Set<ContextKey<?>> getReferencedContextParams() {
|
| 39 | return Set.of(this.source.contextParam());
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public final SlotCollection provide(final LootContext context) {
|
| 44 | return this.source.get(context) instanceof SlotProvider slotProvider ? slotProvider.getSlotsFromRange(this.slotRange.slots()) : SlotCollection.EMPTY;
|
| 45 | }
|
| 46 |
|
| 47 | public static RangeSlotSource slotRange(final SlotRange slotRange) {
|
| 48 | return new RangeSlotSource(CONTAINER_ARG, slotRange);
|
| 49 | }
|
| 50 | }
|
| 51 |
|