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;
}
Aggregations