Search in sources :

Example 6 with BlockFamilyDefinition

use of org.terasology.engine.world.block.loader.BlockFamilyDefinition in project Terasology by MovingBlocks.

the class CoreCommands method findBlockMatches.

/**
 * List blocks that match searched string
 *
 * @param searchLowercase searched string
 * @return List of blocks that match searched string
 */
private List<String> findBlockMatches(String searchLowercase) {
    ResourceUrn curUrn;
    List<String> outputList = new ArrayList<>();
    for (ResourceUrn urn : assetManager.getAvailableAssets(BlockFamilyDefinition.class)) {
        // Current urn for logging purposes to find the broken urn
        curUrn = urn;
        try {
            Optional<BlockFamilyDefinition> def = assetManager.getAsset(urn, BlockFamilyDefinition.class);
            if (def.isPresent() && def.get().isLoadable() && matchesSearch(searchLowercase, def.get())) {
                outputList.add(new BlockUri(def.get().getUrn()).toString());
            }
        } catch (Exception e) {
            // If a prefab is broken , it will throw an exception
            console.addMessage("Note : Search may not return results if invalid assets are present");
            console.addMessage("Error parsing : " + curUrn.toString());
            console.addMessage(e.toString());
        }
    }
    return outputList;
}
Also used : BlockUri(org.terasology.engine.world.block.BlockUri) ArrayList(java.util.ArrayList) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) BlockFamilyDefinition(org.terasology.engine.world.block.loader.BlockFamilyDefinition) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 7 with BlockFamilyDefinition

use of org.terasology.engine.world.block.loader.BlockFamilyDefinition 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.engine.world.block.BlockUri) Iterator(java.util.Iterator) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) BlockFamilyDefinition(org.terasology.engine.world.block.loader.BlockFamilyDefinition)

Aggregations

BlockFamilyDefinition (org.terasology.engine.world.block.loader.BlockFamilyDefinition)7 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)4 PojoPrefab (org.terasology.engine.entitySystem.prefab.internal.PojoPrefab)3 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)3 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 ModuleManager (org.terasology.engine.core.module.ModuleManager)2 BehaviorTree (org.terasology.engine.logic.behavior.asset.BehaviorTree)2 BlockUri (org.terasology.engine.world.block.BlockUri)2 BlockFamilyDefinitionData (org.terasology.engine.world.block.loader.BlockFamilyDefinitionData)2 BlockFamilyDefinitionFormat (org.terasology.engine.world.block.loader.BlockFamilyDefinitionFormat)2 BlockShapeImpl (org.terasology.engine.world.block.shapes.BlockShapeImpl)2 BlockSounds (org.terasology.engine.world.block.sounds.BlockSounds)2 BlockTile (org.terasology.engine.world.block.tiles.BlockTile)2 UIElement (org.terasology.nui.asset.UIElement)2 UISkinAsset (org.terasology.nui.skin.UISkinAsset)2 UnknownHostException (java.net.UnknownHostException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1