Search in sources :

Example 26 with ReceiveEvent

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

the class EventSystemImpl 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(isAutority, 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]);
            }
            ByteCodeEventHandlerInfo handlerInfo = new 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)

Example 27 with ReceiveEvent

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

the class ServerViewDistanceSystem method onChangeViewDistanceChanged.

@ReceiveEvent(components = ClientComponent.class)
public void onChangeViewDistanceChanged(ViewDistanceChangedEvent request, EntityRef entity) {
    Client client = networkSystem.getOwner(entity);
    if (client != null) {
        client.setViewDistanceMode(request.getNewViewRange());
        relevanceSystem.updateRelevanceEntityDistance(entity, client.getViewDistance().getChunkDistance());
    }
}
Also used : Client(org.terasology.engine.network.Client) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 28 with ReceiveEvent

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

the class LoadingChunkEventSystem method onNewChunk.

/**
 * Event handler which waits for new chunk events, then sends those
 * events to the current game state
 * @param chunkAvailable an event which includes the position of the new chunk
 * @param worldEntity the world entity that this event was sent to
 */
@ReceiveEvent(components = WorldComponent.class)
public void onNewChunk(OnChunkLoaded chunkAvailable, EntityRef worldEntity) {
    GameEngine gameEngine = context.get(GameEngine.class);
    GameState gameState = gameEngine.getState();
    gameState.onChunkLoaded(chunkAvailable, worldEntity);
}
Also used : GameEngine(org.terasology.engine.core.GameEngine) GameState(org.terasology.engine.core.modes.GameState) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 29 with ReceiveEvent

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

the class LocalPlayerBlockSelectionByItemSystem method onPlaced.

@ReceiveEvent(components = OnItemActivateSelectionComponent.class)
public void onPlaced(ActivateEvent event, EntityRef itemEntity) {
    if (event.getTargetLocation() == null) {
        return;
    }
    EntityRef targetLocationEntity = event.getTarget();
    this.blockSelectionComponentEntity = itemEntity;
    BlockSelectionComponent blockSelectionComponent = itemEntity.getComponent(BlockSelectionComponent.class);
    if (null == blockSelectionComponent.startPosition) {
        // on the first item click, we start selecting blocks
        targetLocationEntity.send(new SetBlockSelectionStartingPointEvent(itemEntity));
        blockSelectionComponent.shouldRender = true;
    } else {
        // on the second item click, we will set the ending selection point and send an ApplyBlockSelectionEvent
        targetLocationEntity.send(new SetBlockSelectionEndingPointEvent(itemEntity));
        localPlayer.getCharacterEntity().send(new ApplyBlockSelectionEvent(itemEntity, blockSelectionComponent.currentSelection));
        blockSelectionComponent.shouldRender = false;
        blockSelectionComponent.currentSelection = null;
        blockSelectionComponent.startPosition = null;
    }
}
Also used : SetBlockSelectionEndingPointEvent(org.terasology.engine.world.selection.event.SetBlockSelectionEndingPointEvent) BlockSelectionComponent(org.terasology.engine.world.selection.BlockSelectionComponent) SetBlockSelectionStartingPointEvent(org.terasology.engine.world.selection.event.SetBlockSelectionStartingPointEvent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 30 with ReceiveEvent

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

the class SideBlockSupportRequired method checkForSupport.

@ReceiveEvent
public void checkForSupport(DelayedActionTriggeredEvent event, EntityRef entity, BlockComponent block, SideBlockSupportRequiredComponent supportRequired) {
    if (event.getActionId().equals(SUPPORT_CHECK_ACTION_ID)) {
        if (!isSufficientlySupported(block.getPosition(), null, Collections.emptyMap(), supportRequired)) {
            PrefabManager prefabManager = CoreRegistry.get(PrefabManager.class);
            entity.send(new DestroyEvent(entity, EntityRef.NULL, prefabManager.getPrefab("engine" + ":supportRemovedDamage")));
        }
    }
}
Also used : DestroyEvent(org.terasology.engine.logic.health.DestroyEvent) PrefabManager(org.terasology.engine.entitySystem.prefab.PrefabManager) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

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