use of com.minecolonies.coremod.entity.ai.item.handling.ItemStorage in project minecolonies by Minecolonies.
the class AbstractEntityAIBasic method dumpOneMoreSlot.
/**
* Dumps one inventory slot into the building chest.
*
* @param keepIt used to test it that stack should be kept
* @return true if is has to dump more.
*/
private boolean dumpOneMoreSlot(@NotNull final Predicate<ItemStack> keepIt) {
//Items already kept in the inventory
final Map<ItemStorage, Integer> alreadyKept = new HashMap<>();
final Map<ItemStorage, Integer> shouldKeep = getOwnBuilding().getRequiredItemsAndAmount();
@Nullable final AbstractBuildingWorker buildingWorker = getOwnBuilding();
return buildingWorker != null && (walkToBuilding() || InventoryFunctions.matchFirstInProvider(worker, (slot, stack) -> !(InventoryUtils.isItemStackEmpty(stack) || keepIt.test(stack)) && shouldDumpItem(alreadyKept, shouldKeep, buildingWorker, stack, slot)));
}
use of com.minecolonies.coremod.entity.ai.item.handling.ItemStorage in project minecolonies by Minecolonies.
the class AbstractEntityAIBasic method keptEnough.
/**
* Checks if enough items have been marked as to be kept already.
*
* @param alreadyKept kept items.
* @param shouldKeep items to keep.
* @param stack stack to analyse.
* @return true if the the item shouldn't be kept.
*/
private static boolean keptEnough(@NotNull final Map<ItemStorage, Integer> alreadyKept, @NotNull final Map<ItemStorage, Integer> shouldKeep, @NotNull final ItemStack stack) {
final ArrayList<Map.Entry<ItemStorage, Integer>> tempKeep = new ArrayList<>(shouldKeep.entrySet());
for (final Map.Entry<ItemStorage, Integer> tempEntry : tempKeep) {
final ItemStorage tempStorage = tempEntry.getKey();
if (tempStorage != null && tempStorage.getItem() == stack.getItem() && tempStorage.getDamageValue() != stack.getItemDamage()) {
shouldKeep.put(new ItemStorage(stack.getItem(), stack.getItemDamage(), 0, tempStorage.ignoreDamageValue()), tempEntry.getValue());
break;
}
}
final ItemStorage tempStorage = new ItemStorage(stack.getItem(), stack.getItemDamage(), 0, false);
//Check first if the the item shouldn't be kept if it should be kept check if we already kept enough of them.
return shouldKeep.get(tempStorage) == null || (alreadyKept.get(tempStorage) != null && alreadyKept.get(tempStorage) >= shouldKeep.get(tempStorage));
}
use of com.minecolonies.coremod.entity.ai.item.handling.ItemStorage in project minecolonies by Minecolonies.
the class AbstractEntityAIBasic method shouldDumpItem.
/**
* Checks if an item should be kept and deposits the rest into his chest.
*
* @param alreadyKept already kept items.
* @param shouldKeep items that should be kept.
* @param buildingWorker the building of the worker.
* @param stack the stack being analyzed.
* @param slot the iteration inside the inventory.
* @return true if should be dumped.
*/
private boolean shouldDumpItem(@NotNull final Map<ItemStorage, Integer> alreadyKept, @NotNull final Map<ItemStorage, Integer> shouldKeep, @NotNull final AbstractBuildingWorker buildingWorker, @NotNull final ItemStack stack, final int slot) {
@Nullable final ItemStack returnStack;
int amountToKeep = 0;
if (keptEnough(alreadyKept, shouldKeep, stack)) {
returnStack = InventoryUtils.addItemStackToProviderWithResult(buildingWorker.getTileEntity(), stack.copy());
} else {
final ItemStorage tempStorage = new ItemStorage(stack.getItem(), stack.getItemDamage(), stack.getCount(), false);
final ItemStack tempStack = handleKeepX(alreadyKept, shouldKeep, tempStorage);
if (InventoryUtils.isItemStackEmpty(tempStack)) {
return false;
}
amountToKeep = stack.getCount() - tempStorage.getAmount();
returnStack = InventoryUtils.addItemStackToProviderWithResult(buildingWorker.getTileEntity(), tempStack);
}
if (InventoryUtils.isItemStackEmpty(returnStack)) {
new InvWrapper(worker.getInventoryCitizen()).extractItem(slot, stack.getCount() - amountToKeep, false);
return amountToKeep == 0;
}
new InvWrapper(worker.getInventoryCitizen()).extractItem(slot, stack.getCount() - returnStack.getCount() - amountToKeep, false);
//Check that we are not inserting into a full inventory.
return stack.getCount() != returnStack.getCount();
}
Aggregations