Search in sources :

Example 1 with DisplayNameComponent

use of org.terasology.engine.logic.common.DisplayNameComponent 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.engine.logic.common.DisplayNameComponent) PlayerCharacterComponent(org.terasology.engine.logic.players.PlayerCharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ClientComponent(org.terasology.engine.network.ClientComponent)

Example 2 with DisplayNameComponent

use of org.terasology.engine.logic.common.DisplayNameComponent 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.engine.logic.common.DisplayNameComponent) PlayerCharacterComponent(org.terasology.engine.logic.players.PlayerCharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ClientComponent(org.terasology.engine.network.ClientComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab)

Example 3 with DisplayNameComponent

use of org.terasology.engine.logic.common.DisplayNameComponent in project Terasology by MovingBlocks.

the class CharacterSystem method getDamageTypeName.

/**
 * Extracts the damage type name from a prefab. If the prefab has a {@link DisplayNameComponent}, it will be used.
 * Otherwise the damage type name is parsed, e.g. "engine:directDamage" will become "Direct Damage".
 *
 * @param damageType The damage type prefab.
 * @return A readable name for the damage type.
 */
public String getDamageTypeName(Prefab damageType) {
    // Otherwise, the game will attempt to generate one from the name of that prefab
    if (damageType.hasComponent(DisplayNameComponent.class)) {
        DisplayNameComponent displayNameComponent = damageType.getComponent(DisplayNameComponent.class);
        return displayNameComponent.name;
    } else {
        logger.info(String.format("%s is missing a readable DisplayName", damageType.getName()));
        String damageTypeName = damageType.getName();
        // damageType.getName() returns a ResourceUrn String such as "engine:directDamage"
        // The following calls change the damage type to be more readable
        // For instance, "engine:directDamage" becomes "Direct Damage"
        damageTypeName = damageTypeName.replaceAll(".*:(.*)", "$1");
        damageTypeName = damageTypeName.replaceAll("([A-Z])", " $1");
        damageTypeName = Character.toUpperCase(damageTypeName.charAt(0)) + damageTypeName.substring(1);
        return damageTypeName;
    }
}
Also used : DisplayNameComponent(org.terasology.engine.logic.common.DisplayNameComponent)

Example 4 with DisplayNameComponent

use of org.terasology.engine.logic.common.DisplayNameComponent in project Terasology by MovingBlocks.

the class AbstractClient method findNamesOfOtherPlayers.

/**
 * Creates a {@code HashSet<String>} of all connected player names.
 * @param entityManager
 * @param player Client name to make sure it doesn't put its own name in the list.
 * @return Returns all connected player names.
 */
private Set<String> findNamesOfOtherPlayers(EntityManager entityManager, EntityRef player) {
    Set<String> otherNames = new HashSet<>();
    for (EntityRef clientInfo : entityManager.getEntitiesWith(ClientInfoComponent.class)) {
        if (!clientInfo.equals(player)) {
            DisplayNameComponent displayInfo = clientInfo.getComponent(DisplayNameComponent.class);
            String usedName = displayInfo.name;
            otherNames.add(usedName);
        }
    }
    return otherNames;
}
Also used : DisplayNameComponent(org.terasology.engine.logic.common.DisplayNameComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) HashSet(java.util.HashSet)

Example 5 with DisplayNameComponent

use of org.terasology.engine.logic.common.DisplayNameComponent in project Terasology by MovingBlocks.

the class UsernameSuggester method suggest.

@Override
public Set<String> suggest(EntityRef sender, Object... resolvedParameters) {
    Set<String> clientNames = Sets.newHashSet();
    for (EntityRef clientInfo : entityManager.getEntitiesWith(ClientInfoComponent.class)) {
        DisplayNameComponent displayNameComponent = clientInfo.getComponent(DisplayNameComponent.class);
        clientNames.add(displayNameComponent.name);
    }
    return clientNames;
}
Also used : DisplayNameComponent(org.terasology.engine.logic.common.DisplayNameComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Aggregations

DisplayNameComponent (org.terasology.engine.logic.common.DisplayNameComponent)21 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)18 ClientComponent (org.terasology.engine.network.ClientComponent)13 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)10 Vector3f (org.joml.Vector3f)4 CharacterTeleportEvent (org.terasology.engine.logic.characters.CharacterTeleportEvent)4 LocationComponent (org.terasology.engine.logic.location.LocationComponent)4 ColorComponent (org.terasology.engine.network.ColorComponent)3 PlayerCharacterComponent (org.terasology.engine.logic.players.PlayerCharacterComponent)2 HashSet (java.util.HashSet)1 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)1 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)1 CharacterMovementComponent (org.terasology.engine.logic.characters.CharacterMovementComponent)1 MovementMode (org.terasology.engine.logic.characters.MovementMode)1 SetMovementModeEvent (org.terasology.engine.logic.characters.events.SetMovementModeEvent)1 ChatMessageEvent (org.terasology.engine.logic.chat.ChatMessageEvent)1 Client (org.terasology.engine.network.Client)1 ClientInfoComponent (org.terasology.engine.network.ClientInfoComponent)1 NetworkComponent (org.terasology.engine.network.NetworkComponent)1 Color (org.terasology.nui.Color)1