| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import com.mojang.authlib.GameProfile;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.datafixers.util.Either;
|
| 6 | import io.netty.channel.embedded.EmbeddedChannel;
|
| 7 | import java.util.Arrays;
|
| 8 | import java.util.Comparator;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Objects;
|
| 11 | import java.util.UUID;
|
| 12 | import java.util.function.Consumer;
|
| 13 | import java.util.function.Function;
|
| 14 | import java.util.function.IntPredicate;
|
| 15 | import java.util.function.Predicate;
|
| 16 | import java.util.function.Supplier;
|
| 17 | import java.util.stream.LongStream;
|
| 18 | import net.minecraft.commands.arguments.EntityAnchorArgument;
|
| 19 | import net.minecraft.core.BlockPos;
|
| 20 | import net.minecraft.core.Direction;
|
| 21 | import net.minecraft.core.Holder;
|
| 22 | import net.minecraft.core.component.DataComponents;
|
| 23 | import net.minecraft.core.registries.Registries;
|
| 24 | import net.minecraft.network.Connection;
|
| 25 | import net.minecraft.network.chat.CommonComponents;
|
| 26 | import net.minecraft.network.chat.Component;
|
| 27 | import net.minecraft.network.protocol.PacketFlow;
|
| 28 | import net.minecraft.resources.ResourceKey;
|
| 29 | import net.minecraft.server.commands.FillBiomeCommand;
|
| 30 | import net.minecraft.server.level.ClientInformation;
|
| 31 | import net.minecraft.server.level.ServerLevel;
|
| 32 | import net.minecraft.server.level.ServerPlayer;
|
| 33 | import net.minecraft.server.network.CommonListenerCookie;
|
| 34 | import net.minecraft.tags.BlockTags;
|
| 35 | import net.minecraft.tags.TagKey;
|
| 36 | import net.minecraft.world.InteractionHand;
|
| 37 | import net.minecraft.world.InteractionResult;
|
| 38 | import net.minecraft.world.clock.WorldClock;
|
| 39 | import net.minecraft.world.damagesource.DamageSource;
|
| 40 | import net.minecraft.world.effect.MobEffect;
|
| 41 | import net.minecraft.world.effect.MobEffectInstance;
|
| 42 | import net.minecraft.world.entity.Entity;
|
| 43 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 44 | import net.minecraft.world.entity.EntityType;
|
| 45 | import net.minecraft.world.entity.EntityTypes;
|
| 46 | import net.minecraft.world.entity.LivingEntity;
|
| 47 | import net.minecraft.world.entity.Mob;
|
| 48 | import net.minecraft.world.entity.item.ItemEntity;
|
| 49 | import net.minecraft.world.entity.npc.InventoryCarrier;
|
| 50 | import net.minecraft.world.entity.player.Player;
|
| 51 | import net.minecraft.world.item.BlockItem;
|
| 52 | import net.minecraft.world.item.Item;
|
| 53 | import net.minecraft.world.item.ItemStack;
|
| 54 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 55 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 56 | import net.minecraft.world.item.context.UseOnContext;
|
| 57 | import net.minecraft.world.level.GameType;
|
| 58 | import net.minecraft.world.level.Level;
|
| 59 | import net.minecraft.world.level.biome.Biome;
|
| 60 | import net.minecraft.world.level.block.Block;
|
| 61 | import net.minecraft.world.level.block.Blocks;
|
| 62 | import net.minecraft.world.level.block.ButtonBlock;
|
| 63 | import net.minecraft.world.level.block.HorizontalDirectionalBlock;
|
| 64 | import net.minecraft.world.level.block.LeverBlock;
|
| 65 | import net.minecraft.world.level.block.Mirror;
|
| 66 | import net.minecraft.world.level.block.Rotation;
|
| 67 | import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
|
| 68 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 69 | import net.minecraft.world.level.block.state.BlockState;
|
| 70 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 71 | import net.minecraft.world.level.block.state.properties.Property;
|
| 72 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 73 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 74 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 75 | import net.minecraft.world.level.pathfinder.Path;
|
| 76 | import net.minecraft.world.phys.AABB;
|
| 77 | import net.minecraft.world.phys.BlockHitResult;
|
| 78 | import net.minecraft.world.phys.Vec3;
|
| 79 | import org.jspecify.annotations.Nullable;
|
| 80 |
|
| 81 | public class GameTestHelper {
|
| 82 | private final GameTestInfo testInfo;
|
| 83 | private boolean finalCheckAdded;
|
| 84 |
|
| 85 | public GameTestHelper(final GameTestInfo testInfo) {
|
| 86 | this.testInfo = testInfo;
|
| 87 | }
|
| 88 |
|
| 89 | public GameTestAssertException assertionException(final Component description) {
|
| 90 | return new GameTestAssertException(description, this.testInfo.getTick());
|
| 91 | }
|
| 92 |
|
| 93 | public GameTestAssertException assertionException(final String descriptionId, final Object... arguments) {
|
| 94 | return this.assertionException(Component.translatableEscape(descriptionId, arguments));
|
| 95 | }
|
| 96 |
|
| 97 | public GameTestAssertPosException assertionException(final BlockPos pos, final Component description) {
|
| 98 | return new GameTestAssertPosException(description, this.absolutePos(pos), pos, this.testInfo.getTick());
|
| 99 | }
|
| 100 |
|
| 101 | public GameTestAssertPosException assertionException(final BlockPos pos, final String descriptionId, final Object... arguments) {
|
| 102 | return this.assertionException(pos, Component.translatableEscape(descriptionId, arguments));
|
| 103 | }
|
| 104 |
|
| 105 | public ServerLevel getLevel() {
|
| 106 | return this.testInfo.getLevel();
|
| 107 | }
|
| 108 |
|
| 109 | public BlockState getBlockState(final BlockPos pos) {
|
| 110 | return this.getLevel().getBlockState(this.absolutePos(pos));
|
| 111 | }
|
| 112 |
|
| 113 | public <T extends BlockEntity> T getBlockEntity(final BlockPos pos, final Class<T> type) {
|
| 114 | BlockEntity blockEntity = this.getLevel().getBlockEntity(this.absolutePos(pos));
|
| 115 | if (blockEntity == null) {
|
| 116 | throw this.assertionException(pos, "test.error.missing_block_entity");
|
| 117 | } else if (type.isInstance(blockEntity)) {
|
| 118 | return type.cast(blockEntity);
|
| 119 | } else {
|
| 120 | throw this.assertionException(pos, "test.error.wrong_block_entity", blockEntity.typeHolder().getRegisteredName());
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | public void killAllEntities() {
|
| 125 | this.killAllEntitiesOfClass(Entity.class);
|
| 126 | }
|
| 127 |
|
| 128 | public void killAllEntitiesOfClass(final Class<? extends Entity> baseClass) {
|
| 129 | AABB bounds = this.getBounds();
|
| 130 | List<? extends Entity> entities = this.getLevel().getEntitiesOfClass(baseClass, bounds.inflate(1.0), mob -> !(mob instanceof Player));
|
| 131 | entities.forEach(entity -> entity.kill(this.getLevel()));
|
| 132 | }
|
| 133 |
|
| 134 | public ItemEntity spawnItem(final Item item, final Vec3 pos) {
|
| 135 | ServerLevel level = this.getLevel();
|
| 136 | Vec3 absoluteVec = this.absoluteVec(pos);
|
| 137 | ItemEntity itemEntity = new ItemEntity(level, absoluteVec.x, absoluteVec.y, absoluteVec.z, new ItemStack(item, 1));
|
| 138 | itemEntity.setDeltaMovement(0.0, 0.0, 0.0);
|
| 139 | level.addFreshEntity(itemEntity);
|
| 140 | return itemEntity;
|
| 141 | }
|
| 142 |
|
| 143 | public ItemEntity spawnItem(final Item item, final float x, final float y, final float z) {
|
| 144 | return this.spawnItem(item, new Vec3(x, y, z));
|
| 145 | }
|
| 146 |
|
| 147 | public ItemEntity spawnItem(final Item item, final BlockPos pos) {
|
| 148 | return this.spawnItem(item, pos.getX(), pos.getY(), pos.getZ());
|
| 149 | }
|
| 150 |
|
| 151 | public void despawnItem(final BlockPos pos, final double distance) {
|
| 152 | BlockPos absolutePos = this.absolutePos(pos);
|
| 153 |
|
| 154 | for (ItemEntity entity : this.getLevel().getEntities(EntityTypes.ITEM, new AABB(absolutePos).inflate(distance), Entity::isAlive)) {
|
| 155 | entity.remove(Entity.RemovalReason.KILLED);
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | public <E extends Entity> E spawn(final EntityType<E> entityType, final BlockPos pos) {
|
| 160 | return this.spawn(entityType, Vec3.atBottomCenterOf(pos));
|
| 161 | }
|
| 162 |
|
| 163 | public <E extends Entity> List<E> spawn(final EntityType<E> entityType, final BlockPos pos, final int amount) {
|
| 164 | return this.spawn(entityType, Vec3.atBottomCenterOf(pos), amount);
|
| 165 | }
|
| 166 |
|
| 167 | public <E extends Entity> List<E> spawn(final EntityType<E> entityType, final Vec3 pos, final int amount) {
|
| 168 | return this.spawnEntity(entityType, pos).spawn(amount);
|
| 169 | }
|
| 170 |
|
| 171 | public <E extends Entity> E spawn(final EntityType<E> entityType, final Vec3 pos) {
|
| 172 | return this.spawn(entityType, pos, null);
|
| 173 | }
|
| 174 |
|
| 175 | public <E extends Mob> E spawn(final EntityType<E> entityType, final double x, final double y, final double z, final EntitySpawnReason entitySpawnReason) {
|
| 176 | return this.spawn(entityType, new Vec3(x, y, z), entitySpawnReason);
|
| 177 | }
|
| 178 |
|
| 179 | public <E extends Entity> E spawn(final EntityType<E> entityType, final BlockPos pos, final @Nullable EntitySpawnReason spawnReason) {
|
| 180 | return this.spawnEntity(entityType, pos).spawnReason(spawnReason).spawn();
|
| 181 | }
|
| 182 |
|
| 183 | public <E extends Entity> E spawn(final EntityType<E> entityType, final Vec3 pos, final @Nullable EntitySpawnReason spawnReason) {
|
| 184 | return this.spawnEntity(entityType, pos).spawnReason(spawnReason).spawn();
|
| 185 | }
|
| 186 |
|
| 187 | public <E extends Entity> GameTestEntityBuilder<E> spawnEntity(final EntityType<E> entityType, final Vec3 position) {
|
| 188 | return new GameTestEntityBuilder<>(this, entityType, position);
|
| 189 | }
|
| 190 |
|
| 191 | public <E extends Entity> GameTestEntityBuilder<E> spawnEntity(final EntityType<E> entityType, final BlockPos position) {
|
| 192 | return this.spawnEntity(entityType, Vec3.atBottomCenterOf(position));
|
| 193 | }
|
| 194 |
|
| 195 | public <E extends Entity> GameTestEntityBuilder<E> spawnEntity(final EntityType<E> entityType, final int x, final int y, final int z) {
|
| 196 | return this.spawnEntity(entityType, new BlockPos(x, y, z));
|
| 197 | }
|
| 198 |
|
| 199 | public <E extends Entity> GameTestEntityBuilder<E> spawnEntity(final EntityType<E> entityType, final float x, final float y, final float z) {
|
| 200 | return this.spawnEntity(entityType, new Vec3(x, y, z));
|
| 201 | }
|
| 202 |
|
| 203 | public <E extends Mob> GameTestMobBuilder<E> spawnMob(final EntityType<E> entityType, final Vec3 position) {
|
| 204 | return new GameTestMobBuilder<>(this, entityType, position);
|
| 205 | }
|
| 206 |
|
| 207 | public <E extends Mob> GameTestMobBuilder<E> spawnMob(final EntityType<E> entityType, final BlockPos position) {
|
| 208 | return this.spawnMob(entityType, Vec3.atBottomCenterOf(position));
|
| 209 | }
|
| 210 |
|
| 211 | public <E extends Mob> GameTestMobBuilder<E> spawnMob(final EntityType<E> entityType, final int x, final int y, final int z) {
|
| 212 | return this.spawnMob(entityType, new BlockPos(x, y, z));
|
| 213 | }
|
| 214 |
|
| 215 | public <E extends Mob> GameTestMobBuilder<E> spawnMob(final EntityType<E> entityType, final float x, final float y, final float z) {
|
| 216 | return this.spawnMob(entityType, new Vec3(x, y, z));
|
| 217 | }
|
| 218 |
|
| 219 | public void hurt(final Entity entity, final DamageSource source, final float damage) {
|
| 220 | entity.hurtServer(this.getLevel(), source, damage);
|
| 221 | }
|
| 222 |
|
| 223 | public void kill(final Entity entity) {
|
| 224 | entity.kill(this.getLevel());
|
| 225 | }
|
| 226 |
|
| 227 | public void discard(final Entity entity) {
|
| 228 | entity.discard();
|
| 229 | }
|
| 230 |
|
| 231 | public <E extends Entity> E findOneEntity(final EntityType<E> entityType) {
|
| 232 | return this.findClosestEntity(entityType, 0, 0, 0, 2.147483647E9);
|
| 233 | }
|
| 234 |
|
| 235 | public <E extends Entity> E findClosestEntity(final EntityType<E> entityType, final int x, final int y, final int z, final double distance) {
|
| 236 | List<E> entities = this.findEntities(entityType, x, y, z, distance);
|
| 237 | if (entities.isEmpty()) {
|
| 238 | throw this.assertionException("test.error.expected_entity_around", entityType.getDescription(), x, y, z);
|
| 239 | }
|
| 240 |
|
| 241 | if (entities.size() > 1) {
|
| 242 | throw this.assertionException("test.error.too_many_entities", entityType.toShortString(), x, y, z, entities.size());
|
| 243 | }
|
| 244 |
|
| 245 | Vec3 center = this.absoluteVec(new Vec3(x, y, z));
|
| 246 | entities.sort((e1, e2) -> {
|
| 247 | double d1 = e1.position().distanceTo(center);
|
| 248 | double d2 = e2.position().distanceTo(center);
|
| 249 | return Double.compare(d1, d2);
|
| 250 | });
|
| 251 | return entities.get(0);
|
| 252 | }
|
| 253 |
|
| 254 | public <E extends Entity> List<E> findEntities(final EntityType<E> entityType, final int x, final int y, final int z, final double distance) {
|
| 255 | return this.findEntities(entityType, Vec3.atBottomCenterOf(new BlockPos(x, y, z)), distance);
|
| 256 | }
|
| 257 |
|
| 258 | public <E extends Entity> List<E> findEntities(final EntityType<E> entityType, final Vec3 pos, final double distance) {
|
| 259 | ServerLevel level = this.getLevel();
|
| 260 | Vec3 absoluteVec = this.absoluteVec(pos);
|
| 261 | AABB structureBounds = this.testInfo.getStructureBounds();
|
| 262 | AABB containedBounds = new AABB(absoluteVec.add(-distance, -distance, -distance), absoluteVec.add(distance, distance, distance));
|
| 263 | return level.getEntities(entityType, structureBounds, e -> e.getBoundingBox().intersects(containedBounds) && e.isAlive());
|
| 264 | }
|
| 265 |
|
| 266 | public <E extends Entity> E spawn(final EntityType<E> entityType, final int x, final int y, final int z) {
|
| 267 | return this.spawn(entityType, new BlockPos(x, y, z));
|
| 268 | }
|
| 269 |
|
| 270 | public <E extends Entity> E spawn(final EntityType<E> entityType, final float x, final float y, final float z) {
|
| 271 | return this.spawn(entityType, new Vec3(x, y, z));
|
| 272 | }
|
| 273 |
|
| 274 | public <E extends Mob> E spawnWithNoFreeWill(final EntityType<E> entityType, final BlockPos pos) {
|
| 275 | return this.spawnMob(entityType, pos).withNoFreeWill().spawn();
|
| 276 | }
|
| 277 |
|
| 278 | public <E extends Mob> E spawnWithNoFreeWill(final EntityType<E> entityType, final int x, final int y, final int z) {
|
| 279 | return this.spawnWithNoFreeWill(entityType, new BlockPos(x, y, z));
|
| 280 | }
|
| 281 |
|
| 282 | public <E extends Mob> E spawnWithNoFreeWill(final EntityType<E> entityType, final Vec3 pos) {
|
| 283 | E entity = (E)this.spawn(entityType, pos);
|
| 284 | entity.removeFreeWill();
|
| 285 | return entity;
|
| 286 | }
|
| 287 |
|
| 288 | public <E extends Mob> E spawnWithNoFreeWill(final EntityType<E> entityType, final float x, final float y, final float z) {
|
| 289 | return this.spawnWithNoFreeWill(entityType, new Vec3(x, y, z));
|
| 290 | }
|
| 291 |
|
| 292 | public void moveTo(final Mob mob, final float x, final float y, final float z) {
|
| 293 | this.moveTo(mob, new Vec3(x, y, z));
|
| 294 | }
|
| 295 |
|
| 296 | public void moveTo(final Mob mob, final BlockPos pos) {
|
| 297 | this.moveTo(mob, Vec3.atBottomCenterOf(pos));
|
| 298 | }
|
| 299 |
|
| 300 | public void moveTo(final Mob mob, final Vec3 pos) {
|
| 301 | Vec3 absoluteVec = this.absoluteVec(pos);
|
| 302 | mob.snapTo(absoluteVec.x, absoluteVec.y, absoluteVec.z, mob.getYRot(), mob.getXRot());
|
| 303 | }
|
| 304 |
|
| 305 | public GameTestSequence walkTo(final Mob mob, final BlockPos targetPos, final float speedModifier) {
|
| 306 | return this.startSequence().thenExecuteAfter(2, () -> {
|
| 307 | Path path = mob.getNavigation().createPath(this.absolutePos(targetPos), 0);
|
| 308 | mob.getNavigation().moveTo(path, speedModifier);
|
| 309 | });
|
| 310 | }
|
| 311 |
|
| 312 | public void pressButton(final int x, final int y, final int z) {
|
| 313 | this.pressButton(new BlockPos(x, y, z));
|
| 314 | }
|
| 315 |
|
| 316 | public void pressButton(final BlockPos buttonPos) {
|
| 317 | this.assertBlockTag(BlockTags.BUTTONS, buttonPos);
|
| 318 | BlockPos absolutePos = this.absolutePos(buttonPos);
|
| 319 | BlockState blockState = this.getLevel().getBlockState(absolutePos);
|
| 320 | ButtonBlock buttonBlock = (ButtonBlock)blockState.getBlock();
|
| 321 | buttonBlock.press(blockState, this.getLevel(), absolutePos, null);
|
| 322 | }
|
| 323 |
|
| 324 | public void useBlock(final BlockPos relativePos) {
|
| 325 | this.useBlock(relativePos, this.makeMockPlayer(GameType.CREATIVE));
|
| 326 | }
|
| 327 |
|
| 328 | public void useBlock(final BlockPos relativePos, final Player player) {
|
| 329 | BlockPos absolutePos = this.absolutePos(relativePos);
|
| 330 | this.useBlock(relativePos, player, new BlockHitResult(Vec3.atCenterOf(absolutePos), Direction.NORTH, absolutePos, true));
|
| 331 | }
|
| 332 |
|
| 333 | public void useBlock(final BlockPos relativePos, final Player player, final BlockHitResult hitResult) {
|
| 334 | BlockPos absolutePos = this.absolutePos(relativePos);
|
| 335 | BlockState blockState = this.getLevel().getBlockState(absolutePos);
|
| 336 | InteractionHand hand = InteractionHand.MAIN_HAND;
|
| 337 | InteractionResult itemInteractionResult = blockState.useItemOn(player.getItemInHand(hand), this.getLevel(), player, hand, hitResult);
|
| 338 | if (!itemInteractionResult.consumesAction()) {
|
| 339 | if (!(itemInteractionResult instanceof InteractionResult.TryEmptyHandInteraction)
|
| 340 | || !blockState.useWithoutItem(this.getLevel(), player, hitResult).consumesAction()) {
|
| 341 | UseOnContext context = new UseOnContext(player, hand, hitResult);
|
| 342 | player.getItemInHand(hand).useOn(context);
|
| 343 | }
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | public LivingEntity makeAboutToDrown(final LivingEntity entity) {
|
| 348 | entity.setAirSupply(0);
|
| 349 | entity.setHealth(0.25F);
|
| 350 | return entity;
|
| 351 | }
|
| 352 |
|
| 353 | public LivingEntity withLowHealth(final LivingEntity entity) {
|
| 354 | entity.setHealth(0.25F);
|
| 355 | return entity;
|
| 356 | }
|
| 357 |
|
| 358 | public Player makeMockPlayer(final GameType gameType) {
|
| 359 | return new Player(this.getLevel(), new GameProfile(UUID.randomUUID(), "test-mock-player")) {
|
| 360 | @Override
|
| 361 | public GameType gameMode() {
|
| 362 | return gameType;
|
| 363 | }
|
| 364 |
|
| 365 | @Override
|
| 366 | public boolean isClientAuthoritative() {
|
| 367 | return false;
|
| 368 | }
|
| 369 | };
|
| 370 | }
|
| 371 |
|
| 372 | public Player makeMockServerPlayer(final GameType gameType) {
|
| 373 | var player = new ServerPlayer(
|
| 374 | this.getLevel().getServer(), this.getLevel(), new GameProfile(UUID.randomUUID(), "test-mock-player"), ClientInformation.createDefault()
|
| 375 | ) {
|
| 376 | @Override
|
| 377 | public GameType gameMode() {
|
| 378 | return gameType;
|
| 379 | }
|
| 380 |
|
| 381 | @Override
|
| 382 | public boolean isClientAuthoritative() {
|
| 383 | return false;
|
| 384 | }
|
| 385 | };
|
| 386 | gameType.updatePlayerAbilities(player.getAbilities());
|
| 387 | return player;
|
| 388 | }
|
| 389 |
|
| 390 | public void rotateEntityWithTest(final Entity entity) {
|
| 391 | float yRot = this.getTestRotation().rotate(Direction.SOUTH).toYRot();
|
| 392 | entity.setYRot(yRot);
|
| 393 | }
|
| 394 |
|
| 395 | @Deprecated(forRemoval = true)
|
| 396 | public ServerPlayer makeMockServerPlayerInLevel() {
|
| 397 | CommonListenerCookie cookie = CommonListenerCookie.createInitial(new GameProfile(UUID.randomUUID(), "test-mock-player"), false);
|
| 398 | ServerPlayer player = new ServerPlayer(this.getLevel().getServer(), this.getLevel(), cookie.gameProfile(), cookie.clientInformation()) {
|
| 399 | @Override
|
| 400 | public GameType gameMode() {
|
| 401 | return GameType.CREATIVE;
|
| 402 | }
|
| 403 | };
|
| 404 | Connection connection = new Connection(PacketFlow.SERVERBOUND);
|
| 405 | new EmbeddedChannel(connection);
|
| 406 | this.getLevel().getServer().getPlayerList().placeNewPlayer(connection, player, cookie);
|
| 407 | return player;
|
| 408 | }
|
| 409 |
|
| 410 | public void pullLever(final int x, final int y, final int z) {
|
| 411 | this.pullLever(new BlockPos(x, y, z));
|
| 412 | }
|
| 413 |
|
| 414 | public void pullLever(final BlockPos leverPos) {
|
| 415 | this.assertBlockPresent(Blocks.LEVER, leverPos);
|
| 416 | BlockPos absolutePos = this.absolutePos(leverPos);
|
| 417 | BlockState blockState = this.getLevel().getBlockState(absolutePos);
|
| 418 | LeverBlock leverBlock = (LeverBlock)blockState.getBlock();
|
| 419 | leverBlock.pull(blockState, this.getLevel(), absolutePos, null);
|
| 420 | }
|
| 421 |
|
| 422 | public void placeBlock(final BlockPos relativePos, final Block block, final Direction relativePlaceDirection) {
|
| 423 | BlockPos pos = this.absolutePos(relativePos);
|
| 424 | Direction placeDirection = this.getAbsoluteDirection(relativePlaceDirection);
|
| 425 | Item item = block.asItem();
|
| 426 | if (item instanceof BlockItem blockItem) {
|
| 427 | BlockHitResult hitResult = new BlockHitResult(Vec3.atCenterOf(pos), Direction.DOWN, pos, true);
|
| 428 | BlockPlaceContext context = new GameTestHelper.TestBlockPlaceContext(
|
| 429 | this.getLevel(), InteractionHand.MAIN_HAND, item.getDefaultInstance(), hitResult, placeDirection
|
| 430 | );
|
| 431 | blockItem.place(context);
|
| 432 | } else {
|
| 433 | this.fail(Component.translatable("test.error.not_a_block_item"));
|
| 434 | }
|
| 435 | }
|
| 436 |
|
| 437 | public void placeBlock(final int x, final int y, final int z, final Block block, final Direction placeDirection) {
|
| 438 | this.placeBlock(new BlockPos(x, y, z), block, placeDirection);
|
| 439 | }
|
| 440 |
|
| 441 | public void pulseRedstone(final BlockPos pos, final long duration) {
|
| 442 | this.setBlock(pos, Blocks.REDSTONE_BLOCK);
|
| 443 | this.runAfterDelay(duration, () -> this.setBlock(pos, Blocks.AIR));
|
| 444 | }
|
| 445 |
|
| 446 | public void destroyBlock(final BlockPos pos) {
|
| 447 | this.getLevel().destroyBlock(this.absolutePos(pos), false, null);
|
| 448 | }
|
| 449 |
|
| 450 | public void setBlock(final int x, final int y, final int z, final Block block) {
|
| 451 | this.setBlock(new BlockPos(x, y, z), block);
|
| 452 | }
|
| 453 |
|
| 454 | public void setBlock(final int x, final int y, final int z, final BlockState state) {
|
| 455 | this.setBlock(new BlockPos(x, y, z), state);
|
| 456 | }
|
| 457 |
|
| 458 | public void setBlock(final BlockPos blockPos, final Block block) {
|
| 459 | this.setBlock(blockPos, block.defaultBlockState());
|
| 460 | }
|
| 461 |
|
| 462 | public void setBlock(final BlockPos blockPos, final BlockState state) {
|
| 463 | this.getLevel().setBlockAndUpdate(this.absolutePos(blockPos), state);
|
| 464 | }
|
| 465 |
|
| 466 | public void setBlock(final BlockPos blockPos, final Block block, final Direction direction) {
|
| 467 | this.setBlock(blockPos, block.defaultBlockState(), direction);
|
| 468 | }
|
| 469 |
|
| 470 | public void setBlock(final BlockPos blockPos, final BlockState blockState, final Direction direction) {
|
| 471 | BlockState state = blockState;
|
| 472 | if (blockState.hasProperty(HorizontalDirectionalBlock.FACING)) {
|
| 473 | state = blockState.setValue(HorizontalDirectionalBlock.FACING, direction);
|
| 474 | }
|
| 475 |
|
| 476 | if (blockState.hasProperty(BlockStateProperties.FACING)) {
|
| 477 | state = blockState.setValue(BlockStateProperties.FACING, direction);
|
| 478 | }
|
| 479 |
|
| 480 | this.getLevel().setBlockAndUpdate(this.absolutePos(blockPos), state);
|
| 481 | }
|
| 482 |
|
| 483 | public void placeAt(final Player player, final ItemStack blockStack, final BlockPos pos, final Direction face) {
|
| 484 | BlockPos absolute = this.absolutePos(pos.relative(face));
|
| 485 | BlockHitResult hitResult = new BlockHitResult(Vec3.atCenterOf(absolute), face, absolute, false);
|
| 486 | UseOnContext context = new UseOnContext(player, InteractionHand.MAIN_HAND, hitResult);
|
| 487 | blockStack.useOn(context);
|
| 488 | }
|
| 489 |
|
| 490 | public void useItemOnBlock(final Player player, final Item item, final BlockPos pos) {
|
| 491 | Vec3 playerStandPos = this.absoluteVec(Vec3.atBottomCenterOf(pos));
|
| 492 | Vec3 playerLookPos = this.absoluteVec(Vec3.atCenterOf(pos));
|
| 493 | player.setPos(playerStandPos.x, playerStandPos.y, playerStandPos.z);
|
| 494 | player.lookAt(EntityAnchorArgument.Anchor.EYES, playerLookPos);
|
| 495 | item.use(this.getLevel(), player, InteractionHand.MAIN_HAND);
|
| 496 | }
|
| 497 |
|
| 498 | public void assertBlockPresent(final Block blockType, final int x, final int y, final int z) {
|
| 499 | this.assertBlockPresent(blockType, new BlockPos(x, y, z));
|
| 500 | }
|
| 501 |
|
| 502 | public void assertBlockPresent(final Block blockType, final BlockPos pos) {
|
| 503 | BlockState state = this.getBlockState(pos);
|
| 504 | this.assertBlock(pos, block -> state.is(blockType), block -> Component.translatable("test.error.expected_block", blockType.getName(), block.getName()));
|
| 505 | }
|
| 506 |
|
| 507 | public void assertBlockPresent(final Block blockType) {
|
| 508 | AABB aabb = this.getRelativeBounds().contract(1.0, 1.0, 1.0);
|
| 509 | boolean foundBlock = BlockPos.MutableBlockPos.betweenClosedStream(aabb).anyMatch(blockPos -> this.getBlockState(blockPos).is(blockType));
|
| 510 | if (!foundBlock) {
|
| 511 | throw this.assertionException(Component.translatable("test.error.expected_block_present", blockType.getName()));
|
| 512 | }
|
| 513 | }
|
| 514 |
|
| 515 | public void assertBlockNotPresent(final Block blockType, final int x, final int y, final int z) {
|
| 516 | this.assertBlockNotPresent(blockType, new BlockPos(x, y, z));
|
| 517 | }
|
| 518 |
|
| 519 | public void assertBlockNotPresent(final Block blockType, final BlockPos pos) {
|
| 520 | this.assertBlock(pos, block -> !this.getBlockState(pos).is(blockType), block -> Component.translatable("test.error.unexpected_block", blockType.getName()));
|
| 521 | }
|
| 522 |
|
| 523 | public void assertBlockTag(final TagKey<Block> tag, final BlockPos pos) {
|
| 524 | this.assertBlockState(
|
| 525 | pos,
|
| 526 | state -> state.is(tag),
|
| 527 | state -> Component.translatable("test.error.expected_block_tag", Component.translationArg(tag.location()), state.getBlock().getName())
|
| 528 | );
|
| 529 | }
|
| 530 |
|
| 531 | public void succeedWhenBlockPresent(final Block block, final int x, final int y, final int z) {
|
| 532 | this.succeedWhenBlockPresent(block, new BlockPos(x, y, z));
|
| 533 | }
|
| 534 |
|
| 535 | public void succeedWhenBlockPresent(final Block block, final BlockPos pos) {
|
| 536 | this.succeedWhen(() -> this.assertBlockPresent(block, pos));
|
| 537 | }
|
| 538 |
|
| 539 | public void assertBlock(final BlockPos pos, final Predicate<Block> predicate, final Function<Block, Component> errorMessage) {
|
| 540 | this.assertBlockState(pos, blockState -> predicate.test(blockState.getBlock()), state -> errorMessage.apply(state.getBlock()));
|
| 541 | }
|
| 542 |
|
| 543 | public <T extends Comparable<T>> void assertBlockProperty(final BlockPos pos, final Property<T> property, final T value) {
|
| 544 | BlockState blockState = this.getBlockState(pos);
|
| 545 | boolean hasProperty = blockState.hasProperty(property);
|
| 546 | if (!hasProperty) {
|
| 547 | throw this.assertionException(pos, "test.error.block_property_missing", property.getName(), value);
|
| 548 | }
|
| 549 |
|
| 550 | if (!blockState.<T>getValue(property).equals(value)) {
|
| 551 | throw this.assertionException(pos, "test.error.block_property_mismatch", property.getName(), value, blockState.getValue(property));
|
| 552 | }
|
| 553 | }
|
| 554 |
|
| 555 | public <T extends Comparable<T>> void assertBlockProperty(
|
| 556 | final BlockPos pos, final Property<T> property, final Predicate<T> predicate, final Component errorMessage
|
| 557 | ) {
|
| 558 | this.assertBlockState(pos, blockState -> {
|
| 559 | if (!blockState.hasProperty(property)) {
|
| 560 | return false;
|
| 561 | }
|
| 562 |
|
| 563 | T value = blockState.getValue(property);
|
| 564 | return predicate.test(value);
|
| 565 | }, state -> errorMessage);
|
| 566 | }
|
| 567 |
|
| 568 | public void assertBlockState(final BlockPos pos, final BlockState expected) {
|
| 569 | BlockState blockState = this.getBlockState(pos);
|
| 570 | if (!blockState.equals(expected)) {
|
| 571 | throw this.assertionException(pos, "test.error.state_not_equal", expected, blockState);
|
| 572 | }
|
| 573 | }
|
| 574 |
|
| 575 | public void assertBlockState(final BlockPos pos, final Predicate<BlockState> predicate, final Function<BlockState, Component> errorMessage) {
|
| 576 | BlockState blockState = this.getBlockState(pos);
|
| 577 | if (!predicate.test(blockState)) {
|
| 578 | throw this.assertionException(pos, errorMessage.apply(blockState));
|
| 579 | }
|
| 580 | }
|
| 581 |
|
| 582 | public <T extends BlockEntity> void assertBlockEntityData(
|
| 583 | final BlockPos pos, final Class<T> type, final Predicate<T> predicate, final Supplier<Component> errorMessage
|
| 584 | ) {
|
| 585 | T blockEntity = this.getBlockEntity(pos, type);
|
| 586 | if (!predicate.test(blockEntity)) {
|
| 587 | throw this.assertionException(pos, errorMessage.get());
|
| 588 | }
|
| 589 | }
|
| 590 |
|
| 591 | public void assertRedstoneSignal(final BlockPos pos, final Direction direction, final IntPredicate levelPredicate, final Supplier<Component> errorMessage) {
|
| 592 | BlockPos blockPos = this.absolutePos(pos);
|
| 593 | ServerLevel level = this.getLevel();
|
| 594 | BlockState blockState = level.getBlockState(blockPos);
|
| 595 | int signal = blockState.getSignal(level, blockPos, direction);
|
| 596 | if (!levelPredicate.test(signal)) {
|
| 597 | throw this.assertionException(pos, errorMessage.get());
|
| 598 | }
|
| 599 | }
|
| 600 |
|
| 601 | public void assertEntityPresent(final EntityType<?> entityType) {
|
| 602 | if (!this.getLevel().hasEntities(entityType, this.getBounds(), Entity::isAlive)) {
|
| 603 | throw this.assertionException("test.error.expected_entity_in_test", entityType.getDescription());
|
| 604 | }
|
| 605 | }
|
| 606 |
|
| 607 | public void assertEntityPresent(final EntityType<?> entityType, final int x, final int y, final int z) {
|
| 608 | this.assertEntityPresent(entityType, new BlockPos(x, y, z));
|
| 609 | }
|
| 610 |
|
| 611 | public void assertEntityPresent(final EntityType<?> entityType, final BlockPos pos) {
|
| 612 | BlockPos absolutePos = this.absolutePos(pos);
|
| 613 | if (!this.getLevel().hasEntities(entityType, new AABB(absolutePos), Entity::isAlive)) {
|
| 614 | throw this.assertionException(pos, "test.error.expected_entity", entityType.getDescription());
|
| 615 | }
|
| 616 | }
|
| 617 |
|
| 618 | public void assertEntityPresent(final EntityType<?> entityType, final AABB relativeAABB) {
|
| 619 | AABB absoluteAABB = this.absoluteAABB(relativeAABB);
|
| 620 | if (!this.getLevel().hasEntities(entityType, absoluteAABB, Entity::isAlive)) {
|
| 621 | throw this.assertionException(BlockPos.containing(relativeAABB.getCenter()), "test.error.expected_entity", entityType.getDescription());
|
| 622 | }
|
| 623 | }
|
| 624 |
|
| 625 | public void assertEntityPresent(final EntityType<?> entityType, final AABB relativeAABB, final Component message) {
|
| 626 | AABB absoluteAABB = this.absoluteAABB(relativeAABB);
|
| 627 | if (!this.getLevel().hasEntities(entityType, absoluteAABB, Entity::isAlive)) {
|
| 628 | throw this.assertionException(BlockPos.containing(relativeAABB.getCenter()), message);
|
| 629 | }
|
| 630 | }
|
| 631 |
|
| 632 | public void assertEntitiesPresent(final EntityType<?> entityType, final int expectedEntities) {
|
| 633 | List<? extends Entity> entities = this.getLevel().getEntities(entityType, this.getBounds(), Entity::isAlive);
|
| 634 | if (entities.size() != expectedEntities) {
|
| 635 | throw this.assertionException("test.error.expected_entity_count", expectedEntities, entityType.getDescription(), entities.size());
|
| 636 | }
|
| 637 | }
|
| 638 |
|
| 639 | public void assertEntitiesPresent(final EntityType<?> entityType, final BlockPos pos, final int numOfExpectedEntities, final double distance) {
|
| 640 | BlockPos absolutePos = this.absolutePos(pos);
|
| 641 | List<? extends Entity> entities = this.getEntities((EntityType<? extends Entity>)entityType, pos, distance);
|
| 642 | if (entities.size() != numOfExpectedEntities) {
|
| 643 | throw this.assertionException(pos, "test.error.expected_entity_count", numOfExpectedEntities, entityType.getDescription(), entities.size());
|
| 644 | }
|
| 645 | }
|
| 646 |
|
| 647 | public void assertEntityPresent(final EntityType<?> entityType, final BlockPos pos, final double distance) {
|
| 648 | List<? extends Entity> entities = this.getEntities((EntityType<? extends Entity>)entityType, pos, distance);
|
| 649 | if (entities.isEmpty()) {
|
| 650 | BlockPos absolutePos = this.absolutePos(pos);
|
| 651 | throw this.assertionException(pos, "test.error.expected_entity", entityType.getDescription());
|
| 652 | }
|
| 653 | }
|
| 654 |
|
| 655 | public <T extends Entity> List<T> getEntities(final EntityType<T> entityType, final BlockPos pos, final double distance) {
|
| 656 | BlockPos absolutePos = this.absolutePos(pos);
|
| 657 | return this.getLevel().getEntities(entityType, new AABB(absolutePos).inflate(distance), Entity::isAlive);
|
| 658 | }
|
| 659 |
|
| 660 | public <T extends Entity> List<T> getEntities(final EntityType<T> entityType) {
|
| 661 | return this.getLevel().getEntities(entityType, this.getBounds(), Entity::isAlive);
|
| 662 | }
|
| 663 |
|
| 664 | public void assertEntityInstancePresent(final Entity entity, final int x, final int y, final int z) {
|
| 665 | this.assertEntityInstancePresent(entity, new BlockPos(x, y, z));
|
| 666 | }
|
| 667 |
|
| 668 | public void assertEntityInstancePresent(final Entity entity, final BlockPos pos) {
|
| 669 | this.assertEntityInstancePresent(entity, pos, 0.0);
|
| 670 | }
|
| 671 |
|
| 672 | public void assertEntityInstancePresent(final Entity entity, final BlockPos pos, final double inflate) {
|
| 673 | BlockPos absolutePos = this.absolutePos(pos);
|
| 674 | List<? extends Entity> entities = this.getLevel().getEntities(entity.getType(), new AABB(absolutePos).inflate(inflate), Entity::isAlive);
|
| 675 | entities.stream()
|
| 676 | .filter(it -> it == entity)
|
| 677 | .findFirst()
|
| 678 | .orElseThrow(() -> this.assertionException(pos, "test.error.expected_entity", entity.getType().getDescription()));
|
| 679 | }
|
| 680 |
|
| 681 | public void assertItemEntityCountIs(final Item itemType, final BlockPos pos, final double distance, final int count) {
|
| 682 | BlockPos absolutePos = this.absolutePos(pos);
|
| 683 | List<ItemEntity> entities = this.getLevel().getEntities(EntityTypes.ITEM, new AABB(absolutePos).inflate(distance), Entity::isAlive);
|
| 684 | int num = 0;
|
| 685 |
|
| 686 | for (ItemEntity entity : entities) {
|
| 687 | ItemStack itemStack = entity.getItem();
|
| 688 | if (itemStack.is(itemType)) {
|
| 689 | num += itemStack.getCount();
|
| 690 | }
|
| 691 | }
|
| 692 |
|
| 693 | if (num != count) {
|
| 694 | throw this.assertionException(pos, "test.error.expected_items_count", count, getItemName(itemType), num);
|
| 695 | }
|
| 696 | }
|
| 697 |
|
| 698 | public void assertItemEntityPresent(final Item itemType, final BlockPos pos, final double distance) {
|
| 699 | BlockPos absolutePos = this.absolutePos(pos);
|
| 700 | Predicate<ItemEntity> isSameItem = entity -> entity.isAlive() && entity.getItem().is(itemType);
|
| 701 | if (!this.getLevel().hasEntities(EntityTypes.ITEM, new AABB(absolutePos).inflate(distance), isSameItem)) {
|
| 702 | throw this.assertionException(pos, "test.error.expected_item", getItemName(itemType));
|
| 703 | }
|
| 704 | }
|
| 705 |
|
| 706 | public void assertItemEntityNotPresent(final Item itemType, final BlockPos pos, final double distance) {
|
| 707 | BlockPos absolutePos = this.absolutePos(pos);
|
| 708 | Predicate<ItemEntity> isSameItem = entity -> entity.isAlive() && entity.getItem().is(itemType);
|
| 709 | if (this.getLevel().hasEntities(EntityTypes.ITEM, new AABB(absolutePos).inflate(distance), isSameItem)) {
|
| 710 | throw this.assertionException(pos, "test.error.unexpected_item", getItemName(itemType));
|
| 711 | }
|
| 712 | }
|
| 713 |
|
| 714 | public void assertItemEntityPresent(final Item itemType) {
|
| 715 | Predicate<ItemEntity> isSameItem = entity -> entity.isAlive() && entity.getItem().is(itemType);
|
| 716 | if (!this.getLevel().hasEntities(EntityTypes.ITEM, this.getBounds(), isSameItem)) {
|
| 717 | throw this.assertionException("test.error.expected_item", getItemName(itemType));
|
| 718 | }
|
| 719 | }
|
| 720 |
|
| 721 | public void assertItemEntityNotPresent(final Item itemType) {
|
| 722 | Predicate<ItemEntity> isSameItem = entity -> entity.isAlive() && entity.getItem().is(itemType);
|
| 723 | if (this.getLevel().hasEntities(EntityTypes.ITEM, this.getBounds(), isSameItem)) {
|
| 724 | throw this.assertionException("test.error.unexpected_item", getItemName(itemType));
|
| 725 | }
|
| 726 | }
|
| 727 |
|
| 728 | public void assertEntityNotPresent(final EntityType<?> entityType) {
|
| 729 | List<? extends Entity> entities = this.getLevel().getEntities(entityType, this.getBounds(), Entity::isAlive);
|
| 730 | if (!entities.isEmpty()) {
|
| 731 | throw this.assertionException(entities.getFirst().blockPosition(), "test.error.unexpected_entity", entityType.getDescription());
|
| 732 | }
|
| 733 | }
|
| 734 |
|
| 735 | public void assertEntityNotPresent(final EntityType<?> entityType, final int x, final int y, final int z) {
|
| 736 | this.assertEntityNotPresent(entityType, new BlockPos(x, y, z));
|
| 737 | }
|
| 738 |
|
| 739 | public void assertEntityNotPresent(final EntityType<?> entityType, final BlockPos pos) {
|
| 740 | BlockPos absolutePos = this.absolutePos(pos);
|
| 741 | if (this.getLevel().hasEntities(entityType, new AABB(absolutePos), Entity::isAlive)) {
|
| 742 | throw this.assertionException(pos, "test.error.unexpected_entity", entityType.getDescription());
|
| 743 | }
|
| 744 | }
|
| 745 |
|
| 746 | public void assertEntityNotPresent(final EntityType<?> entityType, final AABB relativeAABB) {
|
| 747 | AABB absoluteAABB = this.absoluteAABB(relativeAABB);
|
| 748 | List<? extends Entity> entities = this.getLevel().getEntities(entityType, absoluteAABB, Entity::isAlive);
|
| 749 | if (!entities.isEmpty()) {
|
| 750 | throw this.assertionException(entities.getFirst().blockPosition(), "test.error.unexpected_entity", entityType.getDescription());
|
| 751 | }
|
| 752 | }
|
| 753 |
|
| 754 | public void assertEntityTouching(final EntityType<?> entityType, final double x, final double y, final double z) {
|
| 755 | Vec3 vec = new Vec3(x, y, z);
|
| 756 | Vec3 absoluteVec = this.absoluteVec(vec);
|
| 757 | Predicate<? super Entity> predicate = e -> e.getBoundingBox().intersects(absoluteVec, absoluteVec);
|
| 758 | if (!this.getLevel().hasEntities(entityType, this.getBounds(), predicate)) {
|
| 759 | throw this.assertionException("test.error.expected_entity_touching", entityType.getDescription(), absoluteVec.x(), absoluteVec.y(), absoluteVec.z(), x, y, z);
|
| 760 | }
|
| 761 | }
|
| 762 |
|
| 763 | public void assertEntityNotTouching(final EntityType<?> entityType, final double x, final double y, final double z) {
|
| 764 | Vec3 vec = new Vec3(x, y, z);
|
| 765 | Vec3 absoluteVec = this.absoluteVec(vec);
|
| 766 | Predicate<? super Entity> predicate = e -> !e.getBoundingBox().intersects(absoluteVec, absoluteVec);
|
| 767 | if (!this.getLevel().hasEntities(entityType, this.getBounds(), predicate)) {
|
| 768 | throw this.assertionException(
|
| 769 | "test.error.expected_entity_not_touching", entityType.getDescription(), absoluteVec.x(), absoluteVec.y(), absoluteVec.z(), x, y, z
|
| 770 | );
|
| 771 | }
|
| 772 | }
|
| 773 |
|
| 774 | public <E extends Entity, T> void assertEntityData(final BlockPos pos, final EntityType<E> entityType, final Predicate<E> test) {
|
| 775 | BlockPos absolutePos = this.absolutePos(pos);
|
| 776 | List<E> entities = this.getLevel().getEntities(entityType, new AABB(absolutePos), Entity::isAlive);
|
| 777 | if (entities.isEmpty()) {
|
| 778 | throw this.assertionException(pos, "test.error.expected_entity", entityType.getDescription());
|
| 779 | }
|
| 780 |
|
| 781 | for (E entity : entities) {
|
| 782 | if (!test.test(entity)) {
|
| 783 | throw this.assertionException(entity.blockPosition(), "test.error.expected_entity_data_predicate", entity.getName());
|
| 784 | }
|
| 785 | }
|
| 786 | }
|
| 787 |
|
| 788 | public <E extends Entity, T> void assertEntityData(
|
| 789 | final BlockPos pos, final EntityType<E> entityType, final Function<? super E, T> dataAccessor, final @Nullable T data
|
| 790 | ) {
|
| 791 | this.assertEntityData(new AABB(pos), entityType, dataAccessor, data);
|
| 792 | }
|
| 793 |
|
| 794 | public <E extends Entity, T> void assertEntityData(
|
| 795 | final AABB box, final EntityType<E> entityType, final Function<? super E, T> dataAccessor, final @Nullable T data
|
| 796 | ) {
|
| 797 | List<E> entities = this.getLevel().getEntities(entityType, this.absoluteAABB(box), Entity::isAlive);
|
| 798 | if (entities.isEmpty()) {
|
| 799 | throw this.assertionException(BlockPos.containing(box.getBottomCenter()), "test.error.expected_entity", entityType.getDescription());
|
| 800 | }
|
| 801 |
|
| 802 | for (E entity : entities) {
|
| 803 | T actual = dataAccessor.apply(entity);
|
| 804 | if (!Objects.equals(actual, data)) {
|
| 805 | throw this.assertionException(BlockPos.containing(box.getBottomCenter()), "test.error.expected_entity_data", data, actual);
|
| 806 | }
|
| 807 | }
|
| 808 | }
|
| 809 |
|
| 810 | public <E extends LivingEntity> void assertEntityIsHolding(final BlockPos pos, final EntityType<E> entityType, final Item item) {
|
| 811 | BlockPos absolutePos = this.absolutePos(pos);
|
| 812 | List<E> entities = this.getLevel().getEntities(entityType, new AABB(absolutePos), Entity::isAlive);
|
| 813 | if (entities.isEmpty()) {
|
| 814 | throw this.assertionException(pos, "test.error.expected_entity", entityType.getDescription());
|
| 815 | }
|
| 816 |
|
| 817 | for (E entity : entities) {
|
| 818 | if (entity.isHolding(item)) {
|
| 819 | return;
|
| 820 | }
|
| 821 | }
|
| 822 |
|
| 823 | throw this.assertionException(pos, "test.error.expected_entity_holding", getItemName(item));
|
| 824 | }
|
| 825 |
|
| 826 | public <E extends Entity & InventoryCarrier> void assertEntityInventoryContains(final BlockPos pos, final EntityType<E> entityType, final Item item) {
|
| 827 | BlockPos absolutePos = this.absolutePos(pos);
|
| 828 | List<E> entities = this.getLevel().getEntities(entityType, new AABB(absolutePos), rec$ -> rec$.isAlive());
|
| 829 | if (entities.isEmpty()) {
|
| 830 | throw this.assertionException(pos, "test.error.expected_entity", entityType.getDescription());
|
| 831 | }
|
| 832 |
|
| 833 | for (E entity : entities) {
|
| 834 | if (entity.getInventory().hasAnyMatching(itemStack -> itemStack.is(item))) {
|
| 835 | return;
|
| 836 | }
|
| 837 | }
|
| 838 |
|
| 839 | throw this.assertionException(pos, "test.error.expected_entity_having", getItemName(item));
|
| 840 | }
|
| 841 |
|
| 842 | public void assertContainerEmpty(final BlockPos pos) {
|
| 843 | BaseContainerBlockEntity container = this.getBlockEntity(pos, BaseContainerBlockEntity.class);
|
| 844 | if (!container.isEmpty()) {
|
| 845 | throw this.assertionException(pos, "test.error.expected_empty_container");
|
| 846 | }
|
| 847 | }
|
| 848 |
|
| 849 | public void assertContainerContainsSingle(final BlockPos pos, final Item item) {
|
| 850 | BaseContainerBlockEntity container = this.getBlockEntity(pos, BaseContainerBlockEntity.class);
|
| 851 | if (container.countItem(item) != 1) {
|
| 852 | throw this.assertionException(pos, "test.error.expected_container_contents_single", getItemName(item));
|
| 853 | }
|
| 854 | }
|
| 855 |
|
| 856 | public void assertContainerContains(final BlockPos pos, final Item item) {
|
| 857 | BaseContainerBlockEntity container = this.getBlockEntity(pos, BaseContainerBlockEntity.class);
|
| 858 | if (container.countItem(item) == 0) {
|
| 859 | throw this.assertionException(pos, "test.error.expected_container_contents", getItemName(item));
|
| 860 | }
|
| 861 | }
|
| 862 |
|
| 863 | public void assertSameBlockStates(final BoundingBox sourceBoundingBox, final BlockPos targetBoundingBoxCorner) {
|
| 864 | BlockPos.betweenClosedStream(sourceBoundingBox)
|
| 865 | .forEach(
|
| 866 | sourcePos -> {
|
| 867 | BlockPos targetPos = targetBoundingBoxCorner.offset(
|
| 868 | sourcePos.getX() - sourceBoundingBox.minX(), sourcePos.getY() - sourceBoundingBox.minY(), sourcePos.getZ() - sourceBoundingBox.minZ()
|
| 869 | );
|
| 870 | this.assertSameBlockState(sourcePos, targetPos);
|
| 871 | }
|
| 872 | );
|
| 873 | }
|
| 874 |
|
| 875 | public void assertSameBlockState(final BlockPos sourcePos, final BlockPos targetPos) {
|
| 876 | BlockState sourceState = this.getBlockState(sourcePos);
|
| 877 | BlockState targetState = this.getBlockState(targetPos);
|
| 878 | if (sourceState != targetState) {
|
| 879 | throw this.assertionException(sourcePos, "test.error.state_not_equal", targetState, sourceState);
|
| 880 | }
|
| 881 | }
|
| 882 |
|
| 883 | public void assertAtTickTimeContainerContains(final long time, final BlockPos pos, final Item item) {
|
| 884 | this.runAtTickTime(time, () -> this.assertContainerContainsSingle(pos, item));
|
| 885 | }
|
| 886 |
|
| 887 | public void assertAtTickTimeContainerEmpty(final long time, final BlockPos pos) {
|
| 888 | this.runAtTickTime(time, () -> this.assertContainerEmpty(pos));
|
| 889 | }
|
| 890 |
|
| 891 | public <E extends Entity, T> void succeedWhenEntityData(final BlockPos pos, final EntityType<E> entityType, final Function<E, T> dataAccessor, final T data) {
|
| 892 | this.succeedWhen(() -> this.assertEntityData(pos, entityType, dataAccessor, data));
|
| 893 | }
|
| 894 |
|
| 895 | public <E extends Entity> void assertEntityProperty(final E entity, final Predicate<E> test, final Component description) {
|
| 896 | if (!test.test(entity)) {
|
| 897 | throw this.assertionException(entity.blockPosition(), "test.error.entity_property", entity.getName(), description);
|
| 898 | }
|
| 899 | }
|
| 900 |
|
| 901 | public <E extends Entity, T> void assertEntityProperty(final E entity, final Function<E, T> test, final T expected, final Component description) {
|
| 902 | T actual = test.apply(entity);
|
| 903 | if (!actual.equals(expected)) {
|
| 904 | throw this.assertionException(entity.blockPosition(), "test.error.entity_property_details", entity.getName(), description, actual, expected);
|
| 905 | }
|
| 906 | }
|
| 907 |
|
| 908 | public void assertLivingEntityHasMobEffect(final LivingEntity entity, final Holder<MobEffect> mobEffect, final int amplifier) {
|
| 909 | MobEffectInstance mobEffectInstance = entity.getEffect(mobEffect);
|
| 910 | if (mobEffectInstance == null || mobEffectInstance.getAmplifier() != amplifier) {
|
| 911 | throw this.assertionException("test.error.expected_entity_effect", entity.getName(), PotionContents.getPotionDescription(mobEffect, amplifier));
|
| 912 | }
|
| 913 | }
|
| 914 |
|
| 915 | public void succeedWhenEntityPresent(final EntityType<?> entityType, final int x, final int y, final int z) {
|
| 916 | this.succeedWhenEntityPresent(entityType, new BlockPos(x, y, z));
|
| 917 | }
|
| 918 |
|
| 919 | public void succeedWhenEntityPresent(final EntityType<?> entityType, final BlockPos pos) {
|
| 920 | this.succeedWhen(() -> this.assertEntityPresent(entityType, pos));
|
| 921 | }
|
| 922 |
|
| 923 | public void succeedWhenEntityNotPresent(final EntityType<?> entityType, final int x, final int y, final int z) {
|
| 924 | this.succeedWhenEntityNotPresent(entityType, new BlockPos(x, y, z));
|
| 925 | }
|
| 926 |
|
| 927 | public void succeedWhenEntityNotPresent(final EntityType<?> entityType, final BlockPos pos) {
|
| 928 | this.succeedWhen(() -> this.assertEntityNotPresent(entityType, pos));
|
| 929 | }
|
| 930 |
|
| 931 | public void succeed() {
|
| 932 | this.testInfo.succeed();
|
| 933 | }
|
| 934 |
|
| 935 | private void ensureSingleFinalCheck() {
|
| 936 | if (this.finalCheckAdded) {
|
| 937 | throw new IllegalStateException("This test already has final clause");
|
| 938 | }
|
| 939 |
|
| 940 | this.finalCheckAdded = true;
|
| 941 | }
|
| 942 |
|
| 943 | public void succeedIf(final Runnable asserter) {
|
| 944 | this.ensureSingleFinalCheck();
|
| 945 | this.testInfo.createSequence().thenWaitUntil(0L, asserter).thenSucceed();
|
| 946 | }
|
| 947 |
|
| 948 | public void succeedWhen(final Runnable asserter) {
|
| 949 | this.ensureSingleFinalCheck();
|
| 950 | this.testInfo.createSequence().thenWaitUntil(asserter).thenSucceed();
|
| 951 | }
|
| 952 |
|
| 953 | public void succeedOnTickWhen(final int tick, final Runnable asserter) {
|
| 954 | this.ensureSingleFinalCheck();
|
| 955 | this.testInfo.createSequence().thenWaitUntil(tick, asserter).thenSucceed();
|
| 956 | }
|
| 957 |
|
| 958 | public void runAtTickTime(final long time, final Runnable asserter) {
|
| 959 | this.testInfo.setRunAtTickTime(time, asserter);
|
| 960 | }
|
| 961 |
|
| 962 | public void runBeforeTestEnd(final Runnable asserter) {
|
| 963 | this.runAtTickTime(this.testInfo.getTimeoutTicks() - 1, asserter);
|
| 964 | }
|
| 965 |
|
| 966 | public void runAfterDelay(final long ticksToDelay, final Runnable whatToRun) {
|
| 967 | this.runAtTickTime(this.testInfo.getTick() + ticksToDelay, whatToRun);
|
| 968 | }
|
| 969 |
|
| 970 | public void setTime(final long ticks) {
|
| 971 | ServerLevel level = this.getLevel();
|
| 972 | Holder<WorldClock> clock = level.dimensionType().defaultClock().orElseThrow();
|
| 973 | level.clockManager().setTotalTicks(clock, ticks);
|
| 974 | }
|
| 975 |
|
| 976 | public void randomTick(final BlockPos pos) {
|
| 977 | BlockPos absolutePos = this.absolutePos(pos);
|
| 978 | ServerLevel level = this.getLevel();
|
| 979 | level.getBlockState(absolutePos).randomTick(level, absolutePos, level.getRandom());
|
| 980 | }
|
| 981 |
|
| 982 | public void tickBlock(final BlockPos pos) {
|
| 983 | BlockPos absolutePos = this.absolutePos(pos);
|
| 984 | ServerLevel level = this.getLevel();
|
| 985 | level.getBlockState(absolutePos).tick(level, absolutePos, level.getRandom());
|
| 986 | }
|
| 987 |
|
| 988 | public void tickPrecipitation(final BlockPos pos) {
|
| 989 | BlockPos absolutePos = this.absolutePos(pos);
|
| 990 | ServerLevel level = this.getLevel();
|
| 991 | level.tickPrecipitation(absolutePos);
|
| 992 | }
|
| 993 |
|
| 994 | public void tickPrecipitation() {
|
| 995 | AABB aabb = this.getRelativeBounds();
|
| 996 | int maxX = (int)Math.floor(aabb.maxX);
|
| 997 | int maxZ = (int)Math.floor(aabb.maxZ);
|
| 998 | int maxY = (int)Math.floor(aabb.maxY);
|
| 999 |
|
| 1000 | for (int x = (int)Math.floor(aabb.minX); x < maxX; x++) {
|
| 1001 | for (int z = (int)Math.floor(aabb.minZ); z < maxZ; z++) {
|
| 1002 | this.tickPrecipitation(new BlockPos(x, maxY, z));
|
| 1003 | }
|
| 1004 | }
|
| 1005 | }
|
| 1006 |
|
| 1007 | public int getHeight(final Heightmap.Types heightmap, final int x, final int z) {
|
| 1008 | BlockPos absolutePos = this.absolutePos(new BlockPos(x, 0, z));
|
| 1009 | return this.relativePos(this.getLevel().getHeightmapPos(heightmap, absolutePos)).getY();
|
| 1010 | }
|
| 1011 |
|
| 1012 | public void fail(final Component message, final BlockPos pos) {
|
| 1013 | throw this.assertionException(pos, message);
|
| 1014 | }
|
| 1015 |
|
| 1016 | public void fail(final Component message, final Entity entity) {
|
| 1017 | throw this.assertionException(entity.blockPosition(), message);
|
| 1018 | }
|
| 1019 |
|
| 1020 | public void fail(final Component message) {
|
| 1021 | throw this.assertionException(message);
|
| 1022 | }
|
| 1023 |
|
| 1024 | public void fail(final String message) {
|
| 1025 | throw this.assertionException(Component.literal(message));
|
| 1026 | }
|
| 1027 |
|
| 1028 | public void failIf(final Runnable asserter) {
|
| 1029 | this.testInfo.createSequence().thenWaitUntil(asserter).thenFail(() -> this.assertionException("test.error.fail"));
|
| 1030 | }
|
| 1031 |
|
| 1032 | public void failIfEver(final Runnable asserter) {
|
| 1033 | LongStream.range(this.testInfo.getTick(), this.testInfo.getTimeoutTicks()).forEach(i -> this.testInfo.setRunAtTickTime(i, asserter::run));
|
| 1034 | }
|
| 1035 |
|
| 1036 | public GameTestSequence startSequence() {
|
| 1037 | return this.testInfo.createSequence();
|
| 1038 | }
|
| 1039 |
|
| 1040 | public BlockPos absolutePos(final BlockPos relativePos) {
|
| 1041 | BlockPos testPos = this.testInfo.getTestOrigin();
|
| 1042 | BlockPos absolutePosBeforeTransform = testPos.offset(relativePos);
|
| 1043 | return StructureTemplate.transform(absolutePosBeforeTransform, Mirror.NONE, this.testInfo.getRotation(), testPos);
|
| 1044 | }
|
| 1045 |
|
| 1046 | public BlockPos relativePos(final BlockPos absolutePos) {
|
| 1047 | BlockPos testPos = this.testInfo.getTestOrigin();
|
| 1048 | Rotation inverseRotation = this.testInfo.getRotation().getRotated(Rotation.CLOCKWISE_180);
|
| 1049 | BlockPos absolutePosBeforeTransform = StructureTemplate.transform(absolutePos, Mirror.NONE, inverseRotation, testPos);
|
| 1050 | return absolutePosBeforeTransform.subtract(testPos);
|
| 1051 | }
|
| 1052 |
|
| 1053 | public AABB absoluteAABB(final AABB relativeAABB) {
|
| 1054 | Vec3 min = this.absoluteVec(relativeAABB.getMinPosition());
|
| 1055 | Vec3 max = this.absoluteVec(relativeAABB.getMaxPosition());
|
| 1056 | return new AABB(min, max);
|
| 1057 | }
|
| 1058 |
|
| 1059 | public AABB relativeAABB(final AABB absoluteAABB) {
|
| 1060 | Vec3 min = this.relativeVec(absoluteAABB.getMinPosition());
|
| 1061 | Vec3 max = this.relativeVec(absoluteAABB.getMaxPosition());
|
| 1062 | return new AABB(min, max);
|
| 1063 | }
|
| 1064 |
|
| 1065 | public Vec3 absoluteVec(final Vec3 relativeVec) {
|
| 1066 | Vec3 testPosVec = Vec3.atLowerCornerOf(this.testInfo.getTestOrigin());
|
| 1067 | return StructureTemplate.transform(testPosVec.add(relativeVec), Mirror.NONE, this.testInfo.getRotation(), this.testInfo.getTestOrigin());
|
| 1068 | }
|
| 1069 |
|
| 1070 | public Vec3 relativeVec(final Vec3 absoluteVec) {
|
| 1071 | Vec3 testPosVec = Vec3.atLowerCornerOf(this.testInfo.getTestOrigin());
|
| 1072 | return StructureTemplate.transform(absoluteVec.subtract(testPosVec), Mirror.NONE, this.testInfo.getRotation(), this.testInfo.getTestOrigin());
|
| 1073 | }
|
| 1074 |
|
| 1075 | public Rotation getTestRotation() {
|
| 1076 | return this.testInfo.getRotation();
|
| 1077 | }
|
| 1078 |
|
| 1079 | public Direction getTestDirection() {
|
| 1080 | return this.testInfo.getRotation().rotate(Direction.SOUTH);
|
| 1081 | }
|
| 1082 |
|
| 1083 | public Direction getAbsoluteDirection(final Direction direction) {
|
| 1084 | return this.getTestRotation().rotate(direction);
|
| 1085 | }
|
| 1086 |
|
| 1087 | public void assertTrue(final boolean condition, final Component errorMessage) {
|
| 1088 | if (!condition) {
|
| 1089 | throw this.assertionException(errorMessage);
|
| 1090 | }
|
| 1091 | }
|
| 1092 |
|
| 1093 | public void assertTrue(final boolean condition, final String errorMessage) {
|
| 1094 | this.assertTrue(condition, Component.literal(errorMessage));
|
| 1095 | }
|
| 1096 |
|
| 1097 | public <N> void assertValueEqual(final N value, final N expected, final String valueName) {
|
| 1098 | this.assertValueEqual(value, expected, Component.literal(valueName));
|
| 1099 | }
|
| 1100 |
|
| 1101 | public <N> void assertValueEqual(final N value, final N expected, final Component valueName) {
|
| 1102 | if (!value.equals(expected)) {
|
| 1103 | throw this.assertionException("test.error.value_not_equal", valueName, expected, value);
|
| 1104 | }
|
| 1105 | }
|
| 1106 |
|
| 1107 | public <N extends Comparable<N>> void assertValueInBetween(final N lowerBound, final N value, final N upperBound, final String valueName) {
|
| 1108 | this.assertValueInBetween(lowerBound, value, upperBound, Component.literal(valueName));
|
| 1109 | }
|
| 1110 |
|
| 1111 | public <N extends Comparable<N>> void assertValueInBetween(final N lowerBound, final N value, final N upperBound, final Component valueName) {
|
| 1112 | if (value.compareTo(lowerBound) < 0 || value.compareTo(upperBound) > 0) {
|
| 1113 | throw this.assertionException("test.error.value_not_in_between", valueName, lowerBound, upperBound, value);
|
| 1114 | }
|
| 1115 | }
|
| 1116 |
|
| 1117 | public void assertFalse(final boolean condition, final Component errorMessage) {
|
| 1118 | this.assertTrue(!condition, errorMessage);
|
| 1119 | }
|
| 1120 |
|
| 1121 | public void assertFalse(final boolean condition, final String errorMessage) {
|
| 1122 | this.assertFalse(condition, Component.literal(errorMessage));
|
| 1123 | }
|
| 1124 |
|
| 1125 | public long getTick() {
|
| 1126 | return this.testInfo.getTick();
|
| 1127 | }
|
| 1128 |
|
| 1129 | public AABB getBounds() {
|
| 1130 | return this.testInfo.getStructureBounds();
|
| 1131 | }
|
| 1132 |
|
| 1133 | public AABB getBoundsWithPadding() {
|
| 1134 | return this.getBounds().inflate(this.testInfo.getTest().padding());
|
| 1135 | }
|
| 1136 |
|
| 1137 | public AABB getRelativeBounds() {
|
| 1138 | AABB absolute = this.testInfo.getStructureBounds();
|
| 1139 | Rotation rotation = this.testInfo.getRotation();
|
| 1140 |
|
| 1141 | return switch (rotation) {
|
| 1142 | case COUNTERCLOCKWISE_90, CLOCKWISE_90 -> new AABB(0.0, 0.0, 0.0, absolute.getZsize(), absolute.getYsize(), absolute.getXsize());
|
| 1143 | default -> new AABB(0.0, 0.0, 0.0, absolute.getXsize(), absolute.getYsize(), absolute.getZsize());
|
| 1144 | };
|
| 1145 | }
|
| 1146 |
|
| 1147 | public void forEveryBlockInStructure(final Consumer<BlockPos> forBlock) {
|
| 1148 | AABB aabb = this.getRelativeBounds().contract(1.0, 1.0, 1.0);
|
| 1149 | BlockPos.MutableBlockPos.betweenClosedStream(aabb).forEach(forBlock);
|
| 1150 | }
|
| 1151 |
|
| 1152 | public void onEachTick(final Runnable action) {
|
| 1153 | LongStream.range(this.testInfo.getTick(), this.testInfo.getTimeoutTicks()).forEach(i -> this.testInfo.setRunAtTickTime(i, action::run));
|
| 1154 | }
|
| 1155 |
|
| 1156 | public void setBiome(final ResourceKey<Biome> biome) {
|
| 1157 | AABB bounds = this.getBoundsWithPadding();
|
| 1158 | BlockPos low = BlockPos.containing(bounds.minX, bounds.minY, bounds.minZ);
|
| 1159 | BlockPos high = BlockPos.containing(bounds.maxX, bounds.maxY, bounds.maxZ);
|
| 1160 | Either<Integer, CommandSyntaxException> result = FillBiomeCommand.fill(
|
| 1161 | this.getLevel(), low, high, this.getLevel().registryAccess().lookupOrThrow(Registries.BIOME).getOrThrow(biome)
|
| 1162 | );
|
| 1163 | if (result.right().isPresent() && result.right().get().getType() != FillBiomeCommand.ERROR_NO_BIOMES_SET) {
|
| 1164 | throw this.assertionException("test.error.set_biome");
|
| 1165 | }
|
| 1166 | }
|
| 1167 |
|
| 1168 | private static Component getItemName(final Item itemType) {
|
| 1169 | return itemType.components().getOrDefault(DataComponents.ITEM_NAME, CommonComponents.EMPTY);
|
| 1170 | }
|
| 1171 |
|
| 1172 | private class TestBlockPlaceContext extends BlockPlaceContext {
|
| 1173 | private final Direction placeDirection;
|
| 1174 |
|
| 1175 | public TestBlockPlaceContext(
|
| 1176 | final Level level, final InteractionHand hand, final ItemStack itemStackInHand, final BlockHitResult hitResult, final Direction placeDirection
|
| 1177 | ) {
|
| 1178 | super(level, null, hand, itemStackInHand, hitResult);
|
| 1179 | this.placeDirection = placeDirection;
|
| 1180 | }
|
| 1181 |
|
| 1182 | @Override
|
| 1183 | public Direction getNearestLookingDirection() {
|
| 1184 | return this.placeDirection;
|
| 1185 | }
|
| 1186 |
|
| 1187 | @Override
|
| 1188 | public Direction[] getNearestLookingDirections() {
|
| 1189 | Direction[] directions = Direction.values();
|
| 1190 | Arrays.sort(directions, Comparator.comparingDouble(d -> d.getUnitVec3().distanceTo(this.placeDirection.getUnitVec3())));
|
| 1191 | return directions;
|
| 1192 | }
|
| 1193 | }
|
| 1194 | }
|
| 1195 |
|