Search in sources :

Example 1 with ItemComponent

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

the class BlockItemFactory method newInstance.

public EntityRef newInstance(BlockFamily blockFamily, EntityRef blockEntity) {
    if (blockFamily == null) {
        return EntityRef.NULL;
    }
    EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
    if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
        builder.addComponent(new LightComponent());
    }
    // Copy the components from block prefab into the block item
    for (Component component : blockEntity.iterateComponents()) {
        if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
            builder.addComponent(entityManager.getComponentLibrary().copy(component));
        }
    }
    DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
    if (displayNameComponent != null) {
        displayNameComponent.name = blockFamily.getDisplayName();
    }
    ItemComponent item = builder.getComponent(ItemComponent.class);
    if (blockFamily.getArchetypeBlock().isStackable()) {
        item.stackId = "block:" + blockFamily.getURI().toString();
        item.stackCount = (byte) 1;
    }
    BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
    blockItem.blockFamily = blockFamily;
    return builder.build();
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) ItemComponent(org.terasology.logic.inventory.ItemComponent) LightComponent(org.terasology.rendering.logic.LightComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LightComponent(org.terasology.rendering.logic.LightComponent) ItemComponent(org.terasology.logic.inventory.ItemComponent) Component(org.terasology.entitySystem.Component) DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent)

Example 2 with ItemComponent

use of org.terasology.logic.inventory.ItemComponent 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 3 with ItemComponent

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

the class BlockItemFactory method newBuilder.

/**
 * Use this method instead of {@link #newInstance(BlockFamily)} to modify entity properties like the persistence
 * flag before it gets created.
 *
 * @param blockFamily must not be null
 */
