| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import com.google.common.base.Stopwatch;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import it.unimi.dsi.fastutil.objects.Object2LongMap;
|
| 6 | import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
|
| 7 | import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
| 8 | import it.unimi.dsi.fastutil.objects.Object2LongMap.Entry;
|
| 9 | import java.util.Collection;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Objects;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.concurrent.TimeUnit;
|
| 14 | import java.util.stream.Stream;
|
| 15 | import net.minecraft.core.BlockPos;
|
| 16 | import net.minecraft.core.Holder;
|
| 17 | import net.minecraft.core.Vec3i;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import net.minecraft.resources.ResourceKey;
|
| 21 | import net.minecraft.server.level.ServerLevel;
|
| 22 | import net.minecraft.world.entity.Entity;
|
| 23 | import net.minecraft.world.entity.player.Player;
|
| 24 | import net.minecraft.world.level.block.Blocks;
|
| 25 | import net.minecraft.world.level.block.Rotation;
|
| 26 | import net.minecraft.world.level.block.entity.TestInstanceBlockEntity;
|
| 27 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 28 | import net.minecraft.world.phys.AABB;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 |
|
| 31 | public class GameTestInfo {
|
| 32 | private final Holder.Reference<GameTestInstance> test;
|
| 33 | private @Nullable BlockPos testBlockPos;
|
| 34 | private final ServerLevel level;
|
| 35 | private final Collection<GameTestListener> listeners = Lists.newArrayList();
|
| 36 | private final int timeoutTicks;
|
| 37 | private final Collection<GameTestSequence> sequences = Lists.newCopyOnWriteArrayList();
|
| 38 | private final Object2LongMap<Runnable> runAtTickTimeMap = new Object2LongOpenHashMap<>();
|
| 39 | private boolean placedStructure;
|
| 40 | private boolean chunksLoaded;
|
| 41 | private int tickCount;
|
| 42 | private boolean started;
|
| 43 | private final RetryOptions retryOptions;
|
| 44 | private final Stopwatch timer = Stopwatch.createUnstarted();
|
| 45 | private boolean done;
|
| 46 | private final Rotation extraRotation;
|
| 47 | private @Nullable GameTestException error;
|
| 48 | private @Nullable TestInstanceBlockEntity testInstanceBlockEntity;
|
| 49 |
|
| 50 | public GameTestInfo(final Holder.Reference<GameTestInstance> test, final Rotation extraRotation, final ServerLevel level, final RetryOptions retryOptions) {
|
| 51 | this.test = test;
|
| 52 | this.level = level;
|
| 53 | this.retryOptions = retryOptions;
|
| 54 | this.timeoutTicks = test.value().maxTicks();
|
| 55 | this.extraRotation = extraRotation;
|
| 56 | }
|
| 57 |
|
| 58 | public void setTestBlockPos(final @Nullable BlockPos testBlockPos) {
|
| 59 | this.testBlockPos = testBlockPos;
|
| 60 | }
|
| 61 |
|
| 62 | public GameTestInfo startExecution(final int tickDelay) {
|
| 63 | this.tickCount = -(this.test.value().setupTicks() + tickDelay + 1);
|
| 64 | return this;
|
| 65 | }
|
| 66 |
|
| 67 | public void placeStructure() {
|
| 68 | if (!this.placedStructure) {
|
| 69 | TestInstanceBlockEntity test = this.getTestInstanceBlockEntity();
|
| 70 | if (!test.placeStructure()) {
|
| 71 | this.fail(Component.translatable("test.error.structure.failure", test.getTestName().getString()));
|
| 72 | }
|
| 73 |
|
| 74 | this.placedStructure = true;
|
| 75 | test.encaseStructure();
|
| 76 | BoundingBox boundingBox = test.getTestBoundingBox();
|
| 77 | this.level.getBlockTicks().clearArea(boundingBox);
|
| 78 | this.level.clearBlockEvents(boundingBox);
|
| 79 | this.listeners.forEach(listener -> listener.testStructureLoaded(this));
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | public void tick(final GameTestRunner runner) {
|
| 84 | if (!this.isDone()) {
|
| 85 | if (!this.placedStructure) {
|
| 86 | this.fail(Component.translatable("test.error.ticking_without_structure"));
|
| 87 | }
|
| 88 |
|
| 89 | if (this.testInstanceBlockEntity == null) {
|
| 90 | this.fail(Component.translatable("test.error.missing_block_entity"));
|
| 91 | }
|
| 92 |
|
| 93 | if (this.error != null) {
|
| 94 | this.finish();
|
| 95 | } else {
|
| 96 | if (!this.chunksLoaded
|
| 97 | && !this.testInstanceBlockEntity.getStructureBoundingBox().intersectingChunks().allMatch(this.level::areEntitiesActuallyLoadedAndTicking)) {
|
| 98 | return;
|
| 99 | }
|
| 100 |
|
| 101 | this.chunksLoaded = true;
|
| 102 | }
|
| 103 |
|
| 104 | this.tickInternal();
|
| 105 | if (this.isDone()) {
|
| 106 | if (this.error != null) {
|
| 107 | this.listeners.forEach(listener -> listener.testFailed(this, runner));
|
| 108 | } else {
|
| 109 | this.listeners.forEach(listener -> listener.testPassed(this, runner));
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | private void tickInternal() {
|
| 116 | this.tickCount++;
|
| 117 | if (this.tickCount >= 0) {
|
| 118 | if (!this.started) {
|
| 119 | this.startTest();
|
| 120 | }
|
| 121 |
|
| 122 | ObjectIterator<Entry<Runnable>> it = this.runAtTickTimeMap.object2LongEntrySet().iterator();
|
| 123 |
|
| 124 | while (it.hasNext()) {
|
| 125 | Entry<Runnable> entry = it.next();
|
| 126 | if (entry.getLongValue() <= this.tickCount) {
|
| 127 | try {
|
| 128 | entry.getKey().run();
|
| 129 | } catch (GameTestException error) {
|
| 130 | this.fail(error);
|
| 131 | } catch (Exception exception) {
|
| 132 | this.fail(new UnknownGameTestException(exception));
|
| 133 | }
|
| 134 |
|
| 135 | it.remove();
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | if (this.tickCount > this.timeoutTicks) {
|
| 140 | if (this.sequences.isEmpty()) {
|
| 141 | this.fail(new GameTestTimeoutException(Component.translatable("test.error.timeout.no_result", this.test.value().maxTicks())));
|
| 142 | } else {
|
| 143 | this.sequences.forEach(ticker -> ticker.tickAndFailIfNotComplete(this.tickCount));
|
| 144 | if (this.error == null) {
|
| 145 | this.fail(new GameTestTimeoutException(Component.translatable("test.error.timeout.no_sequences_finished", this.test.value().maxTicks())));
|
| 146 | }
|
| 147 | }
|
| 148 | } else {
|
| 149 | this.sequences.forEach(ticker -> ticker.tickAndContinue(this.tickCount));
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | private void startTest() {
|
| 155 | if (!this.started) {
|
| 156 | this.started = true;
|
| 157 | this.timer.start();
|
| 158 | this.getTestInstanceBlockEntity().setRunning();
|
| 159 |
|
| 160 | try {
|
| 161 | this.test.value().run(new GameTestHelper(this));
|
| 162 | } catch (GameTestException e) {
|
| 163 | this.fail(e);
|
| 164 | } catch (Exception e) {
|
| 165 | this.fail(new UnknownGameTestException(e));
|
| 166 | }
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | public void setRunAtTickTime(final long time, final Runnable assertAtTickTime) {
|
| 171 | this.runAtTickTimeMap.put(assertAtTickTime, time);
|
| 172 | }
|
| 173 |
|
| 174 | public Identifier id() {
|
| 175 | return this.test.key().identifier();
|
| 176 | }
|
| 177 |
|
| 178 | public @Nullable BlockPos getTestBlockPos() {
|
| 179 | return this.testBlockPos;
|
| 180 | }
|
| 181 |
|
| 182 | public BlockPos getTestOrigin() {
|
| 183 | return this.testInstanceBlockEntity.getStartCorner();
|
| 184 | }
|
| 185 |
|
| 186 | public AABB getStructureBounds() {
|
| 187 | TestInstanceBlockEntity blockEntity = this.getTestInstanceBlockEntity();
|
| 188 | return blockEntity.getStructureBounds();
|
| 189 | }
|
| 190 |
|
| 191 | public TestInstanceBlockEntity getTestInstanceBlockEntity() {
|
| 192 | if (this.testInstanceBlockEntity == null) {
|
| 193 | if (this.testBlockPos == null) {
|
| 194 | throw new IllegalStateException("This GameTestInfo has no position");
|
| 195 | }
|
| 196 |
|
| 197 | if (this.level.getBlockEntity(this.testBlockPos) instanceof TestInstanceBlockEntity blockEntity) {
|
| 198 | this.testInstanceBlockEntity = blockEntity;
|
| 199 | }
|
| 200 |
|
| 201 | if (this.testInstanceBlockEntity == null) {
|
| 202 | throw new IllegalStateException("Could not find a test instance block entity at the given coordinate " + this.testBlockPos);
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | return this.testInstanceBlockEntity;
|
| 207 | }
|
| 208 |
|
| 209 | public ServerLevel getLevel() {
|
| 210 | return this.level;
|
| 211 | }
|
| 212 |
|
| 213 | public boolean hasSucceeded() {
|
| 214 | return this.done && this.error == null;
|
| 215 | }
|
| 216 |
|
| 217 | public boolean hasFailed() {
|
| 218 | return this.error != null;
|
| 219 | }
|
| 220 |
|
| 221 | public boolean hasStarted() {
|
| 222 | return this.started;
|
| 223 | }
|
| 224 |
|
| 225 | public boolean isDone() {
|
| 226 | return this.done;
|
| 227 | }
|
| 228 |
|
| 229 | public long getRunTime() {
|
| 230 | return this.timer.elapsed(TimeUnit.MILLISECONDS);
|
| 231 | }
|
| 232 |
|
| 233 | private void finish() {
|
| 234 | if (!this.done) {
|
| 235 | this.done = true;
|
| 236 | if (this.timer.isRunning()) {
|
| 237 | this.timer.stop();
|
| 238 | }
|
| 239 | }
|
| 240 | }
|
| 241 |
|
| 242 | public void succeed() {
|
| 243 | if (this.error == null) {
|
| 244 | this.finish();
|
| 245 | AABB bounds = this.getStructureBounds();
|
| 246 | List<Entity> entities = this.getLevel().getEntitiesOfClass(Entity.class, bounds.inflate(1.0), mob -> !(mob instanceof Player));
|
| 247 | entities.forEach(e -> e.remove(Entity.RemovalReason.DISCARDED));
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | public void fail(final Component message) {
|
| 252 | this.fail(new GameTestAssertException(message, this.tickCount));
|
| 253 | }
|
| 254 |
|
| 255 | public void fail(final GameTestException error) {
|
| 256 | this.error = error;
|
| 257 | }
|
| 258 |
|
| 259 | public @Nullable GameTestException getError() {
|
| 260 | return this.error;
|
| 261 | }
|
| 262 |
|
| 263 | @Override
|
| 264 | public String toString() {
|
| 265 | return this.id().toString();
|
| 266 | }
|
| 267 |
|
| 268 | public void addListener(final GameTestListener listener) {
|
| 269 | this.listeners.add(listener);
|
| 270 | }
|
| 271 |
|
| 272 | public @Nullable GameTestInfo prepareTestStructure() {
|
| 273 | TestInstanceBlockEntity testInstanceBlock = this.createTestInstanceBlock(Objects.requireNonNull(this.testBlockPos), this.extraRotation, this.level);
|
| 274 | if (testInstanceBlock != null) {
|
| 275 | this.testInstanceBlockEntity = testInstanceBlock;
|
| 276 | this.placeStructure();
|
| 277 | return this;
|
| 278 | } else {
|
| 279 | return null;
|
| 280 | }
|
| 281 | }
|
| 282 |
|
| 283 | private @Nullable TestInstanceBlockEntity createTestInstanceBlock(final BlockPos testPos, final Rotation rotation, final ServerLevel level) {
|
| 284 | level.setBlockAndUpdate(testPos, Blocks.TEST_INSTANCE_BLOCK.defaultBlockState());
|
| 285 | if (level.getBlockEntity(testPos) instanceof TestInstanceBlockEntity blockEntity) {
|
| 286 | ResourceKey<GameTestInstance> test = this.getTestHolder().key();
|
| 287 | Vec3i size = TestInstanceBlockEntity.getStructureSize(level, test).orElse(new Vec3i(1, 1, 1));
|
| 288 | blockEntity.set(new TestInstanceBlockEntity.Data(Optional.of(test), size, rotation, false, TestInstanceBlockEntity.Status.CLEARED, Optional.empty()));
|
| 289 | return blockEntity;
|
| 290 | } else {
|
| 291 | return null;
|
| 292 | }
|
| 293 | }
|
| 294 |
|
| 295 | public int getTick() {
|
| 296 | return this.tickCount;
|
| 297 | }
|
| 298 |
|
| 299 | public GameTestSequence createSequence() {
|
| 300 | GameTestSequence sequence = new GameTestSequence(this);
|
| 301 | this.sequences.add(sequence);
|
| 302 | return sequence;
|
| 303 | }
|
| 304 |
|
| 305 | public boolean isRequired() {
|
| 306 | return this.test.value().required();
|
| 307 | }
|
| 308 |
|
| 309 | public boolean isOptional() {
|
| 310 | return !this.test.value().required();
|
| 311 | }
|
| 312 |
|
| 313 | public Identifier getStructure() {
|
| 314 | return this.test.value().structure();
|
| 315 | }
|
| 316 |
|
| 317 | public Rotation getRotation() {
|
| 318 | return this.test.value().info().rotation().getRotated(this.extraRotation);
|
| 319 | }
|
| 320 |
|
| 321 | public GameTestInstance getTest() {
|
| 322 | return this.test.value();
|
| 323 | }
|
| 324 |
|
| 325 | public Holder.Reference<GameTestInstance> getTestHolder() {
|
| 326 | return this.test;
|
| 327 | }
|
| 328 |
|
| 329 | public int getTimeoutTicks() {
|
| 330 | return this.timeoutTicks;
|
| 331 | }
|
| 332 |
|
| 333 | public boolean isFlaky() {
|
| 334 | return this.test.value().maxAttempts() > 1;
|
| 335 | }
|
| 336 |
|
| 337 | public int maxAttempts() {
|
| 338 | return this.test.value().maxAttempts();
|
| 339 | }
|
| 340 |
|
| 341 | public int requiredSuccesses() {
|
| 342 | return this.test.value().requiredSuccesses();
|
| 343 | }
|
| 344 |
|
| 345 | public RetryOptions retryOptions() {
|
| 346 | return this.retryOptions;
|
| 347 | }
|
| 348 |
|
| 349 | public Stream<GameTestListener> getListeners() {
|
| 350 | return this.listeners.stream();
|
| 351 | }
|
| 352 |
|
| 353 | public GameTestInfo copyReset() {
|
| 354 | GameTestInfo i = new GameTestInfo(this.test, this.extraRotation, this.level, this.retryOptions());
|
| 355 | if (this.testBlockPos != null) {
|
| 356 | i.setTestBlockPos(this.testBlockPos);
|
| 357 | }
|
| 358 |
|
| 359 | return i;
|
| 360 | }
|
| 361 | }
|
| 362 |
|