Search in sources :

Example 31 with ReceiveEvent

use of org.terasology.engine.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.

the class InteractionSystem method onScreenLayerClosed.

/**
 * The method listens for the event that the user closes the screen of the current interaction target.
 * <p>
 * When it happens then it cancels the interaction.
 */
@ReceiveEvent(components = ClientComponent.class)
public void onScreenLayerClosed(ScreenLayerClosedEvent event, EntityRef container, ClientComponent clientComponent) {
    EntityRef character = clientComponent.character;
    ResourceUrn activeInteractionScreenUri = InteractionUtil.getActiveInteractionScreenUri(character);
    if ((activeInteractionScreenUri != null) && (activeInteractionScreenUri.equals(event.getClosedScreenUri()))) {
        InteractionUtil.cancelInteractionAsClient(clientComponent.character);
    }
}
Also used : ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 32 with ReceiveEvent

use of org.terasology.engine.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.

the class InteractionSystem method onActivate.

@ReceiveEvent(components = InteractionTargetComponent.class, netFilter = RegisterMode.AUTHORITY)
public void onActivate(ActivateEvent event, EntityRef target) {
    EntityRef instigator = event.getInstigator();
    CharacterComponent characterComponent = instigator.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        logger.error("Interaction start request instigator has no character component");
        return;
    }
    if (characterComponent.authorizedInteractionTarget.exists()) {
        logger.error("Interaction wasn't finished at start of next interaction");
        instigator.send(new InteractionEndEvent(characterComponent.authorizedInteractionId));
    }
    characterComponent.authorizedInteractionTarget = target;
    characterComponent.authorizedInteractionId = event.getActivationId();
    instigator.saveComponent(characterComponent);
}
Also used : CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 33 with ReceiveEvent

use of org.terasology.engine.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.

the class InteractionSystem method onActivationPredicted.

@ReceiveEvent(components = InteractionTargetComponent.class)
public void onActivationPredicted(ActivationPredicted event, EntityRef target) {
    EntityRef character = event.getInstigator();
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        return;
    }
    if (characterComponent.predictedInteractionTarget.exists()) {
        InteractionUtil.cancelInteractionAsClient(character);
    }
    if (target.exists()) {
        characterComponent.predictedInteractionTarget = target;
        characterComponent.predictedInteractionId = event.getActivationId();
        character.saveComponent(characterComponent);
        target.send(new InteractionStartPredicted(character));
    }
}
Also used : CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 34 with ReceiveEvent

use of org.terasology.engine.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.

the class InteractionSystem method onInteractionStartPredicted.

@ReceiveEvent(components = InteractionScreenComponent.class)
public void onInteractionStartPredicted(InteractionStartPredicted event, EntityRef container, InteractionScreenComponent interactionScreenComponent) {
    EntityRef investigator = event.getInstigator();
    CharacterComponent characterComponent = investigator.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        logger.error("Interaction start predicted for entity without character component");
        return;
    }
    ClientComponent controller = characterComponent.controller.getComponent(ClientComponent.class);
    if (controller != null && controller.local) {
        nuiManager.closeAllScreens();
        nuiManager.pushScreen(interactionScreenComponent.screen);
    }
}
Also used : CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ClientComponent(org.terasology.engine.network.ClientComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 35 with ReceiveEvent

use of org.terasology.engine.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.

the class EventSystemReplayImpl method registerEventHandler.

@Override
public void registerEventHandler(ComponentSystem handler) {
    Class handlerClass = handler.getClass();
    if (!Modifier.isPublic(handlerClass.getModifiers())) {
        logger.error("Cannot register handler {}, must be public", handler.getClass().getName());
        return;
    }
    logger.debug("Registering event handler " + handlerClass.getName());
    for (Method method : handlerClass.getMethods()) {
        ReceiveEvent receiveEventAnnotation = method.getAnnotation(ReceiveEvent.class);
        if (receiveEventAnnotation != null) {
            if (!receiveEventAnnotation.netFilter().isValidFor(networkSystem.getMode().isAuthority(), false)) {
                continue;
            }
            Set<Class<? extends Component>> requiredComponents = Sets.newLinkedHashSet();
            method.setAccessible(true);
            Class<?>[] types = method.getParameterTypes();
            logger.debug("Found method: " + method.toString());
            if (!Event.class.isAssignableFrom(types[0]) || !EntityRef.class.isAssignableFrom(types[1])) {
                logger.error("Invalid event handler method: {}", method.getName());
                return;
            }
            requiredComponents.addAll(Arrays.asList(receiveEventAnnotation.components()));
            List<Class<? extends Component>> componentParams = Lists.newArrayList();
            for (int i = 2; i < types.length; ++i) {
                if (!Component.class.isAssignableFrom(types[i])) {
                    logger.error("Invalid event handler method: {} - {} is not a component class", method.getName(), types[i]);
                    return;
                }
                requiredComponents.add((Class<? extends Component>) types[i]);
                componentParams.add((Class<? extends Component>) types[i]);
            }
            EventSystemReplayImpl.ByteCodeEventHandlerInfo handlerInfo = new EventSystemReplayImpl.ByteCodeEventHandlerInfo(handler, method, receiveEventAnnotation.priority(), receiveEventAnnotation.activity(), requiredComponents, componentParams);
            addEventHandler((Class<? extends Event>) types[0], handlerInfo, requiredComponents);
        }
    }
}
Also used : ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent) Method(java.lang.reflect.Method) Component(org.terasology.gestalt.entitysystem.component.Component) NetworkComponent(org.terasology.engine.network.NetworkComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent)

Aggregations

ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)83 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)27 Vector3f (org.joml.Vector3f)18 LocationComponent (org.terasology.engine.logic.location.LocationComponent)12 Vector3i (org.joml.Vector3i)10 StaticSound (org.terasology.engine.audio.StaticSound)10 Vector3ic (org.joml.Vector3ic)9 PlaySoundEvent (org.terasology.engine.audio.events.PlaySoundEvent)9 ClientComponent (org.terasology.engine.network.ClientComponent)8 Quaternionf (org.joml.Quaternionf)6 BlockComponent (org.terasology.engine.world.block.BlockComponent)6 OnItemUseEvent (org.terasology.engine.logic.characters.events.OnItemUseEvent)5 Block (org.terasology.engine.world.block.Block)5 CharacterComponent (org.terasology.engine.logic.characters.CharacterComponent)4 BlockRegion (org.terasology.engine.world.block.BlockRegion)4 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)3 BlockRegionComponent (org.terasology.engine.world.block.regions.BlockRegionComponent)3 Component (org.terasology.gestalt.entitysystem.component.Component)3 Method (java.lang.reflect.Method)2 Vector3fc (org.joml.Vector3fc)2