use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModulePolymorphicItemSink method sinksItem.
@Override
public SinkReply sinksItem(@Nonnull ItemStack stack, ItemIdentifier item, int bestPriority, int bestCustomPriority, boolean allowDefault, boolean includeInTransit, boolean forcePassive) {
if (bestPriority > _sinkReply.fixedPriority.ordinal() || (bestPriority == _sinkReply.fixedPriority.ordinal() && bestCustomPriority >= _sinkReply.customPriority)) {
return null;
}
final IPipeServiceProvider service = _service;
if (service == null)
return null;
final ISlotUpgradeManager upgradeManager = service.getUpgradeManager(slot, positionInt);
IInventoryUtil targetInventory = PipeServiceProviderUtilKt.availableSneakyInventories(service, upgradeManager).stream().findFirst().orElse(null);
if (targetInventory == null) {
return null;
}
if (!targetInventory.containsUndamagedItem(item.getUndamaged())) {
return null;
}
if (service.canUseEnergy(3)) {
return _sinkReply;
}
return null;
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleProvider method getAllItems.
@Override
public void getAllItems(Map<ItemIdentifier, Integer> items, List<IFilter> filters) {
final IPipeServiceProvider service = _service;
if (service == null)
return;
items.putAll(inventoriesWithMode().map(IInventoryUtil::getItemsAndCount).flatMap(inventory -> inventory.entrySet().stream()).filter(item -> {
if (items.containsKey(item.getKey()))
// already provided by any previous module. No comparison of the amount
return false;
// skip provider-filtered items
if (filterBlocksItem(item.getKey()))
return false;
final boolean blockedInFilters = filters.stream().anyMatch(filter -> filter.isBlocked() == filter.isFilteredItem(item.getKey().getUndamaged()) || filter.blockProvider());
// skip filters-parameter-filtered items
return !blockedInFilters;
}).map(item -> new Pair<>(item.getKey(), item.getValue() - service.getItemOrderManager().totalItemsCountInOrders(item.getKey()))).filter(itemIdentAndRemaining -> itemIdentAndRemaining.getValue2() > // reduce what has been reserved
0).collect(// sum up the provided amount by the inventories
Pair.toMap(Integer::sum)));
}
use of logisticspipes.interfaces.IPipeServiceProvider 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;
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModulePassiveSupplier method sinksItem.
@Override
public SinkReply sinksItem(@Nonnull ItemStack stack, ItemIdentifier item, int bestPriority, int bestCustomPriority, boolean allowDefault, boolean includeInTransit, boolean forcePassive) {
if (bestPriority > _sinkReply.fixedPriority.ordinal() || (bestPriority == _sinkReply.fixedPriority.ordinal() && bestCustomPriority >= _sinkReply.customPriority)) {
return null;
}
final IPipeServiceProvider service = _service;
if (service == null)
return null;
final ISlotUpgradeManager upgradeManager = service.getUpgradeManager(slot, positionInt);
IInventoryUtil targetUtil = PipeServiceProviderUtilKt.availableSneakyInventories(service, upgradeManager).stream().findFirst().orElse(null);
if (targetUtil == null) {
return null;
}
if (!filterInventory.containsItem(item)) {
return null;
}
int targetCount = filterInventory.itemCount(item);
int haveCount = targetUtil.itemCount(item);
if (targetCount <= haveCount) {
return null;
}
if (service.canUseEnergy(2)) {
return new SinkReply(_sinkReply, targetCount - haveCount);
}
return null;
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleCrafter method extractFromLogisticsCraftingTable.
@Nonnull
private ItemStack extractFromLogisticsCraftingTable(NeighborTileEntity<LogisticsCraftingTableTileEntity> adjacentCraftingTable, IResource wanteditem, int count) {
final IPipeServiceProvider service = _service;
if (service == null)
return ItemStack.EMPTY;
ItemStack extracted = extractFromInventory(Objects.requireNonNull(LPNeighborTileEntityKt.getInventoryUtil(adjacentCraftingTable)), wanteditem, count);
if (!extracted.isEmpty()) {
return extracted;
}
ItemStack retstack = ItemStack.EMPTY;
while (count > 0) {
ItemStack stack = adjacentCraftingTable.getTileEntity().getOutput(wanteditem, service);
if (stack.isEmpty()) {
break;
}
if (retstack.isEmpty()) {
if (!wanteditem.matches(ItemIdentifier.get(stack), wanteditem instanceof ItemResource ? IResource.MatchSettings.WITHOUT_NBT : IResource.MatchSettings.NORMAL)) {
break;
}
} else {
if (!retstack.isItemEqual(stack)) {
break;
}
if (!ItemStack.areItemStackTagsEqual(retstack, stack)) {
break;
}
}
if (!service.useEnergy(neededEnergy() * stack.getCount())) {
break;
}
if (retstack.isEmpty()) {
retstack = stack;
} else {
retstack.grow(stack.getCount());
}
count -= stack.getCount();
if (Objects.requireNonNull(getUpgradeManager()).isFuzzyUpgrade()) {
break;
}
}
return retstack;
}
Aggregations