public EntityBuilder newBuilder(BlockFamily blockFamily, int quantity) {
    EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
    if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
        builder.addComponent(new LightComponent());
    }
    // Copy the components from block prefab into the block item
    Optional<Prefab> prefab = blockFamily.getArchetypeBlock().getPrefab();
    if (prefab.isPresent()) {
        for (Component component : prefab.get().iterateComponents()) {
            if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
                builder.addComponent(entityManager.getComponentLibrary().copy(component));
            }
        }
    }
    DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
    displayNameComponent.name = blockFamily.getDisplayName();
    ItemComponent item = builder.getComponent(ItemComponent.class);
    if (blockFamily.getArchetypeBlock().isStackable()) {
        item.stackId = "block:" + blockFamily.getURI().toString();
        item.stackCount = (byte) quantity;
    }
    BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
    blockItem.blockFamily = blockFamily;
    return builder;
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) ItemComponent(org.terasology.logic.inventory.ItemComponent) LightComponent(org.terasology.rendering.logic.LightComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LightComponent(org.terasology.rendering.logic.LightComponent) ItemComponent(org.terasology.logic.inventory.ItemComponent) Component(org.terasology.entitySystem.Component) DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 4 with ItemComponent

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

the class CharacterSystem method onItemUse.

@ReceiveEvent(components = { CharacterComponent.class })
public void onItemUse(OnItemUseEvent event, EntityRef entity, CharacterHeldItemComponent characterHeldItemComponent) {
    long currentTime = time.getGameTimeInMs();
    if (characterHeldItemComponent.nextItemUseTime > currentTime) {
        // this character is not yet ready to use another item,  they are still cooling down from last use
        event.consume();
        return;
    }
    EntityRef selectedItemEntity = characterHeldItemComponent.selectedItem;
    characterHeldItemComponent.lastItemUsedTime = currentTime;
    characterHeldItemComponent.nextItemUseTime = currentTime;
    ItemComponent itemComponent = selectedItemEntity.getComponent(ItemComponent.class);
    // Add the cooldown time for the next use of this item.
    if (itemComponent != null) {
        // Send out this event so other systems can alter the cooldown time.
        AffectItemUseCooldownTimeEvent affectItemUseCooldownTimeEvent = new AffectItemUseCooldownTimeEvent(itemComponent.cooldownTime);
        entity.send(affectItemUseCooldownTimeEvent);
        characterHeldItemComponent.nextItemUseTime += affectItemUseCooldownTimeEvent.getResultValue();
    } else {
        characterHeldItemComponent.nextItemUseTime += 200;
    }
    entity.saveComponent(characterHeldItemComponent);
}
Also used : ItemComponent(org.terasology.logic.inventory.ItemComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 5 with ItemComponent

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

the class BlockDropGrammarSystem method onDestroyed.

@ReceiveEvent
public void onDestroyed(DoDestroyEvent event, EntityRef entity, BlockDropGrammarComponent blockDrop, LocationComponent locationComp) {
    BlockDamageModifierComponent blockDamageModifierComponent = event.getDamageType().getComponent(BlockDamageModifierComponent.class);
    float chanceOfBlockDrop = 1;
    if (blockDamageModifierComponent != null) {
        chanceOfBlockDrop = 1 - blockDamageModifierComponent.blockAnnihilationChance;
    }
    if (random.nextFloat() < chanceOfBlockDrop) {
        List<String> blockDrops = blockDrop.blockDrops;
        List<String> itemDrops = blockDrop.itemDrops;
        if (blockDamageModifierComponent != null && blockDrop.droppedWithTool != null) {
            for (String toolType : blockDamageModifierComponent.materialDamageMultiplier.keySet()) {
                if (blockDrop.droppedWithTool.containsKey(toolType)) {
                    BlockDropGrammarComponent.DropDefinition dropDefinition = blockDrop.droppedWithTool.get(toolType);
                    blockDrops = dropDefinition.blockDrops;
                    itemDrops = dropDefinition.itemDrops;
                    break;
                }
            }
        }
        if (blockDrops != null) {
            for (String drop : blockDrops) {
                String dropResult = drop;
                boolean dropping = true;
                int pipeIndex = dropResult.indexOf('|');
                if (pipeIndex > -1) {
                    float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
                    if (random.nextFloat() >= chance) {
                        dropping = false;
                    }
                    dropResult = dropResult.substring(pipeIndex + 1);
                }
                if (dropping) {
                    DropParser dropParser = new DropParser(random, dropResult).invoke();
                    EntityRef dropItem = blockItemFactory.newInstance(blockManager.getBlockFamily(dropParser.getDrop()), dropParser.getCount());
                    if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
                        createDrop(dropItem, locationComp.getWorldPosition(), true);
                    }
                }
            }
        }
        if (itemDrops != null) {
            for (String drop : itemDrops) {
                String dropResult = drop;
                boolean dropping = true;
                int pipeIndex = dropResult.indexOf('|');
                if (pipeIndex > -1) {
                    float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
                    if (random.nextFloat() >= chance) {
                        dropping = false;
                    }
                    dropResult = dropResult.substring(pipeIndex + 1);
                }
                if (dropping) {
                    DropParser dropParser = new DropParser(random, dropResult).invoke();
                    EntityBuilder dropEntity = entityManager.newBuilder(dropParser.getDrop());
                    if (dropParser.getCount() > 1) {
                        ItemComponent itemComponent = dropEntity.getComponent(ItemComponent.class);
                        itemComponent.stackCount = (byte) dropParser.getCount();
                    }
                    EntityRef dropItem = dropEntity.build();
                    if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
                        createDrop(dropItem, locationComp.getWorldPosition(), false);
                    }
                }
            }
        }
    }
}
Also used : ItemComponent(org.terasology.logic.inventory.ItemComponent) BlockDamageModifierComponent(org.terasology.world.block.entity.damage.BlockDamageModifierComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Aggregations

ItemComponent (org.terasology.logic.inventory.ItemComponent)7 EntityRef (org.terasology.entitySystem.entity.EntityRef)4 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)4 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)3 Component (org.terasology.entitySystem.Component)2 Prefab (org.terasology.entitySystem.prefab.Prefab)2 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)2 InventoryComponent (org.terasology.logic.inventory.InventoryComponent)2 LightComponent (org.terasology.rendering.logic.LightComponent)2 BlockDamageModifierComponent (org.terasology.world.block.entity.damage.BlockDamageModifierComponent)1 BlockItemComponent (org.terasology.world.block.items.BlockItemComponent)1 BlockItemFactory (org.terasology.world.block.items.BlockItemFactory)1