Search in sources :

Example 6 with IAirHandler

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;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IAirHandler(pneumaticCraft.api.tileentity.IAirHandler) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) IPneumaticMachine(pneumaticCraft.api.tileentity.IPneumaticMachine) ArrayList(java.util.ArrayList) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ISidedPneumaticMachine(pneumaticCraft.api.tileentity.ISidedPneumaticMachine) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 7 with IAirHandler

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());
    }
}
Also used : IAirHandler(pneumaticCraft.api.tileentity.IAirHandler) MutablePair(org.apache.commons.lang3.tuple.MutablePair) ArrayList(java.util.ArrayList) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 8 with IAirHandler

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;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IAirHandler(pneumaticCraft.api.tileentity.IAirHandler) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) IPneumaticMachine(pneumaticCraft.api.tileentity.IPneumaticMachine) ArrayList(java.util.ArrayList) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ISidedPneumaticMachine(pneumaticCraft.api.tileentity.ISidedPneumaticMachine) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Aggregations

IAirHandler (pneumaticCraft.api.tileentity.IAirHandler)8 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)7 Pair (org.apache.commons.lang3.tuple.Pair)5 TileEntity (net.minecraft.tileentity.TileEntity)4 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)4 IPneumaticMachine (pneumaticCraft.api.tileentity.IPneumaticMachine)4 ArrayList (java.util.ArrayList)3 ItemStack (net.minecraft.item.ItemStack)2 MutablePair (org.apache.commons.lang3.tuple.MutablePair)2 ISidedPneumaticMachine (pneumaticCraft.api.tileentity.ISidedPneumaticMachine)2 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityItem (net.minecraft.entity.item.EntityItem)1 EntityVillager (net.minecraft.entity.passive.EntityVillager)1 IInventory (net.minecraft.inventory.IInventory)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 AxisAlignedBB (net.minecraft.util.AxisAlignedBB)1 ChunkPosition (net.minecraft.world.ChunkPosition)1 IPressurizable (pneumaticCraft.api.item.IPressurizable)1 IPressureChamberRecipe (pneumaticCraft.api.recipe.IPressureChamberRecipe)1 PressureChamberRecipe (pneumaticCraft.api.recipe.PressureChamberRecipe)1