| 1 | package net.minecraft.world.level.storage.loot;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import java.util.function.Function;
|
| 5 | import java.util.function.UnaryOperator;
|
| 6 | import net.minecraft.util.ExtraCodecs;
|
| 7 | import net.minecraft.util.StringRepresentable;
|
| 8 | import net.minecraft.util.context.ContextKey;
|
| 9 | import net.minecraft.world.entity.Entity;
|
| 10 | import net.minecraft.world.item.ItemInstance;
|
| 11 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public interface LootContextArg<R> {
|
| 15 | Codec<LootContextArg<Object>> ENTITY_OR_BLOCK = createArgCodec(
|
| 16 | builder -> builder.anyOf(LootContext.EntityTarget.values()).anyOf(LootContext.BlockEntityTarget.values())
|
| 17 | );
|
| 18 |
|
| 19 | @Nullable R get(LootContext context);
|
| 20 |
|
| 21 | ContextKey<?> contextParam();
|
| 22 |
|
| 23 | static <U> LootContextArg<U> cast(final LootContextArg<? extends U> original) {
|
| 24 | return (LootContextArg<U>)original;
|
| 25 | }
|
| 26 |
|
| 27 | static <R> LootContextArg<R> of(final ContextKey<? extends R> contextParam) {
|
| 28 | return () -> contextParam;
|
| 29 | }
|
| 30 |
|
| 31 | static <R> Codec<LootContextArg<R>> createArgCodec(final UnaryOperator<LootContextArg.ArgCodecBuilder<R>> consumer) {
|
| 32 | return consumer.apply(new LootContextArg.ArgCodecBuilder<>()).build();
|
| 33 | }
|
| 34 |
|
| 35 | final class ArgCodecBuilder<R> {
|
| 36 | private final ExtraCodecs.LateBoundIdMapper<String, LootContextArg<R>> sources = new ExtraCodecs.LateBoundIdMapper<>();
|
| 37 |
|
| 38 | private ArgCodecBuilder() {
|
| 39 | }
|
| 40 |
|
| 41 | public <T> LootContextArg.ArgCodecBuilder<R> anyOf(
|
| 42 | final T[] targets, final Function<T, String> nameGetter, final Function<T, ? extends LootContextArg<R>> argFactory
|
| 43 | ) {
|
| 44 | for (T target : targets) {
|
| 45 | this.sources.put(nameGetter.apply(target), (LootContextArg<R>)argFactory.apply(target));
|
| 46 | }
|
| 47 |
|
| 48 | return this;
|
| 49 | }
|
| 50 |
|
| 51 | public <T extends StringRepresentable> LootContextArg.ArgCodecBuilder<R> anyOf(final T[] targets, final Function<T, ? extends LootContextArg<R>> argFactory) {
|
| 52 | return this.anyOf(targets, StringRepresentable::getSerializedName, argFactory);
|
| 53 | }
|
| 54 |
|
| 55 | public <T extends StringRepresentable & LootContextArg<? extends R>> LootContextArg.ArgCodecBuilder<R> anyOf(final T[] targets) {
|
| 56 | return this.anyOf(targets, x$0 -> LootContextArg.cast((LootContextArg<? extends R>)x$0));
|
| 57 | }
|
| 58 |
|
| 59 | public LootContextArg.ArgCodecBuilder<R> anyEntity(final Function<? super ContextKey<? extends Entity>, ? extends LootContextArg<R>> function) {
|
| 60 | return this.anyOf(LootContext.EntityTarget.values(), target -> function.apply(target.contextParam()));
|
| 61 | }
|
| 62 |
|
| 63 | public LootContextArg.ArgCodecBuilder<R> anyBlockEntity(final Function<? super ContextKey<? extends BlockEntity>, ? extends LootContextArg<R>> function) {
|
| 64 | return this.anyOf(LootContext.BlockEntityTarget.values(), target -> function.apply(target.contextParam()));
|
| 65 | }
|
| 66 |
|
| 67 | public LootContextArg.ArgCodecBuilder<R> anyItemStack(final Function<? super ContextKey<? extends ItemInstance>, ? extends LootContextArg<R>> function) {
|
| 68 | return this.anyOf(LootContext.ItemStackTarget.values(), target -> function.apply(target.contextParam()));
|
| 69 | }
|
| 70 |
|
| 71 | public LootContextArg.ArgCodecBuilder<R> or(final String name, final LootContextArg<R> arg) {
|
| 72 | this.sources.put(name, arg);
|
| 73 | return this;
|
| 74 | }
|
| 75 |
|
| 76 | private Codec<LootContextArg<R>> build() {
|
| 77 | return this.sources.codec(Codec.STRING);
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | interface Getter<T, R> extends LootContextArg<R> {
|
| 82 | @Nullable R get(T value);
|
| 83 |
|
| 84 | @Override
|
| 85 | ContextKey<? extends T> contextParam();
|
| 86 |
|
| 87 | @Override
|
| 88 | default @Nullable R get(final LootContext context) {
|
| 89 | T value = context.getOptional((ContextKey<T>)this.contextParam());
|
| 90 | return value != null ? this.get(value) : null;
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | interface SimpleGetter<T> extends LootContextArg<T> {
|
| 95 | @Override
|
| 96 | ContextKey<? extends T> contextParam();
|
| 97 |
|
| 98 | @Override
|
| 99 | default @Nullable T get(final LootContext context) {
|
| 100 | return context.getOptional((ContextKey<T>)this.contextParam());
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|