Search in sources :

Example 51 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class InteractionUtil method getActiveInteractionScreenUri.

/**
 * @return the active interaction screen uri of the specified character.
 * The method returns null if the player has no interaction screen open.
 * The method is only intended to be called for the own character.
 */
public static ResourceUrn getActiveInteractionScreenUri(EntityRef character) {
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        return null;
    }
    EntityRef interactionTarget = characterComponent.predictedInteractionTarget;
    if (!interactionTarget.exists()) {
        return null;
    }
    InteractionScreenComponent screenComponent = interactionTarget.getComponent(InteractionScreenComponent.class);
    if (screenComponent == null) {
        return null;
    }
    return new ResourceUrn(screenComponent.screen);
}
Also used : CharacterComponent(org.terasology.logic.characters.CharacterComponent) ResourceUrn(org.terasology.assets.ResourceUrn) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 52 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef 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 53 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class CoreCommands method spawnBlock.

/**
 * Spawns a block in front of the player
 * @param sender Sender of command
 * @param blockName String containing name of block to spawn
 * @return String containg final message
 */
@Command(shortDescription = "Spawns a block in front of the player", helpText = "Spawns the specified block as a " + "item in front of the player. You can simply pick it up.", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String spawnBlock(@Sender EntityRef sender, @CommandParam("blockName") String blockName) {
    ClientComponent clientComponent = sender.getComponent(ClientComponent.class);
    LocationComponent characterLocation = clientComponent.character.getComponent(LocationComponent.class);
    Vector3f spawnPos = characterLocation.getWorldPosition();
    Vector3f offset = characterLocation.getWorldDirection();
    offset.scale(3);
    spawnPos.add(offset);
    BlockFamily block = blockManager.getBlockFamily(blockName);
    if (block == null) {
        return "";
    }
    BlockItemFactory blockItemFactory = new BlockItemFactory(entityManager);
    EntityRef blockItem = blockItemFactory.newInstance(block);
    blockItem.send(new DropItemEvent(spawnPos));
    return "Spawned block.";
}
Also used : DropItemEvent(org.terasology.logic.inventory.events.DropItemEvent) Vector3f(org.terasology.math.geom.Vector3f) BlockItemFactory(org.terasology.world.block.items.BlockItemFactory) BlockFamily(org.terasology.world.block.family.BlockFamily) ClientComponent(org.terasology.network.ClientComponent) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) Command(org.terasology.logic.console.commandSystem.annotations.Command) ConsoleCommand(org.terasology.logic.console.commandSystem.ConsoleCommand)

Example 54 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef 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 + "'");
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) Command(org.terasology.logic.console.commandSystem.annotations.Command)

Example 55 with EntityRef

use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class AbstractClient method createClientInfoEntity.

private EntityRef createClientInfoEntity(EntityManager entityManager) {
    EntityRef clientInfo;
    clientInfo = entityManager.create("engine:clientInfo");
    // mark clientInfo entities with a dedicated component
    ClientInfoComponent cic = new ClientInfoComponent();
    cic.playerId = getId();
    clientInfo.addComponent(cic);
    return clientInfo;
}
Also used : ClientInfoComponent(org.terasology.network.ClientInfoComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)337 Test (org.junit.Test)106 ClientComponent (org.terasology.network.ClientComponent)49 LocationComponent (org.terasology.logic.location.LocationComponent)45 Vector3f (org.terasology.math.geom.Vector3f)44 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)36 Vector3i (org.terasology.math.geom.Vector3i)34 Command (org.terasology.logic.console.commandSystem.annotations.Command)28 StringComponent (org.terasology.entitySystem.stubs.StringComponent)26 NetworkComponent (org.terasology.network.NetworkComponent)21 EntityData (org.terasology.protobuf.EntityData)21 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)17 Block (org.terasology.world.block.Block)16 Component (org.terasology.entitySystem.Component)15 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)15 CharacterComponent (org.terasology.logic.characters.CharacterComponent)14 Quat4f (org.terasology.math.geom.Quat4f)14 BlockComponent (org.terasology.world.block.BlockComponent)13 Map (java.util.Map)11 LocalPlayer (org.terasology.logic.players.LocalPlayer)11