Search in sources :

Example 1 with ItemCheckResult

use of com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIStructure.ItemCheckResult in project minecolonies by Minecolonies.

the class AbstractEntityAIStructure method hasListOfResInInvOrRequest.

/**
 * Check the placers inventory for the items in the itemList and remove it of the list if found.
 *
 * @param placer   the placer.
 * @param itemList the list to check.
 * @param force    if force insertion.
 * @return true if need to request.
 */
public static <J extends AbstractJobStructure<?, J>, B extends AbstractBuildingStructureBuilder> ItemCheckResult hasListOfResInInvOrRequest(@NotNull final AbstractEntityAIStructure<J, B> placer, final List<ItemStack> itemList, final boolean force) {
    for (final ItemStack stack : itemList) {
        if (!InventoryUtils.hasItemInItemHandler(placer.getInventory(), stack1 -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, stack1)) && !placer.getOwnBuilding().hasResourceInBucket(stack)) {
            return RECALC;
        }
    }
    final List<ItemStack> foundStacks = InventoryUtils.filterItemHandler(placer.getWorker().getInventoryCitizen(), itemStack -> itemList.stream().anyMatch(targetStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(targetStack, itemStack)));
    if (force) {
        for (final ItemStack foundStack : new ArrayList<>(foundStacks)) {
            final Optional<ItemStack> opt = itemList.stream().filter(targetStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(targetStack, foundStack)).findFirst();
            if (opt.isPresent()) {
                final ItemStack stack = opt.get();
                itemList.remove(stack);
                if (stack.getCount() > foundStack.getCount()) {
                    stack.setCount(stack.getCount() - foundStack.getCount());
                    itemList.add(stack);
                }
            }
        }
    } else {
        itemList.removeIf(itemStack -> ItemStackUtils.isEmpty(itemStack) || foundStacks.stream().anyMatch(target -> ItemStackUtils.compareItemStacksIgnoreStackSize(target, itemStack)));
    }
    itemList.removeIf(itemstack -> itemstack.getItem() instanceof BlockItem && isBlockFree(((BlockItem) itemstack.getItem()).getBlock()));
    final Map<ItemStorage, Integer> list = new HashMap<>();
    for (final ItemStack stack : itemList) {
        ItemStorage tempStorage = new ItemStorage(stack.copy());
        if (list.containsKey(tempStorage)) {
            final int oldSize = list.get(tempStorage);
            tempStorage.setAmount(tempStorage.getAmount() + oldSize);
        }
        list.put(tempStorage, placer.getTotalAmount(tempStorage.getItemStack()).getCount());
    }
    for (final Map.Entry<ItemStorage, Integer> placedStack : list.entrySet()) {
        if (ItemStackUtils.isEmpty(placedStack.getKey().getItemStack())) {
            return FAIL;
        }
        final ImmutableList<IRequest<? extends IDeliverable>> requests = placer.getOwnBuilding().getOpenRequestsOfTypeFiltered(placer.getWorker().getCitizenData(), TypeConstants.DELIVERABLE, (IRequest<? extends IDeliverable> r) -> r.getRequest().matches(placedStack.getKey().getItemStack()));
        final ImmutableList<IRequest<? extends IDeliverable>> completedRequests = placer.getOwnBuilding().getCompletedRequestsOfTypeFiltered(placer.getWorker().getCitizenData(), TypeConstants.DELIVERABLE, (IRequest<? extends IDeliverable> r) -> r.getRequest().matches(placedStack.getKey().getItemStack()));
        if (requests.isEmpty() && completedRequests.isEmpty()) {
            final com.minecolonies.api.colony.requestsystem.requestable.Stack stackRequest = new Stack(placedStack.getKey().getItemStack(), placedStack.getValue(), 1);
            placer.getWorker().getCitizenData().createRequest(stackRequest);
            placer.registerBlockAsNeeded(placedStack.getKey().getItemStack());
            return FAIL;
        } else {
            for (final IRequest<? extends IDeliverable> request : requests) {
                if (placer.worker.getCitizenJobHandler().getColonyJob().getAsyncRequests().contains(request.getId())) {
                    placer.worker.getCitizenJobHandler().getColonyJob().markRequestSync(request.getId());
                }
            }
            for (final IRequest<? extends IDeliverable> request : completedRequests) {
                if (placer.worker.getCitizenJobHandler().getColonyJob().getAsyncRequests().contains(request.getId())) {
                    placer.worker.getCitizenJobHandler().getColonyJob().markRequestSync(request.getId());
                }
            }
        }
        return FAIL;
    }
    return SUCCESS;
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) BLOCK_PLACE_SPEED(com.minecolonies.api.research.util.ResearchConstants.BLOCK_PLACE_SPEED) AirBlock(net.minecraft.block.AirBlock) IBuilderUndestroyable(com.minecolonies.api.entity.ai.citizen.builder.IBuilderUndestroyable) TileEntityDecorationController(com.minecolonies.coremod.tileentities.TileEntityDecorationController) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) TriPredicate(net.minecraftforge.common.util.TriPredicate) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) Block(net.minecraft.block.Block) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) BlockTags(net.minecraft.tags.BlockTags) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) BlockState(net.minecraft.block.BlockState) CitizenConstants(com.minecolonies.api.util.constant.CitizenConstants) TICKS_SECOND(com.minecolonies.api.util.constant.Constants.TICKS_SECOND) AIEventTarget(com.minecolonies.api.entity.ai.statemachine.AIEventTarget) Predicate(java.util.function.Predicate) BlueprintPositionInfo(com.ldtteam.structurize.util.BlueprintPositionInfo) PlacementSettings(com.ldtteam.structurize.util.PlacementSettings) Nullable(org.jetbrains.annotations.Nullable) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) BlockItem(net.minecraft.item.BlockItem) MineColonies(com.minecolonies.coremod.MineColonies) AbstractJobStructure(com.minecolonies.coremod.colony.jobs.AbstractJobStructure) IStructureHandler(com.ldtteam.structurize.placement.structure.IStructureHandler) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) StructurePlacer(com.ldtteam.structurize.placement.StructurePlacer) BuilderBucket(com.minecolonies.coremod.colony.buildings.utils.BuilderBucket) java.util(java.util) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ModBlocks(com.minecolonies.api.blocks.ModBlocks) Mirror(net.minecraft.util.Mirror) ItemStack(net.minecraft.item.ItemStack) BuildingResourcesModule(com.minecolonies.coremod.colony.buildings.modules.BuildingResourcesModule) ImmutableList(com.google.common.collect.ImmutableList) AbstractBuildingStructureBuilder(com.minecolonies.coremod.colony.buildings.AbstractBuildingStructureBuilder) Hand(net.minecraft.util.Hand) com.minecolonies.api.util(com.minecolonies.api.util) StructurePhasePlacementResult(com.ldtteam.structurize.placement.StructurePhasePlacementResult) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) BuildingStructureHandler(com.minecolonies.coremod.entity.ai.util.BuildingStructureHandler) ModTags(com.minecolonies.api.items.ModTags) AIBlockingEventType(com.minecolonies.api.entity.ai.statemachine.states.AIBlockingEventType) BlockPos(net.minecraft.util.math.BlockPos) NULL_POS(com.ldtteam.structurize.placement.AbstractBlueprintIterator.NULL_POS) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) Blocks(net.minecraft.block.Blocks) Stage(com.minecolonies.coremod.entity.ai.util.BuildingStructureHandler.Stage) TypeConstants(com.minecolonies.api.util.constant.TypeConstants) BlockPlacementResult(com.ldtteam.structurize.placement.BlockPlacementResult) BlockUtils(com.ldtteam.structurize.util.BlockUtils) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) BlockFluidSubstitution(com.ldtteam.structurize.blocks.schematic.BlockFluidSubstitution) TileEntity(net.minecraft.tileentity.TileEntity) ItemCheckResult(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIStructure.ItemCheckResult) BlockItem(net.minecraft.item.BlockItem) ItemStorage(com.minecolonies.api.crafting.ItemStorage) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint) ItemStack(net.minecraft.item.ItemStack) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) ItemStack(net.minecraft.item.ItemStack) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack)

