Search in sources :

Example 6 with BlockFamily

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

the class CacheBlocks method step.

@Override
public boolean step() {
    if (blockFamilyIterator.hasNext()) {
        BlockFamily family = blockFamilyIterator.next();
        family.getArchetypeBlock().getMeshGenerator();
        stepDone();
    }
    return !blockFamilyIterator.hasNext();
}
Also used : BlockFamily(org.terasology.world.block.family.BlockFamily)

Example 7 with BlockFamily

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

the class BlockCommands method giveBlock.

/**
 * Called by 'give' command in ItemCommands.java to attempt to put a block in the player's inventory when no item is found.
 * Called by 'giveBulkBlock' command in BlockCommands.java to put a block in the player's inventory.
 * @return Null if not found, otherwise success or warning message
 */
public String giveBlock(@Sender EntityRef sender, @CommandParam("blockName") String uri, @CommandParam(value = "quantity", required = false) Integer quantityParam, @CommandParam(value = "shapeName", required = false) String shapeUriParam) {
    Set<ResourceUrn> matchingUris = Assets.resolveAssetUri(uri, BlockFamilyDefinition.class);
    BlockFamily blockFamily = null;
    if (matchingUris.size() == 1) {
        Optional<BlockFamilyDefinition> def = Assets.get(matchingUris.iterator().next(), BlockFamilyDefinition.class);
        if (def.isPresent()) {
            if (def.get().isFreeform()) {
                if (shapeUriParam == null) {
                    blockFamily = blockManager.getBlockFamily(new BlockUri(def.get().getUrn(), new ResourceUrn("engine:cube")));
                } else {
                    Set<ResourceUrn> resolvedShapeUris = Assets.resolveAssetUri(shapeUriParam, BlockShape.class);
                    if (resolvedShapeUris.isEmpty()) {
                        return "Found block. No shape found for '" + shapeUriParam + "'";
                    } else if (resolvedShapeUris.size() > 1) {
                        StringBuilder builder = new StringBuilder();
                        builder.append("Found block. Non-unique shape name, possible matches: ");
                        Iterator<ResourceUrn> shapeUris = sortItems(resolvedShapeUris).iterator();
                        while (shapeUris.hasNext()) {
                            builder.append(shapeUris.next().toString());
                            if (shapeUris.hasNext()) {
                                builder.append(", ");
                            }
                        }
                        return builder.toString();
                    }
                    blockFamily = blockManager.getBlockFamily(new BlockUri(def.get().getUrn(), resolvedShapeUris.iterator().next()));
                }
            } else {
                blockFamily = blockManager.getBlockFamily(new BlockUri(def.get().getUrn()));
            }
        }
        if (blockFamily == null) {
            // Should never be reached
            return "Block not found";
        }
        int defaultQuantity = blockFamily.getArchetypeBlock().isStackable() ? 16 : 1;
        int quantity = quantityParam != null ? quantityParam : defaultQuantity;
        return giveBlock(blockFamily, quantity, sender);
    } else if (matchingUris.size() > 1) {
        StringBuilder builder = new StringBuilder();
        builder.append("Non-unique block name, possible matches: ");
        Joiner.on(", ").appendTo(builder, matchingUris);
        return builder.toString();
    }
    return null;
}
Also used : BlockUri(org.terasology.world.block.BlockUri) Iterator(java.util.Iterator) BlockFamily(org.terasology.world.block.family.BlockFamily) ResourceUrn(org.terasology.assets.ResourceUrn) BlockFamilyDefinition(org.terasology.world.block.loader.BlockFamilyDefinition)

Example 8 with BlockFamily

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

the class NeighbourBlockFamilyUpdateSystem method processUpdateForBlockLocation.

private void processUpdateForBlockLocation(Vector3i blockLocation) {
    for (Side side : Side.values()) {
        Vector3i neighborLocation = new Vector3i(blockLocation);
        neighborLocation.add(side.getVector3i());
        if (worldProvider.isBlockRelevant(neighborLocation)) {
            Block neighborBlock = worldProvider.getBlock(neighborLocation);
            final BlockFamily blockFamily = neighborBlock.getBlockFamily();
            if (blockFamily instanceof UpdatesWithNeighboursFamily) {
                UpdatesWithNeighboursFamily neighboursFamily = (UpdatesWithNeighboursFamily) blockFamily;
                Block neighborBlockAfterUpdate = neighboursFamily.getBlockForNeighborUpdate(worldProvider, blockEntityRegistry, neighborLocation, neighborBlock);
                if (neighborBlock != neighborBlockAfterUpdate) {
                    worldProvider.setBlock(neighborLocation, neighborBlockAfterUpdate);
                }
            }
        }
    }
}
Also used : Side(org.terasology.math.Side) UpdatesWithNeighboursFamily(org.terasology.world.block.family.UpdatesWithNeighboursFamily) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) OnChangedBlock(org.terasology.world.OnChangedBlock) BlockFamily(org.terasology.world.block.family.BlockFamily)

Example 9 with BlockFamily

use of org.terasology.world.block.family.BlockFamily 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 10 with BlockFamily

use of org.terasology.world.block.family.BlockFamily 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)

Aggregations

BlockFamily (org.terasology.world.block.family.BlockFamily)16 Block (org.terasology.world.block.Block)7 EntityRef (org.terasology.entitySystem.entity.EntityRef)4 Command (org.terasology.logic.console.commandSystem.annotations.Command)4 LocationComponent (org.terasology.logic.location.LocationComponent)4 Vector3f (org.terasology.math.geom.Vector3f)4 ClientComponent (org.terasology.network.ClientComponent)4 ResourceUrn (org.terasology.assets.ResourceUrn)3 ConsoleCommand (org.terasology.logic.console.commandSystem.ConsoleCommand)3 DropItemEvent (org.terasology.logic.inventory.events.DropItemEvent)3 BlockItemFactory (org.terasology.world.block.items.BlockItemFactory)3 Iterator (java.util.Iterator)2 ModuleManager (org.terasology.engine.module.ModuleManager)2 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)2 Side (org.terasology.math.Side)2 Vector3i (org.terasology.math.geom.Vector3i)2 Module (org.terasology.module.Module)2 WorldProvider (org.terasology.world.WorldProvider)2 Biome (org.terasology.world.biomes.Biome)2 BlockComponent (org.terasology.world.block.BlockComponent)2