Search in sources :

Example 21 with ClientComponent

use of org.terasology.network.ClientComponent in project Terasology by MovingBlocks.

the class CharacterSystem method getInstigatorName.

/**
 * Extracts the name from an entity.
 * If the entity is a character, then the display name from the {@link ClientComponent#clientInfo} is used.
 * Otherwise the entity itself is checked for a {@link DisplayNameComponent}.
 * In the last case, the prefab name of the entity is used, e.g. "engine:player" will be parsed to "Player".
 * @param instigator The entity for which an instigator name is needed.
 * @return The instigator name.
 */
public String getInstigatorName(EntityRef instigator) {
    if (instigator.hasComponent(CharacterComponent.class)) {
        EntityRef instigatorClient = instigator.getComponent(CharacterComponent.class).controller;
        EntityRef instigatorClientInfo = instigatorClient.getComponent(ClientComponent.class).clientInfo;
        DisplayNameComponent displayNameComponent = instigatorClientInfo.getComponent(DisplayNameComponent.class);
        return displayNameComponent.name;
    } else if (instigator.getParentPrefab() != null) {
        // A DisplayName can be specified in the entity prefab
        // Otherwise, the game will attempt to generate one from the name of that prefab
        Prefab parentPrefab = instigator.getParentPrefab();
        if (parentPrefab.hasComponent(DisplayNameComponent.class)) {
            DisplayNameComponent displayNameComponent = parentPrefab.getComponent(DisplayNameComponent.class);
            return displayNameComponent.name;
        } else {
            String instigatorName = parentPrefab.getName();
            // getParentPrefab.getName() returns a ResourceUrn String such as "engine:player"
            // The following calls change the damage type to be more readable
            // For instance, "engine:player" becomes "Player"
            instigatorName = instigatorName.replaceAll(".*:(.*)", "$1");
            instigatorName = Character.toUpperCase(instigatorName.charAt(0)) + instigatorName.substring(1);
            return instigatorName;
        }
    } else {
        return null;
    }
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) PlayerCharacterComponent(org.terasology.logic.players.PlayerCharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 22 with ClientComponent

use of org.terasology.network.ClientComponent in project Terasology by MovingBlocks.

the class CharacterSystem method getPlayerNameFromCharacter.

private String getPlayerNameFromCharacter(EntityRef character) {
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        return "?";
    }
    EntityRef controller = characterComponent.controller;
    ClientComponent clientComponent = controller.getComponent(ClientComponent.class);
    EntityRef clientInfo = clientComponent.clientInfo;
    DisplayNameComponent displayNameComponent = clientInfo.getComponent(DisplayNameComponent.class);
    if (displayNameComponent == null) {
        return "?";
    }
    return displayNameComponent.name;
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) PlayerCharacterComponent(org.terasology.logic.players.PlayerCharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent)

Example 23 with ClientComponent

use of org.terasology.network.ClientComponent in project Terasology by MovingBlocks.

the class InteractionSystem method onInteractionStartPredicted.

