Search in sources :

Example 41 with ReceiveEvent

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();
    }
}
Also used : Side(org.terasology.math.Side) BlockComponent(org.terasology.world.block.BlockComponent) PlaySoundEvent(org.terasology.audio.events.PlaySoundEvent) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) BlockFamily(org.terasology.world.block.family.BlockFamily) PlaceBlocks(org.terasology.world.block.entity.placement.PlaceBlocks) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 42 with ReceiveEvent

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")));
        }
    }
}
Also used : DestroyEvent(org.terasology.logic.health.DestroyEvent) PrefabManager(org.terasology.entitySystem.prefab.PrefabManager) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 43 with ReceiveEvent

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);
}
Also used : InventoryComponent(org.terasology.logic.inventory.InventoryComponent) ItemComponent(org.terasology.logic.inventory.ItemComponent) BlockItemFactory(org.terasology.world.block.items.BlockItemFactory) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 44 with ReceiveEvent

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);
        }
    }
}
Also used : ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent) Method(java.lang.reflect.Method) NetworkComponent(org.terasology.network.NetworkComponent) BlockComponent(org.terasology.world.block.BlockComponent) Component(org.terasology.entitySystem.Component)

Example 45 with ReceiveEvent

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);
    }
}
Also used : CharacterMovementComponent(org.terasology.logic.characters.CharacterMovementComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Aggregations

ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)98 EntityRef (org.terasology.entitySystem.entity.EntityRef)36 Vector3f (org.terasology.math.geom.Vector3f)26 LocationComponent (org.terasology.logic.location.LocationComponent)17 Vector3i (org.terasology.math.geom.Vector3i)17 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)15 StaticSound (org.terasology.audio.StaticSound)13 Block (org.terasology.world.block.Block)11 ClientComponent (org.terasology.network.ClientComponent)7 BlockComponent (org.terasology.world.block.BlockComponent)7 BlockRegionComponent (org.terasology.world.block.regions.BlockRegionComponent)7 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)5 CharacterComponent (org.terasology.logic.characters.CharacterComponent)4 OnItemUseEvent (org.terasology.logic.characters.events.OnItemUseEvent)4 ResourceUrn (org.terasology.assets.ResourceUrn)3 Component (org.terasology.entitySystem.Component)3 ItemComponent (org.terasology.logic.inventory.ItemComponent)3 Side (org.terasology.math.Side)3 BoneHealthChangedEvent (org.terasology.anatomy.AnatomySkeleton.event.BoneHealthChangedEvent)2 PartSkeletalDetails (org.terasology.anatomy.component.PartSkeletalDetails)2