use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class BlockItemSystem method onPlaceBlock.
@ReceiveEvent(components = { BlockItemComponent.class, ItemComponent.class })
public void onPlaceBlock(ActivateEvent event, EntityRef item) {
if (!event.getTarget().exists()) {
event.consume();
return;
}
BlockItemComponent blockItem = item.getComponent(BlockItemComponent.class);
BlockFamily type = blockItem.blockFamily;
Side surfaceSide = Side.inDirection(event.getHitNormal());
Side secondaryDirection = ChunkMath.getSecondaryPlacementDirection(event.getDirection(), event.getHitNormal());
BlockComponent blockComponent = event.getTarget().getComponent(BlockComponent.class);
if (blockComponent == null) {
// If there is no block there (i.e. it's a BlockGroup, we don't allow placing block, try somewhere else)
event.consume();
return;
}
Vector3i targetBlock = blockComponent.getPosition();
Vector3i placementPos = new Vector3i(targetBlock);
placementPos.add(surfaceSide.getVector3i());
Block block = type.getBlockForPlacement(worldProvider, blockEntityRegistry, placementPos, surfaceSide, secondaryDirection);
if (canPlaceBlock(block, targetBlock, placementPos)) {
// TODO: Fix this for changes.
if (networkSystem.getMode().isAuthority()) {
PlaceBlocks placeBlocks = new PlaceBlocks(placementPos, block, event.getInstigator());
worldProvider.getWorldEntity().send(placeBlocks);
if (!placeBlocks.isConsumed()) {
item.send(new OnBlockItemPlaced(placementPos, blockEntityRegistry.getBlockEntityAt(placementPos)));
} else {
event.consume();
}
}
recordBlockPlaced(event, type);
event.getInstigator().send(new PlaySoundEvent(Assets.getSound("engine:PlaceBlock").get(), 0.5f));
} else {
event.consume();
}
}
use of org.terasology.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.<Vector3i, Block>emptyMap(), supportRequired)) {
PrefabManager prefabManager = CoreRegistry.get(PrefabManager.class);
entity.send(new DestroyEvent(entity, EntityRef.NULL, prefabManager.getPrefab("engine:supportRemovedDamage")));
}
}
}
use of org.terasology.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class PlayerStartingInventorySystem method onPlayerSpawnedEvent.
@ReceiveEvent(components = InventoryComponent.class)
public void onPlayerSpawnedEvent(OnPlayerSpawnedEvent event, EntityRef player) {
BlockItemFactory blockFactory = new BlockItemFactory(entityManager);
// Goodie chest
EntityRef chest = blockFactory.newInstance(blockManager.getBlockFamily("core:chest"));
chest.addComponent(new InventoryComponent(30));
inventoryManager.giveItem(chest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:companion"), 99));
inventoryManager.giveItem(chest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:brick:engine:stair"), 99));
inventoryManager.giveItem(chest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:Tnt"), 99));
inventoryManager.giveItem(chest, EntityRef.NULL, entityManager.create("core:fuseShort"));
inventoryManager.giveItem(chest, EntityRef.NULL, entityManager.create("core:fuseLong"));
inventoryManager.giveItem(chest, EntityRef.NULL, entityManager.create("core:shallowRailgunTool"));
inventoryManager.giveItem(chest, EntityRef.NULL, entityManager.create("core:thoroughRailgunTool"));
inventoryManager.giveItem(chest, EntityRef.NULL, entityManager.create("core:railgunTool"));
inventoryManager.giveItem(chest, EntityRef.NULL, entityManager.create("core:mrbarsack"));
inventoryManager.giveItem(chest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:Brick"), 99));
inventoryManager.giveItem(chest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:Ice"), 99));
inventoryManager.giveItem(chest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:Plank"), 99));
EntityRef doorItem = entityManager.create("core:door");
ItemComponent doorItemComp = doorItem.getComponent(ItemComponent.class);
doorItemComp.stackCount = 20;
doorItem.saveComponent(doorItemComp);
inventoryManager.giveItem(chest, EntityRef.NULL, doorItem);
// Inner goodie chest
EntityRef innerChest = blockFactory.newInstance(blockManager.getBlockFamily("core:Chest"));
innerChest.addComponent(new InventoryComponent(30));
inventoryManager.giveItem(innerChest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:lava"), 99));
inventoryManager.giveItem(innerChest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:water"), 99));
inventoryManager.giveItem(innerChest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:Iris"), 99));
inventoryManager.giveItem(innerChest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:Dandelion"), 99));
inventoryManager.giveItem(innerChest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:Tulip"), 99));
inventoryManager.giveItem(innerChest, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:YellowFlower"), 99));
// Place inner chest into outer chest
inventoryManager.giveItem(chest, EntityRef.NULL, innerChest);
inventoryManager.giveItem(player, EntityRef.NULL, entityManager.create("core:pickaxe"));
inventoryManager.giveItem(player, EntityRef.NULL, entityManager.create("core:axe"));
inventoryManager.giveItem(player, EntityRef.NULL, entityManager.create("core:shovel"));
inventoryManager.giveItem(player, EntityRef.NULL, blockFactory.newInstance(blockManager.getBlockFamily("core:Torch"), 99));
inventoryManager.giveItem(player, EntityRef.NULL, entityManager.create("core:explodeTool"));
inventoryManager.giveItem(player, EntityRef.NULL, entityManager.create("core:railgunTool"));
inventoryManager.giveItem(player, EntityRef.NULL, chest);
}
use of org.terasology.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(networkSystem.getMode(), 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.entitySystem.event.ReceiveEvent in project Terasology by MovingBlocks.
the class SimpleAISystem method onBump.
@ReceiveEvent(components = { SimpleAIComponent.class })
public void onBump(HorizontalCollisionEvent event, EntityRef entity) {
CharacterMovementComponent moveComp = entity.getComponent(CharacterMovementComponent.class);
if (moveComp != null && moveComp.grounded) {
moveComp.jump = true;
entity.saveComponent(moveComp);
}
}
Aggregations