use of network.rs485.logisticspipes.connection.Adjacent 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);
}
}
}
}
}
use of network.rs485.logisticspipes.connection.Adjacent in project LogisticsPipes by RS485.
the class PipeLogisticsChassis method updateAdjacentCache.
/**
* Updates pointedAdjacent on {@link CoreRoutedPipe}.
*/
@Override
protected void updateAdjacentCache() {
super.updateAdjacentCache();
final Adjacent adjacent = getAdjacent();
if (adjacent instanceof SingleAdjacent) {
pointedAdjacent = ((SingleAdjacent) adjacent);
} else {
final SingleAdjacent oldPointedAdjacent = pointedAdjacent;
SingleAdjacent newPointedAdjacent = null;
if (oldPointedAdjacent != null) {
// update pointed adjacent with connection type or reset it
newPointedAdjacent = adjacent.optionalGet(oldPointedAdjacent.getDir()).map(connectionType -> new SingleAdjacent(this, oldPointedAdjacent.getDir(), connectionType)).orElse(null);
}
if (newPointedAdjacent == null) {
newPointedAdjacent = adjacent.neighbors().entrySet().stream().findAny().map(connectedNeighbor -> new SingleAdjacent(this, connectedNeighbor.getKey().getDirection(), connectedNeighbor.getValue())).orElse(null);
}
pointedAdjacent = newPointedAdjacent;
}
}
Aggregations