use of org.terasology.engine.entitySystem.entity.EntityBuilder in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method createBlockEntity.
private EntityRef createBlockEntity(Vector3ic blockPosition, Block block) {
EntityBuilder builder = entityManager.newBuilder(block.getPrefab().orElse(null));
builder.addComponent(new LocationComponent(new Vector3f(blockPosition)));
builder.addComponent(new BlockComponent(block, blockPosition));
boolean isTemporary = isTemporaryBlock(builder, block);
if (!isTemporary && !builder.hasComponent(NetworkComponent.class)) {
builder.addComponent(new NetworkComponent());
}
EntityRef blockEntity;
if (isTemporary) {
blockEntity = builder.buildWithoutLifecycleEvents();
temporaryBlockEntities.add(blockEntity);
} else {
blockEntity = builder.build();
}
blockEntityLookup.put(new Vector3i(blockPosition), blockEntity);
return blockEntity;
}
use of org.terasology.engine.entitySystem.entity.EntityBuilder in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method updateBlockEntityComponents.
/**
* Transforms a block entity with the change of block type. This is driven from the delta between the old and new
* block type prefabs, but takes into account changes made to the block entity.
* Components contained in `blockEntity` that
* <ul>
* <li>are not "common block components" (e.g. `NetworkComponent`)</li>
* <li>don't have `reatinUnalteredOnBlockChange` metadata</li>
* <li>are not listed in the block prefab</li>
* <li>are not listed in the set of components to be retained</li>
* </ul>
* will be removed.
*
* @param blockEntity The entity to update
* @param oldType The previous type of the block
* @param type The new type of the block
* @param retainComponents List of components to be retained
*/
private void updateBlockEntityComponents(EntityRef blockEntity, Block oldType, Block type, Set<Class<? extends Component>> retainComponents) {
BlockComponent blockComponent = blockEntity.getComponent(BlockComponent.class);
Optional<Prefab> oldPrefab = oldType.getPrefab();
EntityBuilder oldEntityBuilder = entityManager.newBuilder(oldPrefab.orElse(null));
oldEntityBuilder.addComponent(new BlockComponent(oldType, blockComponent.getPosition()));
BeforeEntityCreated oldEntityEvent = new BeforeEntityCreated(oldPrefab.orElse(null), oldEntityBuilder.iterateComponents());
blockEntity.send(oldEntityEvent);
for (Component comp : oldEntityEvent.getResultComponents()) {
oldEntityBuilder.addComponent(comp);
}
Optional<Prefab> newPrefab = type.getPrefab();
EntityBuilder newEntityBuilder = entityManager.newBuilder(newPrefab.orElse(null));
newEntityBuilder.addComponent(new BlockComponent(type, blockComponent.getPosition()));
BeforeEntityCreated newEntityEvent = new BeforeEntityCreated(newPrefab.orElse(null), newEntityBuilder.iterateComponents());
blockEntity.send(newEntityEvent);
for (Component comp : newEntityEvent.getResultComponents()) {
newEntityBuilder.addComponent(comp);
}
for (Component component : blockEntity.iterateComponents()) {
if (!COMMON_BLOCK_COMPONENTS.contains(component.getClass()) && !entityManager.getComponentLibrary().getMetadata(component.getClass()).isRetainUnalteredOnBlockChange() && !newEntityBuilder.hasComponent(component.getClass()) && !retainComponents.contains(component.getClass())) {
blockEntity.removeComponent(component.getClass());
}
}
BlockComponent newBlockComponent = new BlockComponent(type, blockComponent.getPosition());
blockEntity.saveComponent(newBlockComponent);
for (Component comp : newEntityBuilder.iterateComponents()) {
copyIntoPrefab(blockEntity, comp, retainComponents);
}
}
use of org.terasology.engine.entitySystem.entity.EntityBuilder in project Terasology by MovingBlocks.
the class NetworkOwnershipTest method testClientSendInitialForRelevantOwnedItems.
@Test
public void testClientSendInitialForRelevantOwnedItems() {
EntityBuilder builder = entityManager.newBuilder();
NetworkComponent netCompA = builder.addComponent(new NetworkComponent());
netCompA.replicateMode = NetworkComponent.ReplicateMode.RELEVANT;
builder.setOwner(clientEntity);
EntityRef entityA = builder.build();
networkSystem.registerNetworkEntity(entityA);
connectClient();
verify(client, times(1)).setNetInitial(entityA.getComponent(NetworkComponent.class).getNetworkId());
}
use of org.terasology.engine.entitySystem.entity.EntityBuilder in project Terasology by MovingBlocks.
the class NetworkOwnershipTest method testClientSentInitialIfOwnedEntityRegistered.
@Test
public void testClientSentInitialIfOwnedEntityRegistered() {
connectClient();
EntityBuilder builder = entityManager.newBuilder();
NetworkComponent netComp = builder.addComponent(new NetworkComponent());
netComp.replicateMode = NetworkComponent.ReplicateMode.OWNER;
builder.setOwner(clientEntity);
EntityRef entity = builder.build();
networkSystem.registerNetworkEntity(entity);
assertTrue(entity.getComponent(NetworkComponent.class).getNetworkId() != 0);
verify(client).setNetInitial(entity.getComponent(NetworkComponent.class).getNetworkId());
}
use of org.terasology.engine.entitySystem.entity.EntityBuilder in project Terasology by MovingBlocks.
the class NetworkOwnershipTest method testClientSentInitialOnlyOnce.
@Test
public void testClientSentInitialOnlyOnce() {
EntityBuilder builder = entityManager.newBuilder();
NetworkComponent netComp = builder.addComponent(new NetworkComponent());
netComp.replicateMode = NetworkComponent.ReplicateMode.OWNER;
builder.setOwner(clientEntity);
EntityRef entity = builder.build();
networkSystem.registerNetworkEntity(entity);
connectClient();
networkSystem.updateOwnership(entity);
verify(client, times(1)).setNetInitial(entity.getComponent(NetworkComponent.class).getNetworkId());
}
Aggregations