| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.world.entity.Entity;
|
| 8 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 9 | import net.minecraft.world.entity.EntityType;
|
| 10 | import net.minecraft.world.entity.Mob;
|
| 11 | import net.minecraft.world.level.block.Rotation;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class GameTestEntityBuilder<E extends Entity> {
|
| 16 | protected final GameTestHelper testHelper;
|
| 17 | private final EntityType<E> entityType;
|
| 18 | private final Vec3 position;
|
| 19 | private @Nullable EntitySpawnReason spawnReason;
|
| 20 | private @Nullable Rotation rotation;
|
| 21 | private boolean requirePersistence = true;
|
| 22 |
|
| 23 | public GameTestEntityBuilder(final GameTestHelper testHelper, final EntityType<E> entityType, final Vec3 position) {
|
| 24 | this.testHelper = testHelper;
|
| 25 | this.entityType = entityType;
|
| 26 | this.position = position;
|
| 27 | }
|
| 28 |
|
| 29 | public GameTestEntityBuilder<E> spawnReason(final @Nullable EntitySpawnReason spawnReason) {
|
| 30 | this.spawnReason = spawnReason;
|
| 31 | return this;
|
| 32 | }
|
| 33 |
|
| 34 | public GameTestEntityBuilder<E> rotation(final @Nullable Rotation rotation) {
|
| 35 | this.rotation = rotation == null ? null : this.testHelper.getTestRotation().getRotated(rotation);
|
| 36 | return this;
|
| 37 | }
|
| 38 |
|
| 39 | public GameTestEntityBuilder<E> requirePersistence(final boolean requirePersistence) {
|
| 40 | this.requirePersistence = requirePersistence;
|
| 41 | return this;
|
| 42 | }
|
| 43 |
|
| 44 | public E spawn() {
|
| 45 | ServerLevel level = this.testHelper.getLevel();
|
| 46 | E entity = this.entityType.create(level, EntitySpawnReason.STRUCTURE);
|
| 47 | if (entity == null) {
|
| 48 | throw this.testHelper
|
| 49 | .assertionException(BlockPos.containing(this.position), "test.error.spawn_failure", this.entityType.builtInRegistryHolder().getRegisteredName());
|
| 50 | }
|
| 51 |
|
| 52 | if (this.requirePersistence && entity instanceof Mob mob) {
|
| 53 | mob.setPersistenceRequired();
|
| 54 | }
|
| 55 |
|
| 56 | Vec3 absoluteVec = this.testHelper.absoluteVec(this.position);
|
| 57 | float yRot = entity.rotate(this.rotation == null ? this.testHelper.getTestRotation() : this.rotation);
|
| 58 | entity.snapTo(absoluteVec.x, absoluteVec.y, absoluteVec.z, yRot, entity.getXRot());
|
| 59 | entity.setYBodyRot(yRot);
|
| 60 | entity.setYHeadRot(yRot);
|
| 61 | if (this.spawnReason != null && entity instanceof Mob mob) {
|
| 62 | mob.finalizeSpawn(level, level.getCurrentDifficultyAt(mob.blockPosition()), this.spawnReason, null);
|
| 63 | }
|
| 64 |
|
| 65 | level.addFreshEntityWithPassengers(entity);
|
| 66 | return entity;
|
| 67 | }
|
| 68 |
|
| 69 | public List<E> spawn(final int amount) {
|
| 70 | List<E> entities = new ArrayList<>();
|
| 71 |
|
| 72 | for (int i = 0; i < amount; i++) {
|
| 73 | entities.add(this.spawn());
|
| 74 | }
|
| 75 |
|
| 76 | return entities;
|
| 77 | }
|
| 78 | }
|
| 79 |
|