| 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.Optional;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.level.WorldGenLevel;
|
| 10 | import net.minecraft.world.level.block.Blocks;
|
| 11 | import net.minecraft.world.level.block.entity.TheEndGatewayBlockEntity;
|
| 12 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 13 |
|
| 14 | public record EndGatewayFeature(Optional<BlockPos> exit, boolean exact) implements Feature {
|
| 15 | public static final MapCodec<EndGatewayFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 16 | i -> i.group(BlockPos.CODEC.optionalFieldOf("exit").forGetter(EndGatewayFeature::exit), Codec.BOOL.fieldOf("exact").forGetter(EndGatewayFeature::exact))
|
| 17 | .apply(i, EndGatewayFeature::new)
|
| 18 | );
|
| 19 |
|
| 20 | @Override
|
| 21 | public MapCodec<EndGatewayFeature> codec() {
|
| 22 | return CODEC;
|
| 23 | }
|
| 24 |
|
| 25 | public static EndGatewayFeature knownExit(final BlockPos exit, final boolean exact) {
|
| 26 | return new EndGatewayFeature(Optional.of(exit), exact);
|
| 27 | }
|
| 28 |
|
| 29 | public static EndGatewayFeature delayedExitSearch() {
|
| 30 | return new EndGatewayFeature(Optional.empty(), false);
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 35 | for (BlockPos pos : BlockPos.betweenClosed(origin.offset(-1, -2, -1), origin.offset(1, 2, 1))) {
|
| 36 | boolean sameX = pos.getX() == origin.getX();
|
| 37 | boolean sameY = pos.getY() == origin.getY();
|
| 38 | boolean sameZ = pos.getZ() == origin.getZ();
|
| 39 | boolean end = Math.abs(pos.getY() - origin.getY()) == 2;
|
| 40 | if (sameX && sameY && sameZ) {
|
| 41 | BlockPos immutable = pos.immutable();
|
| 42 | this.setBlock(level, immutable, Blocks.END_GATEWAY.defaultBlockState());
|
| 43 | this.exit.ifPresent(targetPos -> {
|
| 44 | if (level.getBlockEntity(immutable) instanceof TheEndGatewayBlockEntity exitGateway) {
|
| 45 | exitGateway.setExitPosition(targetPos, this.exact);
|
| 46 | }
|
| 47 | });
|
| 48 | } else if (sameY) {
|
| 49 | this.setBlock(level, pos, Blocks.AIR.defaultBlockState());
|
| 50 | } else if (end && sameX && sameZ) {
|
| 51 | this.setBlock(level, pos, Blocks.BEDROCK.defaultBlockState());
|
| 52 | } else if ((sameX || sameZ) && !end) {
|
| 53 | this.setBlock(level, pos, Blocks.BEDROCK.defaultBlockState());
|
| 54 | } else {
|
| 55 | this.setBlock(level, pos, Blocks.AIR.defaultBlockState());
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | return true;
|
| 60 | }
|
| 61 | }
|
| 62 |
|