use of org.terasology.logic.common.DisplayNameComponent 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;
}
use of org.terasology.logic.common.DisplayNameComponent in project Terasology by MovingBlocks.
the class ServerCommands method renameUser.
@Command(shortDescription = "Rename a user", runOnServer = true, requiredPermission = PermissionManager.USER_MANAGEMENT_PERMISSION)
public String renameUser(@CommandParam(value = "userName", suggester = UsernameSuggester.class) String userName, @CommandParam(value = "newUserName") String newUserName) {
Iterable<EntityRef> clientInfoEntities = entityManager.getEntitiesWith(ClientInfoComponent.class);
for (EntityRef clientInfo : clientInfoEntities) {
DisplayNameComponent nameComp = clientInfo.getComponent(DisplayNameComponent.class);
if (newUserName.equalsIgnoreCase(nameComp.name)) {
throw new IllegalArgumentException("New user name is already in use");
}
}
for (EntityRef clientInfo : clientInfoEntities) {
DisplayNameComponent nameComp = clientInfo.getComponent(DisplayNameComponent.class);
if (userName.equalsIgnoreCase(nameComp.name)) {
nameComp.name = newUserName;
clientInfo.saveComponent(nameComp);
return "User " + userName + " has been renamed to " + newUserName;
}
}
throw new IllegalArgumentException("No such user '" + userName + "'");
}
use of org.terasology.logic.common.DisplayNameComponent in project Terasology by MovingBlocks.
the class AbstractClient method addOrSetDisplayNameComponent.
private void addOrSetDisplayNameComponent(EntityRef clientInfo, String name) {
DisplayNameComponent component = clientInfo.getComponent(DisplayNameComponent.class);
if (component != null) {
component.name = name;
clientInfo.saveComponent(component);
} else {
component = new DisplayNameComponent();
component.name = name;
clientInfo.addComponent(component);
}
}
use of org.terasology.logic.common.DisplayNameComponent in project Terasology by MovingBlocks.
the class AbstractClient method findNamesOfOtherPlayers.
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.logic.common.DisplayNameComponent in project Terasology by MovingBlocks.
the class BlockItemFactory method newInstance.
public EntityRef newInstance(BlockFamily blockFamily, EntityRef blockEntity) {
if (blockFamily == null) {
return EntityRef.NULL;
}
EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
builder.addComponent(new LightComponent());
}
// Copy the components from block prefab into the block item
for (Component component : blockEntity.iterateComponents()) {
if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
builder.addComponent(entityManager.getComponentLibrary().copy(component));
}
}
DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
if (displayNameComponent != null) {
displayNameComponent.name = blockFamily.getDisplayName();
}
ItemComponent item = builder.getComponent(ItemComponent.class);
if (blockFamily.getArchetypeBlock().isStackable()) {
item.stackId = "block:" + blockFamily.getURI().toString();
item.stackCount = (byte) 1;
}
BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
blockItem.blockFamily = blockFamily;
return builder.build();
}
Aggregations