Search in sources :

Example 1 with IEnergyHandler

use of cofh.api.energy.IEnergyHandler in project Galacticraft by micdoodle8.

the class EnergyUtil method setAdjacentPowerConnections.

/**
 * Similar to getAdjacentPowerConnections but specific to energy receivers only
 * Adds the adjacent power connections found to the passed acceptors, directions parameter Lists
 * (Note: an acceptor can therefore sometimes be entered in the Lists more than once, with a different direction each time:
 * this would represent GC wires connected to the acceptor on more than one side.)
 *
 * @param conductor
 * @param connectedAcceptors
 * @param directions
 * @throws Exception
 */
public static void setAdjacentPowerConnections(TileEntity conductor, List<Object> connectedAcceptors, List<EnumFacing> directions) throws Exception {
    final BlockVec3 thisVec = new BlockVec3(conductor);
    final World world = conductor.getWorld();
    for (EnumFacing direction : EnumFacing.VALUES) {
        TileEntity tileEntity = thisVec.getTileEntityOnSide(world, direction);
        if (// world.getTileEntity will not have returned an invalid tile, invalid tiles are null
        tileEntity == null || tileEntity instanceof IConductor) {
            continue;
        }
        EnumFacing sideFrom = direction.getOpposite();
        if (tileEntity instanceof IElectrical) {
            if (((IElectrical) tileEntity).canConnect(sideFrom, NetworkType.POWER)) {
                connectedAcceptors.add(tileEntity);
                directions.add(sideFrom);
            }
            continue;
        }
        if (isMekLoaded && tileEntity instanceof IStrictEnergyAcceptor) {
            if (clazzMekCable != null && clazzMekCable.isInstance(tileEntity)) {
                continue;
            }
            if (((IStrictEnergyAcceptor) tileEntity).canReceiveEnergy(sideFrom)) {
                connectedAcceptors.add(tileEntity);
                directions.add(sideFrom);
            }
            continue;
        }
        if (isBCReallyLoaded && clazzPipeTile.isInstance(tileEntity)) {
            continue;
        }
        if (isIC2Loaded && !world.isRemote) {
            IEnergyTile IC2tile = null;
            BlockPos checkingIC2 = thisVec.toBlockPos().offset(direction);
            try {
                IC2tile = EnergyNet.instance.getSubTile(world, checkingIC2);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (IC2tile instanceof IEnergyConductor) {
                continue;
            }
            if (IC2tile instanceof IEnergyAcceptor && ((IEnergyAcceptor) IC2tile).acceptsEnergyFrom((IEnergyEmitter) conductor, sideFrom)) {
                connectedAcceptors.add(IC2tile);
                directions.add(sideFrom);
            }
            continue;
        }
        if ((isRF2Loaded && tileEntity instanceof IEnergyReceiver) || (isRF1Loaded && tileEntity instanceof IEnergyHandler)) {
            if (clazzEnderIOCable != null && clazzEnderIOCable.isInstance(tileEntity)) {
                continue;
            }
            if (clazzMFRRednetEnergyCable != null && clazzMFRRednetEnergyCable.isInstance(tileEntity)) {
                continue;
            }
            if (((IEnergyConnection) tileEntity).canConnectEnergy(sideFrom)) {
                connectedAcceptors.add(tileEntity);
                directions.add(sideFrom);
            }
            continue;
        }
    }
    return;
}
Also used : IEnergyConnection(cofh.api.energy.IEnergyConnection) EnumFacing(net.minecraft.util.EnumFacing) World(net.minecraft.world.World) TileEntity(net.minecraft.tileentity.TileEntity) IConductor(micdoodle8.mods.galacticraft.api.transmission.tile.IConductor) IStrictEnergyAcceptor(mekanism.api.energy.IStrictEnergyAcceptor) IEnergyHandler(cofh.api.energy.IEnergyHandler) BlockPos(net.minecraft.util.BlockPos) IElectrical(micdoodle8.mods.galacticraft.api.transmission.tile.IElectrical) IEnergyReceiver(cofh.api.energy.IEnergyReceiver) BlockVec3(micdoodle8.mods.galacticraft.api.vector.BlockVec3)

Example 2 with IEnergyHandler

use of cofh.api.energy.IEnergyHandler in project BuildCraft by BuildCraft.

the class PipeTransportPower method updateEntity.

@Override
public void updateEntity() {
    if (container.getWorld().isRemote) {
        for (int i = 0; i < 6; i++) {
            displayPowerAverage[i].tick(displayPower[i]);
        }
        return;
    }
    if (PipeTransportPower.canExplode) {
        if (overload >= 3) {
            destroyPipe();
            return;
        }
    }
    step();
    init();
    for (EnumFacing side : EnumFacing.VALUES) {
        if (tiles[side.ordinal()] != null && tiles[side.ordinal()].isInvalid()) {
            updateTile(side);
        }
    }
    // FIXME: LEFT OVER FROM MERGE! LOOK AT THIS!
    Arrays.fill(displayFlow, (short) 0);
    // Send the power to nearby pipes who requested it
    for (int i = 0; i < 6; ++i) {
        if (internalPower[i] > 0) {
            int totalPowerQuery = 0;
            for (int j = 0; j < 6; ++j) {
                if (j != i && powerQuery[j] > 0) {
                    Object ep = providers[j];
                    if (ep instanceof IPipeTile || ep instanceof IEnergyReceiver || ep instanceof IEnergyHandler) {
                        totalPowerQuery += powerQuery[j];
                    }
                }
            }
            if (totalPowerQuery > 0) {
                int unusedPowerQuery = totalPowerQuery;
                for (int j = 0; j < 6; ++j) {
                    if (j != i && powerQuery[j] > 0) {
                        Object ep = providers[j];
                        double watts = Math.min(internalPower[i] * powerQuery[j] / unusedPowerQuery, internalPower[i]);
                        unusedPowerQuery -= powerQuery[j];
                        if (ep instanceof IPipeTile && ((IPipeTile) ep).getPipeType() == IPipeTile.PipeType.POWER) {
                            Pipe<?> nearbyPipe = (Pipe<?>) ((IPipeTile) ep).getPipe();
                            PipeTransportPower nearbyTransport = (PipeTransportPower) nearbyPipe.transport;
                            watts = nearbyTransport.receiveEnergy(EnumFacing.VALUES[j].getOpposite(), watts);
                            internalPower[i] -= watts;
                            dbgEnergyOutput[j] += watts;
                            powerAverage[j].push((int) Math.ceil(watts));
                            powerAverage[i].push((int) Math.ceil(watts));
                            displayFlow[i] = 1;
                            displayFlow[j] = -1;
                        } else {
                            int iWatts = (int) watts;
                            if (ep instanceof IEnergyReceiver) {
                                IEnergyReceiver handler = (IEnergyReceiver) ep;
                                if (handler.canConnectEnergy(EnumFacing.values()[j].getOpposite())) {
                                    iWatts = handler.receiveEnergy(EnumFacing.values()[j].getOpposite(), iWatts, false);
                                }
                            }
                            internalPower[i] -= iWatts;
                            dbgEnergyOutput[j] += iWatts;
                            powerAverage[j].push(iWatts);
                            powerAverage[i].push(iWatts);
                            displayFlow[i] = 1;
                            displayFlow[j] = -1;
                        }
                    }
                }
            }
        }
    }
    short highestPower = 0;
    for (int i = 0; i < 6; i++) {
        powerAverage[i].tick();
        displayPower[i] = (short) Math.round(powerAverage[i].getAverage());
        if (displayPower[i] > highestPower) {
            highestPower = displayPower[i];
        }
    }
    if (PipeTransportPower.canExplode) {
        if (energyInputTick > powerLimit || overload > 0) {
            overload++;
        } else {
            overload = 0;
        }
    } else {
        overload += highestPower > (maxPower * 0.95F) ? 1 : -1;
        if (overload < 0) {
            overload = 0;
        }
        if (overload > OVERLOAD_TICKS) {
            overload = OVERLOAD_TICKS;
        }
    }
    energyInputTick = 0;
    // Compute the tiles requesting energy that are not power pipes
    for (EnumFacing dir : EnumFacing.VALUES) {
        if (!outputOpen(dir)) {
            continue;
        }
        Object tile = providers[dir.ordinal()];
        if (tile instanceof IPipeTile && ((IPipeTile) tile).getPipe() != null && ((Pipe<?>) ((IPipeTile) tile).getPipe()).transport instanceof PipeTransportPower) {
            continue;
        }
        if (tile instanceof IEnergyReceiver) {
            IEnergyReceiver handler = (IEnergyReceiver) tile;
            if (handler.canConnectEnergy(dir.getOpposite())) {
                int request = handler.receiveEnergy(dir.getOpposite(), this.maxPower, true);
                if (request > 0) {
                    requestEnergy(dir, request);
                }
            }
        } else if (tile instanceof IEnergyReceiver) {
            IEnergyReceiver handler = (IEnergyReceiver) tile;
            if (handler.canConnectEnergy(dir.getOpposite())) {
                int request = handler.receiveEnergy(dir.getOpposite(), this.maxPower, true);
                if (request > 0) {
                    requestEnergy(dir, request);
                }
            }
        }
    }
    // Sum the amount of energy requested on each side
    int[] transferQuery = new int[6];
    for (int i = 0; i < 6; ++i) {
        transferQuery[i] = 0;
        if (!inputOpen(EnumFacing.getFront(i))) {
            continue;
        }
        for (int j = 0; j < 6; ++j) {
            if (j != i) {
                transferQuery[i] += powerQuery[j];
            }
        }
        transferQuery[i] = Math.min(transferQuery[i], maxPower);
    }
    // Transfer the requested energy to nearby pipes
    for (int i = 0; i < 6; ++i) {
        if (transferQuery[i] != 0 && tiles[i] != null) {
            TileEntity entity = tiles[i];
            if (entity instanceof IPipeTile && ((IPipeTile) entity).getPipeType() == IPipeTile.PipeType.POWER) {
                IPipeTile nearbyTile = (IPipeTile) entity;
                if (nearbyTile.getPipe() == null || nearbyTile.getPipeType() != IPipeTile.PipeType.POWER) {
                    continue;
                }
                PipeTransportPower nearbyTransport = (PipeTransportPower) ((Pipe<?>) nearbyTile.getPipe()).transport;
                nearbyTransport.requestEnergy(EnumFacing.VALUES[i].getOpposite(), transferQuery[i]);
            }
        }
    }
    if (tracker.markTimeIfDelay(container.getWorld())) {
        PacketPowerUpdate packet = new PacketPowerUpdate(container);
        packet.displayPower = new short[6];
        for (int i = 0; i < 6; i++) {
            double val = displayPower[i];
            val /= MAX_POWER;
            val = Math.sqrt(val);
            val *= POWER_STAGES;
            packet.displayPower[i] = (short) val;
        }
        packet.displayFlow = displayFlow;
        packet.overload = isOverloaded();
        BuildCraftTransport.instance.sendToPlayersNear(packet, container);
    }
}
Also used : IPipeTile(buildcraft.api.transport.IPipeTile) EnumFacing(net.minecraft.util.EnumFacing) TileEntity(net.minecraft.tileentity.TileEntity) IEnergyHandler(cofh.api.energy.IEnergyHandler) PacketPowerUpdate(buildcraft.transport.network.PacketPowerUpdate) IEnergyReceiver(cofh.api.energy.IEnergyReceiver)

Example 3 with IEnergyHandler

use of cofh.api.energy.IEnergyHandler in project Galacticraft by micdoodle8.

the class EnergyUtil method getAdjacentPowerConnections.

public static TileEntity[] getAdjacentPowerConnections(TileEntity tile) {
    final TileEntity[] adjacentConnections = new TileEntity[6];
    BlockVec3 thisVec = new BlockVec3(tile);
    for (EnumFacing direction : EnumFacing.VALUES) {
        if (tile instanceof IConductor && !((IConductor) tile).canConnect(direction, NetworkType.POWER)) {
            continue;
        }
        TileEntity tileEntity = thisVec.getTileEntityOnSide(tile.getWorld(), direction);
        if (tileEntity == null) {
            continue;
        }
        if (tileEntity instanceof IConnector) {
            if (((IConnector) tileEntity).canConnect(direction.getOpposite(), NetworkType.POWER)) {
                adjacentConnections[direction.ordinal()] = tileEntity;
            }
            continue;
        }
        if (isMekLoaded && (tileEntity instanceof IStrictEnergyAcceptor || tileEntity instanceof ICableOutputter)) {
            // Do not connect GC wires directly to Mek Universal Cables
            try {
                if (clazzMekCable != null && clazzMekCable.isInstance(tileEntity)) {
                    continue;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (tileEntity instanceof IStrictEnergyAcceptor && ((IStrictEnergyAcceptor) tileEntity).canReceiveEnergy(direction.getOpposite())) {
                adjacentConnections[direction.ordinal()] = tileEntity;
            } else if (tileEntity instanceof ICableOutputter && ((ICableOutputter) tileEntity).canOutputTo(direction.getOpposite())) {
                adjacentConnections[direction.ordinal()] = tileEntity;
            }
            continue;
        }
        if (isBCReallyLoaded) {
            // Do not connect GC wires directly to BC pipes of any type
            try {
                if (clazzPipeTile.isInstance(tileEntity)) {
                    continue;
                }
            } catch (Exception e) {
            }
        }
        if (isRFLoaded && tileEntity instanceof IEnergyConnection) {
            if (isRF2Loaded && (tileEntity instanceof IEnergyProvider || tileEntity instanceof IEnergyReceiver) || isRF1Loaded && tileEntity instanceof IEnergyHandler || clazzRailcraftEngine != null && clazzRailcraftEngine.isInstance(tileEntity)) {
                // Do not connect GC wires directly to power conduits
                if (clazzEnderIOCable != null && clazzEnderIOCable.isInstance(tileEntity)) {
                    continue;
                }
                if (clazzMFRRednetEnergyCable != null && clazzMFRRednetEnergyCable.isInstance(tileEntity)) {
                    continue;
                }
                if (((IEnergyConnection) tileEntity).canConnectEnergy(direction.getOpposite())) {
                    adjacentConnections[direction.ordinal()] = tileEntity;
                }
            }
            continue;
        }
        if (isIC2Loaded) {
            if (tileEntity instanceof IEnergyConductor) {
                continue;
            }
            if (!tile.getWorld().isRemote) {
                Object IC2tile = tileEntity;
                BlockPos checkingIC2 = thisVec.toBlockPos().offset(direction);
                try {
                    IC2tile = EnergyNet.instance.getSubTile(tile.getWorld(), checkingIC2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (IC2tile instanceof IEnergyAcceptor && tile instanceof IEnergyEmitter) {
                    if (((IEnergyAcceptor) IC2tile).acceptsEnergyFrom((IEnergyEmitter) tile, direction.getOpposite())) {
                        adjacentConnections[direction.ordinal()] = tileEntity;
                    }
                    continue;
                }
                if (IC2tile instanceof IEnergyEmitter && tile instanceof IEnergyAcceptor) {
                    if (((IEnergyEmitter) IC2tile).emitsEnergyTo((IEnergyAcceptor) tile, direction.getOpposite())) {
                        adjacentConnections[direction.ordinal()] = tileEntity;
                    }
                    continue;
                }
            } else {
                try {
                    Class clazz = tileEntity.getClass();
                    if (clazz.getName().startsWith("ic2")) {
                        // Special case: IC2's transformers don't seem to setup their sink and source directions in Energy clientside
                        if (clazz.getName().startsWith("ic2.core.block.wiring.TileEntityTransformer")) {
                            adjacentConnections[direction.ordinal()] = tileEntity;
                            continue;
                        }
                        Field energyField = null;
                        fieldLoop: while (energyField == null && clazz != null) {
                            for (Field f : clazz.getDeclaredFields()) {
                                if (f.getName().equals("energy")) {
                                    energyField = f;
                                    break fieldLoop;
                                }
                            }
                            clazz = clazz.getSuperclass();
                        }
                        energyField.setAccessible(true);
                        Object energy = energyField.get(tileEntity);
                        Set<EnumFacing> connections;
                        if (tile instanceof IEnergyEmitter) {
                            connections = (Set<EnumFacing>) energy.getClass().getMethod("getSinkDirs").invoke(energy);
                            if (connections.contains(direction.getOpposite())) {
                                adjacentConnections[direction.ordinal()] = tileEntity;
                                continue;
                            }
                        }
                        if (tile instanceof IEnergyAcceptor) {
                            connections = (Set<EnumFacing>) energy.getClass().getMethod("getSourceDirs").invoke(energy);
                            if (connections.contains(direction.getOpposite())) {
                                adjacentConnections[direction.ordinal()] = tileEntity;
                                continue;
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return adjacentConnections;
}
Also used : IEnergyConnection(cofh.api.energy.IEnergyConnection) EnumFacing(net.minecraft.util.EnumFacing) TileEntity(net.minecraft.tileentity.TileEntity) IConductor(micdoodle8.mods.galacticraft.api.transmission.tile.IConductor) Field(java.lang.reflect.Field) IEnergyProvider(cofh.api.energy.IEnergyProvider) ICableOutputter(mekanism.api.energy.ICableOutputter) IStrictEnergyAcceptor(mekanism.api.energy.IStrictEnergyAcceptor) IEnergyHandler(cofh.api.energy.IEnergyHandler) IConnector(micdoodle8.mods.galacticraft.api.transmission.tile.IConnector) BlockPos(net.minecraft.util.BlockPos) IEnergyReceiver(cofh.api.energy.IEnergyReceiver) BlockVec3(micdoodle8.mods.galacticraft.api.vector.BlockVec3)

Aggregations

IEnergyHandler (cofh.api.energy.IEnergyHandler)3 IEnergyReceiver (cofh.api.energy.IEnergyReceiver)3 TileEntity (net.minecraft.tileentity.TileEntity)3 EnumFacing (net.minecraft.util.EnumFacing)3 IEnergyConnection (cofh.api.energy.IEnergyConnection)2 IStrictEnergyAcceptor (mekanism.api.energy.IStrictEnergyAcceptor)2 IConductor (micdoodle8.mods.galacticraft.api.transmission.tile.IConductor)2 BlockVec3 (micdoodle8.mods.galacticraft.api.vector.BlockVec3)2 BlockPos (net.minecraft.util.BlockPos)2 IPipeTile (buildcraft.api.transport.IPipeTile)1 PacketPowerUpdate (buildcraft.transport.network.PacketPowerUpdate)1 IEnergyProvider (cofh.api.energy.IEnergyProvider)1 Field (java.lang.reflect.Field)1 ICableOutputter (mekanism.api.energy.ICableOutputter)1 IConnector (micdoodle8.mods.galacticraft.api.transmission.tile.IConnector)1 IElectrical (micdoodle8.mods.galacticraft.api.transmission.tile.IElectrical)1 World (net.minecraft.world.World)1