use of org.terasology.engine.network.ClientComponent in project Terasology by MovingBlocks.
the class AbstractClient method createEntity.
/**
* Creates an entity for the client connection, checking if name and color options can be used.
* @param preferredName Passes players preferred name to check availability, giving a best alternative if it is used already.
* @param color Creates or changes the player's color component to match argument
* @param entityManager
*/
protected void createEntity(String preferredName, Color color, EntityManager entityManager) {
// Create player entity
clientEntity = entityManager.create("engine:client");
// TODO: Send event for clientInfo creation, don't create here.
EntityRef clientInfo = findClientEntityRef(entityManager);
if (!clientInfo.exists()) {
clientInfo = createClientInfoEntity(entityManager);
}
ClientInfoComponent clientInfoComp = clientInfo.getComponent(ClientInfoComponent.class);
clientInfoComp.client = clientEntity;
clientInfo.saveComponent(clientInfoComp);
ClientComponent clientComponent = clientEntity.getComponent(ClientComponent.class);
clientComponent.clientInfo = clientInfo;
clientEntity.saveComponent(clientComponent);
addOrSetColorComponent(clientInfo, color);
DisplayNameComponent displayNameComponent = clientInfo.getComponent(DisplayNameComponent.class);
if (displayNameComponent == null || !displayNameComponent.name.equals(preferredName)) {
String bestAvailableName = findUniquePlayerName(preferredName, entityManager, clientInfo);
addOrSetDisplayNameComponent(clientInfo, bestAvailableName);
}
}
use of org.terasology.engine.network.ClientComponent in project Terasology by MovingBlocks.
the class OnlineUsernameSuggester method suggest.
@Override
public Set<String> suggest(EntityRef sender, Object... resolvedParameters) {
Iterable<EntityRef> clients = entityManager.getEntitiesWith(ClientComponent.class);
Set<String> clientNames = Sets.newHashSet();
for (EntityRef client : clients) {
ClientComponent clientComponent = client.getComponent(ClientComponent.class);
DisplayNameComponent displayNameComponent = clientComponent.clientInfo.getComponent(DisplayNameComponent.class);
clientNames.add(displayNameComponent.name);
}
return clientNames;
}
use of org.terasology.engine.network.ClientComponent in project Terasology by MovingBlocks.
the class ConsoleImpl method execute.
@Override
public boolean execute(String rawCommand, EntityRef callingClient) {
String commandName = processCommandName(rawCommand);
List<String> processedParameters = processParameters(rawCommand);
ClientComponent cc = callingClient.getComponent(ClientComponent.class);
if (cc.local) {
if (!rawCommand.isEmpty() && (localCommandHistory.isEmpty() || !localCommandHistory.getLast().equals(rawCommand))) {
localCommandHistory.add(rawCommand);
}
}
return execute(new Name(commandName), processedParameters, callingClient);
}
use of org.terasology.engine.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;
}
use of org.terasology.engine.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;
}
Aggregations