Skip to content

Commit

Permalink
Add "await for" command to add delays
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Dec 26, 2023
1 parent 19efca5 commit 81de9b3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
55 changes: 26 additions & 29 deletions src/main/java/io/github/misode/packtest/PackTestFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.misode.packtest.dummy.Dummy;
import net.minecraft.commands.*;
import net.minecraft.commands.arguments.TimeArgument;
import net.minecraft.commands.arguments.coordinates.Vec3Argument;
import net.minecraft.commands.execution.ExecutionContext;
import net.minecraft.commands.functions.CommandFunction;
Expand Down Expand Up @@ -75,7 +76,11 @@ public static PackTestFunction fromLines(ResourceLocation id, CommandDispatcher<
steps.add(new ExecuteStep(currentLines, dispatcher));
currentLines = Lists.newArrayList();
}
steps.add(new AwaitStep(line, dispatcher));
if (sLine.startsWith("await for ")) {
steps.add(new IdleStep(line.substring("await for ".length())));
} else {
steps.add(new AwaitStep(line, dispatcher));
}
} else {
currentLines.add(line);
}
Expand Down Expand Up @@ -184,47 +189,27 @@ public interface Step {
void register(GameTestSequence sequence, CommandSourceStack source);
}

public static class ExecuteStep implements Step {
private final List<String> lines;
private final CommandDispatcher<CommandSourceStack> dispatcher;

public ExecuteStep(List<String> lines, CommandDispatcher<CommandSourceStack> dispatcher) {
this.lines = lines;
this.dispatcher = dispatcher;
}

public record ExecuteStep(List<String> lines, CommandDispatcher<CommandSourceStack> dispatcher) implements Step {
@Override
public void register(GameTestSequence sequence, CommandSourceStack source) {
CommandFunction<CommandSourceStack> function;
try {
ResourceLocation id = new ResourceLocation("packtest", "internal");
function = CommandFunction.fromLines(id, this.dispatcher, source, this.lines);
CommandFunction<CommandSourceStack> function = CommandFunction.fromLines(id, this.dispatcher, source, this.lines);
InstantiatedFunction<CommandSourceStack> instantiated = function.instantiate(null, this.dispatcher, source);
sequence.thenExecute(() -> Commands.executeCommandInContext(
source,
e -> ExecutionContext.queueInitialFunctionCall(e, instantiated, source, CommandResultCallback.EMPTY)
));
} catch (IllegalArgumentException e) {
throw new GameTestAssertException(e.getMessage());
}
InstantiatedFunction<CommandSourceStack> instantiated;
try {
instantiated = function.instantiate(null, this.dispatcher, source);
} catch (FunctionInstantiationException e) {
String message = e.messageComponent().getString();
throw new GameTestAssertException("Failed to instantiate test function: " + message);
}
sequence.thenExecute(() -> Commands.executeCommandInContext(
source,
e -> ExecutionContext.queueInitialFunctionCall(e, instantiated, source, CommandResultCallback.EMPTY)
));
}
}

public static class AwaitStep implements Step {
private final String line;
private final CommandDispatcher<CommandSourceStack> dispatcher;

public AwaitStep(String line, CommandDispatcher<CommandSourceStack> dispatcher) {
this.line = line;
this.dispatcher = dispatcher;
}

public record AwaitStep(String line, CommandDispatcher<CommandSourceStack> dispatcher) implements Step {
@Override
public void register(GameTestSequence sequence, CommandSourceStack source) {
ParseResults<CommandSourceStack> parseResults = dispatcher.parse(line, source);
Expand All @@ -236,4 +221,16 @@ public void register(GameTestSequence sequence, CommandSourceStack source) {
));
}
}

public record IdleStep(String argument) implements Step {
@Override
public void register(GameTestSequence sequence, CommandSourceStack source) {
try {
int ticks = TimeArgument.time().parse(new StringReader(argument));
sequence.thenIdle(ticks);
} catch (CommandSyntaxException e) {
throw new GameTestAssertException(e.getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher, Co
dispatcher.register(assertBuilder);
}

public static LiteralArgumentBuilder<CommandSourceStack> addConditions(LiteralArgumentBuilder<CommandSourceStack> builder, CommandBuildContext buildContext, Function<AssertPredicate, Command<CommandSourceStack>> expect) {
public static void addConditions(LiteralArgumentBuilder<CommandSourceStack> builder, CommandBuildContext buildContext, Function<AssertPredicate, Command<CommandSourceStack>> expect) {
builder
.then(literal("block")
.then(argument("pos", BlockPosArgument.blockPos())
Expand Down Expand Up @@ -100,7 +100,6 @@ public static LiteralArgumentBuilder<CommandSourceStack> addConditions(LiteralAr
.executes(expect.apply(ctx -> assertData(ctx, dataProvider))))));
}

return builder;
}

private static AssertResult assertBlock(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
Expand Down Expand Up @@ -243,23 +242,23 @@ default AssertResult apply(final CommandContext<CommandSourceStack> sourceStack)
AssertResult applyThrows(CommandContext<CommandSourceStack> elem) throws CommandSyntaxException;
}

private static AssertResult err(String expected) {
public static AssertResult err(String expected) {
return new ExpectedGot(false, expected, null);
}
private static AssertResult err(String expected, String got) {

public static AssertResult err(String expected, String got) {
return new ExpectedGot(false, expected, got);
}
private static AssertResult ok(String match) {

public static AssertResult ok(String match) {
return new ExpectedGot(true, match, null);
}

private static AssertResult ok(String match, String got) {
public static AssertResult ok(String match, String got) {
return new ExpectedGot(true, match, got);
}

private static AssertResult result(boolean ok, String expected, String got) {
public static AssertResult result(boolean ok, String expected, String got) {
return new ExpectedGot(ok, expected, got);
}

Expand Down
16 changes: 14 additions & 2 deletions src/main/java/io/github/misode/packtest/commands/AwaitCommand.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
package io.github.misode.packtest.commands;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.TimeArgument;

import java.util.function.Function;

import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;

public class AwaitCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext buildContext) {
LiteralArgumentBuilder<CommandSourceStack> awaitBuilder = literal("await")
.requires(ctx -> ctx.hasPermission(2));
AssertCommand.addConditions(awaitBuilder, buildContext, predicate -> new AssertCommand.AssertCustomExecutor(true, predicate));
addConditions(awaitBuilder, buildContext, predicate -> new AssertCommand.AssertCustomExecutor(true, predicate));
LiteralArgumentBuilder<CommandSourceStack> notBuilder = literal("not");
AssertCommand.addConditions(notBuilder, buildContext, predicate -> new AssertCommand.AssertCustomExecutor(false, predicate));
addConditions(notBuilder, buildContext, predicate -> new AssertCommand.AssertCustomExecutor(false, predicate));
awaitBuilder = awaitBuilder.then(notBuilder);
dispatcher.register(awaitBuilder);
}

public static void addConditions(LiteralArgumentBuilder<CommandSourceStack> builder, CommandBuildContext buildContext, Function<AssertCommand.AssertPredicate, Command<CommandSourceStack>> expect) {
AssertCommand.addConditions(builder, buildContext, expect);
builder.then(literal("for")
.then(argument("time", TimeArgument.time())
.executes(expect.apply(ctx -> AssertCommand.err("Timed out")))));
}
}

0 comments on commit 81de9b3

Please sign in to comment.