Search in sources :

Example 1 with Delivery

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

the class DeliveryRequestResolver method attemptResolve.

@Nullable
@Override
public List<IToken<?>> attemptResolve(@NotNull final IRequestManager manager, @NotNull final IRequest<? extends Delivery> request) {
    if (manager.getColony().getWorld().isRemote) {
        return null;
    }
    final Colony colony = (Colony) manager.getColony();
    // We can do an instant get here, since we are already filtering on anything that has no entity.
    final CitizenData freeDeliveryMan = colony.getCitizenManager().getCitizens().stream().filter(citizenData -> citizenData.getCitizenEntity().map(entityCitizen -> request.getRequest().getTarget().isReachableFromLocation(entityCitizen.getLocation())).orElse(false)).filter(c -> c.getJob() instanceof JobDeliveryman).min(Comparator.comparing((CitizenData c) -> ((JobDeliveryman) c.getJob()).getTaskQueue().size()).thenComparing(Comparator.comparing(c -> {
        BlockPos targetPos = request.getRequest().getTarget().getInDimensionLocation();
        // We can do an instant get here, since we are already filtering on anything that has no entity.
        BlockPos entityLocation = c.getCitizenEntity().get().getLocation().getInDimensionLocation();
        return BlockPosUtil.getDistanceSquared(targetPos, entityLocation);
    }))).orElse(null);
    if (freeDeliveryMan == null) {
        return null;
    }
    final JobDeliveryman job = (JobDeliveryman) freeDeliveryMan.getJob();
    job.addRequest(request.getToken());
    return Lists.newArrayList();
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Colony(com.minecolonies.coremod.colony.Colony) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) BlockPos(net.minecraft.util.math.BlockPos) TypeToken(com.google.common.reflect.TypeToken) BlockPosUtil(com.minecolonies.api.util.BlockPosUtil) Log(com.minecolonies.blockout.Log) IRequestManager(com.minecolonies.api.colony.requestsystem.manager.IRequestManager) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) Nullable(org.jetbrains.annotations.Nullable) CitizenData(com.minecolonies.coremod.colony.CitizenData) List(java.util.List) Lists(com.google.common.collect.Lists) MineColonies(com.minecolonies.coremod.MineColonies) Delivery(com.minecolonies.api.colony.requestsystem.requestable.Delivery) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) AbstractRequestResolver(com.minecolonies.coremod.colony.requestsystem.resolvers.core.AbstractRequestResolver) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) Colony(com.minecolonies.coremod.colony.Colony) CitizenData(com.minecolonies.coremod.colony.CitizenData) BlockPos(net.minecraft.util.math.BlockPos) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Delivery

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

the class DeliveryRequestResolver method canResolve.

@Override
public boolean canResolve(@NotNull final IRequestManager manager, final IRequest<? extends Delivery> requestToCheck) {
    if (manager.getColony().getWorld().isRemote) {
        return false;
    }
    final Colony colony = (Colony) manager.getColony();
    final CitizenData freeDeliveryMan = colony.getCitizenManager().getCitizens().stream().filter(citizenData -> citizenData.getCitizenEntity().map(entityCitizen -> requestToCheck.getRequest().getTarget().isReachableFromLocation(entityCitizen.getLocation())).orElse(false)).filter(c -> c.getJob() instanceof JobDeliveryman).findFirst().orElse(null);
    return freeDeliveryMan != null;
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Colony(com.minecolonies.coremod.colony.Colony) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) BlockPos(net.minecraft.util.math.BlockPos) TypeToken(com.google.common.reflect.TypeToken) BlockPosUtil(com.minecolonies.api.util.BlockPosUtil) Log(com.minecolonies.blockout.Log) IRequestManager(com.minecolonies.api.colony.requestsystem.manager.IRequestManager) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) Nullable(org.jetbrains.annotations.Nullable) CitizenData(com.minecolonies.coremod.colony.CitizenData) List(java.util.List) Lists(com.google.common.collect.Lists) MineColonies(com.minecolonies.coremod.MineColonies) Delivery(com.minecolonies.api.colony.requestsystem.requestable.Delivery) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) AbstractRequestResolver(com.minecolonies.coremod.colony.requestsystem.resolvers.core.AbstractRequestResolver) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) Colony(com.minecolonies.coremod.colony.Colony) CitizenData(com.minecolonies.coremod.colony.CitizenData)

Example 3 with Delivery

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

the class EntityAIWorkDeliveryman method deliver.

/**
 * Deliver the items to the hut.
 *
 * @return the next state.
 */