Example 2 with ItemCheckResult

use of com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIStructure.ItemCheckResult in project minecolonies by ldtteam.

the class AbstractEntityAIStructure method hasListOfResInInvOrRequest.

/**
 * Check the placers inventory for the items in the itemList and remove it of the list if found.
 *
 * @param placer   the placer.
 * @param itemList the list to check.
 * @param force    if force insertion.
 * @return true if need to request.
 */
public static <J extends AbstractJobStructure<?, J>, B extends AbstractBuildingStructureBuilder> ItemCheckResult hasListOfResInInvOrRequest(@NotNull final AbstractEntityAIStructure<J, B> placer, final List<ItemStack> itemList, final boolean force) {
    final Map<ItemStorage, Integer> requestedMap = new HashMap<>();
    for (final ItemStack stack : itemList) {
        if (stack.getItem() instanceof BlockItem && isBlockFree(((BlockItem) stack.getItem()).getBlock())) {
            continue;
        }
        ItemStorage tempStorage = new ItemStorage(stack.copy());
        if (requestedMap.containsKey(tempStorage)) {
            final int oldSize = requestedMap.get(tempStorage);
            tempStorage.setAmount(tempStorage.getAmount() + oldSize);
        }
        requestedMap.put(tempStorage, tempStorage.getAmount());
    }
    for (final ItemStorage stack : requestedMap.keySet()) {
        if (!InventoryUtils.hasItemInItemHandler(placer.getInventory(), stack1 -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack.getItemStack(), stack1)) && !placer.getOwnBuilding().hasResourceInBucket(stack.getItemStack())) {
            return RECALC;
        }
    }
    final List<ItemStack> foundStacks = InventoryUtils.filterItemHandler(placer.getWorker().getInventoryCitizen(), itemStack -> requestedMap.keySet().stream().anyMatch(storage -> ItemStackUtils.compareItemStacksIgnoreStackSize(storage.getItemStack(), itemStack)));
    final Map<ItemStorage, Integer> localMap = new HashMap<>();
    for (final ItemStack stack : foundStacks) {
        ItemStorage tempStorage = new ItemStorage(stack.copy());
        if (localMap.containsKey(tempStorage)) {
            final int oldSize = localMap.get(tempStorage);
            tempStorage.setAmount(tempStorage.getAmount() + oldSize);
        }
        localMap.put(tempStorage, tempStorage.getAmount());
    }
    if (force) {
        for (final Map.Entry<ItemStorage, Integer> local : localMap.entrySet()) {
            int req = requestedMap.getOrDefault(local.getKey(), 0);
            if (req != 0) {
                if (local.getValue() >= req) {
                    requestedMap.remove(local.getKey());
                } else {
                    requestedMap.put(local.getKey(), req - local.getValue());
                }
            }
        }
    } else {
        requestedMap.entrySet().removeIf(entry -> ItemStackUtils.isEmpty(entry.getKey().getItemStack()) || foundStacks.stream().anyMatch(target -> ItemStackUtils.compareItemStacksIgnoreStackSize(target, entry.getKey().getItemStack())));
    }
    for (final Map.Entry<ItemStorage, Integer> placedStack : requestedMap.entrySet()) {
        final ItemStack stack = placedStack.getKey().getItemStack();
        if (ItemStackUtils.isEmpty(stack)) {
            return FAIL;
        }
        final ImmutableList<IRequest<? extends IDeliverable>> requests = placer.getOwnBuilding().getOpenRequestsOfTypeFiltered(placer.getWorker().getCitizenData(), TypeConstants.DELIVERABLE, (IRequest<? extends IDeliverable> r) -> r.getRequest().matches(stack));
        final ImmutableList<IRequest<? extends IDeliverable>> completedRequests = placer.getOwnBuilding().getCompletedRequestsOfTypeFiltered(placer.getWorker().getCitizenData(), TypeConstants.DELIVERABLE, (IRequest<? extends IDeliverable> r) -> r.getRequest().matches(stack));
        if (requests.isEmpty() && completedRequests.isEmpty()) {
            final com.minecolonies.api.colony.requestsystem.requestable.Stack stackRequest = new Stack(stack, placer.getTotalAmount(stack).getCount(), 1);
            placer.getWorker().getCitizenData().createRequest(stackRequest);
            placer.registerBlockAsNeeded(stack);
            return FAIL;
        } else {
            for (final IRequest<? extends IDeliverable> request : requests) {
                if (placer.worker.getCitizenJobHandler().getColonyJob().getAsyncRequests().contains(request.getId())) {
                    placer.worker.getCitizenJobHandler().getColonyJob().markRequestSync(request.getId());
                }
            }
            for (final IRequest<? extends IDeliverable> request : completedRequests) {
                if (placer.worker.getCitizenJobHandler().getColonyJob().getAsyncRequests().contains(request.getId())) {
                    placer.worker.getCitizenJobHandler().getColonyJob().markRequestSync(request.getId());
                }
            }
        }
        return FAIL;
    }
    return SUCCESS;
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) BLOCK_PLACE_SPEED(com.minecolonies.api.research.util.ResearchConstants.BLOCK_PLACE_SPEED) AirBlock(net.minecraft.block.AirBlock) IBuilderUndestroyable(com.minecolonies.api.entity.ai.citizen.builder.IBuilderUndestroyable) TileEntityDecorationController(com.minecolonies.coremod.tileentities.TileEntityDecorationController) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) TriPredicate(net.minecraftforge.common.util.TriPredicate) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) Block(net.minecraft.block.Block) IAIState(com.minecolonies.api.entity.ai.statemachine.states.IAIState) BlockTags(net.minecraft.tags.BlockTags) AITarget(com.minecolonies.api.entity.ai.statemachine.AITarget) BlockState(net.minecraft.block.BlockState) CitizenConstants(com.minecolonies.api.util.constant.CitizenConstants) TICKS_SECOND(com.minecolonies.api.util.constant.Constants.TICKS_SECOND) AIEventTarget(com.minecolonies.api.entity.ai.statemachine.AIEventTarget) Predicate(java.util.function.Predicate) BlueprintPositionInfo(com.ldtteam.structurize.util.BlueprintPositionInfo) PlacementSettings(com.ldtteam.structurize.util.PlacementSettings) Nullable(org.jetbrains.annotations.Nullable) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) BlockItem(net.minecraft.item.BlockItem) MineColonies(com.minecolonies.coremod.MineColonies) AbstractJobStructure(com.minecolonies.coremod.colony.jobs.AbstractJobStructure) IStructureHandler(com.ldtteam.structurize.placement.structure.IStructureHandler) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) StructurePlacer(com.ldtteam.structurize.placement.StructurePlacer) BuilderBucket(com.minecolonies.coremod.colony.buildings.utils.BuilderBucket) java.util(java.util) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) ModBlocks(com.minecolonies.api.blocks.ModBlocks) Mirror(net.minecraft.util.Mirror) ItemStack(net.minecraft.item.ItemStack) BuildingResourcesModule(com.minecolonies.coremod.colony.buildings.modules.BuildingResourcesModule) ImmutableList(com.google.common.collect.ImmutableList) AbstractBuildingStructureBuilder(com.minecolonies.coremod.colony.buildings.AbstractBuildingStructureBuilder) Hand(net.minecraft.util.Hand) com.minecolonies.api.util(com.minecolonies.api.util) StructurePhasePlacementResult(com.ldtteam.structurize.placement.StructurePhasePlacementResult) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) BuildingStructureHandler(com.minecolonies.coremod.entity.ai.util.BuildingStructureHandler) ModTags(com.minecolonies.api.items.ModTags) AIBlockingEventType(com.minecolonies.api.entity.ai.statemachine.states.AIBlockingEventType) BlockPos(net.minecraft.util.math.BlockPos) NULL_POS(com.ldtteam.structurize.placement.AbstractBlueprintIterator.NULL_POS) AIWorkerState(com.minecolonies.api.entity.ai.statemachine.states.AIWorkerState) AbstractBlockHut(com.minecolonies.api.blocks.AbstractBlockHut) Blocks(net.minecraft.block.Blocks) Stage(com.minecolonies.coremod.entity.ai.util.BuildingStructureHandler.Stage) TypeConstants(com.minecolonies.api.util.constant.TypeConstants) BlockPlacementResult(com.ldtteam.structurize.placement.BlockPlacementResult) BlockUtils(com.ldtteam.structurize.util.BlockUtils) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) BlockFluidSubstitution(com.ldtteam.structurize.blocks.schematic.BlockFluidSubstitution) TileEntity(net.minecraft.tileentity.TileEntity) ItemCheckResult(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIStructure.ItemCheckResult) BlockItem(net.minecraft.item.BlockItem) ItemStorage(com.minecolonies.api.crafting.ItemStorage) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint) ItemStack(net.minecraft.item.ItemStack) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) ItemStack(net.minecraft.item.ItemStack) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)2 Blueprint (com.ldtteam.structures.blueprints.v1.Blueprint)2 BlockFluidSubstitution (com.ldtteam.structurize.blocks.schematic.BlockFluidSubstitution)2 NULL_POS (com.ldtteam.structurize.placement.AbstractBlueprintIterator.NULL_POS)2 BlockPlacementResult (com.ldtteam.structurize.placement.BlockPlacementResult)2 StructurePhasePlacementResult (com.ldtteam.structurize.placement.StructurePhasePlacementResult)2 StructurePlacer (com.ldtteam.structurize.placement.StructurePlacer)2 IStructureHandler (com.ldtteam.structurize.placement.structure.IStructureHandler)2 BlockUtils (com.ldtteam.structurize.util.BlockUtils)2 BlueprintPositionInfo (com.ldtteam.structurize.util.BlueprintPositionInfo)2 PlacementSettings (com.ldtteam.structurize.util.PlacementSettings)2 AbstractBlockHut (com.minecolonies.api.blocks.AbstractBlockHut)2 ModBlocks (com.minecolonies.api.blocks.ModBlocks)2 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)2 IDeliverable (com.minecolonies.api.colony.requestsystem.requestable.IDeliverable)2 Stack (com.minecolonies.api.colony.requestsystem.requestable.Stack)2 ItemStorage (com.minecolonies.api.crafting.ItemStorage)2 IBuilderUndestroyable (com.minecolonies.api.entity.ai.citizen.builder.IBuilderUndestroyable)2 AIEventTarget (com.minecolonies.api.entity.ai.statemachine.AIEventTarget)2