| 1 | package net.minecraft.world.level.levelgen.placement;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 4 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.SharedConstants;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.level.WorldGenLevel;
|
| 12 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 13 | import net.minecraft.world.level.levelgen.feature.Feature;
|
| 14 | import net.minecraft.world.level.levelgen.feature.FeatureCountTracker;
|
| 15 |
|
| 16 | public class FeaturePlacer {
|
| 17 | private final WorldGenLevel level;
|
| 18 | private final ChunkGenerator generator;
|
| 19 | private final List<BlockPos> positions = new ArrayList<>();
|
| 20 | private final IntList modifierIndices = new IntArrayList();
|
| 21 | private final List<BlockPos> modifiedPositions = new ArrayList<>();
|
| 22 |
|
| 23 | public FeaturePlacer(final WorldGenLevel level, final ChunkGenerator generator) {
|
| 24 | this.level = level;
|
| 25 | this.generator = generator;
|
| 26 | }
|
| 27 |
|
| 28 | public boolean place(final PlacedFeature placedFeature, final RandomSource random, final BlockPos origin) {
|
| 29 | return this.place(placedFeature, random, origin, false);
|
| 30 | }
|
| 31 |
|
| 32 | public boolean placeWithBiomeCheck(final PlacedFeature placedFeature, final RandomSource random, final BlockPos origin) {
|
| 33 | return this.place(placedFeature, random, origin, true);
|
| 34 | }
|
| 35 |
|
| 36 | private boolean place(final PlacedFeature placedFeature, final RandomSource random, final BlockPos origin, final boolean biomeCheck) {
|
| 37 | Optional<PlacedFeature> topFeature = biomeCheck ? Optional.of(placedFeature) : Optional.empty();
|
| 38 | PlacementContext context = new PlacementContext(this.level, this.generator, topFeature);
|
| 39 | Feature feature = placedFeature.feature().value();
|
| 40 | List<PlacementModifier> placement = placedFeature.placement();
|
| 41 | if (placement.isEmpty()) {
|
| 42 | if (SharedConstants.DEBUG_FEATURE_COUNT) {
|
| 43 | FeatureCountTracker.featurePlaced(this.level.getLevel(), feature, topFeature);
|
| 44 | }
|
| 45 |
|
| 46 | return feature.place(this.level, this.generator, random, origin);
|
| 47 | } else {
|
| 48 | boolean placedAny = false;
|
| 49 | this.positions.add(origin);
|
| 50 | this.modifierIndices.add(0);
|
| 51 |
|
| 52 | for (; !this.positions.isEmpty(); this.modifiedPositions.clear()) {
|
| 53 | BlockPos pos = this.positions.removeLast();
|
| 54 | int modifierIndex = this.modifierIndices.removeInt(this.modifierIndices.size() - 1);
|
| 55 | PlacementModifier modifier = placement.get(modifierIndex);
|
| 56 | modifier.modify(context, random, pos, this.modifiedPositions::add);
|
| 57 | int nextModifierIndex = modifierIndex + 1;
|
| 58 | if (nextModifierIndex < placement.size()) {
|
| 59 | for (int i = this.modifiedPositions.size() - 1; i >= 0; i--) {
|
| 60 | this.positions.add(this.modifiedPositions.get(i));
|
| 61 | this.modifierIndices.add(nextModifierIndex);
|
| 62 | }
|
| 63 | } else {
|
| 64 | for (BlockPos nextPos : this.modifiedPositions) {
|
| 65 | placedAny |= feature.place(this.level, this.generator, random, nextPos);
|
| 66 | if (SharedConstants.DEBUG_FEATURE_COUNT) {
|
| 67 | FeatureCountTracker.featurePlaced(this.level.getLevel(), feature, topFeature);
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | return placedAny;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|