| 1 | package net.minecraft.world.level.levelgen.structure;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.serialization.Dynamic;
|
| 5 | import com.mojang.serialization.DynamicOps;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Locale;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.nbt.CompoundTag;
|
| 10 | import net.minecraft.nbt.ListTag;
|
| 11 | import net.minecraft.nbt.NbtOps;
|
| 12 | import net.minecraft.nbt.Tag;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.world.level.ChunkPos;
|
| 15 | import net.minecraft.world.level.StructureManager;
|
| 16 | import net.minecraft.world.level.WorldGenLevel;
|
| 17 | import net.minecraft.world.level.block.Rotation;
|
| 18 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 19 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceSerializationContext;
|
| 20 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceType;
|
| 21 | import net.minecraft.world.level.levelgen.structure.pools.JigsawJunction;
|
| 22 | import net.minecraft.world.level.levelgen.structure.pools.StructurePoolElement;
|
| 23 | import net.minecraft.world.level.levelgen.structure.structures.JigsawStructure;
|
| 24 | import net.minecraft.world.level.levelgen.structure.templatesystem.LiquidSettings;
|
| 25 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 26 |
|
| 27 | public class PoolElementStructurePiece extends StructurePiece {
|
| 28 | protected final StructurePoolElement element;
|
| 29 | protected BlockPos position;
|
| 30 | private final int groundLevelDelta;
|
| 31 | protected final Rotation rotation;
|
| 32 | private final List<JigsawJunction> junctions = Lists.newArrayList();
|
| 33 | private final StructureTemplateManager structureTemplateManager;
|
| 34 | private final LiquidSettings liquidSettings;
|
| 35 |
|
| 36 | public PoolElementStructurePiece(
|
| 37 | final StructureTemplateManager structureTemplateManager,
|
| 38 | final StructurePoolElement element,
|
| 39 | final BlockPos position,
|
| 40 | final int groundLevelDelta,
|
| 41 | final Rotation rotation,
|
| 42 | final BoundingBox boundingBox,
|
| 43 | final LiquidSettings liquidSettings
|
| 44 | ) {
|
| 45 | super(StructurePieceType.JIGSAW, 0, boundingBox);
|
| 46 | this.structureTemplateManager = structureTemplateManager;
|
| 47 | this.element = element;
|
| 48 | this.position = position;
|
| 49 | this.groundLevelDelta = groundLevelDelta;
|
| 50 | this.rotation = rotation;
|
| 51 | this.liquidSettings = liquidSettings;
|
| 52 | }
|
| 53 |
|
| 54 | public PoolElementStructurePiece(final StructurePieceSerializationContext context, final CompoundTag tag) {
|
| 55 | super(StructurePieceType.JIGSAW, tag);
|
| 56 | this.structureTemplateManager = context.structureTemplateManager();
|
| 57 | this.position = new BlockPos(tag.getIntOr("PosX", 0), tag.getIntOr("PosY", 0), tag.getIntOr("PosZ", 0));
|
| 58 | this.groundLevelDelta = tag.getIntOr("ground_level_delta", 0);
|
| 59 | DynamicOps<Tag> ops = context.registryAccess().createSerializationContext(NbtOps.INSTANCE);
|
| 60 | this.element = tag.read("pool_element", StructurePoolElement.CODEC, ops).orElseThrow(() -> new IllegalStateException("Invalid pool element found"));
|
| 61 | this.rotation = tag.read("rotation", Rotation.LEGACY_CODEC).orElseThrow();
|
| 62 | this.boundingBox = this.element.getBoundingBox(this.structureTemplateManager, this.position, this.rotation);
|
| 63 | ListTag junctionsTag = tag.getListOrEmpty("junctions");
|
| 64 | this.junctions.clear();
|
| 65 | junctionsTag.forEach(junctionTag -> this.junctions.add(JigsawJunction.deserialize(new Dynamic<>(ops, junctionTag))));
|
| 66 | this.liquidSettings = tag.read("liquid_settings", LiquidSettings.CODEC).orElse(JigsawStructure.DEFAULT_LIQUID_SETTINGS);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected void addAdditionalSaveData(final StructurePieceSerializationContext context, final CompoundTag tag) {
|
| 71 | tag.putInt("PosX", this.position.getX());
|
| 72 | tag.putInt("PosY", this.position.getY());
|
| 73 | tag.putInt("PosZ", this.position.getZ());
|
| 74 | tag.putInt("ground_level_delta", this.groundLevelDelta);
|
| 75 | DynamicOps<Tag> ops = context.registryAccess().createSerializationContext(NbtOps.INSTANCE);
|
| 76 | tag.store("pool_element", StructurePoolElement.CODEC, ops, this.element);
|
| 77 | tag.store("rotation", Rotation.LEGACY_CODEC, this.rotation);
|
| 78 | ListTag junctionsTag = new ListTag();
|
| 79 |
|
| 80 | for (JigsawJunction junction : this.junctions) {
|
| 81 | junctionsTag.add(junction.serialize(ops).getValue());
|
| 82 | }
|
| 83 |
|
| 84 | tag.put("junctions", junctionsTag);
|
| 85 | if (this.liquidSettings != JigsawStructure.DEFAULT_LIQUID_SETTINGS) {
|
| 86 | tag.store("liquid_settings", LiquidSettings.CODEC, ops, this.liquidSettings);
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public void postProcess(
|
| 92 | final WorldGenLevel level,
|
| 93 | final StructureManager structureManager,
|
| 94 | final ChunkGenerator generator,
|
| 95 | final RandomSource random,
|
| 96 | final BoundingBox chunkBB,
|
| 97 | final ChunkPos chunkPos,
|
| 98 | final BlockPos referencePos
|
| 99 | ) {
|
| 100 | this.place(level, structureManager, generator, random, chunkBB, referencePos, false);
|
| 101 | }
|
| 102 |
|
| 103 | public void place(
|
| 104 | final WorldGenLevel level,
|
| 105 | final StructureManager structureManager,
|
| 106 | final ChunkGenerator generator,
|
| 107 | final RandomSource random,
|
| 108 | final BoundingBox chunkBB,
|
| 109 | final BlockPos referencePos,
|
| 110 | final boolean keepJigsaws
|
| 111 | ) {
|
| 112 | this.element
|
| 113 | .place(
|
| 114 | this.structureTemplateManager,
|
| 115 | level,
|
| 116 | structureManager,
|
| 117 | generator,
|
| 118 | this.position,
|
| 119 | referencePos,
|
| 120 | this.rotation,
|
| 121 | chunkBB,
|
| 122 | random,
|
| 123 | this.liquidSettings,
|
| 124 | keepJigsaws
|
| 125 | );
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public void move(final int dx, final int dy, final int dz) {
|
| 130 | super.move(dx, dy, dz);
|
| 131 | this.position = this.position.offset(dx, dy, dz);
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public Rotation getRotation() {
|
| 136 | return this.rotation;
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public String toString() {
|
| 141 | return String.format(Locale.ROOT, "<%s | %s | %s | %s>", this.getClass().getSimpleName(), this.position, this.rotation, this.element);
|
| 142 | }
|
| 143 |
|
| 144 | public StructurePoolElement getElement() {
|
| 145 | return this.element;
|
| 146 | }
|
| 147 |
|
| 148 | public BlockPos getPosition() {
|
| 149 | return this.position;
|
| 150 | }
|
| 151 |
|
| 152 | public int getGroundLevelDelta() {
|
| 153 | return this.groundLevelDelta;
|
| 154 | }
|
| 155 |
|
| 156 | public void addJunction(final JigsawJunction junction) {
|
| 157 | this.junctions.add(junction);
|
| 158 | }
|
| 159 |
|
| 160 | public List<JigsawJunction> getJunctions() {
|
| 161 | return this.junctions;
|
| 162 | }
|
| 163 | }
|
| 164 |
|