use of logisticspipes.interfaces.routing.IFilter 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) {
IInventoryUtil inv = _service.getPointedInventory(_extractionMode, true);
if (inv != null) {
Map<ItemIdentifier, Integer> currentInv = inv.getItemsAndCount();
possible.addAll(currentInv.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.interfaces.routing.IFilter in project LogisticsPipes by RS485.
the class LogisticsManager method getBestReply.
private Triplet<Integer, SinkReply, List<IFilter>> getBestReply(ItemIdentifier stack, IRouter sourceRouter, List<ExitRoute> validDestinations, boolean excludeSource, List<Integer> jamList, Triplet<Integer, SinkReply, List<IFilter>> result, boolean allowDefault) {
if (result == null) {
result = new Triplet<>(null, null, null);
}
outer: for (ExitRoute candidateRouter : validDestinations) {
if (excludeSource) {
if (candidateRouter.destination.getId().equals(sourceRouter.getId())) {
continue;
}
}
if (jamList.contains(candidateRouter.destination.getSimpleID())) {
continue;
}
if (!candidateRouter.containsFlag(PipeRoutingConnectionType.canRouteTo)) {
continue;
}
for (IFilter filter : candidateRouter.filters) {
if (filter.blockRouting() || (filter.isBlocked() == filter.isFilteredItem(stack))) {
continue outer;
}
}
SinkReply reply = LogisticsManager.canSink(candidateRouter.destination, sourceRouter, excludeSource, stack, result.getValue2(), false, allowDefault);
if (reply == null) {
continue;
}
if (result.getValue1() == null) {
result.setValue1(candidateRouter.destination.getSimpleID());
result.setValue2(reply);
List<IFilter> list = new LinkedList<>();
result.setValue3(list);
continue;
}
if (reply.fixedPriority.ordinal() > result.getValue2().fixedPriority.ordinal()) {
result.setValue1(candidateRouter.destination.getSimpleID());
result.setValue2(reply);
List<IFilter> list = new LinkedList<>();
result.setValue3(list);
continue;
}
if (reply.fixedPriority == result.getValue2().fixedPriority && reply.customPriority > result.getValue2().customPriority) {
result.setValue1(candidateRouter.destination.getSimpleID());
result.setValue2(reply);
List<IFilter> list = new LinkedList<>();
result.setValue3(list);
continue;
}
}
if (result.getValue1() != null) {
CoreRoutedPipe pipe = SimpleServiceLocator.routerManager.getRouterUnsafe(result.getValue1(), false).getPipe();
pipe.useEnergy(result.getValue2().energyUse);
pipe.spawnParticle(Particles.BlueParticle, 10);
}
return result;
}
use of logisticspipes.interfaces.routing.IFilter in project LogisticsPipes by RS485.
the class LogisticsManager method getCraftableItems.
/**
* @param validDestinations
* a List of ExitRoute of valid destinations.
* @return LinkedList with ItemIdentifier
*/
@Override
public LinkedList<ItemIdentifier> getCraftableItems(List<ExitRoute> validDestinations) {
LinkedList<ItemIdentifier> craftableItems = new LinkedList<>();
BitSet used = new BitSet(ServerRouter.getBiggestSimpleID());
outer: for (ExitRoute r : validDestinations) {
if (r == null) {
continue;
}
if (!r.containsFlag(PipeRoutingConnectionType.canRequestFrom)) {
continue;
}
if (used.get(r.destination.getSimpleID())) {
continue;
}
if (!(r.destination.getPipe() instanceof ICraftItems)) {
continue;
}
for (IFilter filter : r.filters) {
if (filter.blockCrafting()) {
continue outer;
}
}
ICraftItems crafter = (ICraftItems) r.destination.getPipe();
List<ItemIdentifierStack> craftedItems = crafter.getCraftedItems();
if (craftedItems != null) {
outer2: for (ItemIdentifierStack craftedItem : craftedItems) {
if (craftedItem != null && !craftableItems.contains(craftedItem.getItem())) {
for (IFilter filter : r.filters) {
if (filter.isBlocked() == filter.isFilteredItem(craftedItem.getItem())) {
continue outer2;
}
}
craftableItems.add(craftedItem.getItem());
}
}
}
used.set(r.destination.getSimpleID(), true);
}
return craftableItems;
}
use of logisticspipes.interfaces.routing.IFilter in project LogisticsPipes by RS485.
the class ModuleCCBasedQuickSort method handle.
private boolean handle(IInventoryUtil invUtil, int slot, List<CCSinkResponder> list) {
if (list.isEmpty()) {
return false;
}
ItemIdentifier ident = list.get(0).getStack().getItem();
ItemStack stack = invUtil.getStackInSlot(slot);
if (stack == null || !ItemIdentifier.get(stack).equals(ident)) {
return false;
}
final IRouter source = _service.getRouter();
List<Triplet<Integer, Double, CCSinkResponder>> posibilities = new ArrayList<>();
for (CCSinkResponder sink : list) {
if (!sink.isDone()) {
continue;
}
if (sink.getCanSink() < 1) {
continue;
}
IRouter r = SimpleServiceLocator.routerManager.getRouter(sink.getRouterId());
if (r == null) {
continue;
}
List<ExitRoute> ways = source.getDistanceTo(r);
double minDistance = Double.MAX_VALUE;
outer: for (ExitRoute route : ways) {
for (IFilter filter : route.filters) {
if (filter.blockRouting() || filter.isFilteredItem(ident) == filter.isBlocked()) {
continue outer;
}
}
minDistance = Math.min(route.distanceToDestination, minDistance);
}
if (minDistance != Integer.MAX_VALUE) {
posibilities.add(new Triplet<>(sink.getPriority(), minDistance, sink));
}
}
if (posibilities.isEmpty()) {
return false;
}
Collections.sort(posibilities, (o1, o2) -> {
int c = o2.getValue1() - o1.getValue1();
if (c != 0) {
return c;
}
double e = o1.getValue2() - o2.getValue2();
return e < 0 ? -1 : 1;
});
boolean sended = false;
for (Triplet<Integer, Double, CCSinkResponder> triple : posibilities) {
CCSinkResponder sink = triple.getValue3();
if (sink.getCanSink() < 0) {
continue;
}
stack = invUtil.getStackInSlot(slot);
if (stack == null || stack.stackSize <= 0) {
continue;
}
int amount = Math.min(stack.stackSize, sink.getCanSink());
ItemStack extracted = invUtil.decrStackSize(slot, amount);
_service.sendStack(extracted, sink.getRouterId(), ItemSendMode.Fast, null);
sended = true;
}
return sended;
}
use of logisticspipes.interfaces.routing.IFilter in project LogisticsPipes by RS485.
the class PipeItemsProviderLogistics method getAllItems.
@Override
public void getAllItems(Map<ItemIdentifier, Integer> items, List<IFilter> filters) {
if (!isEnabled()) {
return;
}
HashMap<ItemIdentifier, Integer> addedItems = new HashMap<>();
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(container);
//@formatter:off
Iterator<Map<ItemIdentifier, Integer>> iterator = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).filter(adjacent -> !SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> getAdaptedInventoryUtil(adjacent).getItemsAndCount()).iterator();
outer: while (iterator.hasNext()) {
Iterator<Entry<ItemIdentifier, Integer>> entryIterator = iterator.next().entrySet().stream().filter(currentItem -> !items.containsKey(currentItem.getKey())).filter(currentItem -> !hasFilter() || (!isExcludeFilter() || !itemIsFiltered(currentItem.getKey())) && (isExcludeFilter() || itemIsFiltered(currentItem.getKey()))).iterator();
while (entryIterator.hasNext()) {
Entry<ItemIdentifier, Integer> next = entryIterator.next();
for (IFilter filter : filters) {
if (filter.isBlocked() == filter.isFilteredItem(next.getKey().getUndamaged()) || filter.blockProvider()) {
continue outer;
}
}
Integer addedAmount = addedItems.get(next.getKey());
if (addedAmount == null) {
addedItems.put(next.getKey(), next.getValue());
} else {
addedItems.put(next.getKey(), addedAmount + next.getValue());
}
}
}
// reduce what has been reserved, add.
for (Entry<ItemIdentifier, Integer> item : addedItems.entrySet()) {
int remaining = item.getValue() - _orderManager.totalItemsCountInOrders(item.getKey());
if (remaining < 1) {
continue;
}
items.put(item.getKey(), remaining);
}
}
Aggregations