StructureManager .java

net.minecraft.world.level.StructureManager (class, 201 lines): members: getStructureWithPieceAt, fillStartsForStructure, structureHasPieceAt, addReference.

net.minecraft.world.levelclass201 linescommon8.0 KB

API Surface (20)

StructureManager forWorldGenRegion(final WorldGenRegion region)
List<StructureStart> startsForStructure(final int sectionX, final int sectionZ, final Predicate<Structure> matcher)
List<StructureStart> startsForStructure(final int sectionX, final int sectionZ, final Structure structure)
void fillStartsForStructure(final Structure structure, final LongSet referencesForStructure, final Consumer<StructureStart> consumer)
void setStartForStructure(final Structure structure, final StructureStart start, final StructureAccess chunk)
void addReferenceForStructure(final Structure structure, final long reference, final StructureAccess chunk)
boolean shouldGenerateStructures()
StructureStart getStructureAt(final BlockPos blockPos, final Structure structure)
StructureStart getStructureAt(final BlockPos blockPos, final HolderSet<Structure> structures)
StructureStart getStructureWithPieceAt(final BlockPos blockPos, final TagKey<Structure> structureTag)
StructureStart getStructureWithPieceAt(final BlockPos blockPos, final HolderSet<Structure> structures)
StructureStart getStructureWithPieceAt(final BlockPos blockPos, final Predicate<Holder<Structure>> predicate)
StructureStart getStructureWithPieceAt(final int x, final int y, final int z, final Structure structure)
boolean structureHasPieceAt(final BlockPos blockPos, final StructureStart structureStart)
boolean structureHasPieceAt(final int x, final int y, final int z, final StructureStart structureStart)
Map<Structure, LongSet> getAllStructuresAt(final BlockPos pos)
Map<Structure, LongSet> getAllStructuresAt(final int x, final int y, final int z)
StructureCheckResult checkStructurePresence(final ChunkPos pos, final Structure structure, final StructurePlacement placement, final boolean createReference)
void addReference(final StructureStart start)
RegistryAccess registryAccess()

Fields & Constants (4)

level
worldOptions
structureCheck
placement

String literals (2)

"Using invalid structure manager (source level: "
", region: "

Full Source

