Search in sources :

Example 1 with IStrictEnergyAcceptor

use of mekanism.api.energy.IStrictEnergyAcceptor in project Galacticraft by micdoodle8.

the class EnergyNetwork method doProduce.

/**
 * Complete the energy transfer. Called internally on server tick end.
 *
 * @return Amount of energy SENT to all acceptors
 */
private float doProduce() {
    float sent = 0.0F;
    if (!this.availableAcceptors.isEmpty()) {
        float energyNeeded = this.totalRequested;
        float energyAvailable = this.totalEnergy;
        float reducor = 1.0F;
        float energyStorageReducor = 1.0F;
        if (energyNeeded > energyAvailable) {
            // If not enough energy, try reducing what goes into energy storage (if any)
            energyNeeded -= this.totalStorageExcess;
            // If there's still not enough, put the minimum into energy storage (if any) and, anyhow, reduce everything proportionately
            if (energyNeeded > energyAvailable) {
                energyStorageReducor = 0F;
                reducor = energyAvailable / energyNeeded;
            } else {
                // Energyavailable exceeds the total needed but only if storage does not fill all in one go - this is a common situation
                energyStorageReducor = (energyAvailable - energyNeeded) / this.totalStorageExcess;
            }
        }
        float currentSending;
        float sentToAcceptor;
        int tierProduced = Math.min(this.producersTierGC, this.networkTierGC);
        Object debugTE = null;
        try {
            for (Object tileEntity : this.availableAcceptors) {
                debugTE = tileEntity;
                // Exit the loop if there is no energy left at all (should normally not happen, should be some even for the last acceptor)
                if (sent >= energyAvailable) {
                    break;
                }
                // The base case is to give each acceptor what it is requesting
                currentSending = this.energyRequests.get(tileEntity);
                // If it's an energy store, we may need to damp it down if energyStorageReducor is less than 1
                if (currentSending > EnergyNetwork.ENERGY_STORAGE_LEVEL) {
                    currentSending = EnergyNetwork.ENERGY_STORAGE_LEVEL + (currentSending - EnergyNetwork.ENERGY_STORAGE_LEVEL) * energyStorageReducor;
                }
                // Reduce everything proportionately if there is not enough energy for all needs
                currentSending *= reducor;
                if (currentSending > energyAvailable - sent) {
                    currentSending = energyAvailable - sent;
                }
                EnumFacing sideFrom = this.availableconnectedDirections.get(tileEntity);
                if (tileEntity instanceof IElectrical) {
                    sentToAcceptor = ((IElectrical) tileEntity).receiveElectricity(sideFrom, currentSending, tierProduced, true);
                } else if (isMekLoaded && tileEntity instanceof IStrictEnergyAcceptor) {
                    sentToAcceptor = (float) ((IStrictEnergyAcceptor) tileEntity).transferEnergyToAcceptor(sideFrom, currentSending * EnergyConfigHandler.TO_MEKANISM_RATIO) / EnergyConfigHandler.TO_MEKANISM_RATIO;
                } else if (isIC2Loaded && tileEntity instanceof IEnergySink) {
                    double energySendingIC2 = currentSending * EnergyConfigHandler.TO_IC2_RATIO;
                    if (energySendingIC2 >= 1D) {
                        double result = 0;
                        try {
                            if (EnergyUtil.voltageParameterIC2) {
                                result = (Double) EnergyUtil.injectEnergyIC2.invoke(tileEntity, sideFrom.getOpposite(), energySendingIC2, 120D);
                            } else {
                                result = (Double) EnergyUtil.injectEnergyIC2.invoke(tileEntity, sideFrom.getOpposite(), energySendingIC2);
                            }
                        } catch (Exception ex) {
                            if (ConfigManagerCore.enableDebug) {
                                ex.printStackTrace();
                            }
                        }
                        sentToAcceptor = currentSending - (float) result / EnergyConfigHandler.TO_IC2_RATIO;
                        if (sentToAcceptor < 0F) {
                            sentToAcceptor = 0F;
                        }
                    } else {
                        sentToAcceptor = 0F;
                    }
                } else if (isRF2Loaded && tileEntity instanceof IEnergyReceiver) {
                    final int currentSendinginRF = (currentSending >= Integer.MAX_VALUE / EnergyConfigHandler.TO_RF_RATIO) ? Integer.MAX_VALUE : (int) (currentSending * EnergyConfigHandler.TO_RF_RATIO);
                    sentToAcceptor = ((IEnergyReceiver) tileEntity).receiveEnergy(sideFrom, currentSendinginRF, false) / EnergyConfigHandler.TO_RF_RATIO;
                } else {
                    sentToAcceptor = 0F;
                }
                if (sentToAcceptor / currentSending > 1.002F && sentToAcceptor > 0.01F) {
                    if (!this.spamstop) {
                        FMLLog.info("Energy network: acceptor took too much energy, offered " + currentSending + ", took " + sentToAcceptor + ". " + tileEntity.toString());
                        this.spamstop = true;
                    }
                    sentToAcceptor = currentSending;
                }
                sent += sentToAcceptor;
            }
        } catch (Exception e) {
            GCLog.severe("DEBUG Energy network loop issue, please report this");
            if (debugTE instanceof TileEntity) {
                GCLog.severe("Problem was likely caused by tile in dim " + GCCoreUtil.getDimensionID(((TileEntity) debugTE).getWorld()) + " at " + ((TileEntity) debugTE).getPos() + " Type:" + debugTE.getClass().getSimpleName());
            }
        }
    }
    if (EnergyNetwork.tickCount % 200 == 0) {
        this.spamstop = false;
    }
    float returnvalue = sent;
    if (returnvalue > this.totalEnergy) {
        returnvalue = this.totalEnergy;
    }
    if (returnvalue < 0F) {
        returnvalue = 0F;
    }
    return returnvalue;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) 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 2 with IStrictEnergyAcceptor

use of mekanism.api.energy.IStrictEnergyAcceptor 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 3 with IStrictEnergyAcceptor

use of mekanism.api.energy.IStrictEnergyAcceptor 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 4 with IStrictEnergyAcceptor

use of mekanism.api.energy.IStrictEnergyAcceptor 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)

Aggregations

IEnergyReceiver (cofh.api.energy.IEnergyReceiver)4 IStrictEnergyAcceptor (mekanism.api.energy.IStrictEnergyAcceptor)4 EnumFacing (net.minecraft.util.EnumFacing)4 IElectrical (micdoodle8.mods.galacticraft.api.transmission.tile.IElectrical)3 TileEntity (net.minecraft.tileentity.TileEntity)3 IEnergyConnection (cofh.api.energy.IEnergyConnection)2 IEnergyHandler (cofh.api.energy.IEnergyHandler)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 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 World (net.minecraft.world.World)1