package net.minecraft.gametest.framework; import com.mojang.brigadier.context.CommandContext; import java.util.LinkedList; import java.util.List; import java.util.function.Function; 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; public class TestFinder implements StructureBlockPosFinder, TestFunctionFinder { static final TestFunctionFinder NO_FUNCTIONS = Stream::empty; static final StructureBlockPosFinder NO_STRUCTURES = Stream::empty; private final TestFunctionFinder testFunctionFinder; private final StructureBlockPosFinder structureBlockPosFinder; private final CommandSourceStack source; private final Function, T> contextProvider; @Override public Stream findStructureBlockPos() { return this.structureBlockPosFinder.findStructureBlockPos(); } TestFinder( CommandSourceStack source, Function, T> contextProvider, TestFunctionFinder testFunctionFinder, StructureBlockPosFinder structureBlockPosFinder ) { this.source = source; this.contextProvider = contextProvider; this.testFunctionFinder = testFunctionFinder; this.structureBlockPosFinder = structureBlockPosFinder; } T get() { return (T)this.contextProvider.apply(this); } public CommandSourceStack source() { return this.source; } @Override public Stream findTestFunctions() { return this.testFunctionFinder.findTestFunctions(); } public static class Builder { private final Function, T> contextProvider; private final UnaryOperator>> testFunctionFinderWrapper; private final UnaryOperator>> structureBlockPosFinderWrapper; public Builder(Function, T> contextProvider) { this.contextProvider = contextProvider; this.testFunctionFinderWrapper = supplier -> supplier; this.structureBlockPosFinderWrapper = supplier -> supplier; } private Builder( Function, T> contextProvider, UnaryOperator>> testFunctionFinderWrapper, UnaryOperator>> structureBlockPosFinderWrapper ) { this.contextProvider = contextProvider; this.testFunctionFinderWrapper = testFunctionFinderWrapper; this.structureBlockPosFinderWrapper = structureBlockPosFinderWrapper; } public TestFinder.Builder createMultipleCopies(int count) { return new TestFinder.Builder<>(this.contextProvider, 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 T build(CommandSourceStack source, TestFunctionFinder testFunctionFinder, StructureBlockPosFinder structureBlockPosFinder) { return new TestFinder( source, this.contextProvider, ((Supplier)this.testFunctionFinderWrapper.apply(testFunctionFinder::findTestFunctions))::get, ((Supplier)this.structureBlockPosFinderWrapper.apply(structureBlockPosFinder::findStructureBlockPos))::get ) .get(); } public T radius(CommandContext context, int radius) { CommandSourceStack commandSourceStack = context.getSource(); BlockPos blockPos = BlockPos.containing(commandSourceStack.getPosition()); return this.build(commandSourceStack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.findStructureBlocks(blockPos, radius, commandSourceStack.getLevel())); } public T nearest(CommandContext context) { CommandSourceStack commandSourceStack = context.getSource(); BlockPos blockPos = BlockPos.containing(commandSourceStack.getPosition()); return this.build( commandSourceStack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.findNearestStructureBlock(blockPos, 15, commandSourceStack.getLevel()).stream() ); } public T allNearby(CommandContext context) { CommandSourceStack commandSourceStack = context.getSource(); BlockPos blockPos = BlockPos.containing(commandSourceStack.getPosition()); return this.build(commandSourceStack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.findStructureBlocks(blockPos, 200, commandSourceStack.getLevel())); } public T lookedAt(CommandContext context) { CommandSourceStack commandSourceStack = context.getSource(); return this.build( commandSourceStack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.lookedAtStructureBlockPos( BlockPos.containing(commandSourceStack.getPosition()), commandSourceStack.getPlayer().getCamera(), commandSourceStack.getLevel() ) ); } public T allTests(CommandContext context) { return this.build( context.getSource(), () -> GameTestRegistry.getAllTestFunctions().stream().filter(testFunction -> !testFunction.manualOnly()), TestFinder.NO_STRUCTURES ); } public T allTestsInClass(CommandContext context, String className) { return this.build( context.getSource(), () -> GameTestRegistry.getTestFunctionsForClassName(className).filter(testFunction -> !testFunction.manualOnly()), TestFinder.NO_STRUCTURES ); } public T failedTests(CommandContext context, boolean onlyRequired) { return this.build( context.getSource(), () -> GameTestRegistry.getLastFailedTests().filter(testFunction -> !onlyRequired || testFunction.required()), TestFinder.NO_STRUCTURES ); } public T byArgument(CommandContext context, String argumentName) { return this.build(context.getSource(), () -> Stream.of(TestFunctionArgument.getTestFunction(context, argumentName)), TestFinder.NO_STRUCTURES); } public T locateByName(CommandContext context, String name) { CommandSourceStack commandSourceStack = context.getSource(); BlockPos blockPos = BlockPos.containing(commandSourceStack.getPosition()); return this.build( commandSourceStack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.findStructureByTestFunction(blockPos, 1024, commandSourceStack.getLevel(), name) ); } public T failedTests(CommandContext context) { return this.failedTests(context, false); } } }