use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryUtils method checkForStacking.
private static boolean checkForStacking(EntityRef from, int slotFrom, EntityRef to, int slotTo) {
EntityRef itemFrom = getItemAt(from, slotFrom);
EntityRef itemTo = getItemAt(to, slotTo);
if (itemFrom.exists() && itemTo.exists() && canStackInto(itemFrom, itemTo)) {
int fromCount = itemFrom.getComponent(ItemComponent.class).stackCount;
int toCount = itemTo.getComponent(ItemComponent.class).stackCount;
putItemIntoSlot(from, EntityRef.NULL, slotFrom);
adjustStackSize(to, slotTo, fromCount + toCount);
return true;
}
return false;
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryUtils method moveItemToSlots.
static boolean moveItemToSlots(EntityRef instigator, EntityRef from, int fromSlot, EntityRef to, List<Integer> toSlots) {
EntityRef fromItem = InventoryUtils.getItemAt(from, fromSlot);
BeforeItemRemovedFromInventory removeFrom = new BeforeItemRemovedFromInventory(instigator, fromItem, fromSlot);
from.send(removeFrom);
if (removeFrom.isConsumed()) {
return false;
}
int stackCount = InventoryUtils.getStackCount(fromItem);
int movedToStack = moveToExistingStacksInSlots(from, fromSlot, to, toSlots);
boolean movedToFreeSlot = false;
if (stackCount != movedToStack) {
movedToFreeSlot = moveToFreeSlots(instigator, from, fromSlot, to, toSlots);
}
return movedToStack > 0 || movedToFreeSlot;
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryHud method initialise.
@Override
public void initialise() {
for (InventoryCell cell : findAll(InventoryCell.class)) {
cell.bindSelected(new SlotSelectedBinding(cell.getTargetSlot(), localPlayer));
cell.bindTargetInventory(new ReadOnlyBinding<EntityRef>() {
@Override
public EntityRef get() {
return localPlayer.getCharacterEntity();
}
});
}
crosshair = find("crosshair", UICrosshair.class);
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryCell method takeAmount.
private void takeAmount(int amount) {
EntityRef characterEntity = CoreRegistry.get(LocalPlayer.class).getCharacterEntity();
InventoryManager inventoryManager = CoreRegistry.get(InventoryManager.class);
inventoryManager.moveItem(getTransferEntity(), characterEntity, 0, getTargetInventory(), getTargetSlot(), amount);
}
use of org.terasology.entitySystem.entity.EntityRef 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);
}
Aggregations