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