MonsterRoomFeature .java

net.minecraft.world.level.levelgen.feature.MonsterRoomFeature (record, 132 lines): record. members: randomEntityId, MOBS, AIR, place, LOGGER, codec, CODEC.

net.minecraft.world.level.levelgen.featurerecord132 linescommon4.8 KB

Implements

API Surface (2)

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

Fields & Constants (4)

LOGGER
MOBS
AIR
CODEC

Full Source

MonsterRoomFeature.java132 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.feature;
2
3
import com.mojang.logging.LogUtils;
4
import com.mojang.serialization.MapCodec;
5
import java.util.function.Predicate;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.tags.BlockTags;
9
import net.minecraft.util.RandomSource;
10
import net.minecraft.util.Util;
11
import net.minecraft.world.RandomizableContainer;
12
import net.minecraft.world.entity.EntityType;
13
import net.minecraft.world.entity.EntityTypes;
14
import net.minecraft.world.level.WorldGenLevel;
15
import net.minecraft.world.level.block.Blocks;
16
import net.minecraft.world.level.block.entity.SpawnerBlockEntity;
17
import net.minecraft.world.level.block.state.BlockState;
18
import net.minecraft.world.level.chunk.ChunkGenerator;
19
import net.minecraft.world.level.levelgen.structure.StructurePiece;
20
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
21
import org.slf4j.Logger;
22
23
public record MonsterRoomFeature() implements Feature {
24
	private static final Logger LOGGER = LogUtils.getLogger();
25
	private static final EntityType<?>[] MOBS = new EntityType[]{EntityTypes.SKELETON, EntityTypes.ZOMBIE, EntityTypes.ZOMBIE, EntityTypes.SPIDER};
26
	private static final BlockState AIR = Blocks.CAVE_AIR.defaultBlockState();
27
	public static final MapCodec<MonsterRoomFeature> CODEC = MapCodec.unit(MonsterRoomFeature::new);
28
29
	@Override
30
	public MapCodec<MonsterRoomFeature> codec() {
31
		return CODEC;
32
	}
33
34
	@Override
35
	public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
36
		Predicate<BlockState> replaceableTag = s -> !s.is(BlockTags.FEATURES_CANNOT_REPLACE);
37
		int hr = 3;
38
		int xr = random.nextInt(2) + 2;
39
		int minX = -xr - 1;
40
		int maxX = xr + 1;
41
		int minY = -1;
42
		int maxY = 4;
43
		int zr = random.nextInt(2) + 2;
44
		int minZ = -zr - 1;
45
		int maxZ = zr + 1;
46
		int holeCount = 0;
47
48
		for (int dx = minX; dx <= maxX; dx++) {
49
			for (int dy = -1; dy <= 4; dy++) {
50
				for (int dz = minZ; dz <= maxZ; dz++) {
51
					BlockPos holePos = origin.offset(dx, dy, dz);
52
					boolean solid = level.getBlockState(holePos).isSolid();
53
					if (dy == -1 && !solid) {
54
						return false;
55
					}
56
57
					if (dy == 4 && !solid) {
58
						return false;
59
					}
60
61
					if ((dx == minX || dx == maxX || dz == minZ || dz == maxZ) && dy == 0 && level.isEmptyBlock(holePos) && level.isEmptyBlock(holePos.above())) {
62
						holeCount++;
63
					}
64
				}
65
			}
66
		}
67
68
		if (holeCount >= 1 && holeCount <= 5) {
69
			for (int dx = minX; dx <= maxX; dx++) {
70
				for (int dy = 3; dy >= -1; dy--) {
71
					for (int dz = minZ; dz <= maxZ; dz++) {
72
						BlockPos wallBlock = origin.offset(dx, dy, dz);
73
						BlockState wallState = level.getBlockState(wallBlock);
74
						if (dx == minX || dy == -1 || dz == minZ || dx == maxX || dy == 4 || dz == maxZ) {
75
							if (wallBlock.getY() >= level.getMinY() && !level.getBlockState(wallBlock.below()).isSolid()) {
76
								level.setBlock(wallBlock, AIR, 2);
77
							} else if (wallState.isSolid() && !wallState.is(Blocks.CHEST)) {
78
								if (dy == -1 && random.nextInt(4) != 0) {
79
									this.safeSetBlock(level, wallBlock, Blocks.MOSSY_COBBLESTONE.defaultBlockState(), replaceableTag);
80
								} else {
81
									this.safeSetBlock(level, wallBlock, Blocks.COBBLESTONE.defaultBlockState(), replaceableTag);
82
								}
83
							}
84
						} else if (!wallState.is(Blocks.CHEST) && !wallState.is(Blocks.SPAWNER)) {
85
							this.safeSetBlock(level, wallBlock, AIR, replaceableTag);
86
						}
87
					}
88
				}
89
			}
90
91
			for (int cc = 0; cc < 2; cc++) {
92
				for (int i = 0; i < 3; i++) {
93
					int xc = origin.getX() + random.nextInt(xr * 2 + 1) - xr;
94
					int yc = origin.getY();
95
					int zc = origin.getZ() + random.nextInt(zr * 2 + 1) - zr;
96
					BlockPos chestPos = new BlockPos(xc, yc, zc);
97
					if (level.isEmptyBlock(chestPos)) {
98
						int wallCount = 0;
99
100
						for (Direction direction : Direction.Plane.HORIZONTAL) {
101
							if (level.getBlockState(chestPos.relative(direction)).isSolid()) {
102
								wallCount++;
103
							}
104
						}
105
106
						if (wallCount == 1) {
107
							this.safeSetBlock(level, chestPos, StructurePiece.reorient(level, chestPos, Blocks.CHEST.defaultBlockState()), replaceableTag);
108
							RandomizableContainer.setBlockEntityLootTable(level, random, chestPos, BuiltInLootTables.SIMPLE_DUNGEON);
109
							break;
110
						}
111
					}
112
				}
113
			}
114
115
			this.safeSetBlock(level, origin, Blocks.SPAWNER.defaultBlockState(), replaceableTag);
116
			if (level.getBlockEntity(origin) instanceof SpawnerBlockEntity spawner) {
117
				spawner.setEntityId(this.randomEntityId(random), random);
118
			} else {
119
				LOGGER.error("Failed to fetch mob spawner entity at ({}, {}, {})", origin.getX(), origin.getY(), origin.getZ());
120
			}
121
122
			return true;
123
		} else {
124
			return false;
125
		}
126
	}
127
128
	private EntityType<?> randomEntityId(final RandomSource random) {
129
		return Util.getRandom(MOBS, random);
130
	}
131
}
132

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