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);
}
}
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);
}
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));
}
}
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);
}
}
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);
}
}
}
Aggregations