| 1 | package net.minecraft.world.level.levelgen.structure.structures;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import java.util.Map;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.nbt.CompoundTag;
|
| 7 | import net.minecraft.resources.Identifier;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.level.ChunkPos;
|
| 10 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 11 | import net.minecraft.world.level.StructureManager;
|
| 12 | import net.minecraft.world.level.WorldGenLevel;
|
| 13 | import net.minecraft.world.level.block.Blocks;
|
| 14 | import net.minecraft.world.level.block.Mirror;
|
| 15 | import net.minecraft.world.level.block.Rotation;
|
| 16 | import net.minecraft.world.level.block.entity.ChestBlockEntity;
|
| 17 | import net.minecraft.world.level.block.state.BlockState;
|
| 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.LiquidSettings;
|
| 27 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
|
| 28 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 29 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 30 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 31 |
|
| 32 | public class IglooPieces {
|
| 33 | public static final int GENERATION_HEIGHT = 90;
|
| 34 | private static final Identifier STRUCTURE_LOCATION_IGLOO = Identifier.withDefaultNamespace("igloo/top");
|
| 35 | private static final Identifier STRUCTURE_LOCATION_LADDER = Identifier.withDefaultNamespace("igloo/middle");
|
| 36 | private static final Identifier STRUCTURE_LOCATION_LABORATORY = Identifier.withDefaultNamespace("igloo/bottom");
|
| 37 | private static final Map<Identifier, BlockPos> PIVOTS = ImmutableMap.of(
|
| 38 | STRUCTURE_LOCATION_IGLOO, new BlockPos(3, 5, 5), STRUCTURE_LOCATION_LADDER, new BlockPos(1, 3, 1), STRUCTURE_LOCATION_LABORATORY, new BlockPos(3, 6, 7)
|
| 39 | );
|
| 40 | private static final Map<Identifier, BlockPos> OFFSETS = ImmutableMap.of(
|
| 41 | STRUCTURE_LOCATION_IGLOO, BlockPos.ZERO, STRUCTURE_LOCATION_LADDER, new BlockPos(2, -3, 4), STRUCTURE_LOCATION_LABORATORY, new BlockPos(0, -3, -2)
|
| 42 | );
|
| 43 |
|
| 44 | public static void addPieces(
|
| 45 | final StructureTemplateManager structureTemplateManager,
|
| 46 | final BlockPos position,
|
| 47 | final Rotation rotation,
|
| 48 | final StructurePiecesBuilder builder,
|
| 49 | final RandomSource random
|
| 50 | ) {
|
| 51 | if (random.nextDouble() < 0.5) {
|
| 52 | int depth = random.nextInt(8) + 4;
|
| 53 | builder.addPiece(new IglooPieces.IglooPiece(structureTemplateManager, STRUCTURE_LOCATION_LABORATORY, position, rotation, depth * 3));
|
| 54 |
|
| 55 | for (int i = 0; i < depth - 1; i++) {
|
| 56 | builder.addPiece(new IglooPieces.IglooPiece(structureTemplateManager, STRUCTURE_LOCATION_LADDER, position, rotation, i * 3));
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | builder.addPiece(new IglooPieces.IglooPiece(structureTemplateManager, STRUCTURE_LOCATION_IGLOO, position, rotation, 0));
|
| 61 | }
|
| 62 |
|
| 63 | public static class IglooPiece extends TemplateStructurePiece {
|
| 64 | public IglooPiece(
|
| 65 | final StructureTemplateManager structureTemplateManager,
|
| 66 | final Identifier templateLocation,
|
| 67 | final BlockPos position,
|
| 68 | final Rotation rotation,
|
| 69 | final int depth
|
| 70 | ) {
|
| 71 | super(
|
| 72 | StructurePieceType.IGLOO,
|
| 73 | 0,
|
| 74 | structureTemplateManager,
|
| 75 | templateLocation,
|
| 76 | templateLocation.toString(),
|
| 77 | makeSettings(rotation, templateLocation),
|
| 78 | makePosition(templateLocation, position, depth)
|
| 79 | );
|
| 80 | }
|
| 81 |
|
| 82 | public IglooPiece(final StructureTemplateManager structureTemplateManager, final CompoundTag tag) {
|
| 83 | super(StructurePieceType.IGLOO, tag, structureTemplateManager, location -> makeSettings(tag.read("Rot", Rotation.LEGACY_CODEC).orElseThrow(), location));
|
| 84 | }
|
| 85 |
|
| 86 | private static StructurePlaceSettings makeSettings(final Rotation rotation, final Identifier templateLocation) {
|
| 87 | return new StructurePlaceSettings()
|
| 88 | .setRotation(rotation)
|
| 89 | .setMirror(Mirror.NONE)
|
| 90 | .setRotationPivot(IglooPieces.PIVOTS.get(templateLocation))
|
| 91 | .addProcessor(BlockIgnoreProcessor.STRUCTURE_BLOCK)
|
| 92 | .setLiquidSettings(LiquidSettings.IGNORE_WATERLOGGING);
|
| 93 | }
|
| 94 |
|
| 95 | private static BlockPos makePosition(final Identifier templateLocation, final BlockPos position, final int depth) {
|
| 96 | return position.offset(IglooPieces.OFFSETS.get(templateLocation)).below(depth);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | protected void addAdditionalSaveData(final StructurePieceSerializationContext context, final CompoundTag tag) {
|
| 101 | super.addAdditionalSaveData(context, tag);
|
| 102 | tag.store("Rot", Rotation.LEGACY_CODEC, this.placeSettings.getRotation());
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | protected void handleDataMarker(
|
| 107 | final String markerId, final BlockPos position, final ServerLevelAccessor level, final RandomSource random, final BoundingBox chunkBB
|
| 108 | ) {
|
| 109 | if ("chest".equals(markerId)) {
|
| 110 | level.setBlockAndUpdate(position, Blocks.AIR.defaultBlockState());
|
| 111 | if (level.getBlockEntity(position.below()) instanceof ChestBlockEntity chestBlockEntity) {
|
| 112 | chestBlockEntity.setLootTable(BuiltInLootTables.IGLOO_CHEST, random.nextLong());
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public void postProcess(
|
| 119 | final WorldGenLevel level,
|
| 120 | final StructureManager structureManager,
|
| 121 | final ChunkGenerator generator,
|
| 122 | final RandomSource random,
|
| 123 | final BoundingBox chunkBB,
|
| 124 | final ChunkPos chunkPos,
|
| 125 | final BlockPos referencePos
|
| 126 | ) {
|
| 127 | Identifier templateLocation = Identifier.parse(this.templateName);
|
| 128 | StructurePlaceSettings settings = makeSettings(this.placeSettings.getRotation(), templateLocation);
|
| 129 | BlockPos offset = IglooPieces.OFFSETS.get(templateLocation);
|
| 130 | BlockPos entrancePos = this.templatePosition
|
| 131 | .offset(StructureTemplate.calculateRelativePosition(settings, new BlockPos(3 - offset.getX(), 0, -offset.getZ())));
|
| 132 | int height = level.getHeight(Heightmap.Types.WORLD_SURFACE_WG, entrancePos.getX(), entrancePos.getZ());
|
| 133 | BlockPos oldTemplatePos = this.templatePosition;
|
| 134 | this.templatePosition = this.templatePosition.offset(0, height - 90 - 1, 0);
|
| 135 | super.postProcess(level, structureManager, generator, random, chunkBB, chunkPos, referencePos);
|
| 136 | if (templateLocation.equals(IglooPieces.STRUCTURE_LOCATION_IGLOO)) {
|
| 137 | BlockPos trapDoorPos = this.templatePosition.offset(StructureTemplate.calculateRelativePosition(settings, new BlockPos(3, 0, 5)));
|
| 138 | BlockState belowState = level.getBlockState(trapDoorPos.below());
|
| 139 | if (!belowState.isAir() && !belowState.is(Blocks.LADDER)) {
|
| 140 | level.setBlockAndUpdate(trapDoorPos, Blocks.SNOW_BLOCK.defaultBlockState());
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | this.templatePosition = oldTemplatePos;
|
| 145 | }
|
| 146 | }
|
| 147 | }
|
| 148 |
|