private AIState deliver() {
    if (job.isReturning()) {
        return DUMPING;
    }
    final BuildingDeliveryman deliveryHut = (getOwnBuilding() instanceof BuildingDeliveryman) ? (BuildingDeliveryman) getOwnBuilding() : null;
    final ILocation buildingToDeliver = deliveryHut == null ? null : deliveryHut.getBuildingToDeliver();
    if (deliveryHut == null) {
        return START_WORKING;
    } else if (buildingToDeliver == null) {
        if (job.getCurrentTask() != null && deliveryHut != null) {
            final IRequest<? extends Delivery> request = job.getCurrentTask();
            deliveryHut.setBuildingToDeliver(request.getRequest().getTarget());
            return getState();
        }
        return START_WORKING;
    }
    worker.setLatestStatus(new TextComponentTranslation("com.minecolonies.coremod.status.delivering"));
    if (!buildingToDeliver.isReachableFromLocation(worker.getLocation())) {
        Log.getLogger().info(worker.getColony().getName() + ": " + worker.getName() + ": Can't inter dimension yet: ");
        return START_WORKING;
    }
    if (!worker.isWorkerAtSiteWithMove(buildingToDeliver.getInDimensionLocation(), MIN_DISTANCE_TO_WAREHOUSE)) {
        return DELIVERY;
    }
    final InvWrapper workerInventory = new InvWrapper(worker.getInventoryCitizen());
    for (int i = 0; i < new InvWrapper(worker.getInventoryCitizen()).getSlots(); i++) {
        final ItemStack stack = workerInventory.extractItem(i, Integer.MAX_VALUE, false);
        if (ItemStackUtils.isEmpty(stack)) {
            continue;
        }
        final TileEntity tileEntity = world.getTileEntity(buildingToDeliver.getInDimensionLocation());
        final ItemStack insertionResultStack;
        if (tileEntity instanceof TileEntityColonyBuilding && ((TileEntityColonyBuilding) tileEntity).getBuilding() instanceof AbstractBuildingWorker) {
            final AbstractBuilding building = ((TileEntityColonyBuilding) tileEntity).getBuilding();
            building.alterPickUpPriority(1);
            insertionResultStack = InventoryUtils.forceItemStackToItemHandler(new InvWrapper((TileEntityColonyBuilding) tileEntity), stack, ((AbstractBuildingWorker) building)::isItemStackInRequest);
        } else {
            insertionResultStack = InventoryUtils.forceItemStackToItemHandler(new InvWrapper((TileEntityColonyBuilding) tileEntity), stack, itemStack -> false);
        }
        if (!ItemStackUtils.isEmpty(insertionResultStack)) {
            if (ItemStack.areItemStacksEqual(insertionResultStack, stack)) {
                // same stack, we could not deliver ?
                if (buildingToDeliver instanceof TileEntityColonyBuilding && ((TileEntityColonyBuilding) tileEntity).getBuilding() instanceof AbstractBuildingWorker) {
                    chatSpamFilter.talkWithoutSpam(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_NAMEDCHESTFULL, ((AbstractBuildingWorker) ((TileEntityColonyBuilding) tileEntity).getBuilding()).getMainWorker().getName());
                } else if (buildingToDeliver instanceof TileEntityColonyBuilding) {
                    chatSpamFilter.talkWithoutSpam(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL, new TextComponentString(" :" + ((TileEntityColonyBuilding) tileEntity).getBuilding().getSchematicName()));
                } else {
                    chatSpamFilter.talkWithoutSpam(COM_MINECOLONIES_COREMOD_JOB_DELIVERYMAN_CHESTFULL, new TextComponentString(buildingToDeliver.getInDimensionLocation().toString()));
                }
            }
            // Insert the result back into the inventory so we do not loose it.
            workerInventory.insertItem(i, insertionResultStack, false);
        }
    }
    lastDelivery = deliveryHut.getBuildingToDeliver();
    worker.addExperience(1.0D);
    worker.setHeldItem(SLOT_HAND);
    deliveryHut.setBuildingToDeliver(null);
    job.finishRequest(true);
    setDelay(WAIT_DELAY);
    return START_WORKING;
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) java.util(java.util) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) Log(com.minecolonies.blockout.Log) 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) ImmutableList(com.google.common.collect.ImmutableList) Delivery(com.minecolonies.api.colony.requestsystem.requestable.Delivery) TICKS_SECOND(com.minecolonies.api.util.constant.Constants.TICKS_SECOND) AbstractEntityAIInteract(com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIInteract) TranslationConstants(com.minecolonies.api.util.constant.TranslationConstants) Colony(com.minecolonies.coremod.colony.Colony) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) ItemFood(net.minecraft.item.ItemFood) BlockPos(net.minecraft.util.math.BlockPos) AIState(com.minecolonies.coremod.entity.ai.util.AIState) TileEntityColonyBuilding(com.minecolonies.coremod.tileentities.TileEntityColonyBuilding) Collectors(java.util.stream.Collectors) com.minecolonies.coremod.colony.buildings(com.minecolonies.coremod.colony.buildings) TextComponentString(net.minecraft.util.text.TextComponentString) Nullable(org.jetbrains.annotations.Nullable) InventoryUtils(com.minecolonies.api.util.InventoryUtils) TileEntity(net.minecraft.tileentity.TileEntity) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) Configurations(com.minecolonies.api.configuration.Configurations) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) TileEntityColonyBuilding(com.minecolonies.coremod.tileentities.TileEntityColonyBuilding) TextComponentString(net.minecraft.util.text.TextComponentString) TileEntity(net.minecraft.tileentity.TileEntity) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Delivery(com.minecolonies.api.colony.requestsystem.requestable.Delivery) ItemStack(net.minecraft.item.ItemStack)

