Search in sources :

Example 1 with CommandParam

use of org.terasology.engine.logic.console.commandSystem.annotations.CommandParam in project Terasology by MovingBlocks.

the class CoreCommands method spawnPrefab.

/**
 * Spawns an instance of a prefab in the world
 *
 * @param sender     Sender of command
 * @param prefabName String containing prefab name
 * @return String containing final message
 */
@Command(shortDescription = "Spawns an instance of a prefab in the world", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String spawnPrefab(@Sender EntityRef sender, @CommandParam("prefabId") String prefabName) {
    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(2);
    spawnPos.add(offset);
    Vector3f dir = characterLocation.getWorldDirection(new Vector3f());
    dir.y = 0;
    if (dir.lengthSquared() > 0.001f) {
        dir.normalize();
    } else {
        dir.set(Direction.FORWARD.asVector3f());
    }
    return Assets.getPrefab(prefabName).map(prefab -> {
        LocationComponent loc = prefab.getComponent(LocationComponent.class);
        if (loc != null) {
            entityManager.create(prefab, spawnPos);
            return "Done";
        } else {
            return "Prefab cannot be spawned (no location component)";
        }
    }).orElse("Unknown prefab");
}
Also used : DropItemEvent(org.terasology.engine.logic.inventory.events.DropItemEvent) Server(org.terasology.engine.network.Server) Arrays(java.util.Arrays) Component(org.terasology.gestalt.entitysystem.component.Component) In(org.terasology.engine.registry.In) SystemConfig(org.terasology.engine.config.SystemConfig) Sender(org.terasology.engine.logic.console.commandSystem.annotations.Sender) Console(org.terasology.engine.logic.console.Console) SkinSuggester(org.terasology.engine.logic.console.suggesters.SkinSuggester) DisplayDevice(org.terasology.engine.core.subsystem.DisplayDevice) NUIEditorScreen(org.terasology.engine.rendering.nui.editor.layers.NUIEditorScreen) Locale(java.util.Locale) BlockUri(org.terasology.engine.world.block.BlockUri) FontColor(org.terasology.nui.FontColor) Vector3f(org.joml.Vector3f) TranslationProject(org.terasology.engine.i18n.TranslationProject) Path(java.nio.file.Path) StateLoading(org.terasology.engine.core.modes.StateLoading) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command) TranslationSystem(org.terasology.engine.i18n.TranslationSystem) ConsoleCommand(org.terasology.engine.logic.console.commandSystem.ConsoleCommand) CommandParam(org.terasology.engine.logic.console.commandSystem.annotations.CommandParam) ConsoleColors(org.terasology.engine.logic.console.ConsoleColors) GameEngine(org.terasology.engine.core.GameEngine) CommandNameSuggester(org.terasology.engine.logic.console.suggesters.CommandNameSuggester) WaitPopup(org.terasology.engine.rendering.nui.layers.mainMenu.WaitPopup) Set(java.util.Set) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) NUISkinEditorSystem(org.terasology.engine.rendering.nui.editor.systems.NUISkinEditorSystem) Streams(com.google.common.collect.Streams) BlockFamilyDefinition(org.terasology.engine.world.block.loader.BlockFamilyDefinition) Instant(java.time.Instant) UISkinAsset(org.terasology.nui.skin.UISkinAsset) Collectors(java.util.stream.Collectors) Direction(org.terasology.engine.math.Direction) NUIManager(org.terasology.engine.rendering.nui.NUIManager) List(java.util.List) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) RegisterSystem(org.terasology.engine.entitySystem.systems.RegisterSystem) NUISkinEditorScreen(org.terasology.engine.rendering.nui.editor.layers.NUISkinEditorScreen) ScreenSuggester(org.terasology.engine.logic.console.suggesters.ScreenSuggester) Optional(java.util.Optional) Name(org.terasology.gestalt.naming.Name) TerasologyConstants(org.terasology.engine.core.TerasologyConstants) UIElement(org.terasology.nui.asset.UIElement) ModuleManager(org.terasology.engine.core.module.ModuleManager) EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) NetworkMode(org.terasology.engine.network.NetworkMode) Callable(java.util.concurrent.Callable) BlockItemFactory(org.terasology.engine.world.block.items.BlockItemFactory) ArrayList(java.util.ArrayList) WorldDumper(org.terasology.engine.persistence.WorldDumper) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Time(org.terasology.engine.core.Time) StreamSupport(java.util.stream.StreamSupport) AssetManager(org.terasology.gestalt.assets.management.AssetManager) PingService(org.terasology.engine.network.PingService) MessagePopup(org.terasology.engine.rendering.nui.layers.mainMenu.MessagePopup) Assets(org.terasology.engine.utilities.Assets) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) PermissionManager(org.terasology.engine.logic.permission.PermissionManager) NUIEditorSystem(org.terasology.engine.rendering.nui.editor.systems.NUIEditorSystem) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ClientComponent(org.terasology.engine.network.ClientComponent) EntityManager(org.terasology.engine.entitySystem.entity.EntityManager) PrefabManager(org.terasology.engine.entitySystem.prefab.PrefabManager) IOException(java.io.IOException) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) UnknownHostException(java.net.UnknownHostException) BaseComponentSystem(org.terasology.engine.entitySystem.systems.BaseComponentSystem) Ordering(com.google.common.collect.Ordering) BlockManager(org.terasology.engine.world.block.BlockManager) PrefabSerializer(org.terasology.engine.persistence.serializers.PrefabSerializer) JoinStatus(org.terasology.engine.network.JoinStatus) NetworkSystem(org.terasology.engine.network.NetworkSystem) PathManager(org.terasology.engine.core.PathManager) Vector3f(org.joml.Vector3f) ClientComponent(org.terasology.engine.network.ClientComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command) ConsoleCommand(org.terasology.engine.logic.console.commandSystem.ConsoleCommand)

