use of logisticspipes.utils.FluidIdentifier 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();
int containedAmount = 0;
for (Pair<TileEntity, ForgeDirection> pair : getAdjacentTanks(false)) {
boolean fallback = true;
if (SimpleServiceLocator.specialTankHandler.hasHandlerFor(pair.getValue1())) {
ISpecialTankHandler handler = SimpleServiceLocator.specialTankHandler.getTankHandlerFor(pair.getValue1());
if (handler instanceof ISpecialTankAccessHandler) {
fallback = false;
Map<FluidIdentifier, Long> map = ((ISpecialTankAccessHandler) handler).getAvailableLiquid(pair.getValue1());
if (map.containsKey(fluid)) {
long addition = (containedAmount) + map.get(fluid);
containedAmount = addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition;
}
}
}
if (fallback) {
FluidTankInfo[] tanks = ((IFluidHandler) pair.getValue1()).getTankInfo(pair.getValue2().getOpposite());
if (tanks != null) {
for (FluidTankInfo tank : tanks) {
if (tank == null) {
continue;
}
FluidStack liquid;
if ((liquid = tank.fluid) != null && liquid.getFluidID() != 0) {
if (fluid.equals(FluidIdentifier.get(liquid))) {
if (((IFluidHandler) pair.getValue1()).canDrain(pair.getValue2().getOpposite(), liquid.getFluid())) {
if (((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), 1, false) != null) {
long addition = ((long) containedAmount) + liquid.amount;
containedAmount = addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition;
}
}
}
}
}
}
}
}
FluidLogisticsPromise promise = new FluidLogisticsPromise();
promise.liquid = fluid;
promise.amount = Math.min(tree.getMissingAmount(), containedAmount - root.getAllPromissesFor(this, fluid.getItemIdentifier()));
promise.sender = this;
promise.type = ResourceType.PROVIDER;
if (promise.amount > 0) {
tree.addPromise(promise);
}
}
use of logisticspipes.utils.FluidIdentifier in project LogisticsPipes by RS485.
the class PipeFluidProvider method getSpecificInterests.
@Override
public //work in progress, currently not active code.
Set<ItemIdentifier> getSpecificInterests() {
Set<ItemIdentifier> l1 = new TreeSet<>();
for (Pair<TileEntity, ForgeDirection> pair : getAdjacentTanks(false)) {
boolean fallback = true;
if (SimpleServiceLocator.specialTankHandler.hasHandlerFor(pair.getValue1())) {
ISpecialTankHandler handler = SimpleServiceLocator.specialTankHandler.getTankHandlerFor(pair.getValue1());
if (handler instanceof ISpecialTankAccessHandler) {
fallback = false;
Map<FluidIdentifier, Long> map = ((ISpecialTankAccessHandler) handler).getAvailableLiquid(pair.getValue1());
l1.addAll(map.keySet().stream().map(FluidIdentifier::getItemIdentifier).collect(Collectors.toList()));
}
}
if (fallback) {
FluidTankInfo[] tanks = ((IFluidHandler) pair.getValue1()).getTankInfo(pair.getValue2().getOpposite());
if (tanks != null) {
for (FluidTankInfo tank : tanks) {
if (tank == null) {
continue;
}
FluidStack liquid;
if ((liquid = tank.fluid) != null && liquid.getFluidID() != 0) {
if (((IFluidHandler) pair.getValue1()).canDrain(pair.getValue2().getOpposite(), liquid.getFluid())) {
if (((IFluidHandler) pair.getValue1()).drain(pair.getValue2().getOpposite(), 1, false) != null) {
FluidIdentifier ident = FluidIdentifier.get(liquid);
l1.add(ident.getItemIdentifier());
}
}
}
}
}
}
}
return l1;
}
use of logisticspipes.utils.FluidIdentifier in project LogisticsPipes by RS485.
the class PipeFluidSupplierMk2 method throttledUpdateEntity.
@Override
public void throttledUpdateEntity() {
if (!isEnabled()) {
return;
}
if (MainProxy.isClient(container.getWorld())) {
return;
}
super.throttledUpdateEntity();
if (dummyInventory.getStackInSlot(0) == null) {
return;
}
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(container);
//@formatter:off
worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IFluidHandler).filter(adjacent -> SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> new Pair<>((IFluidHandler) adjacent.tileEntity, adjacent.direction)).forEach(fluidHandlerDirectionPair -> {
FluidTankInfo[] tankInfo = fluidHandlerDirectionPair.getValue1().getTankInfo(ForgeDirection.UNKNOWN);
if (tankInfo == null || tankInfo.length == 0) {
return;
}
Map<FluidIdentifier, Integer> wantFluids = new HashMap<>();
FluidIdentifier fIdent = FluidIdentifier.get(dummyInventory.getIDStackInSlot(0).getItem());
wantFluids.put(fIdent, amount);
HashMap<FluidIdentifier, Integer> haveFluids = new HashMap<>();
FluidTankInfo[] result = container.getTankInfo(ForgeDirection.UNKNOWN);
for (FluidTankInfo slot : result) {
if (slot == null || slot.fluid == null || slot.fluid.getFluidID() == 0 || !wantFluids.containsKey(FluidIdentifier.get(slot.fluid))) {
continue;
}
Integer liquidWant = haveFluids.get(FluidIdentifier.get(slot.fluid));
if (liquidWant == null) {
haveFluids.put(FluidIdentifier.get(slot.fluid), slot.fluid.amount);
} else {
haveFluids.put(FluidIdentifier.get(slot.fluid), liquidWant + slot.fluid.amount);
}
}
if (fluidHandlerDirectionPair.getValue2().ordinal() < ((PipeFluidTransportLogistics) transport).sideTanks.length) {
FluidTank centerTank = ((PipeFluidTransportLogistics) transport).sideTanks[fluidHandlerDirectionPair.getValue2().ordinal()];
if (centerTank != null && centerTank.getFluid() != null && wantFluids.containsKey(FluidIdentifier.get(centerTank.getFluid()))) {
Integer liquidWant = haveFluids.get(FluidIdentifier.get(centerTank.getFluid()));
if (liquidWant == null) {
haveFluids.put(FluidIdentifier.get(centerTank.getFluid()), centerTank.getFluid().amount);
} else {
haveFluids.put(FluidIdentifier.get(centerTank.getFluid()), liquidWant + centerTank.getFluid().amount);
}
}
}
FluidTank centerTank = ((PipeFluidTransportLogistics) transport).internalTank;
if (centerTank != null && centerTank.getFluid() != null && wantFluids.containsKey(FluidIdentifier.get(centerTank.getFluid()))) {
Integer liquidWant = haveFluids.get(FluidIdentifier.get(centerTank.getFluid()));
if (liquidWant == null) {
haveFluids.put(FluidIdentifier.get(centerTank.getFluid()), centerTank.getFluid().amount);
} else {
haveFluids.put(FluidIdentifier.get(centerTank.getFluid()), liquidWant + centerTank.getFluid().amount);
}
}
for (Entry<FluidIdentifier, Integer> liquidId : wantFluids.entrySet()) {
Integer haveCount = haveFluids.get(liquidId.getKey());
if (haveCount != null) {
liquidId.setValue(liquidId.getValue() - haveCount);
}
_requestedItems.entrySet().stream().filter(requestedItem -> requestedItem.getKey().equals(liquidId.getKey())).forEach(requestedItem -> liquidId.setValue(liquidId.getValue() - requestedItem.getValue()));
}
setRequestFailed(false);
for (FluidIdentifier need : wantFluids.keySet()) {
int countToRequest = wantFluids.get(need);
if (countToRequest < 1) {
continue;
}
if (_bucketMinimum.getAmount() != 0 && countToRequest < _bucketMinimum.getAmount()) {
continue;
}
if (!useEnergy(11)) {
break;
}
boolean success = false;
if (_requestPartials) {
countToRequest = RequestTree.requestFluidPartial(need, countToRequest, this, null);
if (countToRequest > 0) {
success = true;
}
} else {
success = RequestTree.requestFluid(need, countToRequest, this, null);
}
if (success) {
Integer currentRequest = _requestedItems.get(need);
if (currentRequest == null) {
_requestedItems.put(need, countToRequest);
} else {
_requestedItems.put(need, currentRequest + countToRequest);
}
} else {
setRequestFailed(true);
}
}
});
}
use of logisticspipes.utils.FluidIdentifier in project LogisticsPipes by RS485.
the class PipeItemsFluidSupplier method throttledUpdateEntity.
@Override
public void throttledUpdateEntity() {
if (!isEnabled()) {
return;
}
if (MainProxy.isClient(getWorld())) {
return;
}
super.throttledUpdateEntity();
//@formatter:off
Iterator<IFluidHandler> iterator = new WorldCoordinatesWrapper(container).getConnectedAdjacentTileEntities().filter(adjacent -> adjacent.tileEntity instanceof IFluidHandler).filter(adjacent -> !SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> (IFluidHandler) adjacent.tileEntity).iterator();
while (iterator.hasNext()) {
IFluidHandler next = iterator.next();
FluidTankInfo[] result = next.getTankInfo(ForgeDirection.UNKNOWN);
if (result == null || result.length == 0) {
continue;
}
//How much do I want?
Map<ItemIdentifier, Integer> wantContainers = dummyInventory.getItemsAndCount();
HashMap<FluidIdentifier, Integer> wantFluids = new HashMap<>();
for (Entry<ItemIdentifier, Integer> item : wantContainers.entrySet()) {
ItemStack wantItem = item.getKey().unsafeMakeNormalStack(1);
FluidStack liquidstack = FluidContainerRegistry.getFluidForFilledItem(wantItem);
if (liquidstack == null) {
continue;
}
wantFluids.put(FluidIdentifier.get(liquidstack), item.getValue() * liquidstack.amount);
}
//How much do I have?
HashMap<FluidIdentifier, Integer> haveFluids = new HashMap<>();
for (FluidTankInfo slot : result) {
if (slot == null || slot.fluid == null || slot.fluid.getFluidID() == 0 || !wantFluids.containsKey(FluidIdentifier.get(slot.fluid))) {
continue;
}
Integer liquidWant = haveFluids.get(FluidIdentifier.get(slot.fluid));
if (liquidWant == null) {
haveFluids.put(FluidIdentifier.get(slot.fluid), slot.fluid.amount);
} else {
haveFluids.put(FluidIdentifier.get(slot.fluid), liquidWant + slot.fluid.amount);
}
}
//Reduce what I have and what have been requested already
for (Entry<FluidIdentifier, Integer> liquidId : wantFluids.entrySet()) {
Integer haveCount = haveFluids.get(liquidId.getKey());
if (haveCount != null) {
liquidId.setValue(liquidId.getValue() - haveCount);
}
}
for (Entry<ItemIdentifier, Integer> requestedItem : _requestedItems.entrySet()) {
ItemStack wantItem = requestedItem.getKey().unsafeMakeNormalStack(1);
FluidStack requestedFluidId = FluidContainerRegistry.getFluidForFilledItem(wantItem);
if (requestedFluidId == null) {
continue;
}
FluidIdentifier requestedFluid = FluidIdentifier.get(requestedFluidId);
Integer want = wantFluids.get(requestedFluid);
if (want != null) {
wantFluids.put(requestedFluid, want - requestedItem.getValue() * requestedFluidId.amount);
}
}
((PipeItemsFluidSupplier) container.pipe).setRequestFailed(false);
for (ItemIdentifier need : wantContainers.keySet()) {
FluidStack requestedFluidId = FluidContainerRegistry.getFluidForFilledItem(need.unsafeMakeNormalStack(1));
if (requestedFluidId == null) {
continue;
}
if (!wantFluids.containsKey(FluidIdentifier.get(requestedFluidId))) {
continue;
}
int countToRequest = wantFluids.get(FluidIdentifier.get(requestedFluidId)) / requestedFluidId.amount;
if (countToRequest < 1) {
continue;
}
if (!useEnergy(11)) {
break;
}
boolean success = false;
if (_requestPartials) {
countToRequest = RequestTree.requestPartial(need.makeStack(countToRequest), (IRequestItems) container.pipe, null);
if (countToRequest > 0) {
success = true;
}
} else {
success = RequestTree.request(need.makeStack(countToRequest), (IRequestItems) container.pipe, null, null);
}
if (success) {
Integer currentRequest = _requestedItems.get(need);
if (currentRequest == null) {
_requestedItems.put(need, countToRequest);
} else {
_requestedItems.put(need, currentRequest + countToRequest);
}
} else {
((PipeItemsFluidSupplier) container.pipe).setRequestFailed(true);
}
}
}
}
use of logisticspipes.utils.FluidIdentifier in project LogisticsPipes by RS485.
the class AETankHandler method getAvailableLiquid.
@SuppressWarnings("unused")
@Override
public Map<FluidIdentifier, Long> getAvailableLiquid(TileEntity tile) {
Map<FluidIdentifier, Long> map = new HashMap<>();
if (tile instanceof ITileStorageMonitorable) {
ITileStorageMonitorable mon = (ITileStorageMonitorable) tile;
if (mon == null) {
return map;
}
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
MachineSource source = new MachineSource(new LPActionHost(((IGridHost) tile).getGridNode(dir)));
IStorageMonitorable monitor = mon.getMonitorable(dir, source);
if (monitor == null || monitor.getFluidInventory() == null) {
continue;
}
IMEMonitor<IAEFluidStack> fluids = monitor.getFluidInventory();
for (IAEFluidStack stack : fluids.getStorageList()) {
if (SimpleServiceLocator.extraCellsProxy.canSeeFluidInNetwork(stack.getFluid())) {
map.put(FluidIdentifier.get(stack.getFluid(), stack.getTagCompound() != null ? stack.getTagCompound().getNBTTagCompoundCopy() : null, null), stack.getStackSize());
}
}
return map;
}
}
return map;
}
Aggregations