| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.util.valueproviders.IntProvider;
|
| 10 | import net.minecraft.util.valueproviders.IntProviders;
|
| 11 | import net.minecraft.world.level.levelgen.placement.PlacementContext;
|
| 12 | import net.minecraft.world.level.levelgen.placement.PlacementModifier;
|
| 13 |
|
| 14 | public record CuboidPlacement(IntProvider xzSize, IntProvider ySize, boolean includeEdges, boolean includeInterior) implements PlacementModifier {
|
| 15 | public static final MapCodec<CuboidPlacement> CODEC = RecordCodecBuilder.mapCodec(
|
| 16 | i -> i.group(
|
| 17 | IntProviders.codec(1, 16).fieldOf("xz_size").forGetter(CuboidPlacement::xzSize),
|
| 18 | IntProviders.codec(1, 16).fieldOf("y_size").forGetter(CuboidPlacement::ySize),
|
| 19 | Codec.BOOL.optionalFieldOf("include_edges", true).forGetter(CuboidPlacement::includeEdges),
|
| 20 | Codec.BOOL.optionalFieldOf("include_interior", true).forGetter(CuboidPlacement::includeInterior)
|
| 21 | )
|
| 22 | .apply(i, CuboidPlacement::new)
|
| 23 | );
|
| 24 |
|
| 25 | @Override
|
| 26 | public MapCodec<CuboidPlacement> codec() {
|
| 27 | return CODEC;
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public void modify(final PlacementContext context, final RandomSource random, final BlockPos origin, final Consumer<BlockPos> output) {
|
| 32 | int height = this.ySize.sample(random);
|
| 33 | int width = this.xzSize.sample(random);
|
| 34 | int length = this.xzSize.sample(random);
|
| 35 | BlockPos.MutableBlockPos mutPos = origin.mutable();
|
| 36 |
|
| 37 | for (int x = 0; x <= width; x++) {
|
| 38 | for (int y = 0; y <= height; y++) {
|
| 39 | for (int z = 0; z <= length; z++) {
|
| 40 | mutPos.set(x + origin.getX(), y + origin.getY(), z + origin.getZ());
|
| 41 | if ((this.includeEdges || x != 0 && x != width || y != 0 && y != height)
|
| 42 | && (this.includeEdges || z != 0 && z != length || y != 0 && y != height)
|
| 43 | && (this.includeEdges || x != 0 && x != width || z != 0 && z != length)
|
| 44 | && (this.includeInterior || x == 0 || x == width || y == 0 || y == height || z == 0 || z == length)) {
|
| 45 | output.accept(mutPos.immutable());
|
| 46 | }
|
| 47 | }
|
| 48 | }
|
| 49 | }
|
| 50 | }
|
| 51 | }
|
| 52 |
|