| 1 | package net.minecraft.world.level.storage.loot.providers.number.ints;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import io.netty.buffer.ByteBuf;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.function.Function;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.core.component.DataComponentType;
|
| 10 | import net.minecraft.core.registries.Registries;
|
| 11 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 12 | import net.minecraft.network.codec.StreamCodec;
|
| 13 | import net.minecraft.resources.ResourceKey;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 16 |
|
| 17 | public sealed interface ResolvableInt permits ResolvableInt.Constant, ResolvableInt.Reference {
|
| 18 | Codec<ResolvableInt> CODEC = Codec.either(ResolvableInt.Constant.CODEC, ResolvableInt.Reference.CODEC).xmap(Either::unwrap, ResolvableInt::wrap);
|
| 19 | StreamCodec<ByteBuf, ResolvableInt> STREAM_CODEC = ByteBufCodecs.either(ResolvableInt.Constant.STREAM_CODEC, ResolvableInt.Reference.STREAM_CODEC)
|
| 20 | .map(Either::unwrap, ResolvableInt::wrap);
|
| 21 |
|
| 22 | private static Either<ResolvableInt.Constant, ResolvableInt.Reference> wrap(final ResolvableInt resolvableNumber) {
|
| 23 | return switch (resolvableNumber) {
|
| 24 | case ResolvableInt.Constant constant -> Either.left(constant);
|
| 25 | case ResolvableInt.Reference reference -> Either.right(reference);
|
| 26 | default -> throw new MatchException(null, null);
|
| 27 | };
|
| 28 | }
|
| 29 |
|
| 30 | int get(LootContext context, int defaultValue);
|
| 31 |
|
| 32 | static ResolvableInt fromKey(final ResourceKey<ContextIntProvider> key) {
|
| 33 | return new ResolvableInt.Reference(key);
|
| 34 | }
|
| 35 |
|
| 36 | static <T> int getFromItem(
|
| 37 | final ItemStack itemStack,
|
| 38 | final DataComponentType<T> componentType,
|
| 39 | final Function<T, ResolvableInt> getter,
|
| 40 | final LootContext context,
|
| 41 | final int defaultValue
|
| 42 | ) {
|
| 43 | T component = itemStack.get(componentType);
|
| 44 | return component != null ? getter.apply(component).get(context, defaultValue) : defaultValue;
|
| 45 | }
|
| 46 |
|
| 47 | record Constant(int value) implements ResolvableInt {
|
| 48 | private static final Codec<ResolvableInt.Constant> CODEC = Codec.INT.xmap(ResolvableInt.Constant::new, ResolvableInt.Constant::value);
|
| 49 | private static final StreamCodec<ByteBuf, ResolvableInt.Constant> STREAM_CODEC = ByteBufCodecs.INT
|
| 50 | .map(ResolvableInt.Constant::new, ResolvableInt.Constant::value);
|
| 51 |
|
| 52 | @Override
|
| 53 | public int get(final LootContext context, final int defaultValue) {
|
| 54 | return this.value;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | record Reference(ResourceKey<ContextIntProvider> key) implements ResolvableInt {
|
| 59 | private static final Codec<ResolvableInt.Reference> CODEC = ResourceKey.codec(Registries.CONTEXT_INT_PROVIDER)
|
| 60 | .xmap(ResolvableInt.Reference::new, ResolvableInt.Reference::key);
|
| 61 | private static final StreamCodec<ByteBuf, ResolvableInt.Reference> STREAM_CODEC = ResourceKey.streamCodec(Registries.CONTEXT_INT_PROVIDER)
|
| 62 | .map(ResolvableInt.Reference::new, ResolvableInt.Reference::key);
|
| 63 |
|
| 64 | @Override
|
| 65 | public int get(final LootContext context, final int defaultValue) {
|
| 66 | return this.getProvider(context).map(provider -> provider.getInt(context)).orElse(defaultValue);
|
| 67 | }
|
| 68 |
|
| 69 | private Optional<ContextIntProvider> getProvider(final LootContext context) {
|
| 70 | return context.getResolver().lookupOrThrow(Registries.CONTEXT_INT_PROVIDER).get(this.key).map(Holder.Reference::value);
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|