Search in sources :

Example 1 with GiveItemEvent

use of org.terasology.engine.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.engine.logic.inventory.events.GiveItemEvent)

Example 2 with GiveItemEvent

use of org.terasology.engine.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.engine.world.block.items.BlockItemComponent) GiveItemEvent(org.terasology.engine.logic.inventory.events.GiveItemEvent) Component(org.terasology.gestalt.entitysystem.component.Component) RigidBodyComponent(org.terasology.engine.physics.components.RigidBodyComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) OnAddedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent) BlockItemComponent(org.terasology.engine.world.block.items.BlockItemComponent) BoxShapeComponent(org.terasology.engine.physics.components.shapes.BoxShapeComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 3 with GiveItemEvent

use of org.terasology.engine.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;
    int stackLimit = blockFamily.getArchetypeBlock().isStackable() ? 99 : 1;
    int quantityLeft;
    for (quantityLeft = quantity; quantityLeft > 0; quantityLeft = quantityLeft - stackLimit) {
        EntityRef item = blockItemFactory.newInstance(blockFamily, Math.min(quantity, stackLimit));
        if (!item.exists()) {
            throw new IllegalArgumentException("Unknown block or item");
        }
        GiveItemEvent giveItemEvent = new GiveItemEvent(playerEntity);
        item.send(giveItemEvent);
        if (!giveItemEvent.isHandled()) {
            item.destroy();
            break;
        }
    }
    return "You received " + (quantity - quantityLeft) + " blocks of " + blockFamily.getDisplayName();
}
Also used : GiveItemEvent(org.terasology.engine.logic.inventory.events.GiveItemEvent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ClientComponent(org.terasology.engine.network.ClientComponent)

Aggregations

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