BonusChestFeature .java

net.minecraft.world.level.levelgen.feature.BonusChestFeature (record, 58 lines): members: place, codec, CODEC. imports: RandomizableContainer, Heightmap.

net.minecraft.world.level.levelgen.featurerecord58 linescommon2.3 KB

Implements

API Surface (2)

MapCodec<BonusChestFeature> codec()
boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin)

Fields & Constants (1)

CODEC

Full Source

BonusChestFeature.java58 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.feature;
2
3
import com.mojang.serialization.MapCodec;
4
import it.unimi.dsi.fastutil.ints.IntArrayList;
5
import java.util.stream.IntStream;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.util.RandomSource;
9
import net.minecraft.util.Util;
10
import net.minecraft.world.RandomizableContainer;
11
import net.minecraft.world.level.ChunkPos;
12
import net.minecraft.world.level.WorldGenLevel;
13
import net.minecraft.world.level.block.Blocks;
14
import net.minecraft.world.level.block.state.BlockState;
15
import net.minecraft.world.level.chunk.ChunkGenerator;
16
import net.minecraft.world.level.levelgen.Heightmap;
17
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
18
19
public record BonusChestFeature() implements Feature {
20
	public static final MapCodec<BonusChestFeature> CODEC = MapCodec.unit(BonusChestFeature::new);
21
22
	@Override
23
	public MapCodec<BonusChestFeature> codec() {
24
		return CODEC;
25
	}
26
27
	@Override
28
	public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
29
		ChunkPos chunkPos = ChunkPos.containing(origin);
30
		IntArrayList xPoses = Util.toShuffledList(IntStream.rangeClosed(chunkPos.getMinBlockX(), chunkPos.getMaxBlockX()), random);
31
		IntArrayList zPoses = Util.toShuffledList(IntStream.rangeClosed(chunkPos.getMinBlockZ(), chunkPos.getMaxBlockZ()), random);
32
		BlockPos.MutableBlockPos mutPos = new BlockPos.MutableBlockPos();
33
34
		for (Integer x : xPoses) {
35
			for (Integer z : zPoses) {
36
				mutPos.set(x, 0, z);
37
				BlockPos chestPos = level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, mutPos);
38
				if (level.isEmptyBlock(chestPos) || level.getBlockState(chestPos).getCollisionShape(level, chestPos).isEmpty()) {
39
					level.setBlock(chestPos, Blocks.CHEST.defaultBlockState(), 2);
40
					RandomizableContainer.setBlockEntityLootTable(level, random, chestPos, BuiltInLootTables.SPAWN_BONUS_CHEST);
41
					BlockState torch = Blocks.TORCH.defaultBlockState();
42
43
					for (Direction direction : Direction.Plane.HORIZONTAL) {
44
						BlockPos torchPos = chestPos.relative(direction);
45
						if (torch.canSurvive(level, torchPos)) {
46
							level.setBlock(torchPos, torch, 2);
47
						}
48
					}
49
50
					return true;
51
				}
52
			}
53
		}
54
55
		return false;
56
	}
57
}
58

Decompiled Minecraft 26.3 source of BonusChestFeature (net/minecraft/world/level/levelgen/feature/BonusChestFeature.java) with Mojang official mappings, stored verbatim on the site.