| 1 | package net.minecraft.world.level.levelgen.structure.templatesystem;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.HolderSet;
|
| 9 | import net.minecraft.core.registries.Registries;
|
| 10 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.world.level.LevelReader;
|
| 13 | import net.minecraft.world.level.block.Block;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public class BlockRotProcessor implements StructureProcessor {
|
| 17 | public static final MapCodec<BlockRotProcessor> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 18 | i -> i.group(
|
| 19 | RegistryCodecs.holderSet(Registries.BLOCK).optionalFieldOf("rottable_blocks").forGetter(t -> t.rottableBlocks),
|
| 20 | Codec.floatRange(0.0F, 1.0F).fieldOf("integrity").forGetter(t -> t.integrity)
|
| 21 | )
|
| 22 | .apply(i, BlockRotProcessor::new)
|
| 23 | );
|
| 24 | private final Optional<HolderSet<Block>> rottableBlocks;
|
| 25 | private final float integrity;
|
| 26 |
|
| 27 | public BlockRotProcessor(final HolderSet<Block> tag, final float integrity) {
|
| 28 | this(Optional.of(tag), integrity);
|
| 29 | }
|
| 30 |
|
| 31 | public BlockRotProcessor(final float integrity) {
|
| 32 | this(Optional.empty(), integrity);
|
| 33 | }
|
| 34 |
|
| 35 | private BlockRotProcessor(final Optional<HolderSet<Block>> blockTagKey, final float integrity) {
|
| 36 | this.integrity = integrity;
|
| 37 | this.rottableBlocks = blockTagKey;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public StructureTemplate.@Nullable StructureBlockInfo processBlock(
|
| 42 | final LevelReader level,
|
| 43 | final BlockPos targetPosition,
|
| 44 | final BlockPos referencePos,
|
| 45 | final BlockPos templateRelativePos,
|
| 46 | final StructureTemplate.StructureBlockInfo processedBlockInfo,
|
| 47 | final StructurePlaceSettings settings
|
| 48 | ) {
|
| 49 | RandomSource random = settings.getRandom(processedBlockInfo.pos());
|
| 50 | return (!this.rottableBlocks.isPresent() || processedBlockInfo.state().is(this.rottableBlocks.get())) && !(random.nextFloat() <= this.integrity)
|
| 51 | ? null
|
| 52 | : processedBlockInfo;
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public MapCodec<BlockRotProcessor> codec() {
|
| 57 | return MAP_CODEC;
|
| 58 | }
|
| 59 | }
|
| 60 |
|