Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 48 lines 2.7 kB view raw
1package net.lerariemann.infinity.features; 2 3import com.mojang.serialization.Codec; 4import com.mojang.serialization.codecs.RecordCodecBuilder; 5import java.util.HashMap; 6import java.util.List; 7import java.util.Map; 8import net.minecraft.core.BlockPos; 9import net.minecraft.util.RandomSource; 10import net.minecraft.util.valueproviders.FloatProvider; 11import net.minecraft.world.level.WorldGenLevel; 12import net.minecraft.world.level.block.state.BlockState; 13import net.minecraft.world.level.levelgen.feature.Feature; 14import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext; 15import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration; 16import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider; 17 18public class RandomCubeFeature extends Feature<RandomCubeFeature.Config> { 19 public RandomCubeFeature(Codec<Config> codec) { 20 super(codec); 21 } 22 23 @Override 24 public boolean place(FeaturePlaceContext<Config> context) { 25 WorldGenLevel structureWorldAccess = context.level(); 26 RandomSource random = context.random(); 27 BlockPos blockPos = context.origin(); 28 Map<Integer, BlockState> blocks = new HashMap<>(); 29 int r = (int)context.config().radius().sample(random); 30 boolean bl = context.config().useBands(); 31 if (bl) for (int i = -3*r; i <= 3*r; i++) blocks.put(i, context.config().blockProvider().getState(random, blockPos.east(i))); 32 for (int i = -r; i <= r; i++) for (int j = -r; j <= r; j++) for (int k = -r; k <= r; k++) { 33 BlockPos blockPos1 = blockPos.offset(i, j, k); 34 if (structureWorldAccess.isEmptyBlock(blockPos1) || context.config().replaceable().contains(structureWorldAccess.getBlockState(blockPos1))) 35 this.setBlock(structureWorldAccess, blockPos1, bl ? blocks.get(i+j+k) : context.config().blockProvider().getState(random, blockPos1)); 36 } 37 return true; 38 } 39 40 public record Config(BlockStateProvider blockProvider, List<BlockState> replaceable, FloatProvider radius, boolean useBands) implements FeatureConfiguration { 41 public static final Codec<Config> CODEC = RecordCodecBuilder.create(instance -> instance.group( 42 (BlockStateProvider.CODEC.fieldOf("block_provider")).forGetter(a -> a.blockProvider), 43 (Codec.list(BlockState.CODEC).fieldOf("replaceable")).forGetter(a -> a.replaceable), 44 (FloatProvider.codec(2.0f, 20.0f).fieldOf("radius")).forGetter(a -> a.radius), 45 (Codec.BOOL.fieldOf("use_bands")).orElse(false).forGetter(a -> a.useBands)).apply( 46 instance, Config::new)); 47 } 48}