Search in sources :

Example 6 with BlockUri

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

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

the class BlockCommands method listBlocks.

@Command(shortDescription = "List all available blocks\nYou can filter by adding the beginning of words after the" + "commands, e.g.: \"listBlocks engine: core:\" will list all blocks from the engine and core module", requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String listBlocks(@CommandParam(value = "startsWith", required = false) String[] startsWith) {
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("Used Blocks");
    stringBuilder.append(Console.NEW_LINE);
    stringBuilder.append("-----------");
    stringBuilder.append(Console.NEW_LINE);
    List<BlockUri> registeredBlocks = sortItems(blockManager.listRegisteredBlockUris());
    for (BlockUri blockUri : registeredBlocks) {
        if (!uriStartsWithAnyString(blockUri.toString(), startsWith)) {
            continue;
        }
        stringBuilder.append(blockUri.toString());
        stringBuilder.append(Console.NEW_LINE);
    }
    stringBuilder.append(Console.NEW_LINE);
    stringBuilder.append("Available Blocks");
    stringBuilder.append(Console.NEW_LINE);
    stringBuilder.append("----------------");
    stringBuilder.append(Console.NEW_LINE);
    List<BlockUri> availableBlocks = sortItems(blockExplorer.getAvailableBlockFamilies());
    for (BlockUri blockUri : availableBlocks) {
        if (!uriStartsWithAnyString(blockUri.toString(), startsWith)) {
            continue;
        }
        stringBuilder.append(blockUri.toString());
        stringBuilder.append(Console.NEW_LINE);
    }
    return stringBuilder.toString();
}
Also used : BlockUri(org.terasology.world.block.BlockUri) Command(org.terasology.logic.console.commandSystem.annotations.Command)

Example 8 with BlockUri

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

the class HorizontalBlockFamilyFactory method createBlockFamily.

@Override
public BlockFamily createBlockFamily(BlockFamilyDefinition definition, BlockShape shape, BlockBuilderHelper blockBuilder) {
    if (!definition.isFreeform()) {
        throw new IllegalStateException("A shape cannot be provided when creating a family for a non-freeform block family definition");
    }
    Map<Side, Block> blockMap = Maps.newHashMap();
    for (Rotation rot : Rotation.horizontalRotations()) {
        Side side = rot.rotate(Side.FRONT);
        blockMap.put(side, blockBuilder.constructTransformedBlock(definition, shape, side.toString().toLowerCase(Locale.ENGLISH), rot));
    }
    BlockUri uri;
    if (CUBE_SHAPE_URN.equals(shape.getUrn())) {
        uri = new BlockUri(definition.getUrn());
    } else {
        uri = new BlockUri(definition.getUrn(), shape.getUrn());
    }
    return new HorizontalBlockFamily(uri, getArchetypeSide(), blockMap, definition.getCategories());
}
Also used : Side(org.terasology.math.Side) BlockUri(org.terasology.world.block.BlockUri) Block(org.terasology.world.block.Block) Rotation(org.terasology.math.Rotation)

Example 9 with BlockUri

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

the class SymmetricBlockFamilyFactory method createBlockFamily.

@Override
public BlockFamily createBlockFamily(BlockFamilyDefinition definition, BlockShape shape, BlockBuilderHelper blockBuilder) {
    if (!definition.isFreeform()) {
        throw new IllegalStateException("A shape cannot be provided when creating a family for a non-freeform block family definition");
    }
    Block block = blockBuilder.constructSimpleBlock(definition, shape);
    BlockUri uri;
    if (CUBE_SHAPE_URN.equals(shape.getUrn())) {
        uri = new BlockUri(definition.getUrn());
    } else {
        uri = new BlockUri(definition.getUrn(), shape.getUrn());
    }
    return new SymmetricFamily(uri, block, definition.getCategories());
}
Also used : BlockUri(org.terasology.world.block.BlockUri) Block(org.terasology.world.block.Block)

Example 10 with BlockUri

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

the class UpdatesWithNeighboursFamilyFactory method createBlockFamily.

@Override
public BlockFamily createBlockFamily(BlockFamilyDefinition definition, BlockBuilderHelper blockBuilder) {
    TByteObjectMap<String>[] basicBlocks = new TByteObjectMap[7];
    TByteObjectMap<Block> blocksForConnections = new TByteObjectHashMap<>();
    addConnections(basicBlocks, 0, NO_CONNECTIONS);
    addConnections(basicBlocks, 1, ONE_CONNECTION);
    addConnections(basicBlocks, 2, TWO_CONNECTIONS_LINE);
    addConnections(basicBlocks, 2, TWO_CONNECTIONS_CORNER);
    addConnections(basicBlocks, 3, THREE_CONNECTIONS_CORNER);
    addConnections(basicBlocks, 3, THREE_CONNECTIONS_T);
    addConnections(basicBlocks, 4, FOUR_CONNECTIONS_CROSS);
    addConnections(basicBlocks, 4, FOUR_CONNECTIONS_SIDE);
    addConnections(basicBlocks, 5, FIVE_CONNECTIONS);
    addConnections(basicBlocks, 6, SIX_CONNECTIONS);
    BlockUri blockUri = new BlockUri(definition.getUrn());
    // Now make sure we have all combinations based on the basic set (above) and rotations
    for (byte connections = 0; connections < 64; connections++) {
        // Only the allowed connections should be created
        if ((connections & connectionSides) == connections) {
            Block block = constructBlockForConnections(connections, blockBuilder, definition, basicBlocks);
            if (block == null) {
                throw new IllegalStateException("Unable to find correct block definition for connections: " + connections);
            }
            block.setUri(new BlockUri(blockUri, new Name(String.valueOf(connections))));
            blocksForConnections.put(connections, block);
        }
    }
    final Block archetypeBlock = blocksForConnections.get(SideBitFlag.getSides(Side.RIGHT, Side.LEFT));
    return new UpdatesWithNeighboursFamily(connectionCondition, blockUri, definition.getCategories(), archetypeBlock, blocksForConnections, connectionSides);
}
Also used : BlockUri(org.terasology.world.block.BlockUri) TByteObjectMap(gnu.trove.map.TByteObjectMap) Block(org.terasology.world.block.Block) TByteObjectHashMap(gnu.trove.map.hash.TByteObjectHashMap) Name(org.terasology.naming.Name)

Aggregations

BlockUri (org.terasology.world.block.BlockUri)15 ResourceUrn (org.terasology.assets.ResourceUrn)7 Block (org.terasology.world.block.Block)7 Before (org.junit.Before)6 AssetManager (org.terasology.assets.management.AssetManager)6 SymmetricBlockFamilyFactory (org.terasology.world.block.family.SymmetricBlockFamilyFactory)6 BlockManagerImpl (org.terasology.world.block.internal.BlockManagerImpl)6 BlockFamilyDefinitionData (org.terasology.world.block.loader.BlockFamilyDefinitionData)6 NullWorldAtlas (org.terasology.world.block.tiles.NullWorldAtlas)6 BiomeManager (org.terasology.world.biomes.BiomeManager)4 Rotation (org.terasology.math.Rotation)3 Side (org.terasology.math.Side)3 Command (org.terasology.logic.console.commandSystem.annotations.Command)2 Vector3i (org.terasology.math.geom.Vector3i)2 BlockFamily (org.terasology.world.block.family.BlockFamily)2 SunlightPropagationRules (org.terasology.world.propagation.light.SunlightPropagationRules)2 SunlightRegenPropagationRules (org.terasology.world.propagation.light.SunlightRegenPropagationRules)2 TByteObjectMap (gnu.trove.map.TByteObjectMap)1 TByteObjectHashMap (gnu.trove.map.hash.TByteObjectHashMap)1 Iterator (java.util.Iterator)1