use of logisticspipes.request.RequestTreeNode in project LogisticsPipes by RS485.
the class ModuleProvider method canProvide.
@Override
public void canProvide(RequestTreeNode tree, RequestTree root, List<IFilter> filters) {
List<ItemIdentifier> possible = new ArrayList<>();
if (tree.getRequestType() instanceof ItemResource) {
possible.add(((ItemResource) tree.getRequestType()).getItem());
} else if (tree.getRequestType() instanceof DictResource) {
possible.addAll(inventoriesWithMode().map(IInventoryUtil::getItemsAndCount).flatMap(inventory -> inventory.keySet().stream()).filter(item -> tree.getRequestType().matches(item, IResource.MatchSettings.NORMAL)).collect(Collectors.toList()));
}
for (ItemIdentifier item : possible) {
int canProvide = getAvailableItemCount(item);
canProvide -= root.getAllPromissesFor((IProvideItems) _service, item);
canProvide = Math.min(canProvide, tree.getMissingAmount());
if (canProvide < 1) {
return;
}
LogisticsPromise promise = new LogisticsPromise(item, canProvide, (IProvideItems) _service, ResourceType.PROVIDER);
tree.addPromise(promise);
}
}
use of logisticspipes.request.RequestTreeNode in project LogisticsPipes by RS485.
the class PipeFluidProvider method canProvide.
@Override
public void canProvide(RequestTreeNode tree, RequestTree root, List<IFilter> filter) {
if (tree.isDone()) {
return;
}
if (!(tree.getRequestType() instanceof FluidResource)) {
return;
}
FluidIdentifier fluid = ((FluidResource) tree.getRequestType()).getFluid();
AtomicInteger containedAmount = new AtomicInteger(0);
for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> pair : PipeFluidUtil.INSTANCE.getAdjacentTanks(this, false)) {
boolean fallback = true;
if (pair.getValue2() instanceof ISpecialTankUtil) {
final ISpecialTankUtil util = (ISpecialTankUtil) pair.getValue2();
fallback = false;
ISpecialTankAccessHandler handler = util.getSpecialHandler();
TileEntity tile = util.getTileEntity();
Map<FluidIdentifier, Long> map = handler.getAvailableLiquid(tile);
if (map.containsKey(fluid)) {
long addition = (containedAmount.get()) + map.get(fluid);
containedAmount.set(addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition);
}
}
if (fallback) {
if (pair.getValue2().containsTanks()) {
pair.getValue2().tanks().map(tank -> FluidIdentifierStack.getFromStack(tank.getContents())).forEach(liquid -> {
if (liquid != null && liquid.getFluid() != null) {
if (fluid.equals(liquid.getFluid())) {
if (pair.getValue2().canDrain(liquid.getFluid())) {
if (pair.getValue2().drain(liquid.getFluid().makeFluidIdentifierStack(1), false) != null) {
long addition = ((long) containedAmount.get()) + liquid.getAmount();
containedAmount.set(addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition);
}
}
}
}
});
}
}
}
FluidLogisticsPromise promise = new FluidLogisticsPromise();
promise.liquid = fluid;
promise.amount = Math.min(tree.getMissingAmount(), containedAmount.get() - root.getAllPromissesFor(this, fluid.getItemIdentifier()));
promise.sender = this;
promise.type = ResourceType.PROVIDER;
if (promise.amount > 0) {
tree.addPromise(promise);
}
}
Aggregations