Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
1package net.lerariemann.infinity.features;
2
3import com.mojang.serialization.Codec;
4import com.mojang.serialization.codecs.RecordCodecBuilder;
5import net.lerariemann.infinity.InfinityMod;
6import net.lerariemann.infinity.util.VersionMethods;
7import net.lerariemann.infinity.util.core.ConfigType;
8import net.minecraft.core.BlockPos;
9import net.minecraft.core.Direction;
10import net.minecraft.core.registries.Registries;
11import net.minecraft.resources.ResourceKey;
12import net.minecraft.resources.ResourceLocation;
13import net.minecraft.util.RandomSource;
14//? if >1.21 {
15import net.minecraft.world.RandomizableContainer;
16//?} else {
17/*import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity;
18*///?}
19import net.minecraft.world.level.WorldGenLevel;
20import net.minecraft.world.level.block.Blocks;
21import net.minecraft.world.level.block.state.BlockState;
22import net.minecraft.world.level.levelgen.feature.Feature;
23import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext;
24import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
25import java.util.Optional;
26
27public class RandomBonusChestFeature extends Feature<RandomBonusChestFeature.Config> {
28 public RandomBonusChestFeature(Codec<Config> codec) {
29 super(codec);
30 }
31
32 @Override
33 public boolean place(FeaturePlaceContext<Config> context) {
34 RandomSource random = context.random();
35 WorldGenLevel structureWorldAccess = context.level();
36
37 BlockPos blockPos = context.origin();
38 if (structureWorldAccess.isEmptyBlock(blockPos) || structureWorldAccess.getBlockState(blockPos).getCollisionShape(structureWorldAccess, blockPos).isEmpty()) {
39 BlockState state = context.config().block.orElseGet(Blocks.CHEST::defaultBlockState);
40 structureWorldAccess.setBlock(blockPos, state, 2);
41 ResourceLocation id = context.config().loot;
42 if (id.toString().equals("infinity:random")) id = VersionMethods.id(InfinityMod.provider.randomName(context.random(), ConfigType.LOOT_TABLES));
43 //? if >1.21 {
44 RandomizableContainer.setBlockEntityLootTable(structureWorldAccess, random, blockPos, ResourceKey.create(Registries.LOOT_TABLE, id));
45 //?} else {
46 /*RandomizableContainerBlockEntity.setLootTable(structureWorldAccess, random, blockPos, id);
47 *///?}
48 BlockState blockState = Blocks.TORCH.defaultBlockState();
49
50 for (Direction direction : Direction.Plane.HORIZONTAL) {
51 BlockPos blockPos2 = blockPos.relative(direction);
52 if (blockState.canSurvive(structureWorldAccess, blockPos2) && structureWorldAccess.getFluidState(blockPos2).isEmpty())
53 structureWorldAccess.setBlock(blockPos2, blockState, 2);
54 }
55 return true;
56 }
57 return false;
58 }
59
60 public record Config(ResourceLocation loot, Optional<BlockState> block) implements FeatureConfiguration {
61 public static final Codec<Config> CODEC = RecordCodecBuilder.create(instance -> instance.group(
62 (ResourceLocation.CODEC.fieldOf("loot")).forGetter(a -> a.loot),
63 (BlockState.CODEC.optionalFieldOf("block")).forGetter(a -> a.block)).apply(
64 instance, Config::new));
65 }
66}