package net.minecraft.gametest.framework; import com.mojang.brigadier.context.CommandContext; import java.util.Collection; import java.util.LinkedList; import java.util.List; import java.util.function.Supplier; import java.util.function.UnaryOperator; import java.util.stream.Stream; import net.minecraft.commands.CommandSourceStack; import net.minecraft.core.BlockPos; import net.minecraft.core.Holder.Reference; public class TestFinder implements TestInstanceFinder, TestPosFinder { static final TestInstanceFinder NO_FUNCTIONS = Stream::empty; static final TestPosFinder NO_STRUCTURES = Stream::empty; private final TestInstanceFinder testInstanceFinder; private final TestPosFinder testPosFinder; private final CommandSourceStack source; @Override public Stream findTestPos() { return this.testPosFinder.findTestPos(); } public static TestFinder.Builder builder() { return new TestFinder.Builder(); } TestFinder(CommandSourceStack source, TestInstanceFinder testInstanceFinder, TestPosFinder testPosFinder) { this.source = source; this.testInstanceFinder = testInstanceFinder; this.testPosFinder = testPosFinder; } public CommandSourceStack source() { return this.source; } @Override public Stream> findTests() { return this.testInstanceFinder.findTests(); } public static class Builder { private final UnaryOperator>>> testFinderWrapper; private final UnaryOperator>> structureBlockPosFinderWrapper; public Builder() { this.testFinderWrapper = supplier -> supplier; this.structureBlockPosFinderWrapper = supplier -> supplier; } private Builder( UnaryOperator>>> testFinderWrapper, UnaryOperator>> structureBlockPosFinderWrapper ) { this.testFinderWrapper = testFinderWrapper; this.structureBlockPosFinderWrapper = structureBlockPosFinderWrapper; } public TestFinder.Builder createMultipleCopies(int count) { return new TestFinder.Builder(createCopies(count), createCopies(count)); } private static UnaryOperator>> createCopies(int count) { return supplier -> { List list = new LinkedList(); List list2 = ((Stream)supplier.get()).toList(); for (int j = 0; j < count; j++) { list.addAll(list2); } return list::stream; }; } private TestFinder build(CommandSourceStack source, TestInstanceFinder instanceFinder, TestPosFinder posFinder) { return new TestFinder( source, ((Supplier)this.testFinderWrapper.apply(instanceFinder::findTests))::get, ((Supplier)this.structureBlockPosFinderWrapper.apply(posFinder::findTestPos))::get ); } public TestFinder radius(CommandContext context, int radius) { CommandSourceStack commandSourceStack = context.getSource(); BlockPos blockPos = BlockPos.containing(commandSourceStack.getPosition()); return this.build(commandSourceStack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.findTestBlocks(blockPos, radius, commandSourceStack.getLevel())); } public TestFinder nearest(CommandContext context) { CommandSourceStack commandSourceStack = context.getSource(); BlockPos blockPos = BlockPos.containing(commandSourceStack.getPosition()); return this.build(commandSourceStack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.findNearestTest(blockPos, 15, commandSourceStack.getLevel()).stream()); } public TestFinder allNearby(CommandContext context) { CommandSourceStack commandSourceStack = context.getSource(); BlockPos blockPos = BlockPos.containing(commandSourceStack.getPosition()); return this.build(commandSourceStack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.findTestBlocks(blockPos, 200, commandSourceStack.getLevel())); } public TestFinder lookedAt(CommandContext context) { CommandSourceStack commandSourceStack = context.getSource(); return this.build( commandSourceStack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.lookedAtTestPos( BlockPos.containing(commandSourceStack.getPosition()), commandSourceStack.getPlayer().getCamera(), commandSourceStack.getLevel() ) ); } public TestFinder failedTests(CommandContext context, boolean onlyRequired) { return this.build( context.getSource(), () -> FailedTestTracker.getLastFailedTests().filter(reference -> !onlyRequired || ((GameTestInstance)reference.value()).required()), TestFinder.NO_STRUCTURES ); } public TestFinder byResourceSelection(CommandContext context, Collection> collection) { return this.build(context.getSource(), collection::stream, TestFinder.NO_STRUCTURES); } public TestFinder failedTests(CommandContext context) { return this.failedTests(context, false); } } }