Search in sources :

Example 1 with GiveItemEvent

use of org.terasology.logic.inventory.events.GiveItemEvent in project Terasology by MovingBlocks.

the class ItemPickupAuthoritySystem method onBumpGiveItemToEntity.

@ReceiveEvent
public void onBumpGiveItemToEntity(CollideEvent event, EntityRef entity, PickupComponent pickupComponent) {
    if (pickupComponent.timeDropped + pickupComponent.timeToPickUp < time.getGameTimeInMs()) {
        GiveItemEvent giveItemEvent = new GiveItemEvent(event.getOtherEntity());
        entity.send(giveItemEvent);
        if (giveItemEvent.isHandled()) {
            // remove all the components added from the pickup prefab
            ItemComponent itemComponent = entity.getComponent(ItemComponent.class);
            if (itemComponent != null) {
                for (Component component : itemComponent.pickupPrefab.iterateComponents()) {
                    entity.removeComponent(component.getClass());
                }
            }
        }
    }
}
Also used : BlockItemComponent(org.terasology.world.block.items.BlockItemComponent) GiveItemEvent(org.terasology.logic.inventory.events.GiveItemEvent) LocationComponent(org.terasology.logic.location.LocationComponent) RigidBodyComponent(org.terasology.physics.components.RigidBodyComponent) OnAddedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent) Component(org.terasology.entitySystem.Component) BoxShapeComponent(org.terasology.physics.components.shapes.BoxShapeComponent) BlockItemComponent(org.terasology.world.block.items.BlockItemComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 2 with GiveItemEvent

use of org.terasology.logic.inventory.events.GiveItemEvent in project Terasology by MovingBlocks.

the class BlockEntitySystem method giveItem.

private boolean giveItem(CreateBlockDropsEvent event, EntityRef item) {
    GiveItemEvent giveItemEvent = new GiveItemEvent(event.getInstigator());
    item.send(giveItemEvent);
    return giveItemEvent.isHandled();
}
Also used : GiveItemEvent(org.terasology.logic.inventory.events.GiveItemEvent)

Example 3 with GiveItemEvent

use of org.terasology.logic.inventory.events.GiveItemEvent in project Terasology by MovingBlocks.

the class BlockCommands method giveBlock.

/**
 * Actual implementation of the giveBlock command.
 *
 * @param blockFamily the block family of the queried block
 * @param quantity    the number of blocks that are queried
 */
private String giveBlock(BlockFamily blockFamily, int quantity, EntityRef client) {
    if (quantity < 1) {
        return "Here, have these zero (0) blocks just like you wanted";
    }
    EntityRef playerEntity = client.getComponent(ClientComponent.class).character;
    for (int quantityLeft = quantity; quantityLeft > 0; quantityLeft--) {
        EntityRef item = blockItemFactory.newInstance(blockFamily, 1);
        if (!item.exists()) {
            throw new IllegalArgumentException("Unknown block or item");
        }
        GiveItemEvent giveItemEvent = new GiveItemEvent(playerEntity);
        item.send(giveItemEvent);
        if (!giveItemEvent.isHandled()) {
            item.destroy();
            quantity -= quantityLeft;
            break;
        }
    }
    return "You received " + quantity + " blocks of " + blockFamily.getDisplayName();
}
Also used : GiveItemEvent(org.terasology.logic.inventory.events.GiveItemEvent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent)

Aggregations

GiveItemEvent (org.terasology.logic.inventory.events.GiveItemEvent)3 Component (org.terasology.entitySystem.Component)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 OnAddedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent)1 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 ClientComponent (org.terasology.network.ClientComponent)1 RigidBodyComponent (org.terasology.physics.components.RigidBodyComponent)1 BoxShapeComponent (org.terasology.physics.components.shapes.BoxShapeComponent)1 BlockItemComponent (org.terasology.world.block.items.BlockItemComponent)1