| 1 | package net.minecraft.world.level.levelgen.structure.pools;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import java.util.stream.Collectors;
|
| 8 | import java.util.stream.Stream;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Vec3i;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.world.level.StructureManager;
|
| 13 | import net.minecraft.world.level.WorldGenLevel;
|
| 14 | import net.minecraft.world.level.block.Rotation;
|
| 15 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 16 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 17 | import net.minecraft.world.level.levelgen.structure.templatesystem.LiquidSettings;
|
| 18 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 19 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 20 |
|
| 21 | public class ListPoolElement extends StructurePoolElement {
|
| 22 | public static final MapCodec<ListPoolElement> CODEC = RecordCodecBuilder.mapCodec(
|
| 23 | i -> i.group(StructurePoolElement.CODEC.listOf().fieldOf("elements").forGetter(e -> e.elements), projectionCodec()).apply(i, ListPoolElement::new)
|
| 24 | );
|
| 25 | private final List<StructurePoolElement> elements;
|
| 26 |
|
| 27 | public ListPoolElement(final List<StructurePoolElement> elements, final StructureTemplatePool.Projection projection) {
|
| 28 | super(projection);
|
| 29 | if (elements.isEmpty()) {
|
| 30 | throw new IllegalArgumentException("Elements are empty");
|
| 31 | }
|
| 32 |
|
| 33 | this.elements = elements;
|
| 34 | this.setProjectionOnEachElement(projection);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public Vec3i getSize(final StructureTemplateManager structureTemplateManager, final Rotation rotation) {
|
| 39 | int sizeX = 0;
|
| 40 | int sizeY = 0;
|
| 41 | int sizeZ = 0;
|
| 42 |
|
| 43 | for (StructurePoolElement element : this.elements) {
|
| 44 | Vec3i size = element.getSize(structureTemplateManager, rotation);
|
| 45 | sizeX = Math.max(sizeX, size.getX());
|
| 46 | sizeY = Math.max(sizeY, size.getY());
|
| 47 | sizeZ = Math.max(sizeZ, size.getZ());
|
| 48 | }
|
| 49 |
|
| 50 | return new Vec3i(sizeX, sizeY, sizeZ);
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public List<StructureTemplate.JigsawBlockInfo> getShuffledJigsawBlocks(
|
| 55 | final StructureTemplateManager structureTemplateManager, final BlockPos position, final Rotation rotation, final RandomSource random
|
| 56 | ) {
|
| 57 | return this.elements.get(0).getShuffledJigsawBlocks(structureTemplateManager, position, rotation, random);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public BoundingBox getBoundingBox(final StructureTemplateManager structureTemplateManager, final BlockPos position, final Rotation rotation) {
|
| 62 | Stream<BoundingBox> stream = this.elements
|
| 63 | .stream()
|
| 64 | .filter(e -> e != EmptyPoolElement.INSTANCE)
|
| 65 | .map(e -> e.getBoundingBox(structureTemplateManager, position, rotation));
|
| 66 | return BoundingBox.encapsulatingBoxes(stream::iterator).orElseThrow(() -> new IllegalStateException("Unable to calculate boundingbox for ListPoolElement"));
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public boolean place(
|
| 71 | final StructureTemplateManager structureTemplateManager,
|
| 72 | final WorldGenLevel level,
|
| 73 | final StructureManager structureManager,
|
| 74 | final ChunkGenerator generator,
|
| 75 | final BlockPos position,
|
| 76 | final BlockPos referencePos,
|
| 77 | final Rotation rotation,
|
| 78 | final BoundingBox chunkBB,
|
| 79 | final RandomSource random,
|
| 80 | final LiquidSettings liquidSettings,
|
| 81 | final boolean keepJigsaws
|
| 82 | ) {
|
| 83 | for (StructurePoolElement element : this.elements) {
|
| 84 | if (!element.place(
|
| 85 | structureTemplateManager, level, structureManager, generator, position, referencePos, rotation, chunkBB, random, liquidSettings, keepJigsaws
|
| 86 | )) {
|
| 87 | return false;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | return true;
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public StructurePoolElementType<?> getType() {
|
| 96 | return StructurePoolElementType.LIST;
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public StructurePoolElement setProjection(final StructureTemplatePool.Projection projection) {
|
| 101 | super.setProjection(projection);
|
| 102 | this.setProjectionOnEachElement(projection);
|
| 103 | return this;
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public String toString() {
|
| 108 | return "List[" + this.elements.stream().map(Object::toString).collect(Collectors.joining(", ")) + "]";
|
| 109 | }
|
| 110 |
|
| 111 | private void setProjectionOnEachElement(final StructureTemplatePool.Projection projection) {
|
| 112 | this.elements.forEach(k -> k.setProjection(projection));
|
| 113 | }
|
| 114 |
|
| 115 | @VisibleForTesting
|
| 116 | public List<StructurePoolElement> getElements() {
|
| 117 | return this.elements;
|
| 118 | }
|
| 119 | }
|
| 120 |
|