Search in sources :

Example 41 with Block

use of org.terasology.world.block.Block in project Terasology by MovingBlocks.

the class BlockManagerImpl method receiveFamilyRegistration.

public void receiveFamilyRegistration(BlockUri familyUri, Map<String, Integer> registration) {
    Optional<BlockFamily> family = loadFamily(familyUri);
    if (family.isPresent()) {
        lock.lock();
        try {
            for (Block block : family.get().getBlocks()) {
                Integer id = registration.get(block.getURI().toString());
                if (id != null) {
                    block.setId((short) id.intValue());
                } else {
                    logger.error("Missing id for block {} in registered family {}", block.getURI(), familyUri);
                    block.setId(UNKNOWN_ID);
                }
            }
            registerFamily(family.get());
        } finally {
            lock.unlock();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Block(org.terasology.world.block.Block) BlockFamily(org.terasology.world.block.family.BlockFamily)

Example 42 with Block

use of org.terasology.world.block.Block in project Terasology by MovingBlocks.

the class BlockManagerImpl method registerFamily.

@VisibleForTesting
protected void registerFamily(BlockFamily family) {
    Preconditions.checkNotNull(family);
    logger.info("Registered {}", family);
    lock.lock();
    try {
        RegisteredState newState = new RegisteredState(registeredBlockInfo.get());
        newState.registeredFamilyByUri.put(family.getURI(), family);
        for (Block block : family.getBlocks()) {
            registerBlock(block, newState);
        }
        registeredBlockInfo.set(newState);
    } finally {
        lock.unlock();
    }
    for (BlockRegistrationListener listener : listeners) {
        listener.onBlockFamilyRegistered(family);
    }
}
Also used : Block(org.terasology.world.block.Block) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 43 with Block

use of org.terasology.world.block.Block in project Terasology by MovingBlocks.

the class BlockManagerImpl method initialise.

public void initialise(List<String> registeredBlockFamilies, Map<String, Short> knownBlockMappings) {
    if (knownBlockMappings.size() >= MAX_ID) {
        nextId.set(UNKNOWN_ID);
    } else if (knownBlockMappings.size() > 0) {
        nextId.set(knownBlockMappings.values().stream().max(Short::compareTo).orElse((short) 0) + 1);
    }
    registeredBlockInfo.set(new RegisteredState());
    for (String rawFamilyUri : registeredBlockFamilies) {
        try {
            BlockUri familyUri = new BlockUri(rawFamilyUri);
            Optional<BlockFamily> family = loadFamily(familyUri);
            if (family.isPresent()) {
                for (Block block : family.get().getBlocks()) {
                    Short id = knownBlockMappings.get(block.getURI().toString());
                    if (id != null) {
                        block.setId(id);
                    } else {
                        logger.error("Missing id for block {} in provided family {}", block.getURI(), family.get().getURI());
                        if (generateNewIds) {
                            block.setId(getNextId());
                        } else {
                            block.setId(UNKNOWN_ID);
                        }
                    }
                }
                registerFamily(family.get());
            }
        } catch (BlockUriParseException e) {
            logger.error("Failed to parse block family, skipping", e);
        }
    }
}
Also used : BlockUri(org.terasology.world.block.BlockUri) BlockUriParseException(org.terasology.world.block.BlockUriParseException) Block(org.terasology.world.block.Block) BlockFamily(org.terasology.world.block.family.BlockFamily)

Example 44 with Block

use of org.terasology.world.block.Block in project Terasology by MovingBlocks.

the class AttachSupportRequired method getEntity.

private EntityRef getEntity(Vector3i location, Map<Vector3i, Block> blockOverrides) {
    final Block overwrittenBlock = blockOverrides.get(location);
    if (overwrittenBlock != null) {
        return overwrittenBlock.getEntity();
    }
    EntityRef blockEntity = getBlockEntityRegistry().getExistingBlockEntityAt(location);
    if (blockEntity.exists()) {
        return blockEntity;
    } else {
        return getWorldProvider().getBlock(location).getEntity();
    }
}
Also used : Block(org.terasology.world.block.Block) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 45 with Block

use of org.terasology.world.block.Block in project Terasology by MovingBlocks.

the class EntityAwareWorldProvider method getBlockEntityAt.

@Override
public EntityRef getBlockEntityAt(Vector3i blockPosition) {
    if (GameThread.isCurrentThread()) {
        EntityRef blockEntity = getExistingBlockEntityAt(blockPosition);
        if ((!blockEntity.exists() || !blockEntity.hasComponent(NetworkComponent.class)) && isBlockRelevant(blockPosition.x, blockPosition.y, blockPosition.z)) {
            Block block = getBlock(blockPosition.x, blockPosition.y, blockPosition.z);
            blockEntity = createBlockEntity(blockPosition, block);
        }
        return blockEntity;
    }
    logger.error("Attempted to get block entity off-thread");
    return EntityRef.NULL;
}
Also used : Block(org.terasology.world.block.Block) OnChangedBlock(org.terasology.world.OnChangedBlock) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Aggregations

Block (org.terasology.world.block.Block)67 Vector3i (org.terasology.math.geom.Vector3i)32 EntityRef (org.terasology.entitySystem.entity.EntityRef)16 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)11 Side (org.terasology.math.Side)11 Vector3f (org.terasology.math.geom.Vector3f)9 LocationComponent (org.terasology.logic.location.LocationComponent)7 BlockUri (org.terasology.world.block.BlockUri)7 BlockFamily (org.terasology.world.block.family.BlockFamily)7 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)6 OnChangedBlock (org.terasology.world.OnChangedBlock)6 BlockComponent (org.terasology.world.block.BlockComponent)5 BlockManager (org.terasology.world.block.BlockManager)5 BlockRegionComponent (org.terasology.world.block.regions.BlockRegionComponent)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Before (org.junit.Before)3 ResourceUrn (org.terasology.assets.ResourceUrn)3 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)3 DoDamageEvent (org.terasology.logic.health.DoDamageEvent)3