ShipwreckPieces .java

net.minecraft.world.level.levelgen.structure.structures.ShipwreckPieces (class, 186 lines): members: NUMBER_OF_BLOCKS_ALLOWED_IN_WORLD_GEN_REGION, PIVOT.

net.minecraft.world.level.levelgen.structure.structuresclass186 linescommon8.7 KB

API Surface (7)

ShipwreckPieces.ShipwreckPiece addRandomPiece(final StructureTemplateManager structureTemplateManager, final BlockPos position, final Rotation rotation, final StructurePiecesBuilder builder, final RandomSource random, final boolean isBeached)
void addAdditionalSaveData(final StructurePieceSerializationContext context, final CompoundTag tag)
void handleDataMarker(final String markerId, final BlockPos position, final ServerLevelAccessor level, final RandomSource random, final BoundingBox chunkBB)
void postProcess(final WorldGenLevel level, final StructureManager structureManager, final ChunkGenerator generator, final RandomSource random, final BoundingBox chunkBB, final ChunkPos chunkPos, final BlockPos referencePos)
boolean isTooBigToFitInWorldGenRegion()
int calculateBeachedPosition(final int minY, final RandomSource random)
void adjustPositionHeight(final int newHeight)

Fields & Constants (10)

NUMBER_OF_BLOCKS_ALLOWED_IN_WORLD_GEN_REGION
PIVOT
STRUCTURE_LOCATION_BEACHED
STRUCTURE_LOCATION_OCEAN
MARKERS_TO_LOOT
structureTemplateManager
position
rotation
builder
random

String literals (25)

"shipwreck/with_mast"
"shipwreck/sideways_full"
"shipwreck/sideways_fronthalf"
"shipwreck/sideways_backhalf"
"shipwreck/rightsideup_full"
"shipwreck/rightsideup_fronthalf"
"shipwreck/rightsideup_backhalf"
"shipwreck/with_mast_degraded"
"shipwreck/rightsideup_full_degraded"
"shipwreck/rightsideup_fronthalf_degraded"
"shipwreck/rightsideup_backhalf_degraded"
"shipwreck/upsidedown_full"
"shipwreck/upsidedown_fronthalf"
"shipwreck/upsidedown_backhalf"
"shipwreck/upsidedown_full_degraded"
"shipwreck/upsidedown_fronthalf_degraded"
"shipwreck/upsidedown_backhalf_degraded"
"shipwreck/sideways_full_degraded"
"shipwreck/sideways_fronthalf_degraded"
"shipwreck/sideways_backhalf_degraded"
"map_chest"
"treasure_chest"
"supply_chest"
"isBeached"

Full Source