@ReceiveEvent(components = { InteractionScreenComponent.class })
public void onInteractionStartPredicted(InteractionStartPredicted event, EntityRef container, InteractionScreenComponent interactionScreenComponent) {
    EntityRef investigator = event.getInstigator();
    CharacterComponent characterComponent = investigator.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        logger.error("Interaction start predicted for entity without character component");
        return;
    }
    ClientComponent controller = characterComponent.controller.getComponent(ClientComponent.class);
    if (controller != null && controller.local) {
        nuiManager.closeAllScreens();
        nuiManager.pushScreen(interactionScreenComponent.screen);
    }
}
Also used : CharacterComponent(org.terasology.logic.characters.CharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 24 with ClientComponent

use of org.terasology.network.ClientComponent in project Terasology by MovingBlocks.

the class ChatSystem method whisper.

@Command(runOnServer = true, requiredPermission = PermissionManager.CHAT_PERMISSION, shortDescription = "Sends a private message to a specified user")
public String whisper(@Sender EntityRef sender, @CommandParam(value = "user", suggester = OnlineUsernameSuggester.class) String username, @CommandParam("message") String[] message) {
    String messageToString = join(message, " ");
    Iterable<EntityRef> clients = entityManager.getEntitiesWith(ClientComponent.class);
    EntityRef targetClient = null;
    boolean unique = true;
    for (EntityRef client : clients) {
        ClientComponent clientComponent = client.getComponent(ClientComponent.class);
        DisplayNameComponent displayNameComponent = clientComponent.clientInfo.getComponent(DisplayNameComponent.class);
        if (displayNameComponent == null) {
            continue;
        }
        if (displayNameComponent.name.equalsIgnoreCase(username)) {
            if (targetClient == null) {
                targetClient = client;
            } else {
                unique = false;
                break;
            }
        }
    }
    if (!unique) {
        targetClient = null;
        for (EntityRef client : clients) {
            ClientComponent clientComponent = client.getComponent(ClientComponent.class);
            DisplayNameComponent displayNameComponent = clientComponent.clientInfo.getComponent(DisplayNameComponent.class);
            if (displayNameComponent == null) {
                continue;
            }
            if (displayNameComponent.name.equals(username)) {
                if (targetClient == null) {
                    targetClient = client;
                } else {
                    return FontColor.getColored("Found more users with name '" + username + "'.", ConsoleColors.ERROR);
                }
            }
        }
    }
    if (targetClient == null) {
        return FontColor.getColored("User with name '" + username + "' not found.", ConsoleColors.ERROR);
    }
    ClientComponent senderClientComponent = sender.getComponent(ClientComponent.class);
    ClientComponent targetClientComponent = targetClient.getComponent(ClientComponent.class);
    DisplayNameComponent targetDisplayNameComponent = targetClientComponent.clientInfo.getComponent(DisplayNameComponent.class);
    String targetMessage = FontColor.getColored("*whispering* ", ConsoleColors.ERROR) + FontColor.getColored(messageToString, ConsoleColors.CHAT);
    String senderMessage = "You -> " + targetDisplayNameComponent.name + ": " + FontColor.getColored(messageToString, ConsoleColors.CHAT);
    targetClient.send(new ChatMessageEvent(targetMessage, senderClientComponent.clientInfo));
    return senderMessage;
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) Command(org.terasology.logic.console.commandSystem.annotations.Command)

Example 25 with ClientComponent

use of org.terasology.network.ClientComponent in project Terasology by MovingBlocks.

the class ConsoleImpl method clientHasPermission.

private boolean clientHasPermission(EntityRef callingClient, String requiredPermission) {
    Preconditions.checkNotNull(callingClient, "The calling client must not be null!");
    PermissionManager permissionManager = context.get(PermissionManager.class);
    boolean hasPermission = true;
    if (permissionManager != null && requiredPermission != null && !requiredPermission.equals(PermissionManager.NO_PERMISSION)) {
        hasPermission = false;
        ClientComponent clientComponent = callingClient.getComponent(ClientComponent.class);
        if (permissionManager.hasPermission(clientComponent.clientInfo, requiredPermission)) {
            hasPermission = true;
        }
    }
    return hasPermission;
}
Also used : PermissionManager(org.terasology.logic.permission.PermissionManager) ClientComponent(org.terasology.network.ClientComponent)

Aggregations

ClientComponent (org.terasology.network.ClientComponent)64 Command (org.terasology.logic.console.commandSystem.annotations.Command)37 EntityRef (org.terasology.entitySystem.entity.EntityRef)28 LocationComponent (org.terasology.logic.location.LocationComponent)13 Vector3f (org.terasology.math.geom.Vector3f)13 CharacterMovementComponent (org.terasology.logic.characters.CharacterMovementComponent)11 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)10 ConsoleCommand (org.terasology.logic.console.commandSystem.ConsoleCommand)8 CharacterTeleportEvent (org.terasology.logic.characters.CharacterTeleportEvent)7 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)5 Prefab (org.terasology.entitySystem.prefab.Prefab)5 LocalPlayer (org.terasology.logic.players.LocalPlayer)4 Quat4f (org.terasology.math.geom.Quat4f)4 ComponentSystemManager (org.terasology.engine.ComponentSystemManager)3 SetMovementModeEvent (org.terasology.logic.characters.events.SetMovementModeEvent)3 DropItemEvent (org.terasology.logic.inventory.events.DropItemEvent)3 Client (org.terasology.network.Client)3 ResourceUrn (org.terasology.assets.ResourceUrn)2 EngineEntityManager (org.terasology.entitySystem.entity.internal.EngineEntityManager)2 EventSystem (org.terasology.entitySystem.event.internal.EventSystem)2