StructureManager.java201 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level;
2
3
import com.google.common.collect.ImmutableList;
4
import com.google.common.collect.ImmutableList.Builder;
5
import it.unimi.dsi.fastutil.longs.LongSet;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Map.Entry;
9
import java.util.function.Consumer;
10
import java.util.function.Predicate;
11
import net.minecraft.core.BlockPos;
12
import net.minecraft.core.Holder;
13
import net.minecraft.core.HolderSet;
14
import net.minecraft.core.Registry;
15
import net.minecraft.core.RegistryAccess;
16
import net.minecraft.core.SectionPos;
17
import net.minecraft.core.registries.Registries;
18
import net.minecraft.server.level.WorldGenRegion;
19
import net.minecraft.tags.TagKey;
20
import net.minecraft.world.level.chunk.StructureAccess;
21
import net.minecraft.world.level.chunk.status.ChunkStatus;
22
import net.minecraft.world.level.levelgen.WorldOptions;
23
import net.minecraft.world.level.levelgen.structure.Structure;
24
import net.minecraft.world.level.levelgen.structure.StructureCheck;
25
import net.minecraft.world.level.levelgen.structure.StructureCheckResult;
26
import net.minecraft.world.level.levelgen.structure.StructurePiece;
27
import net.minecraft.world.level.levelgen.structure.StructureStart;
28
import net.minecraft.world.level.levelgen.structure.placement.StructurePlacement;
29
import org.jspecify.annotations.Nullable;
30
31
public class StructureManager {
32
	private final LevelAccessor level;
33
	private final WorldOptions worldOptions;
34
	private final StructureCheck structureCheck;
35
36
	public StructureManager(final LevelAccessor level, final WorldOptions worldOptions, final StructureCheck structureCheck) {
37
		this.level = level;
38
		this.worldOptions = worldOptions;
39
		this.structureCheck = structureCheck;
40
	}
41
42
	public StructureManager forWorldGenRegion(final WorldGenRegion region) {
43
		if (region.getLevel() != this.level) {
44
			throw new IllegalStateException("Using invalid structure manager (source level: " + region.getLevel() + ", region: " + region);
45
		} else {
46
			return new StructureManager(region, this.worldOptions, this.structureCheck);
47
		}
48
	}
49
50
	public List<StructureStart> startsForStructure(final int sectionX, final int sectionZ, final Predicate<Structure> matcher) {
51
		Map<Structure, LongSet> allReferences = this.level.getChunk(sectionX, sectionZ, ChunkStatus.STRUCTURE_REFERENCES).getAllReferences();
52
		Builder<StructureStart> result = ImmutableList.builder();
53
54
		for (Entry<Structure, LongSet> entry : allReferences.entrySet()) {
55
			Structure structure = entry.getKey();
56
			if (matcher.test(structure)) {
57
				this.fillStartsForStructure(structure, entry.getValue(), result::add);
58
			}
59
		}
60
61
		return result.build();
62
	}
63
64
	public List<StructureStart> startsForStructure(final int sectionX, final int sectionZ, final Structure structure) {
65
		LongSet referencesForStructure = this.level.getChunk(sectionX, sectionZ, ChunkStatus.STRUCTURE_REFERENCES).getReferencesForStructure(structure);
66
		Builder<StructureStart> result = ImmutableList.builder();
67
		this.fillStartsForStructure(structure, referencesForStructure, result::add);
68
		return result.build();
69
	}
70
71
	public void fillStartsForStructure(final Structure structure, final LongSet referencesForStructure, final Consumer<StructureStart> consumer) {
72
		for (long key : referencesForStructure) {
73
			ChunkPos chunkPos = ChunkPos.unpack(key);
74
			StructureStart start = this.getStartForStructure(structure, this.level.getChunk(chunkPos.x(), chunkPos.z(), ChunkStatus.STRUCTURE_STARTS));
75
			if (start != null && start.isValid()) {
76
				consumer.accept(start);
77
			}
78
		}
79
	}
80
81
	public @Nullable StructureStart getStartForStructure(final Structure structure, final StructureAccess chunk) {
82
		return chunk.getStartForStructure(structure);
83
	}
84
85
	public void setStartForStructure(final Structure structure, final StructureStart start, final StructureAccess chunk) {
86
		chunk.setStartForStructure(structure, start);
87
	}
88
89
	public void addReferenceForStructure(final Structure structure, final long reference, final StructureAccess chunk) {
90
		chunk.addReferenceForStructure(structure, reference);
91
	}
92
93
	public boolean shouldGenerateStructures() {
94
		return this.worldOptions.generateStructures();
95
	}
96
97
	public StructureStart getStructureAt(final BlockPos blockPos, final Structure structure) {
98
		int sectionX = SectionPos.blockToSectionCoord(blockPos.getX());
99
		int sectionZ = SectionPos.blockToSectionCoord(blockPos.getZ());
100
101
		for (StructureStart structureStart : this.startsForStructure(sectionX, sectionZ, structure)) {
102
			if (structureStart.getBoundingBox().isInside(blockPos)) {
103
				return structureStart;
104
			}
105
		}
106
107
		return StructureStart.INVALID_START;
108
	}
109
110
	public StructureStart getStructureAt(final BlockPos blockPos, final HolderSet<Structure> structures) {
111
		int sectionX = SectionPos.blockToSectionCoord(blockPos.getX());
112
		int sectionZ = SectionPos.blockToSectionCoord(blockPos.getZ());
113
114
		for (StructureStart structureStart : this.startsForStructure(sectionX, sectionZ, this.matching(structures::contains))) {
115
			if (structureStart.getBoundingBox().isInside(blockPos)) {
116
				return structureStart;
117
			}
118
		}
119
120
		return StructureStart.INVALID_START;
121
	}
122
123
	public StructureStart getStructureWithPieceAt(final BlockPos blockPos, final TagKey<Structure> structureTag) {
124
		return this.getStructureWithPieceAt(blockPos, structure -> structure.is(structureTag));
125
	}
126
127
	public StructureStart getStructureWithPieceAt(final BlockPos blockPos, final HolderSet<Structure> structures) {
128
		return this.getStructureWithPieceAt(blockPos, structures::contains);
129
	}
130
131
	public StructureStart getStructureWithPieceAt(final BlockPos blockPos, final Predicate<Holder<Structure>> predicate) {
132
		int sectionX = SectionPos.blockToSectionCoord(blockPos.getX());
133
		int sectionZ = SectionPos.blockToSectionCoord(blockPos.getZ());
134
135
		for (StructureStart structureStart : this.startsForStructure(sectionX, sectionZ, this.matching(predicate))) {
136
			if (this.structureHasPieceAt(blockPos, structureStart)) {
137
				return structureStart;
138
			}
139
		}
140
141
		return StructureStart.INVALID_START;
142
	}
143
144
	private Predicate<Structure> matching(final Predicate<Holder<Structure>> predicate) {
145
		Registry<Structure> structures = this.registryAccess().lookupOrThrow(Registries.STRUCTURE);
146
		return structure -> structures.get(structures.getId(structure)).map(predicate::test).orElse(false);
147
	}
148
149
	public StructureStart getStructureWithPieceAt(final int x, final int y, final int z, final Structure structure) {
150
		int sectionX = SectionPos.blockToSectionCoord(x);
151
		int sectionZ = SectionPos.blockToSectionCoord(z);
152
153
		for (StructureStart structureStart : this.startsForStructure(sectionX, sectionZ, structure)) {
154
			if (this.structureHasPieceAt(x, y, z, structureStart)) {
155
				return structureStart;
156
			}
157
		}
158
159
		return StructureStart.INVALID_START;
160
	}
161
162
	public boolean structureHasPieceAt(final BlockPos blockPos, final StructureStart structureStart) {
163
		return this.structureHasPieceAt(blockPos.getX(), blockPos.getY(), blockPos.getZ(), structureStart);
164
	}
165
166
	public boolean structureHasPieceAt(final int x, final int y, final int z, final StructureStart structureStart) {
167
		for (StructurePiece piece : structureStart.getPieces()) {
168
			if (piece.getBoundingBox().isInside(x, y, z)) {
169
				return true;
170
			}
171
		}
172
173
		return false;
174
	}
175
176
	public Map<Structure, LongSet> getAllStructuresAt(final BlockPos pos) {
177
		return this.getAllStructuresAt(pos.getX(), pos.getY(), pos.getZ());
178
	}
179
180
	public Map<Structure, LongSet> getAllStructuresAt(final int x, final int y, final int z) {
181
		int sectionX = SectionPos.blockToSectionCoord(x);
182
		int sectionZ = SectionPos.blockToSectionCoord(z);
183
		return this.level.getChunk(sectionX, sectionZ, ChunkStatus.STRUCTURE_REFERENCES).getAllReferences();
184
	}
185
186
	public StructureCheckResult checkStructurePresence(
187
		final ChunkPos pos, final Structure structure, final StructurePlacement placement, final boolean createReference
188
	) {
189
		return this.structureCheck.checkStart(pos, structure, placement, createReference);
190
	}
191
192
	public void addReference(final StructureStart start) {
193
		start.addReference();
194
		this.structureCheck.incrementReference(start.getChunkPos(), start.getStructure());
195
	}
196
197
	public RegistryAccess registryAccess() {
198
		return this.level.registryAccess();
199
	}
200
}
201

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