Search in sources :

Example 6 with IEnergyReceiver

use of cofh.api.energy.IEnergyReceiver 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)

Example 7 with IEnergyReceiver

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

the class EnergyNetwork method doTickStartCalc.

/**
 * Refreshes all tiles in network, and updates requested energy
 */
private void doTickStartCalc() {
    this.tickDone = EnergyNetwork.tickCount;
    this.totalSent = 0F;
    this.refreshAcceptors();
    if (!EnergyUtil.initialisedIC2Methods) {
        EnergyUtil.initialiseIC2Methods();
    }
    if (this.conductors.size() == 0) {
        return;
    }
    this.loopPrevention = true;
    this.availableAcceptors.clear();
    this.availableconnectedDirections.clear();
    this.energyRequests.clear();
    this.totalRequested = 0.0F;
    this.totalStorageExcess = 0F;
    if (!this.connectedAcceptors.isEmpty()) {
        float e;
        final Iterator<EnumFacing> acceptorDirection = this.connectedDirections.iterator();
        for (Object acceptor : this.connectedAcceptors) {
            // This tries all sides of the acceptor which are connected (see refreshAcceptors())
            EnumFacing sideFrom = acceptorDirection.next();
            // But the grid will only put energy into the acceptor from one side - once it's in availableAcceptors
            if (!this.ignoreAcceptors.contains(acceptor) && !this.availableAcceptors.contains(acceptor)) {
                e = 0.0F;
                if (acceptor instanceof IElectrical) {
                    e = ((IElectrical) acceptor).getRequest(sideFrom);
                } else if (isMekLoaded && acceptor instanceof IStrictEnergyAcceptor) {
                    e = (float) ((((IStrictEnergyAcceptor) acceptor).getMaxEnergy() - ((IStrictEnergyAcceptor) acceptor).getEnergy()) / EnergyConfigHandler.TO_MEKANISM_RATIO);
                } else if (isIC2Loaded && acceptor instanceof IEnergySink) {
                    double result = 0;
                    try {
                        result = (Double) EnergyUtil.demandedEnergyIC2.invoke(acceptor);
                    } catch (Exception ex) {
                        if (ConfigManagerCore.enableDebug) {
                            ex.printStackTrace();
                        }
                    }
                    // Cap IC2 power transfer at 128EU/t for standard Alu wire, 256EU/t for heavy Alu wire
                    result = Math.min(result, this.networkTierGC * 128D);
                    e = (float) result / EnergyConfigHandler.TO_IC2_RATIO;
                } else if (isRF2Loaded && acceptor instanceof IEnergyReceiver) {
                    e = ((IEnergyReceiver) acceptor).receiveEnergy(sideFrom, Integer.MAX_VALUE, true) / EnergyConfigHandler.TO_RF_RATIO;
                }
                if (e > 0.0F) {
                    this.availableAcceptors.add(acceptor);
                    this.availableconnectedDirections.put(acceptor, sideFrom);
                    this.energyRequests.put(acceptor, e);
                    this.totalRequested += e;
                    if (e > EnergyNetwork.ENERGY_STORAGE_LEVEL) {
                        this.totalStorageExcess += e - EnergyNetwork.ENERGY_STORAGE_LEVEL;
                    }
                }
            }
        }
    }
    this.loopPrevention = false;
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) IStrictEnergyAcceptor(mekanism.api.energy.IStrictEnergyAcceptor) IElectrical(micdoodle8.mods.galacticraft.api.transmission.tile.IElectrical) IEnergySink(ic2.api.energy.tile.IEnergySink) IEnergyReceiver(cofh.api.energy.IEnergyReceiver)

Example 8 with IEnergyReceiver

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

the class TileEngineBase method getPowerToExtract.

private int getPowerToExtract() {
    Object tile = getEnergyProvider(orientation);
    if (tile instanceof IEngine) {
        IEngine engine = (IEngine) tile;
        int maxEnergy = engine.receiveEnergyFromEngine(orientation.getOpposite(), this.energy, true);
        return extractEnergy(maxEnergy, false);
    } else if (tile instanceof IEnergyReceiver) {
        IEnergyReceiver handler = (IEnergyReceiver) tile;
        int maxEnergy = handler.receiveEnergy(orientation.getOpposite(), this.energy, true);
        return extractEnergy(maxEnergy, false);
    } else {
        return 0;
    }
}
Also used : IEngine(buildcraft.api.power.IEngine) IEnergyReceiver(cofh.api.energy.IEnergyReceiver)

Example 9 with IEnergyReceiver

use of cofh.api.energy.IEnergyReceiver in project PneumaticCraft by MineMaarten.

the class TileEntityPneumaticDynamo method updateEntity.

