| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import java.nio.file.Path;
|
| 4 | import java.util.Collections;
|
| 5 | import java.util.Comparator;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.stream.Stream;
|
| 9 | import net.minecraft.commands.arguments.blocks.BlockInput;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Vec3i;
|
| 12 | import net.minecraft.core.registries.Registries;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.resources.ResourceKey;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.world.entity.Entity;
|
| 17 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 18 | import net.minecraft.world.entity.ai.village.poi.PoiTypes;
|
| 19 | import net.minecraft.world.entity.player.Player;
|
| 20 | import net.minecraft.world.level.block.Blocks;
|
| 21 | import net.minecraft.world.level.block.Mirror;
|
| 22 | import net.minecraft.world.level.block.Rotation;
|
| 23 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 24 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 25 | import net.minecraft.world.level.block.entity.TestInstanceBlockEntity;
|
| 26 | import net.minecraft.world.level.block.state.BlockState;
|
| 27 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 28 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 29 | import net.minecraft.world.phys.AABB;
|
| 30 | import net.minecraft.world.phys.Vec3;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | public class StructureUtils {
|
| 34 | public static final int DEFAULT_Y_SEARCH_RADIUS = 10;
|
| 35 | public static @Nullable Path testStructuresTargetDir;
|
| 36 | public static @Nullable Path testStructuresSourceDir;
|
| 37 |
|
| 38 | public static Rotation getRotationForRotationSteps(final int rotationSteps) {
|
| 39 | return switch (rotationSteps) {
|
| 40 | case 0 -> Rotation.NONE;
|
| 41 | case 1 -> Rotation.CLOCKWISE_90;
|
| 42 | case 2 -> Rotation.CLOCKWISE_180;
|
| 43 | case 3 -> Rotation.COUNTERCLOCKWISE_90;
|
| 44 | default -> throw new IllegalArgumentException("rotationSteps must be a value from 0-3. Got value " + rotationSteps);
|
| 45 | };
|
| 46 | }
|
| 47 |
|
| 48 | public static int getRotationStepsForRotation(final Rotation rotation) {
|
| 49 | return switch (rotation) {
|
| 50 | case NONE -> 0;
|
| 51 | case CLOCKWISE_90 -> 1;
|
| 52 | case CLOCKWISE_180 -> 2;
|
| 53 | case COUNTERCLOCKWISE_90 -> 3;
|
| 54 | default -> throw new IllegalArgumentException("Unknown rotation value, don't know how many steps it represents: " + rotation);
|
| 55 | };
|
| 56 | }
|
| 57 |
|
| 58 | public static TestInstanceBlockEntity createNewEmptyTest(
|
| 59 | final Identifier id, final BlockPos structurePos, final Vec3i size, final Rotation rotation, final ServerLevel level
|
| 60 | ) {
|
| 61 | BoundingBox structureBoundingBox = getStructureBoundingBox(TestInstanceBlockEntity.getStructurePos(structurePos), size, rotation);
|
| 62 | clearSpaceForStructure(structureBoundingBox, level);
|
| 63 | level.setBlockAndUpdate(structurePos, Blocks.TEST_INSTANCE_BLOCK.defaultBlockState());
|
| 64 | TestInstanceBlockEntity test = (TestInstanceBlockEntity)level.getBlockEntity(structurePos);
|
| 65 | ResourceKey<GameTestInstance> key = ResourceKey.create(Registries.TEST_INSTANCE, id);
|
| 66 | test.set(new TestInstanceBlockEntity.Data(Optional.of(key), size, rotation, false, TestInstanceBlockEntity.Status.CLEARED, Optional.empty()));
|
| 67 | return test;
|
| 68 | }
|
| 69 |
|
| 70 | public static void clearSpaceForStructure(final BoundingBox structureBoundingBox, final ServerLevel level) {
|
| 71 | int groundHeight = structureBoundingBox.minY() - 1;
|
| 72 | BlockPos.betweenClosedStream(structureBoundingBox).forEach(pos -> clearBlock(groundHeight, pos, level));
|
| 73 | level.getBlockTicks().clearArea(structureBoundingBox);
|
| 74 | level.clearBlockEvents(structureBoundingBox);
|
| 75 | AABB bounds = AABB.of(structureBoundingBox);
|
| 76 | List<Entity> livingEntities = level.getEntitiesOfClass(Entity.class, bounds, mob -> !(mob instanceof Player));
|
| 77 | livingEntities.forEach(Entity::discard);
|
| 78 | }
|
| 79 |
|
| 80 | public static BlockPos getTransformedFarCorner(final BlockPos structurePosition, final Vec3i size, final Rotation rotation) {
|
| 81 | BlockPos farCornerBeforeTransform = structurePosition.offset(size).offset(-1, -1, -1);
|
| 82 | return StructureTemplate.transform(farCornerBeforeTransform, Mirror.NONE, rotation, structurePosition);
|
| 83 | }
|
| 84 |
|
| 85 | public static BoundingBox getStructureBoundingBox(final BlockPos northWestCorner, final Vec3i size, final Rotation rotation) {
|
| 86 | BlockPos farCorner = getTransformedFarCorner(northWestCorner, size, rotation);
|
| 87 | BoundingBox boundingBox = BoundingBox.fromCorners(northWestCorner, farCorner);
|
| 88 | int currentNorthWestCornerX = Math.min(boundingBox.minX(), boundingBox.maxX());
|
| 89 | int currentNorthWestCornerZ = Math.min(boundingBox.minZ(), boundingBox.maxZ());
|
| 90 | return boundingBox.move(northWestCorner.getX() - currentNorthWestCornerX, 0, northWestCorner.getZ() - currentNorthWestCornerZ);
|
| 91 | }
|
| 92 |
|
| 93 | public static Optional<BlockPos> findTestContainingPos(final BlockPos pos, final int searchRadius, final ServerLevel level) {
|
| 94 | return findTestBlocks(pos, searchRadius, level).filter(testBlockPosToCheck -> doesStructureContain(testBlockPosToCheck, pos, level)).findFirst();
|
| 95 | }
|
| 96 |
|
| 97 | public static Optional<BlockPos> findNearestTest(final BlockPos relativeToPos, final int searchRadius, final ServerLevel level) {
|
| 98 | Comparator<BlockPos> distanceToPlayer = Comparator.comparingInt(pos -> pos.distManhattan(relativeToPos));
|
| 99 | return findTestBlocks(relativeToPos, searchRadius, level).min(distanceToPlayer);
|
| 100 | }
|
| 101 |
|
| 102 | public static Stream<BlockPos> findTestBlocks(final BlockPos centerPos, final int searchRadius, final ServerLevel level) {
|
| 103 | return level.getPoiManager()
|
| 104 | .findAll(p -> p.is(PoiTypes.TEST_INSTANCE), p -> true, centerPos, searchRadius, PoiManager.Occupancy.ANY)
|
| 105 | .map(BlockPos::immutable);
|
| 106 | }
|
| 107 |
|
| 108 | public static Stream<BlockPos> lookedAtTestPos(final BlockPos pos, final Entity camera, final ServerLevel level) {
|
| 109 | int radius = 250;
|
| 110 | Vec3 start = camera.getEyePosition();
|
| 111 | Vec3 end = start.add(camera.getLookAngle().scale(250.0));
|
| 112 | return findTestBlocks(pos, 250, level)
|
| 113 | .map(blockPos -> level.getBlockEntity(blockPos, BlockEntityTypes.TEST_INSTANCE_BLOCK))
|
| 114 | .flatMap(Optional::stream)
|
| 115 | .filter(blockEntity -> blockEntity.getStructureBounds().clip(start, end).isPresent())
|
| 116 | .map(BlockEntity::getBlockPos)
|
| 117 | .sorted(Comparator.comparing(pos::distSqr))
|
| 118 | .limit(1L);
|
| 119 | }
|
| 120 |
|
| 121 | private static void clearBlock(final int airIfAboveThisY, final BlockPos pos, final ServerLevel level) {
|
| 122 | BlockState blockState;
|
| 123 | if (pos.getY() < airIfAboveThisY) {
|
| 124 | blockState = Blocks.STONE.defaultBlockState();
|
| 125 | } else {
|
| 126 | blockState = Blocks.AIR.defaultBlockState();
|
| 127 | }
|
| 128 |
|
| 129 | BlockInput blockInput = new BlockInput(blockState, Collections.emptySet(), null);
|
| 130 | blockInput.place(level, pos, 818);
|
| 131 | level.updateNeighborsAt(pos, blockState.getBlock());
|
| 132 | }
|
| 133 |
|
| 134 | private static boolean doesStructureContain(final BlockPos testInstanceBlockPos, final BlockPos pos, final ServerLevel level) {
|
| 135 | return level.getBlockEntity(testInstanceBlockPos) instanceof TestInstanceBlockEntity blockEntity
|
| 136 | ? blockEntity.getStructureBoundingBox().isInside(pos)
|
| 137 | : false;
|
| 138 | }
|
| 139 | }
|
| 140 |
|