| 1 | package net.minecraft.world.level.levelgen.feature.stateproviders;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.util.valueproviders.IntProvider;
|
| 12 | import net.minecraft.util.valueproviders.IntProviders;
|
| 13 | import net.minecraft.world.level.LevelAccessor;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 16 | import net.minecraft.world.level.block.state.properties.Property;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public class RandomizedIntStateProvider implements BlockStateProvider {
|
| 20 | public static final MapCodec<RandomizedIntStateProvider> CODEC = RecordCodecBuilder.mapCodec(
|
| 21 | i -> i.group(
|
| 22 | BlockStateProvider.CODEC.fieldOf("source").forGetter(c -> c.source),
|
| 23 | Codec.STRING.fieldOf("property").forGetter(c -> c.propertyName),
|
| 24 | IntProviders.CODEC.fieldOf("values").forGetter(c -> c.values)
|
| 25 | )
|
| 26 | .apply(i, RandomizedIntStateProvider::new)
|
| 27 | );
|
| 28 | private final Holder<BlockStateProvider> source;
|
| 29 | private final String propertyName;
|
| 30 | private @Nullable IntegerProperty property;
|
| 31 | private final IntProvider values;
|
| 32 |
|
| 33 | public RandomizedIntStateProvider(final BlockStateProvider source, final IntegerProperty property, final IntProvider values) {
|
| 34 | this.source = Holder.direct(source);
|
| 35 | this.property = property;
|
| 36 | this.propertyName = property.getName();
|
| 37 | this.values = values;
|
| 38 | Collection<Integer> possibleValues = property.getPossibleValues();
|
| 39 |
|
| 40 | for (int i = values.minInclusive(); i <= values.maxInclusive(); i++) {
|
| 41 | if (!possibleValues.contains(i)) {
|
| 42 | throw new IllegalArgumentException("Property value out of range: " + property.getName() + ": " + i);
|
| 43 | }
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | public RandomizedIntStateProvider(final Holder<BlockStateProvider> source, final String propertyName, final IntProvider values) {
|
| 48 | this.source = source;
|
| 49 | this.propertyName = propertyName;
|
| 50 | this.values = values;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public MapCodec<RandomizedIntStateProvider> codec() {
|
| 55 | return CODEC;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public BlockState getState(final LevelAccessor level, final RandomSource random, final BlockPos pos) {
|
| 60 | BlockState unmodifiedState = this.source.value().getState(level, random, pos);
|
| 61 | if (this.property == null || !unmodifiedState.hasProperty(this.property)) {
|
| 62 | IntegerProperty property = findProperty(unmodifiedState, this.propertyName);
|
| 63 | if (property == null) {
|
| 64 | return unmodifiedState;
|
| 65 | }
|
| 66 |
|
| 67 | this.property = property;
|
| 68 | }
|
| 69 |
|
| 70 | return unmodifiedState.setValue(this.property, this.values.sample(random));
|
| 71 | }
|
| 72 |
|
| 73 | private static @Nullable IntegerProperty findProperty(final BlockState source, final String propertyName) {
|
| 74 | Collection<Property<?>> properties = source.getProperties();
|
| 75 | Optional<IntegerProperty> found = properties.stream()
|
| 76 | .filter(p -> p.getName().equals(propertyName))
|
| 77 | .filter(p -> p instanceof IntegerProperty)
|
| 78 | .map(p -> (IntegerProperty)p)
|
| 79 | .findAny();
|
| 80 | return found.orElse(null);
|
| 81 | }
|
| 82 | }
|
| 83 |
|