| 1 | package net.minecraft.world.level.storage.loot.providers.number.floats;
|
| 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 ResolvableFloat permits ResolvableFloat.Constant, ResolvableFloat.Reference {
|
| 18 | Codec<ResolvableFloat> CODEC = Codec.either(ResolvableFloat.Constant.CODEC, ResolvableFloat.Reference.CODEC).xmap(Either::unwrap, ResolvableFloat::wrap);
|
| 19 | StreamCodec<ByteBuf, ResolvableFloat> STREAM_CODEC = ByteBufCodecs.either(ResolvableFloat.Constant.STREAM_CODEC, ResolvableFloat.Reference.STREAM_CODEC)
|
| 20 | .map(Either::unwrap, ResolvableFloat::wrap);
|
| 21 |
|
| 22 | private static Either<ResolvableFloat.Constant, ResolvableFloat.Reference> wrap(final ResolvableFloat resolvableNumber) {
|
| 23 | return switch (resolvableNumber) {
|
| 24 | case ResolvableFloat.Constant constant -> Either.left(constant);
|
| 25 | case ResolvableFloat.Reference reference -> Either.right(reference);
|
| 26 | default -> throw new MatchException(null, null);
|
| 27 | };
|
| 28 | }
|
| 29 |
|
| 30 | float get(LootContext context, float defaultValue);
|
| 31 |
|
| 32 | static ResolvableFloat fromKey(final ResourceKey<ContextFloatProvider> key) {
|
| 33 | return new ResolvableFloat.Reference(key);
|
| 34 | }
|
| 35 |
|
| 36 | static <T> float getFromItem(
|
| 37 | final ItemStack itemStack,
|
| 38 | final DataComponentType<T> componentType,
|
| 39 | final Function<T, ResolvableFloat> getter,
|
| 40 | final LootContext context,
|
| 41 | final float defaultValue
|
| 42 | ) {
|
| 43 | T component = itemStack.get(componentType);
|
| 44 | return component != null ? getter.apply(component).get(context, defaultValue) : defaultValue;
|
| 45 | }
|
| 46 |
|
| 47 | record Constant(float value) implements ResolvableFloat {
|
| 48 | private static final Codec<ResolvableFloat.Constant> CODEC = Codec.FLOAT.xmap(ResolvableFloat.Constant::new, ResolvableFloat.Constant::value);
|
| 49 | private static final StreamCodec<ByteBuf, ResolvableFloat.Constant> STREAM_CODEC = ByteBufCodecs.FLOAT
|
| 50 | .map(ResolvableFloat.Constant::new, ResolvableFloat.Constant::value);
|
| 51 |
|
| 52 | @Override
|
| 53 | public float get(final LootContext context, final float defaultValue) {
|
| 54 | return this.value;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | record Reference(ResourceKey<ContextFloatProvider> key) implements ResolvableFloat {
|
| 59 | private static final Codec<ResolvableFloat.Reference> CODEC = ResourceKey.codec(Registries.CONTEXT_FLOAT_PROVIDER)
|
| 60 | .xmap(ResolvableFloat.Reference::new, ResolvableFloat.Reference::key);
|
| 61 | private static final StreamCodec<ByteBuf, ResolvableFloat.Reference> STREAM_CODEC = ResourceKey.streamCodec(Registries.CONTEXT_FLOAT_PROVIDER)
|
| 62 | .map(ResolvableFloat.Reference::new, ResolvableFloat.Reference::key);
|
| 63 |
|
| 64 | @Override
|
| 65 | public float get(final LootContext context, final float defaultValue) {
|
| 66 | return this.getProvider(context).map(provider -> provider.getFloat(context)).orElse(defaultValue);
|
| 67 | }
|
| 68 |
|
| 69 | private Optional<ContextFloatProvider> getProvider(final LootContext context) {
|
| 70 | return context.getResolver().lookupOrThrow(Registries.CONTEXT_FLOAT_PROVIDER).get(this.key).map(Holder.Reference::value);
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|