use of logisticspipes.interfaces.IInventoryUtil in project LogisticsPipes by RS485.
the class ModuleCrafter method extractFromIInventoryFiltered.
private ItemStack extractFromIInventoryFiltered(IInventory inv, ItemIdentifierInventory filter, boolean isExcluded, int filterInvLimit, ForgeDirection dir) {
IInventoryUtil invUtil = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(inv, dir);
ItemIdentifier wanteditem = null;
for (ItemIdentifier item : invUtil.getItemsAndCount().keySet()) {
if (isExcluded) {
boolean found = false;
for (int i = 0; i < filter.getSizeInventory() && i < filterInvLimit; i++) {
ItemIdentifierStack identStack = filter.getIDStackInSlot(i);
if (identStack == null) {
continue;
}
if (identStack.getItem().equalsWithoutNBT(item)) {
found = true;
break;
}
}
if (!found) {
wanteditem = item;
}
} else {
boolean found = false;
for (int i = 0; i < filter.getSizeInventory() && i < filterInvLimit; i++) {
ItemIdentifierStack identStack = filter.getIDStackInSlot(i);
if (identStack == null) {
continue;
}
if (identStack.getItem().equalsWithoutNBT(item)) {
found = true;
break;
}
}
if (found) {
wanteditem = item;
}
}
}
if (wanteditem == null) {
return null;
}
int available = invUtil.itemCount(wanteditem);
if (available == 0) {
return null;
}
if (!_service.useEnergy(neededEnergy() * Math.min(64, available))) {
return null;
}
return invUtil.getMultipleItems(wanteditem, Math.min(64, available));
}
use of logisticspipes.interfaces.IInventoryUtil in project LogisticsPipes by RS485.
the class ModuleCrafter method spaceFor.
protected int spaceFor(ItemIdentifier item, boolean includeInTransit) {
Pair<String, ItemIdentifier> key = new Pair<>("spaceFor", item);
Object cache = _service.getCacheHolder().getCacheFor(CacheTypes.Inventory, key);
if (cache != null) {
int count = (Integer) cache;
if (includeInTransit) {
count -= _service.countOnRoute(item);
}
return count;
}
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(getWorld(), _service.getX(), _service.getY(), _service.getZ());
//@formatter:off
int count = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).map(adjacent -> new Pair<>((IInventory) adjacent.tileEntity, adjacent.direction)).map(invDirPair -> {
if (invDirPair.getValue1() instanceof ISidedInventory) {
invDirPair.setValue1(new SidedInventoryMinecraftAdapter((ISidedInventory) invDirPair.getValue1(), invDirPair.getValue2().getOpposite(), false));
}
if (getUpgradeManager().hasSneakyUpgrade()) {
invDirPair.setValue2(getUpgradeManager().getSneakyOrientation());
}
IInventoryUtil inv = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(invDirPair.getValue1(), invDirPair.getValue2());
return inv.roomForItem(item, 9999);
}).reduce(Integer::sum).orElse(0);
_service.getCacheHolder().setCache(CacheTypes.Inventory, key, count);
if (includeInTransit) {
count -= _service.countOnRoute(item);
}
return count;
}
use of logisticspipes.interfaces.IInventoryUtil in project LogisticsPipes by RS485.
the class ChassiModule method sinksItem.
@Override
public SinkReply sinksItem(ItemIdentifier item, int bestPriority, int bestCustomPriority, boolean allowDefault, boolean includeInTransit) {
SinkReply bestresult = null;
for (LogisticsModule module : modules) {
if (module != null) {
SinkReply result = module.sinksItem(item, bestPriority, bestCustomPriority, allowDefault, includeInTransit);
if (result != null && result.maxNumberOfItems >= 0) {
bestresult = result;
bestPriority = result.fixedPriority.ordinal();
bestCustomPriority = result.customPriority;
}
}
}
if (bestresult == null) {
return null;
}
//Always deny items when we can't put the item anywhere
IInventoryUtil invUtil = parentChassis.getSneakyInventory(false, ModulePositionType.SLOT, ((ChassiTargetInformation) bestresult.addInfo).getModuleSlot());
if (invUtil == null) {
return null;
}
int roomForItem = invUtil.roomForItem(item);
if (roomForItem < 1) {
return null;
}
if (includeInTransit) {
int onRoute = parentChassis.countOnRoute(item);
roomForItem = invUtil.roomForItem(item, onRoute + item.getMaxStackSize());
roomForItem -= onRoute;
if (roomForItem < 1) {
return null;
}
}
if (bestresult.maxNumberOfItems == 0) {
return new SinkReply(bestresult, roomForItem);
}
return new SinkReply(bestresult, Math.min(bestresult.maxNumberOfItems, roomForItem));
}
use of logisticspipes.interfaces.IInventoryUtil 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.IInventoryUtil in project LogisticsPipes by RS485.
the class ModuleSatellite method spaceFor.
private int spaceFor(ItemIdentifier item, boolean includeInTransit) {
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(pipe.container);
//@formatter:off
int count = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).map(adjacent -> {
IInventory inv = (IInventory) adjacent.tileEntity;
if (inv instanceof net.minecraft.inventory.ISidedInventory) {
inv = new SidedInventoryMinecraftAdapter((net.minecraft.inventory.ISidedInventory) inv, adjacent.direction.getOpposite(), false);
}
IInventoryUtil util = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(inv, adjacent.direction);
return util.roomForItem(item, 9999);
}).reduce(Integer::sum).orElse(0);
if (includeInTransit) {
count -= pipe.countOnRoute(item);
}
return count;
}
Aggregations