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;
}
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;
}
}
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;
}
}
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;
}
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;
}
Aggregations