| 1 | package net.minecraft.util.valueproviders;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.MapCodec;
|
| 7 | import net.minecraft.core.Registry;
|
| 8 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 9 |
|
| 10 | public class IntProviders {
|
| 11 | private static final Codec<Either<Integer, IntProvider>> CONSTANT_OR_DISPATCH_CODEC = Codec.either(
|
| 12 | Codec.INT, BuiltInRegistries.INT_PROVIDER_TYPE.byNameCodec().dispatch(IntProvider::codec, t -> t)
|
| 13 | );
|
| 14 | public static final Codec<IntProvider> CODEC = CONSTANT_OR_DISPATCH_CODEC.xmap(
|
| 15 | either -> either.map(ConstantInt::of, f -> (IntProvider)f), f -> f instanceof ConstantInt constantInt ? Either.left(constantInt.value()) : Either.right(f)
|
| 16 | );
|
| 17 | public static final Codec<IntProvider> NON_NEGATIVE_CODEC = codec(0, Integer.MAX_VALUE);
|
| 18 | public static final Codec<IntProvider> POSITIVE_CODEC = codec(1, Integer.MAX_VALUE);
|
| 19 |
|
| 20 | public static Codec<IntProvider> codec(final int minValue, final int maxValue) {
|
| 21 | return validateCodec(minValue, maxValue, CODEC);
|
| 22 | }
|
| 23 |
|
| 24 | public static <T extends IntProvider> Codec<T> validateCodec(final int minValue, final int maxValue, final Codec<T> codec) {
|
| 25 | return codec.validate(value -> validate(minValue, maxValue, value));
|
| 26 | }
|
| 27 |
|
| 28 | private static <T extends IntProvider> DataResult<T> validate(final int minValue, final int maxValue, final T value) {
|
| 29 | if (value.minInclusive() < minValue) {
|
| 30 | return DataResult.error(() -> "Value provider too low: " + minValue + " [" + value.minInclusive() + "-" + value.maxInclusive() + "]");
|
| 31 | } else {
|
| 32 | return value.maxInclusive() > maxValue
|
| 33 | ? DataResult.error(() -> "Value provider too high: " + maxValue + " [" + value.minInclusive() + "-" + value.maxInclusive() + "]")
|
| 34 | : DataResult.success(value);
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | public static MapCodec<? extends IntProvider> bootstrap(final Registry<MapCodec<? extends IntProvider>> registry) {
|
| 39 | Registry.register(registry, "constant", ConstantInt.MAP_CODEC);
|
| 40 | Registry.register(registry, "uniform", UniformInt.MAP_CODEC);
|
| 41 | Registry.register(registry, "biased_to_bottom", BiasedToBottomInt.MAP_CODEC);
|
| 42 | Registry.register(registry, "very_biased_to_bottom", VeryBiasedToBottomInt.MAP_CODEC);
|
| 43 | Registry.register(registry, "clamped", ClampedInt.MAP_CODEC);
|
| 44 | Registry.register(registry, "weighted_list", WeightedListInt.MAP_CODEC);
|
| 45 | Registry.register(registry, "clamped_normal", ClampedNormalInt.MAP_CODEC);
|
| 46 | return Registry.register(registry, "trapezoid", TrapezoidInt.MAP_CODEC);
|
| 47 | }
|
| 48 | }
|
| 49 |
|