| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import com.mojang.brigadier.context.CommandContext;
|
| 4 | import java.util.Collection;
|
| 5 | import java.util.LinkedList;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.Supplier;
|
| 8 | import java.util.function.UnaryOperator;
|
| 9 | import java.util.stream.Stream;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.core.BlockPos;
|
| 12 | import net.minecraft.core.GlobalPos;
|
| 13 | import net.minecraft.core.Holder;
|
| 14 |
|
| 15 | public class TestFinder implements TestInstanceFinder, TestPosFinder {
|
| 16 | private static final TestInstanceFinder NO_FUNCTIONS = Stream::empty;
|
| 17 | private static final TestPosFinder NO_STRUCTURES = Stream::empty;
|
| 18 | private final TestInstanceFinder testInstanceFinder;
|
| 19 | private final TestPosFinder testPosFinder;
|
| 20 | private final CommandSourceStack source;
|
| 21 |
|
| 22 | @Override
|
| 23 | public Stream<GlobalPos> findTestPos() {
|
| 24 | return this.testPosFinder.findTestPos();
|
| 25 | }
|
| 26 |
|
| 27 | public static TestFinder.Builder builder() {
|
| 28 | return new TestFinder.Builder();
|
| 29 | }
|
| 30 |
|
| 31 | private TestFinder(final CommandSourceStack source, final TestInstanceFinder testInstanceFinder, final TestPosFinder testPosFinder) {
|
| 32 | this.source = source;
|
| 33 | this.testInstanceFinder = testInstanceFinder;
|
| 34 | this.testPosFinder = testPosFinder;
|
| 35 | }
|
| 36 |
|
| 37 | public CommandSourceStack source() {
|
| 38 | return this.source;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public Stream<Holder.Reference<GameTestInstance>> findTests() {
|
| 43 | return this.testInstanceFinder.findTests();
|
| 44 | }
|
| 45 |
|
| 46 | public static class Builder {
|
| 47 | private final UnaryOperator<Supplier<Stream<Holder.Reference<GameTestInstance>>>> testFinderWrapper;
|
| 48 | private final UnaryOperator<Supplier<Stream<GlobalPos>>> structureBlockPosFinderWrapper;
|
| 49 |
|
| 50 | public Builder() {
|
| 51 | this.testFinderWrapper = f -> f;
|
| 52 | this.structureBlockPosFinderWrapper = f -> f;
|
| 53 | }
|
| 54 |
|
| 55 | private Builder(
|
| 56 | final UnaryOperator<Supplier<Stream<Holder.Reference<GameTestInstance>>>> testFinderWrapper,
|
| 57 | final UnaryOperator<Supplier<Stream<GlobalPos>>> structureBlockPosFinderWrapper
|
| 58 | ) {
|
| 59 | this.testFinderWrapper = testFinderWrapper;
|
| 60 | this.structureBlockPosFinderWrapper = structureBlockPosFinderWrapper;
|
| 61 | }
|
| 62 |
|
| 63 | public TestFinder.Builder createMultipleCopies(final int amount) {
|
| 64 | return new TestFinder.Builder(createCopies(amount), createCopies(amount));
|
| 65 | }
|
| 66 |
|
| 67 | private static <Q> UnaryOperator<Supplier<Stream<Q>>> createCopies(final int amount) {
|
| 68 | return source -> {
|
| 69 | List<Q> copyList = new LinkedList<>();
|
| 70 | List<Q> sourceList = ((Stream)source.get()).toList();
|
| 71 |
|
| 72 | for (int i = 0; i < amount; i++) {
|
| 73 | copyList.addAll(sourceList);
|
| 74 | }
|
| 75 |
|
| 76 | return copyList::stream;
|
| 77 | };
|
| 78 | }
|
| 79 |
|
| 80 | private TestFinder build(final CommandSourceStack source, final TestInstanceFinder testInstanceFinder, final TestPosFinder testPosFinder) {
|
| 81 | List<Holder.Reference<GameTestInstance>> selectedTests = testInstanceFinder.findTests().toList();
|
| 82 | return new TestFinder(
|
| 83 | source, this.testFinderWrapper.apply(selectedTests::stream)::get, () -> this.structureBlockPosFinderWrapper.apply(testPosFinder::findTestPos).get()
|
| 84 | );
|
| 85 | }
|
| 86 |
|
| 87 | public TestFinder radius(final CommandContext<CommandSourceStack> sourceStack, final int radius) {
|
| 88 | CommandSourceStack source = sourceStack.getSource();
|
| 89 | BlockPos pos = BlockPos.containing(source.getPosition());
|
| 90 | return this.build(
|
| 91 | source,
|
| 92 | TestFinder.NO_FUNCTIONS,
|
| 93 | () -> StructureUtils.findTestBlocks(pos, radius, source.getLevel()).map(p -> new GlobalPos(source.getLevel().dimension(), p))
|
| 94 | );
|
| 95 | }
|
| 96 |
|
| 97 | public TestFinder nearest(final CommandContext<CommandSourceStack> sourceStack) {
|
| 98 | CommandSourceStack source = sourceStack.getSource();
|
| 99 | BlockPos pos = BlockPos.containing(source.getPosition());
|
| 100 | return this.build(
|
| 101 | source,
|
| 102 | TestFinder.NO_FUNCTIONS,
|
| 103 | () -> StructureUtils.findNearestTest(pos, 15, source.getLevel()).map(p -> new GlobalPos(source.getLevel().dimension(), p)).stream()
|
| 104 | );
|
| 105 | }
|
| 106 |
|
| 107 | public TestFinder allNearby(final CommandContext<CommandSourceStack> sourceStack) {
|
| 108 | CommandSourceStack source = sourceStack.getSource();
|
| 109 | BlockPos pos = BlockPos.containing(source.getPosition());
|
| 110 | return this.build(
|
| 111 | source, TestFinder.NO_FUNCTIONS, () -> StructureUtils.findTestBlocks(pos, 250, source.getLevel()).map(p -> new GlobalPos(source.getLevel().dimension(), p))
|
| 112 | );
|
| 113 | }
|
| 114 |
|
| 115 | public TestFinder lookedAt(final CommandContext<CommandSourceStack> sourceStack) {
|
| 116 | CommandSourceStack source = sourceStack.getSource();
|
| 117 | return this.build(
|
| 118 | source,
|
| 119 | TestFinder.NO_FUNCTIONS,
|
| 120 | () -> StructureUtils.lookedAtTestPos(BlockPos.containing(source.getPosition()), source.getPlayer().getCamera(), source.getLevel())
|
| 121 | .map(p -> new GlobalPos(source.getLevel().dimension(), p))
|
| 122 | );
|
| 123 | }
|
| 124 |
|
| 125 | public TestFinder failedTests(final CommandContext<CommandSourceStack> sourceStack, final boolean onlyRequiredTests) {
|
| 126 | return this.build(
|
| 127 | sourceStack.getSource(),
|
| 128 | () -> FailedTestTracker.getLastFailedTests().filter(test -> !onlyRequiredTests || test.value().required()),
|
| 129 | TestFinder.NO_STRUCTURES
|
| 130 | );
|
| 131 | }
|
| 132 |
|
| 133 | public TestFinder byResourceSelection(final CommandContext<CommandSourceStack> sourceStack, final Collection<Holder.Reference<GameTestInstance>> holders) {
|
| 134 | return this.build(sourceStack.getSource(), holders::stream, TestFinder.NO_STRUCTURES);
|
| 135 | }
|
| 136 |
|
| 137 | public TestFinder failedTests(final CommandContext<CommandSourceStack> sourceStack) {
|
| 138 | return this.failedTests(sourceStack, false);
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 |
|