2025-11-24 22:52:51 +03:00

68 lines
2.4 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.mojang.datafixers.kinds.App
* com.mojang.datafixers.kinds.Applicative
* com.mojang.serialization.Codec
* com.mojang.serialization.DataResult
* com.mojang.serialization.MapCodec
* com.mojang.serialization.codecs.RecordCodecBuilder
*/
package net.minecraft.util.valueproviders;
import com.mojang.datafixers.kinds.App;
import com.mojang.datafixers.kinds.Applicative;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.util.valueproviders.IntProvider;
import net.minecraft.util.valueproviders.IntProviderType;
public class ClampedInt
extends IntProvider {
public static final MapCodec<ClampedInt> CODEC = RecordCodecBuilder.mapCodec(i -> i.group((App)IntProvider.CODEC.fieldOf("source").forGetter(u -> u.source), (App)Codec.INT.fieldOf("min_inclusive").forGetter(u -> u.minInclusive), (App)Codec.INT.fieldOf("max_inclusive").forGetter(u -> u.maxInclusive)).apply((Applicative)i, ClampedInt::new)).validate(u -> {
if (u.maxInclusive < u.minInclusive) {
return DataResult.error(() -> "Max must be at least min, min_inclusive: " + u.minInclusive + ", max_inclusive: " + u.maxInclusive);
}
return DataResult.success((Object)u);
});
private final IntProvider source;
private final int minInclusive;
private final int maxInclusive;
public static ClampedInt of(IntProvider source, int minInclusive, int maxInclusive) {
return new ClampedInt(source, minInclusive, maxInclusive);
}
public ClampedInt(IntProvider source, int minInclusive, int maxInclusive) {
this.source = source;
this.minInclusive = minInclusive;
this.maxInclusive = maxInclusive;
}
@Override
public int sample(RandomSource random) {
return Mth.clamp(this.source.sample(random), this.minInclusive, this.maxInclusive);
}
@Override
public int getMinValue() {
return Math.max(this.minInclusive, this.source.getMinValue());
}
@Override
public int getMaxValue() {
return Math.min(this.maxInclusive, this.source.getMaxValue());
}
@Override
public IntProviderType<?> getType() {
return IntProviderType.CLAMPED;
}
}