use of logisticspipes.pipes.basic.CoreRoutedPipe.ItemSendMode in project LogisticsPipes by RS485.
the class ModuleProvider method sendStack.
// returns -1 on permanently failed, don't try another stack this tick
// returns 0 on "unable to do this delivery"
public int sendStack(ItemIdentifierStack stack, int maxCount, int destination, IAdditionalTargetInformation info) {
final IPipeServiceProvider service = _service;
if (service == null)
return -1;
ItemIdentifier item = stack.getItem();
Iterator<Pair<IInventoryUtil, EnumFacing>> iterator = service.getAvailableAdjacent().inventories().stream().flatMap(neighbor -> {
final IInventoryUtil invUtil = getInventoryUtilWithMode(neighbor);
if (invUtil == null)
return Stream.empty();
return Stream.of(new Pair<>(invUtil, neighbor.getDirection()));
}).iterator();
while (iterator.hasNext()) {
final Pair<IInventoryUtil, EnumFacing> current = iterator.next();
int available = current.getValue1().itemCount(item);
if (available == 0) {
continue;
}
int wanted = Math.min(available, stack.getStackSize());
wanted = Math.min(wanted, maxCount);
wanted = Math.min(wanted, item.getMaxStackSize());
IRouter dRtr = SimpleServiceLocator.routerManager.getServerRouter(destination);
if (dRtr == null) {
service.getItemOrderManager().sendFailed();
return 0;
}
SinkReply reply = LogisticsManager.canSink(stack.makeNormalStack(), dRtr, null, true, stack.getItem(), null, true, false);
boolean defersend = false;
if (reply != null) {
// some pipes are not aware of the space in the adjacent inventory, so they return null
if (reply.maxNumberOfItems < wanted) {
wanted = reply.maxNumberOfItems;
if (wanted <= 0) {
service.getItemOrderManager().deferSend();
return 0;
}
defersend = true;
}
}
if (!service.canUseEnergy(wanted * neededEnergy())) {
return -1;
}
ItemStack removed = current.getValue1().getMultipleItems(item, wanted);
if (removed.isEmpty()) {
continue;
}
int sent = removed.getCount();
service.useEnergy(sent * neededEnergy());
final IRoutedItem routedItem = service.sendStack(removed, destination, itemSendMode(), info, current.getValue2());
service.getItemOrderManager().sendSuccessfull(sent, defersend, routedItem);
return sent;
}
service.getItemOrderManager().sendFailed();
return 0;
}
Aggregations