Example 4 with Delivery

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

the class WarehouseRequestResolver method attemptResolve.

@Nullable
@Override
@SuppressWarnings("squid:LeftCurlyBraceStartLineCheck")
public /**
 * Moving the curly braces really makes the code hard to read.
 */
List<IToken<?>> attemptResolve(@NotNull final IRequestManager manager, @NotNull final IRequest<? extends IDeliverable> request) {
    if (manager.getColony().getWorld().isRemote) {
        return null;
    }
    final Colony colony = (Colony) manager.getColony();
    final Set<TileEntityWareHouse> wareHouses = getWareHousesInColony(colony);
    for (final TileEntityWareHouse wareHouse : wareHouses) {
        ItemStack matchingStack = wareHouse.getFirstMatchingItemStackInWarehouse(itemStack -> request.getRequest().matches(itemStack));
        if (ItemStackUtils.isEmpty(matchingStack)) {
            continue;
        }
        matchingStack = matchingStack.copy();
        matchingStack.setCount(Math.min(request.getRequest().getCount(), matchingStack.getCount()));
        final ItemStack deliveryStack = matchingStack.copy();
        request.setDelivery(deliveryStack.copy());
        final BlockPos itemStackPos = wareHouse.getPositionOfChestWithItemStack(itemStack -> ItemStack.areItemsEqual(itemStack, deliveryStack));
        final ILocation itemStackLocation = manager.getFactoryController().getNewInstance(TypeConstants.ILOCATION, itemStackPos, wareHouse.getWorld().provider.getDimension());
        final Delivery delivery = new Delivery(itemStackLocation, request.getRequester().getRequesterLocation(), deliveryStack.copy());
        final IToken<?> requestToken = manager.createRequest(new WarehouseRequestResolver(request.getRequester().getRequesterLocation(), request.getToken()), delivery);
        return ImmutableList.of(requestToken);
    }
    return Lists.newArrayList();
}
Also used : ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) TileEntityWareHouse(com.minecolonies.coremod.tileentities.TileEntityWareHouse) Colony(com.minecolonies.coremod.colony.Colony) BlockPos(net.minecraft.util.math.BlockPos) Delivery(com.minecolonies.api.colony.requestsystem.requestable.Delivery) ItemStack(net.minecraft.item.ItemStack) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ILocation (com.minecolonies.api.colony.requestsystem.location.ILocation)4 Delivery (com.minecolonies.api.colony.requestsystem.requestable.Delivery)4 Colony (com.minecolonies.coremod.colony.Colony)4 BlockPos (net.minecraft.util.math.BlockPos)4 Nullable (org.jetbrains.annotations.Nullable)4 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)3 IToken (com.minecolonies.api.colony.requestsystem.token.IToken)3 Log (com.minecolonies.blockout.Log)3 JobDeliveryman (com.minecolonies.coremod.colony.jobs.JobDeliveryman)3 NotNull (org.jetbrains.annotations.NotNull)3 Lists (com.google.common.collect.Lists)2 TypeToken (com.google.common.reflect.TypeToken)2 IRequestManager (com.minecolonies.api.colony.requestsystem.manager.IRequestManager)2 BlockPosUtil (com.minecolonies.api.util.BlockPosUtil)2 MineColonies (com.minecolonies.coremod.MineColonies)2 CitizenData (com.minecolonies.coremod.colony.CitizenData)2 AbstractRequestResolver (com.minecolonies.coremod.colony.requestsystem.resolvers.core.AbstractRequestResolver)2 Comparator (java.util.Comparator)2 List (java.util.List)2 ItemStack (net.minecraft.item.ItemStack)2