use of logisticspipes.interfaces.ITankUtil 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);
}
}
use of logisticspipes.interfaces.ITankUtil in project LogisticsPipes by RS485.
the class PipeItemsFluidSupplier method endReached.
public void endReached(LPTravelingItemServer data, TileEntity tile) {
getCacheHolder().trigger(CacheTypes.Inventory);
transport.markChunkModified(tile);
notifyOfItemArival(data.getInfo());
EnumFacing orientation = data.output.getOpposite();
if (getOriginalUpgradeManager().hasSneakyUpgrade()) {
orientation = getOriginalUpgradeManager().getSneakyOrientation();
}
ITankUtil util = SimpleServiceLocator.tankUtilFactory.getTankUtilForTE(tile, orientation);
if (util == null) {
return;
}
if (SimpleServiceLocator.pipeInformationManager.isItemPipe(tile)) {
return;
}
if (data.getItemIdentifierStack() == null) {
return;
}
FluidIdentifierStack liquidId = FluidIdentifierStack.getFromStack(data.getItemIdentifierStack());
if (liquidId == null) {
return;
}
while (data.getItemIdentifierStack().getStackSize() > 0 && util.fill(liquidId, false) == liquidId.getAmount() && this.useEnergy(5)) {
util.fill(liquidId, true);
data.getItemIdentifierStack().lowerStackSize(1);
Item item = data.getItemIdentifierStack().getItem().item;
if (item.hasContainerItem(data.getItemIdentifierStack().makeNormalStack())) {
Item containerItem = Objects.requireNonNull(item.getContainerItem());
transport.sendItem(new ItemStack(containerItem, 1));
}
}
}
use of logisticspipes.interfaces.ITankUtil in project LogisticsPipes by RS485.
the class PipeItemsFluidSupplier method throttledUpdateEntity.
@Override
public void throttledUpdateEntity() {
if (!isEnabled()) {
return;
}
if (MainProxy.isClient(getWorld())) {
return;
}
super.throttledUpdateEntity();
for (NeighborTileEntity<TileEntity> neighbor : getAdjacent().fluidTanks()) {
final ITankUtil tankUtil = LPNeighborTileEntityKt.getTankUtil(neighbor);
if (tankUtil == null || !tankUtil.containsTanks()) {
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 = FluidUtil.getFluidContained(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<>();
tankUtil.tanks().map(tank -> FluidIdentifierStack.getFromStack(tank.getContents())).filter(Objects::nonNull).forEach(fluid -> {
if (wantFluids.containsKey(fluid.getFluid())) {
haveFluids.merge(fluid.getFluid(), fluid.getAmount(), Integer::sum);
}
});
// 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 = FluidUtil.getFluidContained(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) Objects.requireNonNull(container).pipe).setRequestFailed(false);
for (ItemIdentifier need : wantContainers.keySet()) {
FluidStack requestedFluidId = FluidUtil.getFluidContained(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);
}
}
}
}
Aggregations