use of logisticspipes.interfaces.routing.IAdditionalTargetInformation in project LogisticsPipes by RS485.
the class ModuleCrafter method tick.
@Override
public void tick() {
enabledUpdateEntity();
if (_lostItems.isEmpty()) {
return;
}
// if(true) return;
DelayedGeneric<Pair<ItemIdentifierStack, IAdditionalTargetInformation>> lostItem = _lostItems.poll();
int rerequested = 0;
while (lostItem != null && rerequested < 100) {
Pair<ItemIdentifierStack, IAdditionalTargetInformation> pair = lostItem.get();
if (_service.getItemOrderManager().hasOrders(ResourceType.CRAFTING)) {
SinkReply reply = LogisticsManager.canSink(getRouter(), null, true, pair.getValue1().getItem(), null, true, true);
if (reply == null || reply.maxNumberOfItems < 1) {
_lostItems.add(new DelayedGeneric<>(pair, 9000 + (int) (Math.random() * 2000)));
lostItem = _lostItems.poll();
continue;
}
}
int received = RequestTree.requestPartial(pair.getValue1(), (CoreRoutedPipe) _service, pair.getValue2());
rerequested++;
if (received < pair.getValue1().getStackSize()) {
pair.getValue1().setStackSize(pair.getValue1().getStackSize() - received);
_lostItems.add(new DelayedGeneric<>(pair, 4500 + (int) (Math.random() * 1000)));
}
lostItem = _lostItems.poll();
}
}
use of logisticspipes.interfaces.routing.IAdditionalTargetInformation in project LogisticsPipes by RS485.
the class ModuleActiveSupplier method createPatternRequest.
private void createPatternRequest(IInventoryUtil invUtil) {
_service.getDebug().log("Supplier: Start calculating pattern request");
setRequestFailed(false);
for (int i = 0; i < 9; i++) {
ItemIdentifierStack needed = dummyInventory.getIDStackInSlot(i);
if (needed == null) {
continue;
}
if (invUtil.getSizeInventory() <= slotArray[i]) {
continue;
}
ItemStack stack = invUtil.getStackInSlot(slotArray[i]);
ItemIdentifierStack have = null;
if (stack != null) {
have = ItemIdentifierStack.getFromStack(stack);
}
int haveCount = 0;
if (have != null) {
if (!have.getItem().equals(needed.getItem())) {
_service.getDebug().log("Supplier: Slot for " + i + ", " + needed + " already taken by " + have);
setRequestFailed(true);
continue;
}
haveCount = have.getStackSize();
}
if ((_patternMode == PatternMode.Bulk50 && haveCount > needed.getStackSize() / 2) || (_patternMode == PatternMode.Bulk100 && haveCount >= needed.getStackSize())) {
continue;
}
Integer requestedCount = _requestedItems.get(needed.getItem());
if (requestedCount != null) {
haveCount += requestedCount;
}
int neededCount = needed.getStackSize() - haveCount;
if (neededCount < 1) {
continue;
}
ItemIdentifierStack toRequest = new ItemIdentifierStack(needed.getItem(), neededCount);
_service.getDebug().log("Supplier: Missing for slot " + i + ": " + toRequest);
if (!_service.useEnergy(10)) {
break;
}
boolean success = false;
IAdditionalTargetInformation targetInformation = new PatternSupplierTargetInformation(slotArray[i], needed.getStackSize());
if (_patternMode != PatternMode.Full) {
_service.getDebug().log("Supplier: Requesting partial: " + toRequest);
neededCount = RequestTree.requestPartial(toRequest, this, targetInformation);
_service.getDebug().log("Supplier: Requested: " + toRequest.getItem().makeStack(neededCount));
if (neededCount > 0) {
success = true;
}
} else {
_service.getDebug().log("Supplier: Requesting: " + toRequest);
success = RequestTree.request(toRequest, this, null, targetInformation);
if (success) {
_service.getDebug().log("Supplier: Request success");
} else {
_service.getDebug().log("Supplier: Request failed");
}
}
if (success) {
Integer currentRequest = _requestedItems.get(toRequest.getItem());
if (currentRequest == null) {
_requestedItems.put(toRequest.getItem(), neededCount);
} else {
_requestedItems.put(toRequest.getItem(), currentRequest + neededCount);
}
} else {
setRequestFailed(true);
}
}
}
use of logisticspipes.interfaces.routing.IAdditionalTargetInformation in project LogisticsPipes by RS485.
the class ModuleActiveSupplier method createSupplyRequest.
private void createSupplyRequest(IInventoryUtil invUtil) {
_service.getDebug().log("Supplier: Start calculating supply request");
//How many do I want?
HashMap<ItemIdentifier, Integer> needed = new HashMap<>(dummyInventory.getItemsAndCount());
_service.getDebug().log("Supplier: Needed: " + needed);
//How many do I have?
Map<ItemIdentifier, Integer> have = invUtil.getItemsAndCount();
_service.getDebug().log("Supplier: Have: " + have);
//How many do I have?
HashMap<ItemIdentifier, Integer> haveUndamaged = new HashMap<>();
for (Entry<ItemIdentifier, Integer> item : have.entrySet()) {
Integer n = haveUndamaged.get(item.getKey().getUndamaged());
if (n == null) {
haveUndamaged.put(item.getKey().getUndamaged(), item.getValue());
} else {
haveUndamaged.put(item.getKey().getUndamaged(), item.getValue() + n);
}
}
//Reduce what I have and what have been requested already
for (Entry<ItemIdentifier, Integer> item : needed.entrySet()) {
Integer haveCount = haveUndamaged.get(item.getKey().getUndamaged());
if (haveCount == null) {
haveCount = 0;
}
int spaceAvailable = invUtil.roomForItem(item.getKey());
if (_requestMode == SupplyMode.Infinite) {
Integer requestedCount = _requestedItems.get(item.getKey());
if (requestedCount != null) {
spaceAvailable -= requestedCount;
}
item.setValue(Math.min(item.getKey().getMaxStackSize(), spaceAvailable));
continue;
}
if (spaceAvailable == 0 || (_requestMode == SupplyMode.Bulk50 && haveCount > item.getValue() / 2) || (_requestMode == SupplyMode.Bulk100 && haveCount >= item.getValue())) {
item.setValue(0);
continue;
}
if (haveCount > 0) {
item.setValue(item.getValue() - haveCount);
// so that 1 damaged item can't satisfy a request for 2 other damage values.
haveUndamaged.put(item.getKey().getUndamaged(), haveCount - item.getValue());
}
Integer requestedCount = _requestedItems.get(item.getKey());
if (requestedCount != null) {
item.setValue(item.getValue() - requestedCount);
}
}
_service.getDebug().log("Supplier: Missing: " + needed);
setRequestFailed(false);
//Make request
for (Entry<ItemIdentifier, Integer> need : needed.entrySet()) {
Integer amountRequested = need.getValue();
if (amountRequested == null || amountRequested < 1) {
continue;
}
int neededCount = amountRequested;
if (!_service.useEnergy(10)) {
break;
}
boolean success = false;
IAdditionalTargetInformation targetInformation = new SupplierTargetInformation();
if (_requestMode != SupplyMode.Full) {
_service.getDebug().log("Supplier: Requesting partial: " + need.getKey().makeStack(neededCount));
neededCount = RequestTree.requestPartial(need.getKey().makeStack(neededCount), this, targetInformation);
_service.getDebug().log("Supplier: Requested: " + need.getKey().makeStack(neededCount));
if (neededCount > 0) {
success = true;
}
} else {
_service.getDebug().log("Supplier: Requesting: " + need.getKey().makeStack(neededCount));
success = RequestTree.request(need.getKey().makeStack(neededCount), this, null, targetInformation);
if (success) {
_service.getDebug().log("Supplier: Request success");
} else {
_service.getDebug().log("Supplier: Request failed");
}
}
if (success) {
Integer currentRequest = _requestedItems.get(need.getKey());
if (currentRequest == null) {
_requestedItems.put(need.getKey(), neededCount);
_service.getDebug().log("Supplier: Inserting Requested Items: " + neededCount);
} else {
_requestedItems.put(need.getKey(), currentRequest + neededCount);
_service.getDebug().log("Supplier: Raising Requested Items from: " + currentRequest + " to: " + currentRequest + neededCount);
}
} else {
setRequestFailed(true);
}
}
}
use of logisticspipes.interfaces.routing.IAdditionalTargetInformation in project LogisticsPipes by RS485.
the class PipeItemsProviderLogistics method sendStack.
private int sendStack(ItemIdentifierStack stack, int maxCount, int destination, IAdditionalTargetInformation info) {
ItemIdentifier item = stack.getItem();
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(container);
//@formatter:off
Iterator<Pair<IInventoryUtil, ForgeDirection>> iterator = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).filter(adjacent -> !SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> new Pair<>(getAdaptedInventoryUtil(adjacent), adjacent.direction)).iterator();
while (iterator.hasNext()) {
Pair<IInventoryUtil, ForgeDirection> next = iterator.next();
int available = next.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.getRouterUnsafe(destination, false);
if (dRtr == null) {
_orderManager.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) {
_orderManager.deferSend();
return 0;
}
defersend = true;
}
}
if (!canUseEnergy(wanted * neededEnergy())) {
return -1;
}
ItemStack removed = next.getValue1().getMultipleItems(item, wanted);
if (removed == null || removed.stackSize == 0) {
continue;
}
int sent = removed.stackSize;
useEnergy(sent * neededEnergy());
IRoutedItem routedItem = SimpleServiceLocator.routedItemHelper.createNewTravelItem(removed);
routedItem.setDestination(destination);
routedItem.setTransportMode(TransportMode.Active);
routedItem.setAdditionalTargetInformation(info);
super.queueRoutedItem(routedItem, next.getValue2());
_orderManager.sendSuccessfull(sent, defersend, routedItem);
return sent;
}
_orderManager.sendFailed();
return 0;
}
Aggregations