// returns -1 on permanently failed, don't try another stack this tick
// returns 0 on "unable to do this delivery"
private int sendStack(ItemIdentifierStack stack, int maxCount, int destination, IAdditionalTargetInformation info) {
ItemIdentifier item = stack.getItem();
IInventoryUtil inv = _service.getPointedInventory(_extractionMode,true);
if (inv == null) {
_service.getOrderManager().sendFailed();
return 0;
}
int available = inv.itemCount(item);
if (available == 0) {
_service.getOrderManager().sendFailed();
return 0;
}
int wanted = Math.min(available, stack.getStackSize());
wanted = Math.min(wanted, maxCount);
wanted = Math.min(wanted, item.getMaxStackSize());
IRouter dRtr = SimpleServiceLocator.routerManager.getRouterUnsafe(destination,false);
if(dRtr == null) {
_service.getOrderManager().sendFailed();
return 0;
}
SinkReply reply = LogisticsManager.canSink(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.getOrderManager().deferSend();
return 0;
}
defersend = true;
}
}
if(!_service.canUseEnergy(wanted * neededEnergy())) return -1;
ItemStack removed = inv.getMultipleItems(item, wanted);
if(removed == null || removed.stackSize == 0) {
_service.getOrderManager().sendFailed();
return 0;
}
int sent = removed.stackSize;