Search in sources :

Example 6 with NetworkComponent

use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.

the class BlockTypeEntityGenerator method generateBlockTypeEntity.

private void generateBlockTypeEntity(Block block) {
    EntityBuilder builder = entityManager.newBuilder(blockTypePrefab);
    builder.getComponent(BlockTypeComponent.class).block = block;
    // TODO: Copy across settings as necessary
    Optional<Prefab> prefab = block.getPrefab();
    if (prefab.isPresent()) {
        for (Component comp : prefab.get().iterateComponents()) {
            if (!(comp instanceof NetworkComponent)) {
                builder.addComponent(entityManager.getComponentLibrary().copy(comp));
            }
        }
    }
    block.setEntity(builder.build());
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) NetworkComponent(org.terasology.network.NetworkComponent) Component(org.terasology.entitySystem.Component) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 7 with NetworkComponent

use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.

the class EntityAwareWorldProvider method createBlockEntity.

private EntityRef createBlockEntity(Vector3i blockPosition, Block block) {
    EntityBuilder builder = entityManager.newBuilder(block.getPrefab().orElse(null));
    builder.addComponent(new LocationComponent(blockPosition.toVector3f()));
    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;
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) Vector3i(org.terasology.math.geom.Vector3i) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 8 with NetworkComponent

use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.

the class CreateWorldEntity method step.

@Override
public boolean step() {
    EntityManager entityManager = context.get(EntityManager.class);
    ChunkProvider chunkProvider = context.get(ChunkProvider.class);
    Iterator<EntityRef> worldEntityIterator = entityManager.getEntitiesWith(WorldComponent.class).iterator();
    if (worldEntityIterator.hasNext()) {
        EntityRef worldEntity = worldEntityIterator.next();
        chunkProvider.setWorldEntity(worldEntity);
        // get the world generator config from the world entity
        // replace the world generator values from the components in the world entity
        WorldGenerator worldGenerator = context.get(WorldGenerator.class);
        WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
        Map<String, Component> params = worldConfigurator.getProperties();
        for (Map.Entry<String, Component> entry : params.entrySet()) {
            Class<? extends Component> clazz = entry.getValue().getClass();
            Component comp = worldEntity.getComponent(clazz);
            if (comp != null) {
                worldConfigurator.setProperty(entry.getKey(), comp);
            }
        }
    } else {
        EntityRef worldEntity = entityManager.create();
        worldEntity.addComponent(new WorldComponent());
        NetworkComponent networkComponent = new NetworkComponent();
        networkComponent.replicateMode = NetworkComponent.ReplicateMode.ALWAYS;
        worldEntity.addComponent(networkComponent);
        chunkProvider.setWorldEntity(worldEntity);
        // transfer all world generation parameters from Config to WorldEntity
        WorldGenerator worldGenerator = context.get(WorldGenerator.class);
        SimpleUri generatorUri = worldGenerator.getUri();
        Config config = context.get(Config.class);
        // get the map of properties from the world generator.
        // Replace its values with values from the config set by the UI.
        // Also set all the components to the world entity.
        WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
        Map<String, Component> params = worldConfigurator.getProperties();
        for (Map.Entry<String, Component> entry : params.entrySet()) {
            Class<? extends Component> clazz = entry.getValue().getClass();
            Component comp = config.getModuleConfig(generatorUri, entry.getKey(), clazz);
            if (comp != null) {
                worldEntity.addComponent(comp);
                worldConfigurator.setProperty(entry.getKey(), comp);
            } else {
                worldEntity.addComponent(entry.getValue());
            }
        }
    }
    return true;
}
Also used : WorldGenerator(org.terasology.world.generator.WorldGenerator) WorldConfigurator(org.terasology.world.generator.WorldConfigurator) Config(org.terasology.config.Config) SimpleUri(org.terasology.engine.SimpleUri) EntityManager(org.terasology.entitySystem.entity.EntityManager) NetworkComponent(org.terasology.network.NetworkComponent) ChunkProvider(org.terasology.world.chunks.ChunkProvider) WorldComponent(org.terasology.world.WorldComponent) NetworkComponent(org.terasology.network.NetworkComponent) WorldComponent(org.terasology.world.WorldComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) Map(java.util.Map)

Example 9 with NetworkComponent

use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.

the class NetworkOwnershipTest method setup.

@Before
public void setup() throws Exception {
    super.setup();
    ModuleManager moduleManager = ModuleManagerFactory.create();
    context.put(ModuleManager.class, moduleManager);
    EngineTime mockTime = mock(EngineTime.class);
    networkSystem = new NetworkSystemImpl(mockTime, context);
    networkSystem.setContext(context);
    context.put(NetworkSystem.class, networkSystem);
    EntitySystemSetupUtil.addReflectionBasedLibraries(context);
    EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
    entityManager = (PojoEntityManager) context.get(EntityManager.class);
    context.put(ComponentSystemManager.class, new ComponentSystemManager(context));
    entityManager.clear();
    client = mock(NetClient.class);
    NetworkComponent clientNetComp = new NetworkComponent();
    clientNetComp.replicateMode = NetworkComponent.ReplicateMode.OWNER;
    clientEntity = entityManager.create(clientNetComp);
    when(client.getEntity()).thenReturn(clientEntity);
    when(client.getId()).thenReturn("dummyID");
    networkSystem.mockHost();
    networkSystem.connectToEntitySystem(entityManager, context.get(EventLibrary.class), mock(BlockEntityRegistry.class));
    networkSystem.registerNetworkEntity(clientEntity);
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) EventLibrary(org.terasology.entitySystem.metadata.EventLibrary) EngineTime(org.terasology.engine.EngineTime) BlockEntityRegistry(org.terasology.world.BlockEntityRegistry) ModuleManager(org.terasology.engine.module.ModuleManager) ComponentSystemManager(org.terasology.engine.ComponentSystemManager) Before(org.junit.Before)

Example 10 with NetworkComponent

use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.

the class NetworkOwnershipTest method testClientSentNetInitialForNewNetworkEntity.

@Test
public void testClientSentNetInitialForNewNetworkEntity() {
    connectClient();
    EntityRef entity = entityManager.create(new NetworkComponent());
    networkSystem.registerNetworkEntity(entity);
    assertTrue(entity.getComponent(NetworkComponent.class).getNetworkId() != 0);
    verify(client).setNetInitial(entity.getComponent(NetworkComponent.class).getNetworkId());
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) Test(org.junit.Test)

Aggregations

NetworkComponent (org.terasology.network.NetworkComponent)27 EntityRef (org.terasology.entitySystem.entity.EntityRef)18 Test (org.junit.Test)7 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)6 BlockComponent (org.terasology.world.block.BlockComponent)5 Component (org.terasology.entitySystem.Component)3 Prefab (org.terasology.entitySystem.prefab.Prefab)3 Vector3i (org.terasology.math.geom.Vector3i)3 Command (org.terasology.logic.console.commandSystem.annotations.Command)2 Map (java.util.Map)1 Before (org.junit.Before)1 Config (org.terasology.config.Config)1 ComponentSystemManager (org.terasology.engine.ComponentSystemManager)1 EngineTime (org.terasology.engine.EngineTime)1 SimpleUri (org.terasology.engine.SimpleUri)1 ModuleManager (org.terasology.engine.module.ModuleManager)1 EntityManager (org.terasology.entitySystem.entity.EntityManager)1 EventLibrary (org.terasology.entitySystem.metadata.EventLibrary)1 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)1 LocationComponent (org.terasology.logic.location.LocationComponent)1