Example 2 with CommandParam

use of org.terasology.engine.logic.console.commandSystem.annotations.CommandParam in project Terasology by MovingBlocks.

the class MethodCommand method getParameterTypeFor.

private static Parameter getParameterTypeFor(Class<?> type, Annotation[] annotations, Context context) {
    for (Annotation annotation : annotations) {
        if (annotation instanceof CommandParam) {
            CommandParam parameterAnnotation = (CommandParam) annotation;
            String name = parameterAnnotation.value();
            Class<? extends CommandParameterSuggester> suggesterClass = parameterAnnotation.suggester();
            boolean required = parameterAnnotation.required();
            CommandParameterSuggester suggester = InjectionHelper.createWithConstructorInjection(suggesterClass, context);
            if (type.isArray()) {
                Class<?> childType = type.getComponentType();
                return CommandParameter.array(name, childType, required, suggester, context);
            } else {
                return CommandParameter.single(name, type, required, suggester, context);
            }
        } else if (annotation instanceof Sender) {
            return MarkerParameters.SENDER;
        }
    }
    return MarkerParameters.INVALID;
}
Also used : Sender(org.terasology.engine.logic.console.commandSystem.annotations.Sender) CommandParam(org.terasology.engine.logic.console.commandSystem.annotations.CommandParam) Annotation(java.lang.annotation.Annotation)

Example 3 with CommandParam

use of org.terasology.engine.logic.console.commandSystem.annotations.CommandParam in project Terasology by MovingBlocks.

the class CoreCommands method dumpEntities.

/**
 * Writes out information on entities having specific components to a text file for debugging
 * If no component names provided - writes out information on all entities
 *
 * @param componentNames string contains one or several component names, if more then one name
 *                       provided - they must be braced with double quotes and all names separated
 *                       by space
 * @return String containing information about number of entities saved
 * @throws IOException thrown when error with saving file occures
 */
