Search in sources :

Example 11 with BlockUri

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

use of org.terasology.engine.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.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)

Example 13 with BlockUri

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

the class CeilingSupportingHorizontalFamily method populateBlockMaps.

/**
 * Populates the map with all 8 rotations of the block that are possible.
 * <p>
 * These are all four 90 degree rotations about the Y-axis (YAW) for each case where the TOP side is
 * - facing upwards
 * - facing downwards
 *
 * @param blockBuilder The block builder to use to produce blocks
 * @param shape        The shape the block should be made in
 * @param definition   The definition for the family
 * @param uri          The base URI for the block
 */
private void populateBlockMaps(BlockBuilderHelper blockBuilder, BlockShape shape, BlockFamilyDefinition definition, BlockUri uri) {
    for (Rotation rotation : Rotation.horizontalRotations()) {
        Side horizontalSide = rotation.rotate(Side.FRONT);
        ExtendedSide extendedSideTop = ExtendedSide.getExtendedSideFor(Side.TOP, horizontalSide);
        blocks.put(extendedSideTop, transformBlock(blockBuilder, shape, definition, new BlockUri(uri, new Name(extendedSideTop.name())), rotation, extendedSideTop));
        ExtendedSide extendedSideBottom = ExtendedSide.getExtendedSideFor(Side.BOTTOM, horizontalSide);
        Yaw yaw = Rotation.horizontalRotations().get((rotation.getYaw().getIndex() + 2) % 4).getYaw();
        blocks.put(extendedSideBottom, transformBlock(blockBuilder, shape, definition, new BlockUri(uri, new Name(extendedSideBottom.name())), Rotation.rotate(yaw, Pitch.CLOCKWISE_180, Roll.NONE), extendedSideBottom));
    }
}
Also used : Side(org.terasology.engine.math.Side) BlockUri(org.terasology.engine.world.block.BlockUri) Rotation(org.terasology.engine.math.Rotation) Yaw(org.terasology.engine.math.Yaw) Name(org.terasology.gestalt.naming.Name)

Example 14 with BlockUri

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

the class MultiConnectFamily method registerBlock.

/**
 * @param root The root block URI of the family
 * @param definition The definition of the block family as passed down from the engine
 * @param blockBuilder The block builder to make the blocks in the family
 * @param name The name of the section of the block to be registered, ex: "no_connections"
 * @param sides A byte representing the sides which should be connected for this block
 * @param rotations All of the ways the block should be rotated
 * @return All of the rotations possible for the block with the given sides
 */
public Set<Block> registerBlock(BlockUri root, BlockFamilyDefinition definition, BlockBuilderHelper blockBuilder, String name, byte sides, Iterable<Rotation> rotations) {
    Set<Block> result = Sets.newLinkedHashSet();
    for (Rotation rotation : rotations) {
        byte sideBits = 0;
        for (Side side : SideBitFlag.getSides(sides)) {
            sideBits |= SideBitFlag.getSide(rotation.rotate(side));
        }
        Block block = blockBuilder.constructTransformedBlock(definition, name, rotation, new BlockUri(root, new Name(String.valueOf(sideBits))), this);
        blocks.put(sideBits, block);
        result.add(block);
    }
    return result;
}
Also used : Side(org.terasology.engine.math.Side) BlockUri(org.terasology.engine.world.block.BlockUri) Block(org.terasology.engine.world.block.Block) Rotation(org.terasology.engine.math.Rotation) Name(org.terasology.gestalt.naming.Name)

Aggregations

BlockUri (org.terasology.engine.world.block.BlockUri)14 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)8 BeforeEach (org.junit.jupiter.api.BeforeEach)6 BlockManagerImpl (org.terasology.engine.world.block.internal.BlockManagerImpl)6 BlockFamilyDefinitionData (org.terasology.engine.world.block.loader.BlockFamilyDefinitionData)6 NullWorldAtlas (org.terasology.engine.world.block.tiles.NullWorldAtlas)6 AssetManager (org.terasology.gestalt.assets.management.AssetManager)6 Block (org.terasology.engine.world.block.Block)4 ExtraBlockDataManager (org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager)4 Rotation (org.terasology.engine.math.Rotation)3 Side (org.terasology.engine.math.Side)3 Name (org.terasology.gestalt.naming.Name)3 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)2 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)2 BlockFamilyDefinition (org.terasology.engine.world.block.loader.BlockFamilyDefinition)2 SunlightPropagationRules (org.terasology.engine.world.propagation.light.SunlightPropagationRules)2 SunlightRegenPropagationRules (org.terasology.engine.world.propagation.light.SunlightRegenPropagationRules)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1