| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.Collection;
|
| 5 | import java.util.HashMap;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.function.Function;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.resources.ResourceKey;
|
| 11 | import net.minecraft.server.MinecraftServer;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 14 | import net.minecraft.world.phys.AABB;
|
| 15 |
|
| 16 | public class StructureGridSpawner implements GameTestRunner.StructureSpawner {
|
| 17 | private static final int SPACE_BETWEEN_COLUMNS = 5;
|
| 18 | private static final int SPACE_BETWEEN_ROWS = 6;
|
| 19 | private final int testsPerRow;
|
| 20 | private final Function<ResourceKey<Level>, BlockPos> firstTestNorthWestCorner;
|
| 21 | private final boolean clearOnBatch;
|
| 22 | private final Map<ResourceKey<Level>, StructureGridSpawner.DimensionGridState> grids = new HashMap<>();
|
| 23 |
|
| 24 | public StructureGridSpawner(final Function<ResourceKey<Level>, BlockPos> firstTestNorthWestCorner, final int testsPerRow, final boolean clearOnBatch) {
|
| 25 | this.testsPerRow = testsPerRow;
|
| 26 | this.firstTestNorthWestCorner = firstTestNorthWestCorner;
|
| 27 | this.clearOnBatch = clearOnBatch;
|
| 28 | }
|
| 29 |
|
| 30 | private StructureGridSpawner.DimensionGridState gridFor(final GameTestInfo testInfo) {
|
| 31 | return this.grids
|
| 32 | .computeIfAbsent(
|
| 33 | testInfo.getTest().info().dimension(),
|
| 34 | dimension -> new StructureGridSpawner.DimensionGridState(this.firstTestNorthWestCorner.apply((ResourceKey<Level>)dimension))
|
| 35 | );
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public void onBatchStart(final MinecraftServer server) {
|
| 40 | if (this.clearOnBatch) {
|
| 41 | for (StructureGridSpawner.DimensionGridState grid : this.grids.values()) {
|
| 42 | grid.testsInLastBatch.forEach(info -> {
|
| 43 | BoundingBox boundingBox = info.getTestInstanceBlockEntity().getTestBoundingBox();
|
| 44 | StructureUtils.clearSpaceForStructure(boundingBox, info.getLevel());
|
| 45 | });
|
| 46 | grid.testsInLastBatch.clear();
|
| 47 | grid.nextCorner.set(grid.firstTestNorthWestCorner);
|
| 48 | grid.rowBounds = new AABB(grid.firstTestNorthWestCorner);
|
| 49 | grid.currentRowCount = 0;
|
| 50 | }
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public Optional<GameTestInfo> spawnStructure(final GameTestInfo testInfo) {
|
| 56 | StructureGridSpawner.DimensionGridState grid = this.gridFor(testInfo);
|
| 57 | BlockPos northWestCorner = grid.nextCorner.immutable();
|
| 58 | testInfo.setTestBlockPos(northWestCorner);
|
| 59 | GameTestInfo infoWithStructure = testInfo.prepareTestStructure();
|
| 60 | if (infoWithStructure == null) {
|
| 61 | return Optional.empty();
|
| 62 | }
|
| 63 |
|
| 64 | infoWithStructure.startExecution(1);
|
| 65 | AABB structureBounds = testInfo.getTestInstanceBlockEntity().getTestBounds();
|
| 66 | grid.rowBounds = grid.rowBounds.minmax(structureBounds);
|
| 67 | grid.nextCorner.move((int)structureBounds.getXsize() + 5, 0, 0);
|
| 68 | if (++grid.currentRowCount >= this.testsPerRow) {
|
| 69 | grid.currentRowCount = 0;
|
| 70 | grid.nextCorner.move(0, 0, (int)grid.rowBounds.getZsize() + 6);
|
| 71 | grid.nextCorner.setX(grid.firstTestNorthWestCorner.getX());
|
| 72 | grid.rowBounds = new AABB(grid.nextCorner);
|
| 73 | }
|
| 74 |
|
| 75 | grid.testsInLastBatch.add(testInfo);
|
| 76 | return Optional.of(testInfo);
|
| 77 | }
|
| 78 |
|
| 79 | private static final class DimensionGridState {
|
| 80 | private final BlockPos firstTestNorthWestCorner;
|
| 81 | private final BlockPos.MutableBlockPos nextCorner;
|
| 82 | private AABB rowBounds;
|
| 83 | private int currentRowCount;
|
| 84 | private final Collection<GameTestInfo> testsInLastBatch = new ArrayList<>();
|
| 85 |
|
| 86 | private DimensionGridState(final BlockPos start) {
|
| 87 | this.firstTestNorthWestCorner = start;
|
| 88 | this.nextCorner = start.mutable();
|
| 89 | this.rowBounds = new AABB(start);
|
| 90 | this.currentRowCount = 0;
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 |
|