TemplateStructurePiece .java

net.minecraft.world.level.levelgen.structure.TemplateStructurePiece (class): members: makeTemplateLocation, placeSettings, templateLocation, templateName.

net.minecraft.world.level.levelgen.structureclass146 linescommonextends StructurePiece5.8 KB

API Surface (8)

Identifier makeTemplateLocation()
void addAdditionalSaveData(final StructurePieceSerializationContext context, final CompoundTag tag)
void postProcess(final WorldGenLevel level, final StructureManager structureManager, final ChunkGenerator generator, final RandomSource random, final BoundingBox chunkBB, final ChunkPos chunkPos, final BlockPos referencePos)
void move(final int dx, final int dy, final int dz)
Rotation getRotation()
StructureTemplate template()
BlockPos templatePosition()
StructurePlaceSettings placeSettings()

Fields & Constants (16)

LOGGER
templateName
template
placeSettings
templatePosition
type
genDepth
structureTemplateManager
templateLocation
tag
level
structureManager
generator
random
chunkBB
chunkPos

String literals (5)

"Template"
"mode"
"metadata"
"final_state"
"minecraft:air"

Full Source

TemplateStructurePiece.java146 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.structure;
2
3
import com.mojang.brigadier.exceptions.CommandSyntaxException;
4
import com.mojang.logging.LogUtils;
5
import java.util.function.Function;
6
import net.minecraft.commands.arguments.blocks.BlockStateParser;
7
import net.minecraft.core.BlockPos;
8
import net.minecraft.core.Direction;
9
import net.minecraft.core.registries.Registries;
10
import net.minecraft.nbt.CompoundTag;
11
import net.minecraft.resources.Identifier;
12
import net.minecraft.util.RandomSource;
13
import net.minecraft.world.level.ChunkPos;
14
import net.minecraft.world.level.ServerLevelAccessor;
15
import net.minecraft.world.level.StructureManager;
16
import net.minecraft.world.level.WorldGenLevel;
17
import net.minecraft.world.level.block.Blocks;
18
import net.minecraft.world.level.block.Rotation;
19
import net.minecraft.world.level.block.state.BlockState;
20
import net.minecraft.world.level.block.state.properties.StructureMode;
21
import net.minecraft.world.level.chunk.ChunkGenerator;
22
import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceSerializationContext;
23
import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceType;
24
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
25
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
26
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
27
import org.slf4j.Logger;
28
29
public abstract class TemplateStructurePiece extends StructurePiece {
30
	private static final Logger LOGGER = LogUtils.getLogger();
31
	protected final String templateName;
32
	protected final StructureTemplate template;
33
	protected final StructurePlaceSettings placeSettings;
34
	protected BlockPos templatePosition;
35
36
	public TemplateStructurePiece(
37
		final StructurePieceType type,
38
		final int genDepth,
39
		final StructureTemplateManager structureTemplateManager,
40
		final Identifier templateLocation,
41
		final String templateName,
42
		final StructurePlaceSettings placeSettings,
43
		final BlockPos position
44
	) {
45
		super(type, genDepth, structureTemplateManager.getOrCreate(templateLocation).getBoundingBox(placeSettings, position));
46
		this.setOrientation(Direction.NORTH);
47
		this.templateName = templateName;
48
		this.templatePosition = position;
49
		this.template = structureTemplateManager.getOrCreate(templateLocation);
50
		this.placeSettings = placeSettings;
51
	}
52
53
	public TemplateStructurePiece(
54
		final StructurePieceType type,
55
		final CompoundTag tag,
56
		final StructureTemplateManager structureTemplateManager,
57
		final Function<Identifier, StructurePlaceSettings> structurePlaceSettingsSupplier
58
	) {
59
		super(type, tag);
60
		this.setOrientation(Direction.NORTH);
61
		this.templateName = tag.getStringOr("Template", "");
62
		this.templatePosition = new BlockPos(tag.getIntOr("TPX", 0), tag.getIntOr("TPY", 0), tag.getIntOr("TPZ", 0));
63
		Identifier templateLocation = this.makeTemplateLocation();
64
		this.template = structureTemplateManager.getOrCreate(templateLocation);
65
		this.placeSettings = structurePlaceSettingsSupplier.apply(templateLocation);
66
		this.boundingBox = this.template.getBoundingBox(this.placeSettings, this.templatePosition);
67
	}
68
69
	protected Identifier makeTemplateLocation() {
70
		return Identifier.parse(this.templateName);
71
	}
72
73
	@Override
74
	protected void addAdditionalSaveData(final StructurePieceSerializationContext context, final CompoundTag tag) {
75
		tag.putInt("TPX", this.templatePosition.getX());
76
		tag.putInt("TPY", this.templatePosition.getY());
77
		tag.putInt("TPZ", this.templatePosition.getZ());
78
		tag.putString("Template", this.templateName);
79
	}
80
81
	@Override
82
	public void postProcess(
83
		final WorldGenLevel level,
84
		final StructureManager structureManager,
85
		final ChunkGenerator generator,
86
		final RandomSource random,
87
		final BoundingBox chunkBB,
88
		final ChunkPos chunkPos,
89
		final BlockPos referencePos
90
	) {
91
		this.placeSettings.setBoundingBox(chunkBB);
92
		this.boundingBox = this.template.getBoundingBox(this.placeSettings, this.templatePosition);
93
		if (this.template.placeInWorld(level, this.templatePosition, referencePos, this.placeSettings, random, 2)) {
94
			for (StructureTemplate.StructureBlockInfo dataMarker : this.template.filterBlocks(this.templatePosition, this.placeSettings, Blocks.STRUCTURE_BLOCK)) {
95
				if (dataMarker.nbt() != null) {
96
					StructureMode mode = dataMarker.nbt().read("mode", StructureMode.LEGACY_CODEC).orElseThrow();
97
					if (mode == StructureMode.DATA) {
98
						this.handleDataMarker(dataMarker.nbt().getStringOr("metadata", ""), dataMarker.pos(), level, random, chunkBB);
99
					}
100
				}
101
			}
102
103
			for (StructureTemplate.StructureBlockInfo jigsawBlock : this.template.filterBlocks(this.templatePosition, this.placeSettings, Blocks.JIGSAW)) {
104
				if (jigsawBlock.nbt() != null) {
105
					String stateString = jigsawBlock.nbt().getStringOr("final_state", "minecraft:air");
106
					BlockState targetState = Blocks.AIR.defaultBlockState();
107
108
					try {
109
						targetState = BlockStateParser.parseForBlock(level.holderLookup(Registries.BLOCK), stateString, true).blockState();
110
					} catch (CommandSyntaxException e) {
111
						LOGGER.error("Error while parsing blockstate {} in jigsaw block @ {}", stateString, jigsawBlock.pos());
112
					}
113
114
					level.setBlockAndUpdate(jigsawBlock.pos(), targetState);
115
				}
116
			}
117
		}
118
	}
119
120
	protected abstract void handleDataMarker(String markerId, BlockPos position, ServerLevelAccessor level, RandomSource random, BoundingBox chunkBB);
121
122
	@Deprecated
123
	@Override
124
	public void move(final int dx, final int dy, final int dz) {
125
		super.move(dx, dy, dz);
126
		this.templatePosition = this.templatePosition.offset(dx, dy, dz);
127
	}
128
129
	@Override
130
	public Rotation getRotation() {
131
		return this.placeSettings.getRotation();
132
	}
133
134
	public StructureTemplate template() {
135
		return this.template;
136
	}
137
138
	public BlockPos templatePosition() {
139
		return this.templatePosition;
140
	}
141
142
	public StructurePlaceSettings placeSettings() {
143
		return this.placeSettings;
144
	}
145
}
146

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