use of org.terasology.logic.inventory.InventoryComponent in project Terasology by MovingBlocks.
the class InventoryCell method moveItemSmartly.
private void moveItemSmartly() {
EntityRef fromEntity = getTargetInventory();
int fromSlot = getTargetSlot();
EntityRef playerEntity = CoreRegistry.get(LocalPlayer.class).getCharacterEntity();
InventoryComponent playerInventory = playerEntity.getComponent(InventoryComponent.class);
if (playerInventory == null) {
return;
}
CharacterComponent characterComponent = playerEntity.getComponent(CharacterComponent.class);
if (characterComponent == null) {
logger.error("Character entity of player had no character component");
return;
}
int totalSlotCount = playerInventory.itemSlots.size();
EntityRef interactionTarget = characterComponent.predictedInteractionTarget;
InventoryComponent interactionTargetInventory = interactionTarget.getComponent(InventoryComponent.class);
EntityRef targetEntity;
List<Integer> toSlots = new ArrayList<>(totalSlotCount);
if (fromEntity.equals(playerEntity)) {
if (interactionTarget.exists() && interactionTargetInventory != null) {
targetEntity = interactionTarget;
toSlots = numbersBetween(0, interactionTargetInventory.itemSlots.size());
} else {
targetEntity = playerEntity;
// TODO use a constant once there is one
int hudSlotCount = 10;
boolean fromHud = (fromSlot < hudSlotCount);
boolean toHud = !fromHud;
if (toHud) {
toSlots = numbersBetween(0, hudSlotCount);
} else {
toSlots = numbersBetween(hudSlotCount, totalSlotCount);
}
}
} else {
targetEntity = playerEntity;
toSlots = numbersBetween(0, totalSlotCount);
}
CoreRegistry.get(InventoryManager.class).moveItemToSlots(getTransferEntity(), fromEntity, fromSlot, targetEntity, toSlots);
}
use of org.terasology.logic.inventory.InventoryComponent 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.logic.inventory.InventoryComponent in project Terasology by MovingBlocks.
the class BlockInventorySystem method copyBlockInventory.
@ReceiveEvent(components = { InventoryComponent.class, RetainBlockInventoryComponent.class })
public void copyBlockInventory(OnBlockToItem event, EntityRef blockEntity) {
EntityRef inventoryItem = event.getItem();
int slotCount = InventoryUtils.getSlotCount(blockEntity);
inventoryItem.addComponent(new InventoryComponent(slotCount));
for (int i = 0; i < slotCount; i++) {
inventoryManager.switchItem(blockEntity, blockEntity, i, inventoryItem, i);
}
ItemComponent itemComponent = inventoryItem.getComponent(ItemComponent.class);
if (itemComponent != null && !itemComponent.stackId.isEmpty()) {
itemComponent.stackId = "";
inventoryItem.saveComponent(itemComponent);
}
}
Aggregations