use of pneumaticCraft.api.tileentity.IAirHandler in project PneumaticCraft by MineMaarten.
the class TileEntityPneumaticBase method getConnectedPneumatics.
/**
* Retrieves a list of all the connecting pneumatics. It takes sides in account.
* @return
*/
@Override
public List<Pair<ForgeDirection, IAirHandler>> getConnectedPneumatics() {
List<Pair<ForgeDirection, IAirHandler>> teList = new ArrayList<Pair<ForgeDirection, IAirHandler>>();
for (IAirHandler specialConnection : specialConnectedHandlers) {
teList.add(new ImmutablePair(ForgeDirection.UNKNOWN, specialConnection));
}
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) {
TileEntity te = getTileCache()[direction.ordinal()].getTileEntity();
IPneumaticMachine machine = ModInteractionUtils.getInstance().getMachine(te);
if (machine != null && isConnectedTo(direction) && machine.isConnectedTo(direction.getOpposite())) {
teList.add(new ImmutablePair(direction, machine.getAirHandler()));
} else if (te instanceof ISidedPneumaticMachine) {
IAirHandler handler = ((ISidedPneumaticMachine) te).getAirHandler(direction.getOpposite());
if (handler != null) {
teList.add(new ImmutablePair(direction, handler));
}
}
}
return teList;
}
use of pneumaticCraft.api.tileentity.IAirHandler in project PneumaticCraft by MineMaarten.
the class TileEntityPneumaticBase method disperseAir.
private void disperseAir(List<Pair<ForgeDirection, IAirHandler>> teList) {
boolean shouldRepeat = false;
List<Pair<Integer, Integer>> dispersion = new ArrayList<Pair<Integer, Integer>>();
do {
shouldRepeat = false;
//Add up every volume and air.
int totalVolume = getVolume();
int totalAir = currentAir;
for (Pair<ForgeDirection, IAirHandler> entry : teList) {
IAirHandler airHandler = entry.getValue();
totalVolume += airHandler.getVolume();
totalAir += airHandler.getCurrentAir(entry.getKey().getOpposite());
}
//Only go push based, ignore any machines that have a higher pressure than this block.
Iterator<Pair<ForgeDirection, IAirHandler>> iterator = teList.iterator();
while (iterator.hasNext()) {
Pair<ForgeDirection, IAirHandler> entry = iterator.next();
IAirHandler airHandler = entry.getValue();
//Calculate the total air the machine is going to get.
int totalMachineAir = (int) ((long) totalAir * airHandler.getVolume() / totalVolume);
int airDispersed = totalMachineAir - airHandler.getCurrentAir(entry.getKey().getOpposite());
if (airDispersed < 0) {
iterator.remove();
shouldRepeat = true;
dispersion.clear();
break;
} else {
dispersion.add(new MutablePair(getMaxDispersion(entry.getKey()), airDispersed));
}
}
} while (shouldRepeat);
int toBeDivided = 0;
int receivers = dispersion.size();
for (Pair<Integer, Integer> disp : dispersion) {
if (disp.getValue() > disp.getKey()) {
//Any air that wants to go to a neighbor, but can't (because of regulator module) gives back its air.
toBeDivided += disp.getValue() - disp.getKey();
disp.setValue(disp.getKey());
receivers--;
}
}
while (toBeDivided >= receivers && receivers > 0) {
//try to give every receiver an equal part of the to be divided air.
int dividedValue = toBeDivided / receivers;
for (Pair<Integer, Integer> disp : dispersion) {
int maxTransfer = disp.getKey() - disp.getValue();
if (maxTransfer > 0) {
if (maxTransfer <= dividedValue) {
//next step this receiver won't be able to receive any air.
receivers--;
}
//cap it at the max it can have.
int transfered = Math.min(dividedValue, maxTransfer);
disp.setValue(disp.getValue() + transfered);
toBeDivided -= transfered;
} else {
receivers--;
}
}
}
for (int i = 0; i < teList.size(); i++) {
IAirHandler neighbor = teList.get(i).getValue();
int transferedAir = dispersion.get(i).getValue();
onAirDispersion(transferedAir, teList.get(i).getKey());
neighbor.addAir(transferedAir, teList.get(i).getKey().getOpposite());
addAir(-transferedAir, teList.get(i).getKey());
}
}
use of pneumaticCraft.api.tileentity.IAirHandler in project PneumaticCraft by MineMaarten.
the class TileEntityVacuumPump method getConnectedPneumatics.
@Override
public List<Pair<ForgeDirection, IAirHandler>> getConnectedPneumatics() {
List<Pair<ForgeDirection, IAirHandler>> teList = new ArrayList<Pair<ForgeDirection, IAirHandler>>();
ForgeDirection direction = getInputSide();
TileEntity te = getTileCache()[direction.ordinal()].getTileEntity();
IPneumaticMachine machine = ModInteractionUtils.getInstance().getMachine(te);
if (machine != null && isConnectedTo(direction) && machine.isConnectedTo(direction.getOpposite())) {
teList.add(new ImmutablePair(direction, machine.getAirHandler()));
} else if (te instanceof ISidedPneumaticMachine) {
IAirHandler handler = ((ISidedPneumaticMachine) te).getAirHandler(direction);
if (handler != null) {
teList.add(new ImmutablePair(direction, handler));
}
}
return teList;
}
Aggregations