@Override
public void updateEntity() {
    super.updateEntity();
    if (!worldObj.isRemote) {
        if (worldObj.getTotalWorldTime() % 20 == 0) {
            int efficiency = Config.pneumaticDynamoEfficiency;
            if (efficiency < 1)
                efficiency = 1;
            airPerTick = (int) (40 * this.getSpeedUsageMultiplierFromUpgrades() * 100 / efficiency);
            rfPerTick = (int) (40 * this.getSpeedUsageMultiplierFromUpgrades() * getEfficiency() / 100);
        }
        boolean newEnabled;
        if (redstoneAllows() && getPressure(ForgeDirection.UNKNOWN) > PneumaticValues.MIN_PRESSURE_PNEUMATIC_DYNAMO && getMaxEnergyStored(ForgeDirection.UNKNOWN) - getEnergyStored(ForgeDirection.UNKNOWN) >= rfPerTick) {
            this.addAir(-airPerTick, ForgeDirection.UNKNOWN);
            heatExchanger.addHeat(airPerTick / 100D);
            energy.receiveEnergy(rfPerTick, false);
            newEnabled = true;
        } else {
            newEnabled = false;
        }
        if (worldObj.getTotalWorldTime() % 20 == 0 && newEnabled != isEnabled) {
            isEnabled = newEnabled;
            sendDescriptionPacket();
        }
        TileEntity receiver = getTileCache()[getRotation().getOpposite().ordinal()].getTileEntity();
        if (receiver instanceof IEnergyReceiver) {
            IEnergyReceiver recv = (IEnergyReceiver) receiver;
            if (recv.canConnectEnergy(getRotation())) {
                int extracted = energy.extractEnergy(rfPerTick * 2, true);
                int energyPushed = recv.receiveEnergy(getRotation(), extracted, true);
                if (energyPushed > 0) {
                    recv.receiveEnergy(getRotation(), energy.extractEnergy(energyPushed, false), false);
                }
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IEnergyReceiver(cofh.api.energy.IEnergyReceiver)

Example 10 with IEnergyReceiver

use of cofh.api.energy.IEnergyReceiver in project PneumaticCraft by MineMaarten.

the class DroneInteractRFExport method doInteract.

@Override
public boolean doInteract(ChunkPosition pos, IDrone drone, IBlockInteractHandler interactHandler, boolean simulate) {
    IEnergyStorage droneEnergy = CoFHCore.getEnergyStorage(drone);
    if (droneEnergy.getEnergyStored() == 0) {
        interactHandler.abort();
        return false;
    } else {
        TileEntity te = drone.getWorld().getTileEntity(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ);
        if (te instanceof IEnergyReceiver) {
            IEnergyReceiver receiver = (IEnergyReceiver) te;
            for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) {
                if (interactHandler.getSides()[d.ordinal()]) {
                    int transferedEnergy = droneEnergy.extractEnergy(receiver.receiveEnergy(d, interactHandler.useCount() ? interactHandler.getRemainingCount() : Integer.MAX_VALUE, true), true);
                    if (transferedEnergy > 0) {
                        if (!simulate) {
                            interactHandler.decreaseCount(transferedEnergy);
                            droneEnergy.extractEnergy(transferedEnergy, false);
                            receiver.receiveEnergy(d, transferedEnergy, false);
                        }
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IEnergyStorage(cofh.api.energy.IEnergyStorage) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) IEnergyReceiver(cofh.api.energy.IEnergyReceiver)

Aggregations

IEnergyReceiver (cofh.api.energy.IEnergyReceiver)11 TileEntity (net.minecraft.tileentity.TileEntity)8 EnumFacing (net.minecraft.util.EnumFacing)6 IStrictEnergyAcceptor (mekanism.api.energy.IStrictEnergyAcceptor)4 IEnergyHandler (cofh.api.energy.IEnergyHandler)3 IElectrical (micdoodle8.mods.galacticraft.api.transmission.tile.IElectrical)3 IEngine (buildcraft.api.power.IEngine)2 IEnergyConnection (cofh.api.energy.IEnergyConnection)2 IEnergyProvider (cofh.api.energy.IEnergyProvider)2 IEnergySink (ic2.api.energy.tile.IEnergySink)2 IConductor (micdoodle8.mods.galacticraft.api.transmission.tile.IConductor)2 BlockVec3 (micdoodle8.mods.galacticraft.api.vector.BlockVec3)2 BlockPos (net.minecraft.util.BlockPos)2 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)2 IPipeTile (buildcraft.api.transport.IPipeTile)1 PacketPowerUpdate (buildcraft.transport.network.PacketPowerUpdate)1 IEnergyStorage (cofh.api.energy.IEnergyStorage)1 Field (java.lang.reflect.Field)1 ICableOutputter (mekanism.api.energy.ICableOutputter)1 IConnector (micdoodle8.mods.galacticraft.api.transmission.tile.IConnector)1