Search in sources :

Example 11 with BlockFamily

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

the class CoreCommands method spawnBlock.

/**
 * Spawns a block in front of the player
 *
 * @param sender    Sender of command
 * @param blockName String containing name of block to spawn
 * @return String containg final message
 */
@Command(shortDescription = "Spawns a block in front of the player", helpText = "Spawns the specified block as a " + "item in front of the player. You can simply pick it up.", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String spawnBlock(@Sender EntityRef sender, @CommandParam("blockName") String blockName) {
    ClientComponent clientComponent = sender.getComponent(ClientComponent.class);
    LocationComponent characterLocation = clientComponent.character.getComponent(LocationComponent.class);
    Vector3f spawnPos = characterLocation.getWorldPosition(new Vector3f());
    Vector3f offset = characterLocation.getWorldDirection(new Vector3f());
    offset.mul(3);
    spawnPos.add(offset);
    BlockFamily block = blockManager.getBlockFamily(blockName);
    if (block == null) {
        return "";
    }
    BlockItemFactory blockItemFactory = new BlockItemFactory(entityManager);
    EntityRef blockItem = blockItemFactory.newInstance(block);
    blockItem.send(new DropItemEvent(spawnPos));
    return "Spawned block.";
}
Also used : DropItemEvent(org.terasology.engine.logic.inventory.events.DropItemEvent) Vector3f(org.joml.Vector3f) BlockItemFactory(org.terasology.engine.world.block.items.BlockItemFactory) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) ClientComponent(org.terasology.engine.network.ClientComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command) ConsoleCommand(org.terasology.engine.logic.console.commandSystem.ConsoleCommand)

Example 12 with BlockFamily

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

the class ItemPickupAuthoritySystem method updateExtentsOnBlockItemBoxShape.

@ReceiveEvent
public void updateExtentsOnBlockItemBoxShape(OnAddedComponent event, EntityRef itemEntity, BlockItemComponent blockItemComponent, BoxShapeComponent boxShapeComponent) {
    BlockFamily blockFamily = blockItemComponent.blockFamily;
    if (blockFamily == null) {
        LOGGER.warn("Prefab " + itemEntity.getParentPrefab().getName() + " does not have a block family");
        return;
    }
    if (blockFamily.getArchetypeBlock().getCollisionShape() instanceof btBoxShape) {
        Vector3f extents = ((btBoxShape) blockFamily.getArchetypeBlock().getCollisionShape()).getHalfExtentsWithoutMargin();
        extents.x = Math.max(extents.x, 0.5f);
        extents.y = Math.max(extents.y, 0.5f);
        extents.z = Math.max(extents.z, 0.5f);
        boxShapeComponent.extents.set(extents);
        itemEntity.saveComponent(boxShapeComponent);
    }
}
Also used : com.badlogic.gdx.physics.bullet.collision.btBoxShape(com.badlogic.gdx.physics.bullet.collision.btBoxShape) Vector3f(org.joml.Vector3f) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 13 with BlockFamily

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

the class ReadWriteStorageManager method addGameManifestToSaveTransaction.

private void addGameManifestToSaveTransaction(SaveTransactionBuilder saveTransactionBuilder) {
    BlockManager blockManager = CoreRegistry.get(BlockManager.class);
    UniverseConfig universeConfig = config.getUniverseConfig();
    Time time = CoreRegistry.get(Time.class);
    Game game = CoreRegistry.get(Game.class);
    GameManifest gameManifest = new GameManifest(game.getName(), game.getSeed(), time.getGameTimeInMs());
    for (Module module : CoreRegistry.get(ModuleManager.class).getEnvironment()) {
        gameManifest.addModule(module.getId(), module.getVersion());
    }
    List<String> registeredBlockFamilies = Lists.newArrayList();
    for (BlockFamily family : blockManager.listRegisteredBlockFamilies()) {
        registeredBlockFamilies.add(family.getURI().toString());
    }
    gameManifest.setRegisteredBlockFamilies(registeredBlockFamilies);
    gameManifest.setBlockIdMap(blockManager.getBlockIdMap());
    List<WorldInfo> worlds = universeConfig.getWorlds();
    for (WorldInfo worldInfo : worlds) {
        gameManifest.addWorld(worldInfo);
    }
    WorldGenerator worldGenerator = CoreRegistry.get(WorldGenerator.class);
    if (worldGenerator != null) {
        WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
        Map<String, Component> params = worldConfigurator.getProperties();
        gameManifest.setModuleConfigs(worldGenerator.getUri(), params);
    }
    saveTransactionBuilder.setGameManifest(gameManifest);
}
Also used : WorldGenerator(org.terasology.engine.world.generator.WorldGenerator) WorldConfigurator(org.terasology.engine.world.generator.WorldConfigurator) Time(org.terasology.engine.core.Time) UniverseConfig(org.terasology.engine.config.UniverseConfig) ModuleManager(org.terasology.engine.core.module.ModuleManager) Game(org.terasology.engine.game.Game) GameManifest(org.terasology.engine.game.GameManifest) BlockManager(org.terasology.engine.world.block.BlockManager) WorldInfo(org.terasology.engine.world.internal.WorldInfo) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) Module(org.terasology.gestalt.module.Module) Component(org.terasology.gestalt.entitysystem.component.Component) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ClientComponent(org.terasology.engine.network.ClientComponent)

Example 14 with BlockFamily

use of org.terasology.engine.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.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 15 with BlockFamily

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

Aggregations

BlockFamily (org.terasology.engine.world.block.family.BlockFamily)15 Block (org.terasology.engine.world.block.Block)7 Vector3f (org.joml.Vector3f)5 LocationComponent (org.terasology.engine.logic.location.LocationComponent)5 ClientComponent (org.terasology.engine.network.ClientComponent)5 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)4 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)4 ConsoleCommand (org.terasology.engine.logic.console.commandSystem.ConsoleCommand)3 DropItemEvent (org.terasology.engine.logic.inventory.events.DropItemEvent)3 BlockItemFactory (org.terasology.engine.world.block.items.BlockItemFactory)3 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)3 Iterator (java.util.Iterator)2 Vector3i (org.joml.Vector3i)2 ModuleManager (org.terasology.engine.core.module.ModuleManager)2 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)2 Side (org.terasology.engine.math.Side)2 BlockComponent (org.terasology.engine.world.block.BlockComponent)2 BlockManager (org.terasology.engine.world.block.BlockManager)2 BlockUri (org.terasology.engine.world.block.BlockUri)2 BlockFamilyDefinition (org.terasology.engine.world.block.loader.BlockFamilyDefinition)2