Search in sources :

Example 1 with Delivery

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

the class PublicWorkerCraftingProductionResolver method getFollowupRequestForCompletion.

@Nullable
@Override
public List<IRequest<?>> getFollowupRequestForCompletion(@NotNull final IRequestManager manager, @NotNull final IRequest<? extends PublicCrafting> completedRequest) {
    final IColony colony = manager.getColony();
    if (colony instanceof Colony || !completedRequest.hasParent()) {
        // Remove it from the task list.
        removeRequestFromTaskList(completedRequest, colony);
        // This is the crafting that got completed.
        // We go up the tree one level to get the actual request.
        // Get the requester for that request and ask where he wants his stuff delivered.
        final IRequest<?> parentRequest = manager.getRequestForToken(completedRequest.getParent());
        final IRequester parentRequestRequester = parentRequest.getRequester();
        if (parentRequestRequester.getLocation().equals(getLocation())) {
            return null;
        }
        final List<IRequest<?>> deliveries = Lists.newArrayList();
        parentRequest.addDelivery(completedRequest.getDeliveries());
        completedRequest.getDeliveries().forEach(itemStack -> {
            final Delivery delivery = new Delivery(getLocation(), parentRequestRequester.getLocation(), itemStack, getDefaultDeliveryPriority(true));
            final IToken<?> requestToken = manager.createRequest(this, delivery);
            deliveries.add(manager.getRequestForToken(requestToken));
        });
        return deliveries;
    }
    return null;
}
Also used : IColony(com.minecolonies.api.colony.IColony) Colony(com.minecolonies.coremod.colony.Colony) IColony(com.minecolonies.api.colony.IColony) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) IRequester(com.minecolonies.api.colony.requestsystem.requester.IRequester) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Delivery

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

the class AbstractWarehouseRequestResolver method getFollowupRequestForCompletion.

@Nullable
@Override
public List<IRequest<?>> getFollowupRequestForCompletion(@NotNull final IRequestManager manager, @NotNull final IRequest<? extends IDeliverable> completedRequest) {
    if (manager.getColony().getWorld().isClientSide) {
        return null;
    }
    final Colony colony = (Colony) manager.getColony();
    final List<TileEntityWareHouse> wareHouses = getWareHousesInColony(colony, completedRequest.getRequester().getLocation().getInDimensionLocation());
    List<IRequest<?>> deliveries = Lists.newArrayList();
    int remainingCount = completedRequest.getRequest().getCount();
    final Map<ItemStorage, Integer> storages = new HashMap<>();
    final int keep = completedRequest.getRequest() instanceof INonExhaustiveDeliverable ? ((INonExhaustiveDeliverable) completedRequest.getRequest()).getLeftOver() : 0;
    tileentities: for (final TileEntityWareHouse wareHouse : wareHouses) {
        final List<Tuple<ItemStack, BlockPos>> targetStacks = wareHouse.getMatchingItemStacksInWarehouse(itemStack -> completedRequest.getRequest().matches(itemStack));
        for (final Tuple<ItemStack, BlockPos> tuple : targetStacks) {
            if (ItemStackUtils.isEmpty(tuple.getA())) {
                continue;
            }
            int leftOver = tuple.getA().getCount();
            if (keep > 0) {
                int kept = storages.getOrDefault(new ItemStorage(tuple.getA()), 0);
                if (kept < keep) {
                    if (leftOver + kept <= keep) {
                        storages.put(new ItemStorage(tuple.getA()), storages.getOrDefault(new ItemStorage(tuple.getA()), 0) + tuple.getA().getCount());
                        continue;
                    }
                    int toKeep = (leftOver + kept) - keep;
                    leftOver -= toKeep;
                    storages.put(new ItemStorage(tuple.getA()), storages.getOrDefault(new ItemStorage(tuple.getA()), 0) + toKeep);
                }
            }
            int count = Math.min(remainingCount, leftOver);
            final ItemStack matchingStack = tuple.getA().copy();
            matchingStack.setCount(count);
            completedRequest.addDelivery(matchingStack);
            final ILocation itemStackLocation = manager.getFactoryController().getNewInstance(TypeConstants.ILOCATION, tuple.getB(), wareHouse.getLevel().dimension());
            final Delivery delivery = new Delivery(itemStackLocation, completedRequest.getRequester().getLocation(), matchingStack, getDefaultDeliveryPriority(true));
            final IToken<?> requestToken = manager.createRequest(manager.getFactoryController().getNewInstance(TypeToken.of(this.getClass()), completedRequest.getRequester().getLocation(), completedRequest.getId()), delivery);
            deliveries.add(manager.getRequestForToken(requestToken));
            remainingCount -= count;
            if (remainingCount <= 0) {
                break tileentities;
            }
        }
    }
    return deliveries.isEmpty() ? null : deliveries;
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) java.util(java.util) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) AbstractDeliverymanRequestable.getDefaultDeliveryPriority(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.AbstractDeliverymanRequestable.getDefaultDeliveryPriority) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) IFormattableTextComponent(net.minecraft.util.text.IFormattableTextComponent) TypeToken(com.google.common.reflect.TypeToken) BuildingWareHouse(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingWareHouse) IRequestManager(com.minecolonies.api.colony.requestsystem.manager.IRequestManager) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ItemStack(net.minecraft.item.ItemStack) CONST_WAREHOUSE_RESOLVER_PRIORITY(com.minecolonies.api.util.constant.RSConstants.CONST_WAREHOUSE_RESOLVER_PRIORITY) Lists(com.google.common.collect.Lists) Tuple(com.minecolonies.api.util.Tuple) BuildingBasedRequester(com.minecolonies.coremod.colony.requestsystem.requesters.BuildingBasedRequester) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) Log(com.minecolonies.api.util.Log) TileEntityWareHouse(com.minecolonies.coremod.tileentities.TileEntityWareHouse) TranslationConstants(com.minecolonies.api.util.constant.TranslationConstants) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) IRequester(com.minecolonies.api.colony.requestsystem.requester.IRequester) Colony(com.minecolonies.coremod.colony.Colony) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) RequestState(com.minecolonies.api.colony.requestsystem.request.RequestState) BlockPos(net.minecraft.util.math.BlockPos) Nullable(org.jetbrains.annotations.Nullable) TypeConstants(com.minecolonies.api.util.constant.TypeConstants) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) INonExhaustiveDeliverable(com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) TileEntityWareHouse(com.minecolonies.coremod.tileentities.TileEntityWareHouse) ItemStorage(com.minecolonies.api.crafting.ItemStorage) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) Colony(com.minecolonies.coremod.colony.Colony) INonExhaustiveDeliverable(com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable) BlockPos(net.minecraft.util.math.BlockPos) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) ItemStack(net.minecraft.item.ItemStack) Tuple(com.minecolonies.api.util.Tuple) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Delivery

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

