use of network.rs485.logisticspipes.connection.NeighborTileEntity in project LogisticsPipes by RS485.
the class FluidRoutedPipe method enabledUpdateEntity.
@Override
public void enabledUpdateEntity() {
super.enabledUpdateEntity();
if (canInsertFromSideToTanks()) {
int validDirections = 0;
final List<Pair<NeighborTileEntity<TileEntity>, ITankUtil>> list = PipeFluidUtil.INSTANCE.getAdjacentTanks(this, true);
for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> pair : list) {
if (pair.getValue2() instanceof LogisticsTileGenericPipe) {
if (((LogisticsTileGenericPipe) pair.getValue2()).pipe instanceof CoreRoutedPipe) {
continue;
}
}
FluidTank internalTank = ((PipeFluidTransportLogistics) transport).sideTanks[pair.getValue1().getDirection().ordinal()];
validDirections++;
if (internalTank.getFluid() == null) {
continue;
}
int filled = pair.getValue2().fill(FluidIdentifierStack.getFromStack(internalTank.getFluid()), true);
if (filled == 0) {
continue;
}
FluidStack drain = internalTank.drain(filled, true);
if (drain == null || filled != drain.amount) {
if (LogisticsPipes.isDEBUG()) {
throw new UnsupportedOperationException("Fluid Multiplication");
}
}
}
if (validDirections == 0) {
return;
}
FluidTank tank = ((PipeFluidTransportLogistics) transport).internalTank;
FluidStack stack = tank.getFluid();
if (stack == null) {
return;
}
for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> pair : list) {
if (pair.getValue1().isLogisticsPipe()) {
if (((LogisticsTileGenericPipe) pair.getValue1().getTileEntity()).pipe instanceof CoreRoutedPipe) {
continue;
}
}
FluidTank tankSide = ((PipeFluidTransportLogistics) transport).sideTanks[pair.getValue1().getDirection().ordinal()];
stack = tank.getFluid();
if (stack == null) {
continue;
}
stack = stack.copy();
int filled = tankSide.fill(stack, true);
if (filled == 0) {
continue;
}
FluidStack drain = tank.drain(filled, true);
if (drain == null || filled != drain.amount) {
if (LogisticsPipes.isDEBUG()) {
throw new UnsupportedOperationException("Fluid Multiplication");
}
}
}
}
}
use of network.rs485.logisticspipes.connection.NeighborTileEntity 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 network.rs485.logisticspipes.connection.NeighborTileEntity 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);
}
}
}
}
use of network.rs485.logisticspipes.connection.NeighborTileEntity in project LogisticsPipes by RS485.
the class PipeLogisticsChassis method nextOrientation.
@Override
public void nextOrientation() {
final SingleAdjacent pointedAdjacent = this.pointedAdjacent;
Pair<NeighborTileEntity<TileEntity>, ConnectionType> newNeighbor;
if (pointedAdjacent == null) {
newNeighbor = nextPointedOrientation(null);
} else {
newNeighbor = nextPointedOrientation(pointedAdjacent.getDir());
}
final ChassisOrientationPacket packet = PacketHandler.getPacket(ChassisOrientationPacket.class);
if (newNeighbor == null) {
this.pointedAdjacent = null;
packet.setDir(null);
} else {
this.pointedAdjacent = new SingleAdjacent(this, newNeighbor.getValue1().getDirection(), newNeighbor.getValue2());
packet.setDir(newNeighbor.getValue1().getDirection());
}
MainProxy.sendPacketToAllWatchingChunk(_module, packet.setTilePos(container));
refreshRender(true);
}
Aggregations