Skip to content

Commit d284720

Browse files
committed
Add "assert entity ... inside" command
1 parent 9b88522 commit d284720

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/main/java/io/github/misode/packtest/commands/AssertCommand.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
4040
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
4141
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
42+
import net.minecraft.world.phys.AABB;
4243
import net.minecraft.world.scores.Objective;
4344
import net.minecraft.world.scores.ReadOnlyScoreInfo;
4445
import net.minecraft.world.scores.ScoreHolder;
@@ -84,7 +85,9 @@ public static void addConditions(LiteralArgumentBuilder<CommandSourceStack> buil
8485
.then(literal("data"))
8586
.then(literal("entity")
8687
.then(argument("entities", EntityArgument.entities())
87-
.executes(expect.apply(AssertCommand::assertEntity))))
88+
.executes(expect.apply(AssertCommand::assertEntity))
89+
.then(literal("inside")
90+
.executes(expect.apply(AssertCommand::assertEntityInside)))))
8891
.then(literal("predicate")
8992
.then(argument("predicate", ResourceLocationArgument.id())
9093
.suggests(SUGGEST_PREDICATE)
@@ -130,15 +133,34 @@ private static AssertResult assertBlock(CommandContext<CommandSourceStack> ctx)
130133
private static AssertResult assertEntity(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
131134
EntitySelector selector = ctx.getArgument("entities", EntitySelector.class);
132135
String source = ((PackTestArgumentSource)selector).packtest$getSource();
133-
Collection<? extends Entity> entities = selector.findEntities(ctx.getSource());
136+
List<? extends Entity> entities = selector.findEntities(ctx.getSource());
134137
if (!entities.isEmpty()) {
135138
Entity firstEntity = entities.stream().findFirst().orElseThrow();
136139
String firstName = Objects.requireNonNull(firstEntity.getDisplayName()).getString();
137-
return ok(source, firstName + (entities.size() <= 1 ? "" : " and " + (entities.size() - 1) + " more"));
140+
return ok(source, firstName + (entities.size() == 1 ? "" : " and " + (entities.size() - 1) + " more"));
138141
}
139142
return err(source);
140143
}
141144

145+
private static AssertResult assertEntityInside(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
146+
EntitySelector selector = ctx.getArgument("entities", EntitySelector.class);
147+
String source = ((PackTestArgumentSource)selector).packtest$getSource();
148+
GameTestHelper helper = ((PackTestSourceStack)ctx.getSource()).packtest$getHelper();
149+
if (helper == null) {
150+
throw ERROR_NO_HELPER.create();
151+
}
152+
AABB bounds = helper.getBounds().inflate(1);
153+
List<? extends Entity> entities = selector.findEntities(ctx.getSource()).stream()
154+
.filter(entity -> bounds.contains(entity.position()))
155+
.toList();
156+
if (!entities.isEmpty()) {
157+
Entity firstEntity = entities.stream().findFirst().orElseThrow();
158+
String firstName = Objects.requireNonNull(firstEntity.getDisplayName()).getString();
159+
return ok(source + " inside test", firstName + (entities.size() == 1 ? "" : " and " + (entities.size() - 1) + " more"));
160+
}
161+
return err(source + " inside test");
162+
}
163+
142164
private static AssertResult assertPredicate(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
143165
ResourceLocation id = ctx.getArgument("predicate", ResourceLocation.class);
144166
LootItemCondition predicate = ResourceLocationArgument.getPredicate(ctx, "predicate");

0 commit comments

Comments
 (0)