Search in sources :

Example 16 with Delivery

use of com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery in project minecolonies by ldtteam.

the class EntityAIWorkDeliveryman method prepareDelivery.

/**
 * Prepare deliveryman for delivery. Check if the building still needs the item and if the required items are still in the warehouse.
 *
 * @return the next state to go to.
 */
private IAIState prepareDelivery() {
    final IRequest<? extends IRequestable> currentTask = job.getCurrentTask();
    if (!(currentTask instanceof DeliveryRequest)) {
        // Restart.
        return START_WORKING;
    }
    final List<IRequest<? extends Delivery>> taskList = job.getTaskListWithSameDestination((IRequest<? extends Delivery>) currentTask);
    final List<ItemStack> alreadyInInv = new ArrayList<>();
    IRequest<? extends Delivery> nextPickUp = null;
    int parallelDeliveryCount = 0;
    for (final IRequest<? extends Delivery> task : taskList) {
        parallelDeliveryCount++;
        int totalCount = InventoryUtils.getItemCountInItemHandler(worker.getInventoryCitizen(), itemStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(task.getRequest().getStack(), itemStack));
        int hasCount = 0;
        for (final ItemStack stack : alreadyInInv) {
            if (ItemStackUtils.compareItemStacksIgnoreStackSize(stack, task.getRequest().getStack())) {
                hasCount += stack.getCount();
            }
        }
        if (totalCount < hasCount + task.getRequest().getStack().getCount()) {
            nextPickUp = task;
            break;
        } else {
            alreadyInInv.add(task.getRequest().getStack());
        }
    }
    if (nextPickUp == null || parallelDeliveryCount > 1 + (getSecondarySkillLevel() / 5)) {
        return DELIVERY;
    }
    final ILocation location = nextPickUp.getRequest().getStart();
    if (!location.isReachableFromLocation(worker.getLocation())) {
        job.finishRequest(false);
        return START_WORKING;
    }
    if (walkToBlock(location.getInDimensionLocation())) {
        return PREPARE_DELIVERY;
    }
    if (getInventory().isFull()) {
        return DUMPING;
    }
    final TileEntity tileEntity = world.getBlockEntity(location.getInDimensionLocation());
    job.addConcurrentDelivery(nextPickUp.getId());
    if (gatherIfInTileEntity(tileEntity, nextPickUp.getRequest().getStack())) {
        return PREPARE_DELIVERY;
    }
    if (parallelDeliveryCount > 1) {
        job.removeConcurrentDelivery(nextPickUp.getId());
        return DELIVERY;
    }
    job.finishRequest(false);
    job.removeConcurrentDelivery(nextPickUp.getId());
    return START_WORKING;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) DeliveryRequest(com.minecolonies.coremod.colony.requestsystem.requests.StandardRequests.DeliveryRequest) ArrayList(java.util.ArrayList) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) ItemStack(net.minecraft.item.ItemStack)

Aggregations

Delivery (com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery)16 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)12 ItemStack (net.minecraft.item.ItemStack)8 ArrayList (java.util.ArrayList)7 Nullable (org.jetbrains.annotations.Nullable)7 ILocation (com.minecolonies.api.colony.requestsystem.location.ILocation)6 ItemStackUtils (com.minecolonies.api.util.ItemStackUtils)6 TileEntity (net.minecraft.tileentity.TileEntity)5 BlockPos (net.minecraft.util.math.BlockPos)5 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)5 NotNull (org.jetbrains.annotations.NotNull)5 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)4 IRequester (com.minecolonies.api.colony.requestsystem.requester.IRequester)4 IToken (com.minecolonies.api.colony.requestsystem.token.IToken)4 ItemStorage (com.minecolonies.api.crafting.ItemStorage)4 InventoryUtils (com.minecolonies.api.util.InventoryUtils)4 Log (com.minecolonies.api.util.Log)4 Constants (com.minecolonies.api.util.constant.Constants)4 TranslationConstants (com.minecolonies.api.util.constant.TranslationConstants)4 Colony (com.minecolonies.coremod.colony.Colony)4