_service.spawnParticle(Particles.VioletParticle, 2);
int itemsleft = itemsToExtract();
int stacksleft = stacksToExtract();
while (itemsleft > 0 && stacksleft > 0 && (_service.getOrderManager().hasOrders(RequestType.CRAFTING) || !_extras.isEmpty())) {
LogisticsOrder nextOrder;
boolean processingOrder=false;
if(_service.getOrderManager().hasOrders(RequestType.CRAFTING)){
nextOrder = _service.getOrderManager().peekAtTopRequest(RequestType.CRAFTING); // fetch but not remove.
processingOrder=true;
} else {
nextOrder = _extras.getFirst(); // fetch but not remove.
}
int maxtosend = Math.min(itemsleft, nextOrder.getItem().getStackSize());
maxtosend = Math.min(nextOrder.getItem().getItem().getMaxStackSize(), maxtosend);
// retrieve the new crafted items
ItemStack extracted = null;
AdjacentTile tile = null;
for (Iterator<AdjacentTile> it = crafters.iterator(); it.hasNext();) {
tile = it.next();
extracted = extract(tile, nextOrder.getItem().getItem(), maxtosend);
if (extracted != null && extracted.stackSize > 0) {
break;
}
}
if(extracted == null || extracted.stackSize == 0) {
if(processingOrder)
_service.getOrderManager().deferSend();
break;
}
lastAccessedCrafter = new WeakReference<TileEntity>(tile.tile);
// send the new crafted items to the destination
ItemIdentifier extractedID = ItemIdentifier.get(extracted);
while (extracted.stackSize > 0) {
if(!nextOrder.getItem().getItem().equals(extractedID)) {
LogisticsOrder startOrder = nextOrder;
if(_service.getOrderManager().hasOrders(RequestType.CRAFTING)) {
do {
_service.getOrderManager().deferSend();
nextOrder = _service.getOrderManager().peekAtTopRequest(RequestType.CRAFTING);
} while(!nextOrder.getItem().getItem().equals(extractedID) && startOrder != nextOrder);