ShipwreckPieces.java186 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.structure.structures;
2
3
import java.util.Map;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Vec3i;
6
import net.minecraft.nbt.CompoundTag;
7
import net.minecraft.resources.Identifier;
8
import net.minecraft.resources.ResourceKey;
9
import net.minecraft.util.RandomSource;
10
import net.minecraft.util.Util;
11
import net.minecraft.world.RandomizableContainer;
12
import net.minecraft.world.level.ChunkPos;
13
import net.minecraft.world.level.ServerLevelAccessor;
14
import net.minecraft.world.level.StructureManager;
15
import net.minecraft.world.level.WorldGenLevel;
16
import net.minecraft.world.level.block.Mirror;
17
import net.minecraft.world.level.block.Rotation;
18
import net.minecraft.world.level.chunk.ChunkGenerator;
19
import net.minecraft.world.level.levelgen.Heightmap;
20
import net.minecraft.world.level.levelgen.structure.BoundingBox;
21
import net.minecraft.world.level.levelgen.structure.TemplateStructurePiece;
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.pieces.StructurePiecesBuilder;
25
import net.minecraft.world.level.levelgen.structure.templatesystem.BlockIgnoreProcessor;
26
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
27
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
28
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
29
import net.minecraft.world.level.storage.loot.LootTable;
30
31
public class ShipwreckPieces {
32
	private static final int NUMBER_OF_BLOCKS_ALLOWED_IN_WORLD_GEN_REGION = 32;
33
	private static final BlockPos PIVOT = new BlockPos(4, 0, 15);
34
	private static final Identifier[] STRUCTURE_LOCATION_BEACHED = new Identifier[]{
35
		Identifier.withDefaultNamespace("shipwreck/with_mast"),
36
		Identifier.withDefaultNamespace("shipwreck/sideways_full"),
37
		Identifier.withDefaultNamespace("shipwreck/sideways_fronthalf"),
38
		Identifier.withDefaultNamespace("shipwreck/sideways_backhalf"),
39
		Identifier.withDefaultNamespace("shipwreck/rightsideup_full"),
40
		Identifier.withDefaultNamespace("shipwreck/rightsideup_fronthalf"),
41
		Identifier.withDefaultNamespace("shipwreck/rightsideup_backhalf"),
42
		Identifier.withDefaultNamespace("shipwreck/with_mast_degraded"),
43
		Identifier.withDefaultNamespace("shipwreck/rightsideup_full_degraded"),
44
		Identifier.withDefaultNamespace("shipwreck/rightsideup_fronthalf_degraded"),
45
		Identifier.withDefaultNamespace("shipwreck/rightsideup_backhalf_degraded")
46
	};
47
	private static final Identifier[] STRUCTURE_LOCATION_OCEAN = new Identifier[]{
48
		Identifier.withDefaultNamespace("shipwreck/with_mast"),
49
		Identifier.withDefaultNamespace("shipwreck/upsidedown_full"),
50
		Identifier.withDefaultNamespace("shipwreck/upsidedown_fronthalf"),
51
		Identifier.withDefaultNamespace("shipwreck/upsidedown_backhalf"),
52
		Identifier.withDefaultNamespace("shipwreck/sideways_full"),
53
		Identifier.withDefaultNamespace("shipwreck/sideways_fronthalf"),
54
		Identifier.withDefaultNamespace("shipwreck/sideways_backhalf"),
55
		Identifier.withDefaultNamespace("shipwreck/rightsideup_full"),
56
		Identifier.withDefaultNamespace("shipwreck/rightsideup_fronthalf"),
57
		Identifier.withDefaultNamespace("shipwreck/rightsideup_backhalf"),
58
		Identifier.withDefaultNamespace("shipwreck/with_mast_degraded"),
59
		Identifier.withDefaultNamespace("shipwreck/upsidedown_full_degraded"),
60
		Identifier.withDefaultNamespace("shipwreck/upsidedown_fronthalf_degraded"),
61
		Identifier.withDefaultNamespace("shipwreck/upsidedown_backhalf_degraded"),
62
		Identifier.withDefaultNamespace("shipwreck/sideways_full_degraded"),
63
		Identifier.withDefaultNamespace("shipwreck/sideways_fronthalf_degraded"),
64
		Identifier.withDefaultNamespace("shipwreck/sideways_backhalf_degraded"),
65
		Identifier.withDefaultNamespace("shipwreck/rightsideup_full_degraded"),
66
		Identifier.withDefaultNamespace("shipwreck/rightsideup_fronthalf_degraded"),
67
		Identifier.withDefaultNamespace("shipwreck/rightsideup_backhalf_degraded")
68
	};
69
	private static final Map<String, ResourceKey<LootTable>> MARKERS_TO_LOOT = Map.of(
70
		"map_chest", BuiltInLootTables.SHIPWRECK_MAP, "treasure_chest", BuiltInLootTables.SHIPWRECK_TREASURE, "supply_chest", BuiltInLootTables.SHIPWRECK_SUPPLY
71
	);
72
73
	public static ShipwreckPieces.ShipwreckPiece addRandomPiece(
74
		final StructureTemplateManager structureTemplateManager,
75
		final BlockPos position,
76
		final Rotation rotation,
77
		final StructurePiecesBuilder builder,
78
		final RandomSource random,
79
		final boolean isBeached
80
	) {
81
		Identifier identifier = Util.getRandom(isBeached ? STRUCTURE_LOCATION_BEACHED : STRUCTURE_LOCATION_OCEAN, random);
82
		ShipwreckPieces.ShipwreckPiece piece = new ShipwreckPieces.ShipwreckPiece(structureTemplateManager, identifier, position, rotation, isBeached);
83
		builder.addPiece(piece);
84
		return piece;
85
	}
86
87
	public static class ShipwreckPiece extends TemplateStructurePiece {
88
		private final boolean isBeached;
89
		private boolean heightAdjusted;
90
91
		public ShipwreckPiece(
92
			final StructureTemplateManager structureTemplateManager,
93
			final Identifier templateLocation,
94
			final BlockPos position,
95
			final Rotation rotation,
96
			final boolean isBeached
97
		) {
98
			super(StructurePieceType.SHIPWRECK_PIECE, 0, structureTemplateManager, templateLocation, templateLocation.toString(), makeSettings(rotation), position);
99
			this.isBeached = isBeached;
100
		}
101
102
		public ShipwreckPiece(final StructureTemplateManager structureTemplateManager, final CompoundTag tag) {
103
			super(StructurePieceType.SHIPWRECK_PIECE, tag, structureTemplateManager, location -> makeSettings(tag.read("Rot", Rotation.LEGACY_CODEC).orElseThrow()));
104
			this.isBeached = tag.getBooleanOr("isBeached", false);
105
			this.heightAdjusted = tag.getBooleanOr("height_adjusted", false);
106
		}
107
108
		@Override
109
		protected void addAdditionalSaveData(final StructurePieceSerializationContext context, final CompoundTag tag) {
110
			super.addAdditionalSaveData(context, tag);
111
			tag.putBoolean("isBeached", this.isBeached);
112
			tag.store("Rot", Rotation.LEGACY_CODEC, this.placeSettings.getRotation());
113
			tag.putBoolean("height_adjusted", this.heightAdjusted);
114
		}
115
116
		private static StructurePlaceSettings makeSettings(final Rotation rotation) {
117
			return new StructurePlaceSettings()
118
				.setRotation(rotation)
119
				.setMirror(Mirror.NONE)
120
				.setRotationPivot(ShipwreckPieces.PIVOT)
121
				.addProcessor(BlockIgnoreProcessor.STRUCTURE_AND_AIR);
122
		}
123
124
		@Override
125
		protected void handleDataMarker(
126
			final String markerId, final BlockPos position, final ServerLevelAccessor level, final RandomSource random, final BoundingBox chunkBB
127
		) {
128
			ResourceKey<LootTable> lootTable = ShipwreckPieces.MARKERS_TO_LOOT.get(markerId);
129
			if (lootTable != null) {
130
				RandomizableContainer.setBlockEntityLootTable(level, random, position.below(), lootTable);
131
			}
132
		}
133
134
		@Override
135
		public void postProcess(
136
			final WorldGenLevel level,
137
			final StructureManager structureManager,
138
			final ChunkGenerator generator,
139
			final RandomSource random,
140
			final BoundingBox chunkBB,
141
			final ChunkPos chunkPos,
142
			final BlockPos referencePos
143
		) {
144
			if (!this.heightAdjusted && !this.isTooBigToFitInWorldGenRegion()) {
145
				int minY = level.getMaxY() + 1;
146
				int mean = 0;
147
				Vec3i templateSize = this.template.getSize();
148
				Heightmap.Types heightmapType = this.isBeached ? Heightmap.Types.WORLD_SURFACE_WG : Heightmap.Types.OCEAN_FLOOR_WG;
149
				int baseSize = templateSize.getX() * templateSize.getZ();
150
				if (baseSize == 0) {
151
					mean = level.getHeight(heightmapType, this.templatePosition.getX(), this.templatePosition.getZ());
152
				} else {
153
					BlockPos corner = this.templatePosition.offset(templateSize.getX() - 1, 0, templateSize.getZ() - 1);
154
155
					for (BlockPos p : BlockPos.betweenClosed(this.templatePosition, corner)) {
156
						int heightmap = level.getHeight(heightmapType, p.getX(), p.getZ());
157
						mean += heightmap;
158
						minY = Math.min(minY, heightmap);
159
					}
160
161
					mean /= baseSize;
162
				}
163
164
				this.adjustPositionHeight(this.isBeached ? this.calculateBeachedPosition(minY, random) : mean);
165
				super.postProcess(level, structureManager, generator, random, chunkBB, chunkPos, referencePos);
166
			} else {
167
				super.postProcess(level, structureManager, generator, random, chunkBB, chunkPos, referencePos);
168
			}
169
		}
170
171
		public boolean isTooBigToFitInWorldGenRegion() {
172
			Vec3i size = this.template.getSize();
173
			return size.getX() > 32 || size.getY() > 32;
174
		}
175
176
		public int calculateBeachedPosition(final int minY, final RandomSource random) {
177
			return minY - this.template.getSize().getY() / 2 - random.nextInt(3);
178
		}
179
180
		public void adjustPositionHeight(final int newHeight) {
181
			this.heightAdjusted = true;
182
			this.templatePosition = new BlockPos(this.templatePosition.getX(), newHeight, this.templatePosition.getZ());
183
		}
184
	}
185
}
186

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