Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 63 lines 3.3 kB view raw
1package net.lerariemann.infinity.features; 2 3import com.mojang.serialization.Codec; 4import com.mojang.serialization.codecs.RecordCodecBuilder; 5import net.lerariemann.infinity.util.VersionMethods; 6import net.minecraft.core.BlockPos; 7import net.minecraft.util.ExtraCodecs; 8import net.minecraft.world.level.WorldGenLevel; 9import net.minecraft.world.level.block.Blocks; 10import net.minecraft.world.level.block.entity.TheEndGatewayBlockEntity; 11import net.minecraft.world.level.block.state.BlockState; 12import net.minecraft.world.level.levelgen.feature.Feature; 13import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext; 14import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration; 15import java.util.Optional; 16 17public class RandomEndGatewayFeature extends Feature<RandomEndGatewayFeature.Config> { 18 public RandomEndGatewayFeature(Codec<Config> codec) { 19 super(codec); 20 } 21 22 @Override 23 public boolean place(FeaturePlaceContext<Config> context) { 24 BlockPos origin = context.origin(); 25 WorldGenLevel structureWorldAccess = context.level(); 26 Config config = context.config(); 27 BlockState bedrock = config.block.orElse(Blocks.BEDROCK.defaultBlockState()); 28 for (BlockPos bp : BlockPos.betweenClosed(origin.offset(-1, -2, -1), origin.offset(1, 2, 1))) { 29 boolean bl = bp.getX() == origin.getX(); 30 boolean bl2 = bp.getY() == origin.getY(); 31 boolean bl3 = bp.getZ() == origin.getZ(); 32 boolean bl4 = Math.abs(bp.getY() - origin.getY()) == 2; 33 BlockState localFluid = structureWorldAccess.getFluidState(bp).createLegacyBlock().getBlock().defaultBlockState(); 34 if (bl && bl2 && bl3) { 35 BlockPos destination = new BlockPos(bp.getX() + context.random().nextIntBetweenInclusive(-config.spread, config.spread), 36 bp.getY(), bp.getZ() + context.random().nextIntBetweenInclusive(-config.spread, config.spread)); 37 destination = VersionMethods.clampToBounds(structureWorldAccess, destination); 38 this.setBlock(structureWorldAccess, bp, Blocks.END_GATEWAY.defaultBlockState()); 39 if (structureWorldAccess.getBlockEntity(bp) instanceof TheEndGatewayBlockEntity egbe) { 40 egbe.setExitPosition(destination, false); 41 } 42 } else if (bl2) { 43 this.setBlock(structureWorldAccess, bp, localFluid); 44 } else if (bl4 && bl && bl3) { 45 this.setBlock(structureWorldAccess, bp, bedrock); 46 } else if ((bl || bl3) && !bl4) { 47 this.setBlock(structureWorldAccess, bp, bedrock); 48 } else { 49 this.setBlock(structureWorldAccess, bp, localFluid); 50 } 51 } 52 53 return true; 54 } 55 56 public record Config(int spread, Optional<BlockState> block) implements FeatureConfiguration { 57 public static final Codec<Config> CODEC = RecordCodecBuilder.create(instance -> instance.group( 58 (ExtraCodecs.intRange(1, 30000000).fieldOf("spread")).forGetter(a -> a.spread), 59 (BlockState.CODEC.optionalFieldOf("block")).forGetter(a -> a.block)).apply( 60 instance, Config::new)); 61 } 62} 63