use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleCrafter method cacheAreAllOrderesToBuffer.
public void cacheAreAllOrderesToBuffer() {
final IPipeServiceProvider service = _service;
if (service == null)
return;
boolean result = true;
for (LogisticsItemOrder order : service.getItemOrderManager()) {
if (order.getDestination() instanceof IItemSpaceControl) {
SinkReply reply = LogisticsManager.canSink(order.getResource().stack.makeNormalStack(), order.getDestination().getRouter(), null, true, order.getResource().getItem(), null, true, false);
if (reply != null && reply.bufferMode == BufferMode.NONE && reply.maxNumberOfItems >= 1) {
result = false;
break;
}
} else {
// No Space control
result = false;
break;
}
}
cachedAreAllOrderesToBuffer = result;
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleCrafter method tick.
@Override
public void tick() {
final IPipeServiceProvider service = _service;
if (service == null)
return;
enabledUpdateEntity();
if (updateSatelliteFromIDs != null && service.isNthTick(100)) {
if (updateSatelliteFromIDs.advancedSatelliteIdArray != null) {
boolean canBeRemoved = true;
for (int i = 0; i < updateSatelliteFromIDs.advancedSatelliteIdArray.length; i++) {
if (updateSatelliteFromIDs.advancedSatelliteIdArray[i] != -1) {
UUID uuid = getUUIDForSatelliteName(Integer.toString(updateSatelliteFromIDs.advancedSatelliteIdArray[i]));
if (uuid != null) {
updateSatelliteFromIDs.advancedSatelliteIdArray[i] = -1;
advancedSatelliteUUIDList.set(i, uuid);
} else {
canBeRemoved = false;
}
}
}
if (canBeRemoved) {
updateSatelliteFromIDs.advancedSatelliteIdArray = null;
}
}
if (updateSatelliteFromIDs.liquidSatelliteIdArray != null) {
boolean canBeRemoved = true;
for (int i = 0; i < updateSatelliteFromIDs.liquidSatelliteIdArray.length; i++) {
if (updateSatelliteFromIDs.liquidSatelliteIdArray[i] != -1) {
UUID uuid = getUUIDForFluidSatelliteName(Integer.toString(updateSatelliteFromIDs.liquidSatelliteIdArray[i]));
if (uuid != null) {
updateSatelliteFromIDs.liquidSatelliteIdArray[i] = -1;
liquidSatelliteUUIDList.set(i, uuid);
} else {
canBeRemoved = false;
}
}
}
if (canBeRemoved) {
updateSatelliteFromIDs.liquidSatelliteIdArray = null;
}
}
if (updateSatelliteFromIDs.liquidSatelliteId != -1) {
UUID uuid = getUUIDForFluidSatelliteName(Integer.toString(updateSatelliteFromIDs.liquidSatelliteId));
if (uuid != null) {
updateSatelliteFromIDs.liquidSatelliteId = -1;
liquidSatelliteUUID.setValue(uuid);
}
}
if (updateSatelliteFromIDs.satelliteId != -1) {
UUID uuid = getUUIDForFluidSatelliteName(Integer.toString(updateSatelliteFromIDs.satelliteId));
if (uuid != null) {
updateSatelliteFromIDs.satelliteId = -1;
satelliteUUID.setValue(uuid);
}
}
if (updateSatelliteFromIDs.advancedSatelliteIdArray == null && updateSatelliteFromIDs.liquidSatelliteId == -1 && updateSatelliteFromIDs.liquidSatelliteIdArray == null && updateSatelliteFromIDs.satelliteId == -1) {
updateSatelliteFromIDs = null;
}
}
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(pair.getValue1().makeNormalStack(), getRouter(), null, true, pair.getValue1().getItem(), null, true, true, false);
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.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleCrafter method enabledUpdateEntity.
public void enabledUpdateEntity() {
final IPipeServiceProvider service = _service;
if (service == null)
return;
if (service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
if (service.isNthTick(6)) {
cacheAreAllOrderesToBuffer();
}
if (service.getItemOrderManager().isFirstOrderWatched()) {
TileEntity tile = lastAccessedCrafter.get();
if (tile != null) {
service.getItemOrderManager().setMachineProgress(SimpleServiceLocator.machineProgressProvider.getProgressForTile(tile));
} else {
service.getItemOrderManager().setMachineProgress((byte) 0);
}
}
} else {
cachedAreAllOrderesToBuffer = false;
}
if (!service.isNthTick(6)) {
return;
}
final List<NeighborTileEntity<TileEntity>> adjacentInventories = service.getAvailableAdjacent().inventories();
if (!service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
final ISlotUpgradeManager upgradeManager = Objects.requireNonNull(getUpgradeManager());
if (upgradeManager.getCrafterCleanup() > 0) {
adjacentInventories.stream().map(neighbor -> extractFiltered(neighbor, cleanupInventory, cleanupModeIsExclude.getValue(), upgradeManager.getCrafterCleanup() * 3)).filter(stack -> !stack.isEmpty()).findFirst().ifPresent(extracted -> {
service.queueRoutedItem(SimpleServiceLocator.routedItemHelper.createNewTravelItem(extracted), EnumFacing.UP);
service.getCacheHolder().trigger(CacheTypes.Inventory);
});
}
return;
}
if (adjacentInventories.size() < 1) {
if (service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
service.getItemOrderManager().sendFailed();
}
return;
}
List<ItemIdentifierStack> wanteditem = getCraftedItems();
if (wanteditem == null || wanteditem.isEmpty()) {
return;
}
service.spawnParticle(Particles.VioletParticle, 2);
int itemsleft = itemsToExtract();
int stacksleft = stacksToExtract();
while (itemsleft > 0 && stacksleft > 0 && (service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA))) {
LogisticsItemOrder nextOrder = service.getItemOrderManager().peekAtTopRequest(ResourceType.CRAFTING, // fetch but not remove.
ResourceType.EXTRA);
int maxtosend = Math.min(itemsleft, nextOrder.getResource().stack.getStackSize());
maxtosend = Math.min(nextOrder.getResource().getItem().getMaxStackSize(), maxtosend);
// retrieve the new crafted items
ItemStack extracted = ItemStack.EMPTY;
// there has to be at least one adjacentCrafter at this point; adjacent wont stay null
NeighborTileEntity<TileEntity> adjacent = null;
for (NeighborTileEntity<TileEntity> adjacentCrafter : adjacentInventories) {
adjacent = adjacentCrafter;
extracted = extract(adjacent, nextOrder.getResource(), maxtosend);
if (!extracted.isEmpty()) {
break;
}
}
if (extracted.isEmpty()) {
service.getItemOrderManager().deferSend();
break;
}
service.getCacheHolder().trigger(CacheTypes.Inventory);
Objects.requireNonNull(adjacent);
lastAccessedCrafter = new WeakReference<>(adjacent.getTileEntity());
// send the new crafted items to the destination
ItemIdentifier extractedID = ItemIdentifier.get(extracted);
while (!extracted.isEmpty()) {
if (isExtractedMismatch(nextOrder, extractedID)) {
LogisticsItemOrder startOrder = nextOrder;
if (service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
do {
service.getItemOrderManager().deferSend();
nextOrder = service.getItemOrderManager().peekAtTopRequest(ResourceType.CRAFTING, ResourceType.EXTRA);
} while (isExtractedMismatch(nextOrder, extractedID) && startOrder != nextOrder);
}
if (startOrder == nextOrder) {
int numtosend = Math.min(extracted.getCount(), extractedID.getMaxStackSize());
if (numtosend == 0) {
break;
}
stacksleft -= 1;
itemsleft -= numtosend;
ItemStack stackToSend = extracted.splitStack(numtosend);
// Route the unhandled item
service.sendStack(stackToSend, -1, ItemSendMode.Normal, null, adjacent.getDirection());
continue;
}
}
int numtosend = Math.min(extracted.getCount(), extractedID.getMaxStackSize());
numtosend = Math.min(numtosend, nextOrder.getResource().stack.getStackSize());
if (numtosend == 0) {
break;
}
stacksleft -= 1;
itemsleft -= numtosend;
ItemStack stackToSend = extracted.splitStack(numtosend);
if (nextOrder.getDestination() != null) {
SinkReply reply = LogisticsManager.canSink(stackToSend, nextOrder.getDestination().getRouter(), null, true, ItemIdentifier.get(stackToSend), null, true, false);
boolean defersend = (reply == null || reply.bufferMode != BufferMode.NONE || reply.maxNumberOfItems < 1);
IRoutedItem item = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stackToSend);
item.setDestination(nextOrder.getDestination().getRouter().getSimpleID());
item.setTransportMode(TransportMode.Active);
item.setAdditionalTargetInformation(nextOrder.getInformation());
service.queueRoutedItem(item, adjacent.getDirection());
service.getItemOrderManager().sendSuccessfull(stackToSend.getCount(), defersend, item);
} else {
service.sendStack(stackToSend, -1, ItemSendMode.Normal, nextOrder.getInformation(), adjacent.getDirection());
service.getItemOrderManager().sendSuccessfull(stackToSend.getCount(), false, null);
}
if (service.getItemOrderManager().hasOrders(ResourceType.CRAFTING, ResourceType.EXTRA)) {
nextOrder = service.getItemOrderManager().peekAtTopRequest(ResourceType.CRAFTING, // fetch but not remove.
ResourceType.EXTRA);
}
}
}
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleCrafter method extractFromInventory.
@Nonnull
private ItemStack extractFromInventory(@Nonnull IInventoryUtil invUtil, IResource wanteditem, int count) {
final IPipeServiceProvider service = _service;
if (service == null)
return ItemStack.EMPTY;
ItemIdentifier itemToExtract = null;
if (wanteditem instanceof ItemResource) {
itemToExtract = ((ItemResource) wanteditem).getItem();
} else if (wanteditem instanceof DictResource) {
int max = Integer.MIN_VALUE;
ItemIdentifier toExtract = null;
for (Map.Entry<ItemIdentifier, Integer> content : invUtil.getItemsAndCount().entrySet()) {
if (wanteditem.matches(content.getKey(), IResource.MatchSettings.NORMAL)) {
if (content.getValue() > max) {
max = content.getValue();
toExtract = content.getKey();
}
}
}
if (toExtract == null) {
return ItemStack.EMPTY;
}
itemToExtract = toExtract;
}
if (itemToExtract == null)
return ItemStack.EMPTY;
int available = invUtil.itemCount(itemToExtract);
if (available == 0 || !service.canUseEnergy(neededEnergy() * Math.min(count, available))) {
return ItemStack.EMPTY;
}
ItemStack extracted = invUtil.getMultipleItems(itemToExtract, Math.min(count, available));
service.useEnergy(neededEnergy() * extracted.getCount());
return extracted;
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleProvider method fullFill.
@Override
public LogisticsOrder fullFill(LogisticsPromise promise, IRequestItems destination, IAdditionalTargetInformation info) {
final IPipeServiceProvider service = _service;
if (service == null)
return null;
service.spawnParticle(Particles.WhiteParticle, 2);
return service.getItemOrderManager().addOrder(new ItemIdentifierStack(promise.item, promise.numberOfItems), destination, ResourceType.PROVIDER, info);
}
Aggregations