| 1 | package net.minecraft.world.level.levelgen.structure.templatesystem;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 6 | import it.unimi.dsi.fastutil.ints.IntIterator;
|
| 7 | import java.util.List;
|
| 8 | import java.util.stream.IntStream;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.util.Util;
|
| 12 | import net.minecraft.util.valueproviders.IntProvider;
|
| 13 | import net.minecraft.util.valueproviders.IntProviders;
|
| 14 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 15 |
|
| 16 | public class CappedProcessor implements StructureProcessor {
|
| 17 | public static final MapCodec<CappedProcessor> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 18 | i -> i.group(
|
| 19 | StructureProcessorType.SINGLE_CODEC.fieldOf("delegate").forGetter(c -> c.delegate), IntProviders.POSITIVE_CODEC.fieldOf("limit").forGetter(c -> c.limit)
|
| 20 | )
|
| 21 | .apply(i, CappedProcessor::new)
|
| 22 | );
|
| 23 | private final StructureProcessor delegate;
|
| 24 | private final IntProvider limit;
|
| 25 |
|
| 26 | public CappedProcessor(final StructureProcessor delegate, final IntProvider limit) {
|
| 27 | this.delegate = delegate;
|
| 28 | this.limit = limit;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public MapCodec<CappedProcessor> codec() {
|
| 33 | return MAP_CODEC;
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public boolean evaluatesEntirePieceState() {
|
| 38 | return true;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public final List<StructureTemplate.StructureBlockInfo> finalizeProcessing(
|
| 43 | final ServerLevelAccessor level,
|
| 44 | final BlockPos position,
|
| 45 | final BlockPos referencePos,
|
| 46 | final List<StructureTemplate.StructureBlockInfo> originalBlockInfoList,
|
| 47 | final List<StructureTemplate.StructureBlockInfo> processedBlockInfoList,
|
| 48 | final StructurePlaceSettings settings
|
| 49 | ) {
|
| 50 | if (this.limit.maxInclusive() != 0 && !processedBlockInfoList.isEmpty()) {
|
| 51 | if (originalBlockInfoList.size() != processedBlockInfoList.size()) {
|
| 52 | Util.logAndPauseIfInIde(
|
| 53 | "Original block info list not in sync with processed list, skipping processing. Original size: "
|
| 54 | + originalBlockInfoList.size()
|
| 55 | + ", Processed size: "
|
| 56 | + processedBlockInfoList.size()
|
| 57 | );
|
| 58 | return processedBlockInfoList;
|
| 59 | }
|
| 60 |
|
| 61 | RandomSource random = RandomSource.createThreadLocalInstance(level.getLevel().getSeed()).forkPositional().at(position);
|
| 62 | int maxToReplace = Math.min(this.limit.sample(random), processedBlockInfoList.size());
|
| 63 | if (maxToReplace < 1) {
|
| 64 | return processedBlockInfoList;
|
| 65 | }
|
| 66 |
|
| 67 | IntArrayList indices = Util.toShuffledList(IntStream.range(0, processedBlockInfoList.size()), random);
|
| 68 | IntIterator indexIterator = indices.intIterator();
|
| 69 | int replaced = 0;
|
| 70 |
|
| 71 | while (indexIterator.hasNext() && replaced < maxToReplace) {
|
| 72 | int index = indexIterator.nextInt();
|
| 73 | StructureTemplate.StructureBlockInfo originalBlockInfo = originalBlockInfoList.get(index);
|
| 74 | StructureTemplate.StructureBlockInfo processedBlockInfo = processedBlockInfoList.get(index);
|
| 75 | StructureTemplate.StructureBlockInfo maybeAltered = this.delegate
|
| 76 | .processBlock(level, position, referencePos, originalBlockInfo.pos(), processedBlockInfo, settings);
|
| 77 | if (maybeAltered != null && !processedBlockInfo.equals(maybeAltered)) {
|
| 78 | replaced++;
|
| 79 | processedBlockInfoList.set(index, maybeAltered);
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | return processedBlockInfoList;
|
| 84 | } else {
|
| 85 | return processedBlockInfoList;
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|