Search in sources :

Example 1 with ItemStorage

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)));
}
Also used : InventoryCitizen(com.minecolonies.coremod.inventory.InventoryCitizen) java.util(java.util) Blocks(net.minecraft.init.Blocks) ICapabilityProvider(net.minecraftforge.common.capabilities.ICapabilityProvider) AbstractJob(com.minecolonies.coremod.colony.jobs.AbstractJob) TextComponentBase(net.minecraft.util.text.TextComponentBase) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) TileEntityChest(net.minecraft.tileentity.TileEntityChest) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) ItemStack(net.minecraft.item.ItemStack) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) AITarget(com.minecolonies.coremod.entity.ai.util.AITarget) Block(net.minecraft.block.Block) com.minecolonies.api.util(com.minecolonies.api.util) TranslationConstants(com.minecolonies.api.util.constant.TranslationConstants) ItemStorage(com.minecolonies.coremod.entity.ai.item.handling.ItemStorage) WalkToProxy(com.minecolonies.coremod.entity.pathfinding.WalkToProxy) Predicate(java.util.function.Predicate) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) AIState(com.minecolonies.coremod.entity.ai.util.AIState) Nullable(org.jetbrains.annotations.Nullable) AbstractBuildingWorker(com.minecolonies.coremod.colony.buildings.AbstractBuildingWorker) TileEntity(net.minecraft.tileentity.TileEntity) NotNull(org.jetbrains.annotations.NotNull) AbstractBuildingWorker(com.minecolonies.coremod.colony.buildings.AbstractBuildingWorker) ItemStorage(com.minecolonies.coremod.entity.ai.item.handling.ItemStorage) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ItemStorage

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));
}
Also used : ItemStorage(com.minecolonies.coremod.entity.ai.item.handling.ItemStorage)

Example 3 with ItemStorage

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();
}
Also used : InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable) ItemStorage(com.minecolonies.coremod.entity.ai.item.handling.ItemStorage)

Aggregations

ItemStorage (com.minecolonies.coremod.entity.ai.item.handling.ItemStorage)3 ItemStack (net.minecraft.item.ItemStack)2 InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)2 Nullable (org.jetbrains.annotations.Nullable)2 com.minecolonies.api.util (com.minecolonies.api.util)1 TranslationConstants (com.minecolonies.api.util.constant.TranslationConstants)1 AbstractBuildingWorker (com.minecolonies.coremod.colony.buildings.AbstractBuildingWorker)1 AbstractJob (com.minecolonies.coremod.colony.jobs.AbstractJob)1 JobDeliveryman (com.minecolonies.coremod.colony.jobs.JobDeliveryman)1 AIState (com.minecolonies.coremod.entity.ai.util.AIState)1 AITarget (com.minecolonies.coremod.entity.ai.util.AITarget)1 WalkToProxy (com.minecolonies.coremod.entity.pathfinding.WalkToProxy)1 InventoryCitizen (com.minecolonies.coremod.inventory.InventoryCitizen)1 java.util (java.util)1 Predicate (java.util.function.Predicate)1 Block (net.minecraft.block.Block)1 Blocks (net.minecraft.init.Blocks)1 TileEntity (net.minecraft.tileentity.TileEntity)1 TileEntityChest (net.minecraft.tileentity.TileEntityChest)1 EnumFacing (net.minecraft.util.EnumFacing)1