-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathTestingStateHeadlessSetup.java
74 lines (59 loc) · 2.92 KB
/
TestingStateHeadlessSetup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.integrationenvironment;
import org.terasology.engine.config.Config;
import org.terasology.engine.config.ModuleConfig;
import org.terasology.engine.config.WorldGenerationConfig;
import org.terasology.engine.core.GameEngine;
import org.terasology.engine.core.SimpleUri;
import org.terasology.engine.core.TerasologyConstants;
import org.terasology.engine.core.TerasologyEngine;
import org.terasology.engine.core.subsystem.headless.mode.StateHeadlessSetup;
import org.terasology.engine.game.GameManifest;
import org.terasology.engine.world.time.WorldTime;
import org.terasology.gestalt.naming.Name;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
public class TestingStateHeadlessSetup extends StateHeadlessSetup {
static final Name MTE_MODULE_NAME = new Name("unittest");
static final String WORLD_TITLE = "testworld";
static final String DEFAULT_SEED = "seed";
private final Collection<String> dependencies;
private final SimpleUri worldGeneratorUri;
{
strictModuleRequirements = true;
}
public TestingStateHeadlessSetup(Collection<String> dependencies, String worldGeneratorUri) {
this.dependencies = dependencies;
this.worldGeneratorUri = new SimpleUri(worldGeneratorUri);
checkArgument(this.worldGeneratorUri.isValid(), "Not a valid URI `%s`", worldGeneratorUri);
}
void configForTest(Config config) {
Set<Name> dependencyNames = dependencies.stream().map(Name::new).collect(Collectors.toSet());
// Include the MTE module to provide world generators and suchlike.
dependencyNames.add(MTE_MODULE_NAME);
ModuleConfig moduleSelection = config.getDefaultModSelection();
moduleSelection.clear();
dependencyNames.forEach(moduleSelection::addModule);
WorldGenerationConfig worldGenerationConfig = config.getWorldGeneration();
worldGenerationConfig.setDefaultGenerator(worldGeneratorUri);
worldGenerationConfig.setWorldTitle(WORLD_TITLE);
worldGenerationConfig.setDefaultSeed(DEFAULT_SEED);
}
@Override
public GameManifest createGameManifest() {
GameManifest gameManifest = super.createGameManifest();
float timeOffset = 0.25f + 0.025f; // Time at dawn + little offset to spawn in a brighter env.
gameManifest.getWorldInfo(TerasologyConstants.MAIN_WORLD).setTime((long) (WorldTime.DAY_LENGTH * timeOffset));
return gameManifest;
}
@Override
public void init(GameEngine engine) {
// We want to modify Config before super.init calls createGameManifest, but the child context
// does not exist before we call super.init.
configForTest(((TerasologyEngine) engine).getFromEngineContext(Config.class));
super.init(engine);
}
}