use of network.rs485.logisticspipes.connection.NeighborTileEntity in project LogisticsPipes by RS485.
the class PipeFluidProvider method collectSpecificInterests.
@Override
public // work in progress, currently not active code.
void collectSpecificInterests(@Nonnull Collection<ItemIdentifier> itemidCollection) {
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();
handler.getAvailableLiquid(tile).keySet().stream().map(FluidIdentifier::getItemIdentifier).forEach(itemidCollection::add);
}
if (fallback) {
if (pair.getValue2().containsTanks()) {
pair.getValue2().tanks().map(tank -> FluidIdentifierStack.getFromStack(tank.getContents())).forEach(liquid -> {
if (liquid != null && liquid.getFluid() != null) {
if (pair.getValue2().canDrain(liquid.getFluid())) {
if (pair.getValue2().drain(1, false) != null) {
FluidIdentifier ident = liquid.getFluid();
itemidCollection.add(ident.getItemIdentifier());
}
}
}
});
}
}
}
}
use of network.rs485.logisticspipes.connection.NeighborTileEntity in project LogisticsPipes by RS485.
the class PipeFluidProvider method getAvailableFluids.
@Override
public Map<FluidIdentifier, Integer> getAvailableFluids() {
Map<FluidIdentifier, Integer> map = new HashMap<>();
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> tmp = handler.getAvailableLiquid(tile);
for (Entry<FluidIdentifier, Long> entry : tmp.entrySet()) {
if (map.containsKey(entry.getKey())) {
long addition = ((long) map.get(entry.getKey())) + entry.getValue();
map.put(entry.getKey(), addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition);
} else {
map.put(entry.getKey(), entry.getValue() > Integer.MAX_VALUE ? Integer.MAX_VALUE : entry.getValue().intValue());
}
}
}
if (fallback) {
if (pair.getValue2().containsTanks()) {
pair.getValue2().tanks().map(tank -> FluidIdentifierStack.getFromStack(tank.getContents())).forEach(liquid -> {
if (liquid != null && liquid.getFluid() != null) {
FluidIdentifier ident = liquid.getFluid();
if (pair.getValue2().canDrain(ident)) {
if (pair.getValue2().drain(ident.makeFluidIdentifierStack(1), false) != null) {
if (map.containsKey(ident)) {
long addition = ((long) map.get(ident)) + liquid.getAmount();
map.put(ident, addition > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) addition);
} else {
map.put(ident, liquid.getAmount());
}
}
}
}
});
}
}
}
Map<FluidIdentifier, Integer> result = new HashMap<>();
// Reduce what has been reserved, add.
for (Entry<FluidIdentifier, Integer> fluid : map.entrySet()) {
int remaining = fluid.getValue() - getFluidOrderManager().totalFluidsCountInOrders(fluid.getKey());
if (remaining < 1) {
continue;
}
result.put(fluid.getKey(), remaining);
}
return result;
}
use of network.rs485.logisticspipes.connection.NeighborTileEntity in project LogisticsPipes by RS485.
the class PipeFluidProvider method enabledUpdateEntity.
@Override
public void enabledUpdateEntity() {
super.enabledUpdateEntity();
if (!getFluidOrderManager().hasOrders(ResourceType.PROVIDER) || !isNthTick(6)) {
return;
}
LogisticsFluidOrder order = getFluidOrderManager().peekAtTopRequest(ResourceType.PROVIDER);
AtomicInteger amountToSend = new AtomicInteger();
AtomicInteger attemptedAmount = new AtomicInteger();
amountToSend.set(Math.min(order.getAmount(), 5000));
attemptedAmount.set(Math.min(order.getAmount(), 5000));
for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> pair : PipeFluidUtil.INSTANCE.getAdjacentTanks(this, false)) {
if (amountToSend.get() <= 0) {
break;
}
boolean fallback = true;
if (pair.getValue2() instanceof ISpecialTankUtil) {
ISpecialTankUtil util = (ISpecialTankUtil) pair.getValue2();
fallback = false;
ISpecialTankAccessHandler handler = util.getSpecialHandler();
FluidStack drained = handler.drainFrom(pair.getValue1().getTileEntity(), order.getFluid(), amountToSend.get(), false);
if (drained != null && drained.amount > 0 && order.getFluid().equals(FluidIdentifier.get(drained))) {
drained = handler.drainFrom(pair.getValue1().getTileEntity(), order.getFluid(), amountToSend.get(), true);
int amount = drained.amount;
amountToSend.addAndGet(-amount);
ItemIdentifierStack stack = SimpleServiceLocator.logisticsFluidManager.getFluidContainer(FluidIdentifierStack.getFromStack(drained));
IRoutedItem item = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stack);
item.setDestination(order.getRouter().getSimpleID());
item.setTransportMode(TransportMode.Active);
this.queueRoutedItem(item, pair.getValue1().getDirection());
getFluidOrderManager().sendSuccessfull(amount, false, item);
if (amountToSend.get() <= 0) {
break;
}
}
}
if (fallback) {
if (pair.getValue2().containsTanks()) {
pair.getValue2().tanks().map(tank -> FluidIdentifierStack.getFromStack(tank.getContents())).forEach(fluidStack -> {
if (amountToSend.get() <= 0) {
return;
}
if (fluidStack != null && fluidStack.getFluid() != null) {
if (order.getFluid().equals(fluidStack.getFluid())) {
int amount = Math.min(fluidStack.getAmount(), amountToSend.get());
FluidIdentifierStack drained = pair.getValue2().drain(amount, false);
if (drained != null && drained.getAmount() > 0 && order.getFluid().equals(drained.getFluid())) {
drained = pair.getValue2().drain(amount, true);
while (drained.getAmount() < amountToSend.get()) {
FluidIdentifierStack addition = pair.getValue2().drain(amountToSend.get() - drained.getAmount(), false);
if (addition != null && addition.getAmount() > 0 && order.getFluid().equals(addition.getFluid())) {
addition = pair.getValue2().drain(amountToSend.get() - drained.getAmount(), true);
drained.raiseAmount(addition.getAmount());
} else {
break;
}
}
amount = drained.getAmount();
amountToSend.addAndGet(-amount);
ItemIdentifierStack stack = SimpleServiceLocator.logisticsFluidManager.getFluidContainer(drained);
IRoutedItem item = SimpleServiceLocator.routedItemHelper.createNewTravelItem(stack);
item.setDestination(order.getRouter().getSimpleID());
item.setTransportMode(TransportMode.Active);
queueRoutedItem(item, pair.getValue1().getDirection());
getFluidOrderManager().sendSuccessfull(amount, false, item);
}
}
}
});
}
}
}
if (amountToSend.get() >= attemptedAmount.get()) {
getFluidOrderManager().sendFailed();
}
}
use of network.rs485.logisticspipes.connection.NeighborTileEntity in project LogisticsPipes by RS485.
the class LogisticsPowerProviderTileEntity method update.
@Override
public void update() {
super.update();
pauseRequesting = false;
if (!init) {
if (MainProxy.isClient(getWorld())) {
LogisticsHUDRenderer.instance().add(this);
}
init = true;
}
double globalRequest = orders.values().stream().reduce(Double::sum).orElse(0.0);
if (globalRequest > 0) {
final double fullfillRatio = Math.min(1, Math.min(internalStorage, getMaxProvidePerTick()) / globalRequest);
if (fullfillRatio > 0) {
final Function<NeighborTileEntity<LogisticsTileGenericPipe>, CoreRoutedPipe> getPipe = (NeighborTileEntity<LogisticsTileGenericPipe> neighbor) -> (CoreRoutedPipe) neighbor.getTileEntity().pipe;
orders.entrySet().stream().map(routerIdToOrderCount -> new Pair<>(SimpleServiceLocator.routerManager.getRouter(routerIdToOrderCount.getKey()), Math.min(internalStorage, routerIdToOrderCount.getValue() * fullfillRatio))).filter(destinationToPower -> destinationToPower.getValue1() != null && destinationToPower.getValue1().getPipe() != null).forEach(destinationToPower -> new WorldCoordinatesWrapper(this).allNeighborTileEntities().stream().flatMap(neighbor -> LPNeighborTileEntityKt.optionalIs(neighbor, LogisticsTileGenericPipe.class).map(Stream::of).orElseGet(Stream::empty)).filter(neighbor -> neighbor.getTileEntity().pipe instanceof CoreRoutedPipe && !getPipe.apply(neighbor).stillNeedReplace()).flatMap(neighbor -> getPipe.apply(neighbor).getRouter().getDistanceTo(destinationToPower.getValue1()).stream().map(exitRoute -> new Pair<>(neighbor, exitRoute))).filter(neighborToExit -> neighborToExit.getValue2().containsFlag(PipeRoutingConnectionType.canPowerSubSystemFrom) && neighborToExit.getValue2().filters.stream().noneMatch(IFilter::blockPower)).findFirst().ifPresent(neighborToSource -> {
CoreRoutedPipe sourcePipe = getPipe.apply(neighborToSource.getValue1());
if (sourcePipe.isInitialized()) {
sourcePipe.container.addLaser(neighborToSource.getValue1().getOurDirection(), 1, getLaserColor(), true, true);
}
sendPowerLaserPackets(sourcePipe.getRouter(), destinationToPower.getValue1(), neighborToSource.getValue2().exitOrientation, neighborToSource.getValue2().exitOrientation != neighborToSource.getValue1().getDirection());
internalStorage -= destinationToPower.getValue2();
// because calculations with floats
if (internalStorage <= 0)
internalStorage = 0;
handlePower(destinationToPower.getValue1().getPipe(), destinationToPower.getValue2());
}));
}
}
orders.clear();
if (MainProxy.isServer(world)) {
if (internalStorage != lastUpdateStorage) {
updateClients();
lastUpdateStorage = internalStorage;
}
}
}
use of network.rs485.logisticspipes.connection.NeighborTileEntity in project LogisticsPipes by RS485.
the class PowerSupplierHandler method requestICPower.
private void requestICPower() {
// Use Buffer
final List<LPNeighborTileEntity<TileEntity>> adjacentTileEntities = new WorldCoordinatesWrapper(pipe.container).allNeighborTileEntities();
double globalNeed = 0;
double[] need = new double[adjacentTileEntities.size()];
int i = 0;
for (NeighborTileEntity<TileEntity> adjacent : adjacentTileEntities) {
if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.getTileEntity())) {
if (pipe.canPipeConnect(adjacent.getTileEntity(), adjacent.getDirection())) {
if (SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.getTileEntity(), pipe.container, adjacent.getOurDirection())) {
// TODO pipe.container must be IEnergySource
globalNeed += need[i] = SimpleServiceLocator.IC2Proxy.demandedEnergyUnits(adjacent.getTileEntity());
}
}
}
++i;
}
if (globalNeed != 0 && !Double.isNaN(globalNeed)) {
double fullfillable = Math.min(1, internalBufferIC2 / globalNeed);
i = 0;
for (NeighborTileEntity<TileEntity> adjacent : adjacentTileEntities) {
if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.getTileEntity()) && pipe.canPipeConnect(adjacent.getTileEntity(), adjacent.getDirection()) && SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.getTileEntity(), pipe.container, adjacent.getOurDirection())) {
// TODO pipe.container must be IEnergySource
if (internalBufferIC2 + 1 < need[i] * fullfillable) {
return;
}
double toUse = Math.min(pipe.getUpgradeManager().getIC2PowerLevel(), need[i] * fullfillable);
double unUsed = SimpleServiceLocator.IC2Proxy.injectEnergyUnits(adjacent.getTileEntity(), adjacent.getOurDirection(), toUse);
double used = toUse - unUsed;
if (used > 0) {
// MainProxy.sendPacketToAllWatchingChunk(this.pipe.getX(), this.pipe.getZ(), MainProxy.getDimensionForWorld(this.pipe.getWorld()), PacketHandler.getPacket(PowerPacketLaser.class).setColor(LogisticsPowerProviderTileEntity.IC2_COLOR).setPos(this.pipe.getLPPosition()).setRenderBall(true).setDir(adTile.orientation).setLength(0.5F));
pipe.container.addLaser(adjacent.getDirection(), 0.5F, LogisticsPowerProviderTileEntity.IC2_COLOR, false, true);
internalBufferIC2 -= used;
}
if (internalBufferIC2 < 0) {
internalBufferIC2 = 0;
return;
}
}
++i;
}
}
// Rerequest Buffer
List<Pair<ISubSystemPowerProvider, List<IFilter>>> provider = pipe.getRouter().getSubSystemPowerProvider();
double available = 0;
outer: for (Pair<ISubSystemPowerProvider, List<IFilter>> pair : provider) {
for (IFilter filter : pair.getValue2()) {
if (filter.blockPower()) {
continue outer;
}
}
if (pair.getValue1().usePaused()) {
continue;
}
if (!pair.getValue1().getBrand().equals("EU")) {
continue;
}
available += pair.getValue1().getPowerLevel();
}
if (available > 0) {
double neededPower = PowerSupplierHandler.INTERNAL_IC2_BUFFER_MAX - internalBufferIC2;
if (neededPower > 0) {
if (pipe.useEnergy((int) (neededPower / 10000), false)) {
outer: for (Pair<ISubSystemPowerProvider, List<IFilter>> pair : provider) {
for (IFilter filter : pair.getValue2()) {
if (filter.blockPower()) {
continue outer;
}
}
if (pair.getValue1().usePaused()) {
continue;
}
if (!pair.getValue1().getBrand().equals("EU")) {
continue;
}
double requestamount = neededPower * (pair.getValue1().getPowerLevel() / available);
pair.getValue1().requestPower(pipe.getRouterId(), requestamount);
}
}
}
}
}
Aggregations