use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryAuthoritySystem method giveItemToSlots.
private boolean giveItemToSlots(EntityRef instigator, EntityRef entity, EntityRef item, List<Integer> slots) {
int toConsume = InventoryUtils.getStackCount(item);
Map<Integer, Integer> consumableCount = new LinkedHashMap<>();
// First: check which slots we can merge into
for (int slot : slots) {
EntityRef itemAtEntity = InventoryUtils.getItemAt(entity, slot);
ItemComponent itemAt = itemAtEntity.getComponent(ItemComponent.class);
if (itemAt != null && InventoryUtils.isSameItem(item, itemAtEntity)) {
int spaceInSlot = itemAt.maxStackSize - itemAt.stackCount;
int toAdd = Math.min(toConsume, spaceInSlot);
if (toAdd > 0) {
consumableCount.put(slot, toAdd);
toConsume -= toAdd;
if (toConsume == 0) {
break;
}
}
}
}
int emptySlotNo = -1;
int emptySlotCount = toConsume;
if (toConsume > 0) {
// Next: check which slots are empty and figure out where to add
for (int slot : slots) {
EntityRef itemAtEntity = InventoryUtils.getItemAt(entity, slot);
ItemComponent itemAt = itemAtEntity.getComponent(ItemComponent.class);
if (itemAt == null && canPutItemIntoSlot(instigator, entity, item, slot)) {
emptySlotNo = slot;
emptySlotCount = toConsume;
toConsume = 0;
break;
}
}
}
if (toConsume > 0) {
return false;
}
for (Map.Entry<Integer, Integer> slotCount : consumableCount.entrySet()) {
int slot = slotCount.getKey();
int count = slotCount.getValue();
EntityRef itemAtEntity = InventoryUtils.getItemAt(entity, slot);
ItemComponent itemAt = itemAtEntity.getComponent(ItemComponent.class);
InventoryUtils.adjustStackSize(entity, slot, itemAt.stackCount + count);
}
if (emptySlotNo > -1) {
ItemComponent sourceItem = item.getComponent(ItemComponent.class);
sourceItem.stackCount = (byte) emptySlotCount;
item.saveComponent(sourceItem);
InventoryUtils.putItemIntoSlot(entity, item, emptySlotNo);
} else {
item.destroy();
}
return true;
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryAuthoritySystem method removeItemInternal.
private EntityRef removeItemInternal(EntityRef inventory, EntityRef instigator, List<EntityRef> items, boolean destroyRemoved, Integer count) {
final EntityRef firstItem = items.get(0);
for (EntityRef item : items) {
if (item != firstItem && !InventoryUtils.isSameItem(firstItem, item)) {
return null;
}
}
for (EntityRef item : items) {
ItemComponent itemToRemove = item.getComponent(ItemComponent.class);
if (itemToRemove == null) {
return EntityRef.NULL;
}
}
List<Integer> slotsWithItem = new LinkedList<>();
for (EntityRef item : items) {
int slotWithItem = InventoryUtils.getSlotWithItem(inventory, item);
if (slotWithItem == -1) {
return null;
}
slotsWithItem.add(slotWithItem);
}
Integer toRemove = count;
if (toRemove == null) {
toRemove = 0;
for (EntityRef item : items) {
toRemove += InventoryUtils.getStackCount(item);
}
}
return removeItemFromSlots(instigator, destroyRemoved, inventory, slotsWithItem, toRemove);
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryClientSystem method moveItemFillClientTempEntities.
private boolean moveItemFillClientTempEntities(EntityRef fromInventory, EntityRef instigator, int slotFrom, EntityRef toInventory, int slotTo, Collection<EntityRef> clientTempEntities) {
boolean slotFromEmpty = !InventoryUtils.getItemAt(fromInventory, slotFrom).exists();
boolean slotToEmpty = !InventoryUtils.getItemAt(toInventory, slotTo).exists();
if (!InventoryUtils.moveItem(instigator, fromInventory, slotFrom, toInventory, slotTo)) {
return true;
}
if (slotFromEmpty) {
EntityRef itemAt = InventoryUtils.getItemAt(fromInventory, slotFrom);
if (itemAt.exists()) {
clientTempEntities.add(itemAt);
}
}
if (slotToEmpty) {
EntityRef itemAt = InventoryUtils.getItemAt(toInventory, slotTo);
if (itemAt.exists()) {
clientTempEntities.add(itemAt);
}
}
return false;
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryClientSystem method switchItem.
@Override
public boolean switchItem(EntityRef fromInventory, EntityRef instigator, int slotFrom, EntityRef toInventory, int slotTo) {
Collection<EntityRef> clientTempEntities = new HashSet<>();
if (moveItemFillClientTempEntities(fromInventory, instigator, slotFrom, toInventory, slotTo, clientTempEntities)) {
return false;
}
MoveItemRequest request = new MoveItemRequest(instigator, fromInventory, slotFrom, toInventory, slotTo, changeId++, clientTempEntities);
pendingMoves.put(request.getChangeId(), request);
localPlayer.getClientEntity().send(request);
return true;
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class InventoryClientSystem method moveItem.
@Override
public boolean moveItem(EntityRef fromInventory, EntityRef instigator, int slotFrom, EntityRef toInventory, int slotTo, int count) {
Collection<EntityRef> clientTempEntities = new HashSet<>();
if (moveItemAmountFillClientTempEntities(fromInventory, instigator, slotFrom, toInventory, slotTo, count, clientTempEntities)) {
return false;
}
MoveItemAmountRequest request = new MoveItemAmountRequest(instigator, fromInventory, slotFrom, toInventory, slotTo, count, changeId++, clientTempEntities);
pendingMoves.put(request.getChangeId(), request);
localPlayer.getClientEntity().send(request);
return true;
}
Aggregations