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 p_332130_, Function, T> p_332876_, TestFunctionFinder p_330000_, StructureBlockPosFinder p_332515_) { this.source = p_332130_; this.contextProvider = p_332876_; this.testFunctionFinder = p_330000_; this.structureBlockPosFinder = p_332515_; } T get() { return 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> p_329391_) { this.contextProvider = p_329391_; this.testFunctionFinderWrapper = p_333647_ -> p_333647_; this.structureBlockPosFinderWrapper = p_327811_ -> p_327811_; } private Builder( Function, T> p_329078_, UnaryOperator>> p_334250_, UnaryOperator>> p_334300_ ) { this.contextProvider = p_329078_; this.testFunctionFinderWrapper = p_334250_; this.structureBlockPosFinderWrapper = p_334300_; } public TestFinder.Builder createMultipleCopies(int p_329806_) { return new TestFinder.Builder<>(this.contextProvider, createCopies(p_329806_), createCopies(p_329806_)); } private static UnaryOperator>> createCopies(int p_334571_) { return p_333976_ -> { List list = new LinkedList<>(); List list1 = ((Stream)p_333976_.get()).toList(); for (int i = 0; i < p_334571_; i++) { list.addAll(list1); } return list::stream; }; } private T build(CommandSourceStack p_334153_, TestFunctionFinder p_330203_, StructureBlockPosFinder p_328202_) { return new TestFinder<>(p_334153_, this.contextProvider, this.testFunctionFinderWrapper.apply(p_330203_::findTestFunctions)::get, this.structureBlockPosFinderWrapper.apply(p_328202_::findStructureBlockPos)::get) .get(); } public T radius(CommandContext p_330481_, int p_334173_) { CommandSourceStack commandsourcestack = p_330481_.getSource(); BlockPos blockpos = BlockPos.containing(commandsourcestack.getPosition()); return this.build(commandsourcestack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.findStructureBlocks(blockpos, p_334173_, commandsourcestack.getLevel())); } public T nearest(CommandContext p_332654_) { CommandSourceStack commandsourcestack = p_332654_.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 p_335428_) { CommandSourceStack commandsourcestack = p_335428_.getSource(); BlockPos blockpos = BlockPos.containing(commandsourcestack.getPosition()); return this.build(commandsourcestack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.findStructureBlocks(blockpos, 200, commandsourcestack.getLevel())); } public T lookedAt(CommandContext p_328071_) { CommandSourceStack commandsourcestack = p_328071_.getSource(); return this.build( commandsourcestack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.lookedAtStructureBlockPos( BlockPos.containing(commandsourcestack.getPosition()), commandsourcestack.getPlayer().getCamera(), commandsourcestack.getLevel() ) ); } public T allTests(CommandContext p_331369_) { return this.build( p_331369_.getSource(), () -> GameTestRegistry.getAllTestFunctions().stream().filter(p_334467_ -> !p_334467_.manualOnly()), TestFinder.NO_STRUCTURES ); } public T allTestsInClass(CommandContext p_333766_, String p_332600_) { return this.build( p_333766_.getSource(), () -> GameTestRegistry.getTestFunctionsForClassName(p_332600_).filter(p_328668_ -> !p_328668_.manualOnly()), TestFinder.NO_STRUCTURES ); } public T failedTests(CommandContext p_332736_, boolean p_336399_) { return this.build( p_332736_.getSource(), () -> GameTestRegistry.getLastFailedTests().filter(p_328598_ -> !p_336399_ || p_328598_.required()), TestFinder.NO_STRUCTURES ); } public T byArgument(CommandContext p_329167_, String p_334913_) { return this.build(p_329167_.getSource(), () -> Stream.of(TestFunctionArgument.getTestFunction(p_329167_, p_334913_)), TestFinder.NO_STRUCTURES); } public T locateByName(CommandContext p_330730_, String p_336390_) { CommandSourceStack commandsourcestack = p_330730_.getSource(); BlockPos blockpos = BlockPos.containing(commandsourcestack.getPosition()); return this.build( commandsourcestack, TestFinder.NO_FUNCTIONS, () -> StructureUtils.findStructureByTestFunction(blockpos, 1024, commandsourcestack.getLevel(), p_336390_) ); } public T failedTests(CommandContext p_331687_) { return this.failedTests(p_331687_, false); } } }