the class JobDeliveryman method hasSameDestinationDelivery.

/**
 * Check if the dman has the same destination request.
 *
 * @param request the incoming request.
 * @return 0 if so, and 1 if not.
 */
public int hasSameDestinationDelivery(@NotNull final IRequest<? extends Delivery> request) {
    for (final IToken<?> requestToken : getTaskQueue()) {
        final IRequest<?> compareRequest = getColony().getRequestManager().getRequestForToken(requestToken);
        if (compareRequest != null && compareRequest.getRequest() instanceof Delivery) {
            final Delivery current = (Delivery) compareRequest.getRequest();
            final Delivery newDev = request.getRequest();
            if (haveTasksSameSourceAndDest(current, newDev)) {
                return 0;
            }
        }
    }
    return 1;
}
Also used : Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery)

Example 4 with Delivery

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

the class WindowResourceList method pullResourcesFromHut.

/**
 * Retrieve resources from the building to display in GUI.
 */
private void pullResourcesFromHut() {
    final IBuildingView newView = builder.getColony().getBuilding(builder.getID());
    if (newView instanceof BuildingBuilder.View) {
        final BuildingResourcesModuleView moduleView = newView.getModuleView(BuildingResourcesModuleView.class);
        final PlayerInventory inventory = this.mc.player.inventory;
        final boolean isCreative = this.mc.player.isCreative();
        final List<Delivery> deliveries = new ArrayList<>();
        for (Map.Entry<Integer, Collection<IToken<?>>> entry : builder.getOpenRequestsByCitizen().entrySet()) {
            addDeliveryRequestsToList(deliveries, ImmutableList.copyOf(entry.getValue()));
        }
        resources.clear();
        resources.addAll(moduleView.getResources().values());
        double supplied = 0;
        double total = 0;
        for (final BuildingBuilderResource resource : resources) {
            final int amountToSet;
            if (isCreative) {
                amountToSet = resource.getAmount();
            } else {
                amountToSet = InventoryUtils.getItemCountInItemHandler(new InvWrapper(inventory), stack -> !ItemStackUtils.isEmpty(stack) && ItemStackUtils.compareItemStacksIgnoreStackSize(stack, resource.getItemStack()));
            }
            resource.setPlayerAmount(amountToSet);
            resource.setAmountInDelivery(0);
            for (final Delivery delivery : deliveries) {
                if (ItemStackUtils.compareItemStacksIgnoreStackSize(resource.getItemStack(), delivery.getStack(), false, false)) {
                    resource.setAmountInDelivery(resource.getAmountInDelivery() + delivery.getStack().getCount());
                }
            }
            supplied += Math.min(resource.getAvailable(), resource.getAmount());
            total += resource.getAmount();
        }
        if (total > 0) {
            findPaneOfTypeByID(LABEL_PROGRESS, Text.class).setText(new TranslationTextComponent("com.minecolonies.coremod.gui.progress.res", (int) ((supplied / total) * 100) + "%", moduleView.getProgress() + "%"));
        }
        resources.sort(new BuildingBuilderResource.ResourceComparator(NOT_NEEDED, HAVE_ENOUGH, IN_DELIVERY, NEED_MORE, DONT_HAVE));
    }
}
Also used : MarkBuildingDirtyMessage(com.minecolonies.coremod.network.messages.server.colony.building.MarkBuildingDirtyMessage) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) WindowBuilderResModule(com.minecolonies.coremod.client.gui.modules.WindowBuilderResModule) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) ClientPlayerEntity(net.minecraft.client.entity.player.ClientPlayerEntity) ImmutableCollection(com.google.common.collect.ImmutableCollection) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) BuildingBuilder(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingBuilder) ScrollingList(com.ldtteam.blockout.views.ScrollingList) ItemIcon(com.ldtteam.blockout.controls.ItemIcon) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) ImmutableList(com.google.common.collect.ImmutableList) Minecraft(net.minecraft.client.Minecraft) Map(java.util.Map) RessourceAvailability(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource.RessourceAvailability) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) Network(com.minecolonies.coremod.Network) Constants(com.minecolonies.api.util.constant.Constants) PlayerInventory(net.minecraft.entity.player.PlayerInventory) IColonyView(com.minecolonies.api.colony.IColonyView) BuildingResourcesModuleView(com.minecolonies.coremod.colony.buildings.moduleviews.BuildingResourcesModuleView) Collection(java.util.Collection) IColonyManager(com.minecolonies.api.colony.IColonyManager) IBuildingView(com.minecolonies.api.colony.buildings.views.IBuildingView) BlockPos(net.minecraft.util.math.BlockPos) Pane(com.ldtteam.blockout.Pane) Text(com.ldtteam.blockout.controls.Text) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Image(com.ldtteam.blockout.controls.Image) WindowConstants(com.minecolonies.api.util.constant.WindowConstants) InventoryUtils(com.minecolonies.api.util.InventoryUtils) NotNull(org.jetbrains.annotations.NotNull) ArrayList(java.util.ArrayList) Text(com.ldtteam.blockout.controls.Text) PlayerInventory(net.minecraft.entity.player.PlayerInventory) IColonyView(com.minecolonies.api.colony.IColonyView) BuildingResourcesModuleView(com.minecolonies.coremod.colony.buildings.moduleviews.BuildingResourcesModuleView) IBuildingView(com.minecolonies.api.colony.buildings.views.IBuildingView) BuildingResourcesModuleView(com.minecolonies.coremod.colony.buildings.moduleviews.BuildingResourcesModuleView) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) IBuildingView(com.minecolonies.api.colony.buildings.views.IBuildingView) ImmutableCollection(com.google.common.collect.ImmutableCollection) Collection(java.util.Collection) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) Map(java.util.Map)

Example 5 with Delivery

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

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 ArrayList (java.util.ArrayList)8 ItemStack (net.minecraft.item.ItemStack)8 Nullable (org.jetbrains.annotations.Nullable)7 ILocation (com.minecolonies.api.colony.requestsystem.location.ILocation)6 ItemStackUtils (com.minecolonies.api.util.ItemStackUtils)6 BlockPos (net.minecraft.util.math.BlockPos)6 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 List (java.util.List)4