@Command(shortDescription = "Writes out information on all entities to a JSON file for debugging", helpText = "Writes entity information out into a file named \"<timestamp>-entityDump.json\"." + " Supports list of component names, which will be used to only save entities that contains" + " one or more of those components. Names should be separated by spaces.")
public String dumpEntities(@CommandParam(value = "componentNames", required = false) String... componentNames) throws IOException {
    int savedEntityCount;
    EngineEntityManager engineEntityManager = (EngineEntityManager) entityManager;
    PrefabSerializer prefabSerializer = new PrefabSerializer(engineEntityManager.getComponentLibrary(), engineEntityManager.getTypeSerializerLibrary());
    WorldDumper worldDumper = new WorldDumper(engineEntityManager, prefabSerializer);
    Path outFile = PathManager.getInstance().getHomePath().resolve(Instant.now() + "-entityDump.json");
    if (componentNames.length == 0) {
        savedEntityCount = worldDumper.save(outFile);
    } else {
        List<Class<? extends Component>> filterComponents = Arrays.stream(componentNames).map(// Trim off whitespace
        String::trim).filter(// Remove empty strings
        o -> !o.isEmpty()).map(// All component class names end with "component"
        o -> o.toLowerCase().endsWith("component") ? o : o + "component").map(o -> Streams.stream(moduleManager.getEnvironment().getSubtypesOf(Component.class)).filter(e -> e.getSimpleName().equalsIgnoreCase(o)).findFirst()).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
        if (!filterComponents.isEmpty()) {
            savedEntityCount = worldDumper.save(outFile, filterComponents);
        } else {
            return "Could not find components matching given names";
        }
    }
    return "Number of entities saved: " + savedEntityCount;
}
Also used : EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) Path(java.nio.file.Path) DropItemEvent(org.terasology.engine.logic.inventory.events.DropItemEvent) Server(org.terasology.engine.network.Server) Arrays(java.util.Arrays) Component(org.terasology.gestalt.entitysystem.component.Component) In(org.terasology.engine.registry.In) SystemConfig(org.terasology.engine.config.SystemConfig) Sender(org.terasology.engine.logic.console.commandSystem.annotations.Sender) Console(org.terasology.engine.logic.console.Console) SkinSuggester(org.terasology.engine.logic.console.suggesters.SkinSuggester) DisplayDevice(org.terasology.engine.core.subsystem.DisplayDevice) NUIEditorScreen(org.terasology.engine.rendering.nui.editor.layers.NUIEditorScreen) Locale(java.util.Locale) BlockUri(org.terasology.engine.world.block.BlockUri) FontColor(org.terasology.nui.FontColor) Vector3f(org.joml.Vector3f) TranslationProject(org.terasology.engine.i18n.TranslationProject) Path(java.nio.file.Path) StateLoading(org.terasology.engine.core.modes.StateLoading) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command) TranslationSystem(org.terasology.engine.i18n.TranslationSystem) ConsoleCommand(org.terasology.engine.logic.console.commandSystem.ConsoleCommand) CommandParam(org.terasology.engine.logic.console.commandSystem.annotations.CommandParam) ConsoleColors(org.terasology.engine.logic.console.ConsoleColors) GameEngine(org.terasology.engine.core.GameEngine) CommandNameSuggester(org.terasology.engine.logic.console.suggesters.CommandNameSuggester) WaitPopup(org.terasology.engine.rendering.nui.layers.mainMenu.WaitPopup) Set(java.util.Set) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) NUISkinEditorSystem(org.terasology.engine.rendering.nui.editor.systems.NUISkinEditorSystem) Streams(com.google.common.collect.Streams) BlockFamilyDefinition(org.terasology.engine.world.block.loader.BlockFamilyDefinition) Instant(java.time.Instant) UISkinAsset(org.terasology.nui.skin.UISkinAsset) Collectors(java.util.stream.Collectors) Direction(org.terasology.engine.math.Direction) NUIManager(org.terasology.engine.rendering.nui.NUIManager) List(java.util.List) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) RegisterSystem(org.terasology.engine.entitySystem.systems.RegisterSystem) NUISkinEditorScreen(org.terasology.engine.rendering.nui.editor.layers.NUISkinEditorScreen) ScreenSuggester(org.terasology.engine.logic.console.suggesters.ScreenSuggester) Optional(java.util.Optional) Name(org.terasology.gestalt.naming.Name) TerasologyConstants(org.terasology.engine.core.TerasologyConstants) UIElement(org.terasology.nui.asset.UIElement) ModuleManager(org.terasology.engine.core.module.ModuleManager) EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) NetworkMode(org.terasology.engine.network.NetworkMode) Callable(java.util.concurrent.Callable) BlockItemFactory(org.terasology.engine.world.block.items.BlockItemFactory) ArrayList(java.util.ArrayList) WorldDumper(org.terasology.engine.persistence.WorldDumper) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Time(org.terasology.engine.core.Time) StreamSupport(java.util.stream.StreamSupport) AssetManager(org.terasology.gestalt.assets.management.AssetManager) PingService(org.terasology.engine.network.PingService) MessagePopup(org.terasology.engine.rendering.nui.layers.mainMenu.MessagePopup) Assets(org.terasology.engine.utilities.Assets) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) PermissionManager(org.terasology.engine.logic.permission.PermissionManager) NUIEditorSystem(org.terasology.engine.rendering.nui.editor.systems.NUIEditorSystem) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ClientComponent(org.terasology.engine.network.ClientComponent) EntityManager(org.terasology.engine.entitySystem.entity.EntityManager) PrefabManager(org.terasology.engine.entitySystem.prefab.PrefabManager) IOException(java.io.IOException) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) UnknownHostException(java.net.UnknownHostException) BaseComponentSystem(org.terasology.engine.entitySystem.systems.BaseComponentSystem) Ordering(com.google.common.collect.Ordering) BlockManager(org.terasology.engine.world.block.BlockManager) PrefabSerializer(org.terasology.engine.persistence.serializers.PrefabSerializer) JoinStatus(org.terasology.engine.network.JoinStatus) NetworkSystem(org.terasology.engine.network.NetworkSystem) PathManager(org.terasology.engine.core.PathManager) Optional(java.util.Optional) WorldDumper(org.terasology.engine.persistence.WorldDumper) Component(org.terasology.gestalt.entitysystem.component.Component) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ClientComponent(org.terasology.engine.network.ClientComponent) PrefabSerializer(org.terasology.engine.persistence.serializers.PrefabSerializer) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command) ConsoleCommand(org.terasology.engine.logic.console.commandSystem.ConsoleCommand)

Aggregations

CommandParam (org.terasology.engine.logic.console.commandSystem.annotations.CommandParam)3 Sender (org.terasology.engine.logic.console.commandSystem.annotations.Sender)3 Ordering (com.google.common.collect.Ordering)2 Streams (com.google.common.collect.Streams)2 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 Path (java.nio.file.Path)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Locale (java.util.Locale)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Callable (java.util.concurrent.Callable)2 Collectors (java.util.stream.Collectors)2 StreamSupport (java.util.stream.StreamSupport)2 Vector3f (org.joml.Vector3f)2 SystemConfig (org.terasology.engine.config.SystemConfig)2 GameEngine (org.terasology.engine.core.GameEngine)2