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