Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add relevance check to avoid creating additional entity #27

Merged
merged 5 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ void setWorldGeneratorUri(String worldGeneratorUri) {
* available
*/
public void forceAndWaitForGeneration(Vector3i blockPos) {
WorldProvider worldProvider = hostContext.get(WorldProvider.class);
if (worldProvider.isBlockRelevant(blockPos)) {
return;
}
Comment on lines +252 to +254
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally makes sense! 👍


// we need to add an entity with RegionRelevance in order to get a chunk generated
LocationComponent locationComponent = new LocationComponent();
locationComponent.setWorldPosition(blockPos.toVector3f());
Expand All @@ -258,7 +263,7 @@ public void forceAndWaitForGeneration(Vector3i blockPos) {

hostContext.get(EntityManager.class).create(locationComponent, relevanceRegionComponent).setAlwaysRelevant(true);

runWhile(() -> hostContext.get(WorldProvider.class).getBlock(blockPos).getURI().toString().equalsIgnoreCase("engine:unloaded"));
runWhile(() -> !worldProvider.isBlockRelevant(blockPos));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.moduletestingenvironment.fixtures;

import org.terasology.engine.SimpleUri;
import org.terasology.registry.In;
import org.terasology.world.generation.BaseFacetedWorldGenerator;
import org.terasology.world.generation.WorldBuilder;
import org.terasology.world.generator.RegisterWorldGenerator;
import org.terasology.world.generator.plugin.WorldGeneratorPlugin;
import org.terasology.world.generator.plugin.WorldGeneratorPluginLibrary;

import java.util.ArrayList;
import java.util.List;

/**
* Bare World Generator to generating empty chunks for testing.
*/
@RegisterWorldGenerator(id = "empty", displayName = "empty")
public class EmptyWorldGenerator extends BaseFacetedWorldGenerator {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this supposed to be used for? Could we add a little bit of JavaDoc explaining the intended use case?

@In
private WorldGeneratorPluginLibrary worldGeneratorPluginLibrary;

public EmptyWorldGenerator(SimpleUri uri) {
super(uri);
}

@Override
protected WorldBuilder createWorld() {
return new WorldBuilder(new WorldGeneratorPluginLibrary() {
@Override
public <U extends WorldGeneratorPlugin> List<U> instantiateAllOfType(Class<U> ofType) {
return new ArrayList<>();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2020 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.moduletestingenvironment;

import com.google.common.collect.Lists;
Expand All @@ -24,7 +11,6 @@
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.logic.players.event.ResetCameraEvent;
import org.terasology.math.geom.Vector3f;
import org.terasology.math.geom.Vector3i;
import org.terasology.moduletestingenvironment.extension.Dependencies;
import org.terasology.network.ClientComponent;
Expand All @@ -49,30 +35,46 @@ public class ExampleTest {
private ModuleTestingHelper helper;

@Test
public void testExample() {
public void testClientConnection() {
int currentClients = Lists.newArrayList(entityManager.getEntitiesWith(ClientComponent.class)).size();

// create some clients (the library connects them automatically)
Context clientContext1 = helper.createClient();
Context clientContext2 = helper.createClient();

int expectedClients = currentClients + 2;

// wait for both clients to be known to the server
helper.runUntil(()-> Lists.newArrayList(entityManager.getEntitiesWith(ClientComponent.class)).size() == 2);
Assertions.assertEquals(2, Lists.newArrayList(entityManager.getEntitiesWith(ClientComponent.class)).size());
helper.runUntil(() -> Lists.newArrayList(entityManager.getEntitiesWith(ClientComponent.class)).size() >= expectedClients);
Assertions.assertEquals(expectedClients,
Lists.newArrayList(entityManager.getEntitiesWith(ClientComponent.class)).size());
}

@Test
public void testRunWhileTimeout() {
// run while a condition is true or until a timeout passes
long expectedTime = time.getGameTimeInMs() + 1000;
boolean timedOut = helper.runWhile(1000, ()-> true);
long expectedTime = time.getGameTimeInMs() + 500;
boolean timedOut = helper.runWhile(500, () -> true);
Assertions.assertTrue(timedOut);
long currentTime = time.getGameTimeInMs();
Assertions.assertTrue(currentTime >= expectedTime);
}

@Test
public void testSendEvent() {
Context clientContext = helper.createClient();

// send an event to a client's local player just for fun
clientContext1.get(LocalPlayer.class).getClientEntity().send(new ResetCameraEvent());
clientContext.get(LocalPlayer.class).getClientEntity().send(new ResetCameraEvent());
}

@Test
public void testWorldProvider() {
// wait for a chunk to be generated
helper.forceAndWaitForGeneration(Vector3i.zero());

// set a block's type and immediately read it back
worldProvider.setBlock(Vector3i.zero(), blockManager.getBlock("engine:air"));
Assertions.assertEquals("engine:air", worldProvider.getBlock(Vector3f.zero()).getURI().toString());
worldProvider.setBlock(new org.joml.Vector3i(), blockManager.getBlock("engine:air"));
Assertions.assertEquals("engine:air", worldProvider.getBlock(new org.joml.Vector3i()).getURI().toString());
}
}