Search in sources :

Example 6 with IConductor

use of micdoodle8.mods.galacticraft.api.transmission.tile.IConductor in project Galacticraft by micdoodle8.

the class TileBaseUniversalElectricalSource method produce.

/*
     * Function to produce energy each tick into the outputs of a source.
     * If simulate is true, no energy is in fact transferred.
     *
     * Note: even if simulate is false this does NOT reduce the source's own
     * energy storage by the amount produced, that needs to be done elsewhere
     * See this.produce() for an example.
     */
public float produce(boolean simulate) {
    float amountProduced = 0;
    if (!this.worldObj.isRemote) {
        EnumSet<EnumFacing> outputDirections = this.getElectricalOutputDirections();
        // outputDirections.remove(EnumFacing.UNKNOWN);
        BlockVec3 thisVec = new BlockVec3(this);
        for (EnumFacing direction : outputDirections) {
            TileEntity tileAdj = thisVec.getTileEntityOnSide(this.worldObj, direction);
            if (tileAdj != null) {
                float toSend = this.extractEnergyGC(null, Math.min(this.getEnergyStoredGC() - amountProduced, this.getEnergyStoredGC() / outputDirections.size()), true);
                if (toSend <= 0) {
                    continue;
                }
                if (tileAdj instanceof TileBaseConductor && ((TileBaseConductor) tileAdj).canConnect(direction.getOpposite(), NetworkType.POWER)) {
                    IElectricityNetwork network = ((IConductor) tileAdj).getNetwork();
                    if (network != null) {
                        amountProduced += (toSend - network.produce(toSend, !simulate, this.tierGC, this));
                    }
                } else if (tileAdj instanceof TileBaseUniversalElectrical) {
                    amountProduced += ((TileBaseUniversalElectrical) tileAdj).receiveElectricity(direction.getOpposite(), toSend, this.tierGC, !simulate);
                } else {
                    amountProduced += EnergyUtil.otherModsEnergyTransfer(tileAdj, direction.getOpposite(), toSend, simulate);
                }
            }
        }
    }
    return amountProduced;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IConductor(micdoodle8.mods.galacticraft.api.transmission.tile.IConductor) EnumFacing(net.minecraft.util.EnumFacing) IElectricityNetwork(micdoodle8.mods.galacticraft.api.transmission.grid.IElectricityNetwork) BlockVec3(micdoodle8.mods.galacticraft.api.vector.BlockVec3)

Example 7 with IConductor

use of micdoodle8.mods.galacticraft.api.transmission.tile.IConductor in project Galacticraft by micdoodle8.

the class BlockBeamReceiver method getMetadataFromAngle.

private int getMetadataFromAngle(World world, BlockPos pos, EnumFacing side) {
    EnumFacing direction = side.getOpposite();
    TileEntity tileAt = world.getTileEntity(pos.add(direction.getFrontOffsetX(), direction.getFrontOffsetY(), direction.getFrontOffsetZ()));
    if (tileAt instanceof EnergyStorageTile) {
        if (((EnergyStorageTile) tileAt).getModeFromDirection(direction.getOpposite()) != null) {
            return direction.ordinal();
        } else {
            return -1;
        }
    }
    if (EnergyUtil.otherModCanReceive(tileAt, direction.getOpposite())) {
        return direction.ordinal();
    }
    for (EnumFacing adjacentDir : EnumFacing.VALUES) {
        if (adjacentDir == direction) {
            continue;
        }
        tileAt = world.getTileEntity(pos.add(adjacentDir.getFrontOffsetX(), adjacentDir.getFrontOffsetY(), adjacentDir.getFrontOffsetZ()));
        if (tileAt instanceof IConductor) {
            continue;
        }
        if (tileAt instanceof EnergyStorageTile && ((EnergyStorageTile) tileAt).getModeFromDirection(adjacentDir.getOpposite()) != null) {
            return adjacentDir.ordinal();
        }
        if (EnergyUtil.otherModCanReceive(tileAt, adjacentDir.getOpposite())) {
            return adjacentDir.ordinal();
        }
    }
    return -1;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IConductor(micdoodle8.mods.galacticraft.api.transmission.tile.IConductor) EnumFacing(net.minecraft.util.EnumFacing) EnergyStorageTile(micdoodle8.mods.galacticraft.core.energy.tile.EnergyStorageTile)

Example 8 with IConductor

use of micdoodle8.mods.galacticraft.api.transmission.tile.IConductor 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 9 with IConductor

use of micdoodle8.mods.galacticraft.api.transmission.tile.IConductor in project Galacticraft by micdoodle8.

the class EnergyNetwork method refreshWithChecks.

/**
 * Refresh validity of each conductor in the network
 */
public void refreshWithChecks() {
    int tierfound = Integer.MAX_VALUE;
    Iterator<IConductor> it = this.conductors.iterator();
    while (it.hasNext()) {
        IConductor conductor = it.next();
        if (conductor == null) {
            it.remove();
            continue;
        }
        TileEntity tile = (TileEntity) conductor;
        World world = tile.getWorld();
        // Remove any conductors in unloaded chunks
        if (tile.isInvalid() || world == null || !world.isBlockLoaded(tile.getPos())) {
            it.remove();
            continue;
        }
        if (conductor != world.getTileEntity(tile.getPos())) {
            it.remove();
            continue;
        }
        if (conductor.getTierGC() < tierfound) {
            tierfound = conductor.getTierGC();
        }
        if (conductor.getNetwork() != this) {
            conductor.setNetwork(this);
            conductor.onNetworkChanged();
        }
    }
    // This will set the network tier to 2 if all the conductors are tier 2
    if (tierfound == Integer.MAX_VALUE) {
        tierfound = 1;
    }
    this.networkTierGC = tierfound;
}
Also used : IConductor(micdoodle8.mods.galacticraft.api.transmission.tile.IConductor) TileEntity(net.minecraft.tileentity.TileEntity) World(net.minecraft.world.World)

Example 10 with IConductor

use of micdoodle8.mods.galacticraft.api.transmission.tile.IConductor in project Galacticraft by micdoodle8.

the class NetworkFinder method loopAll.

private void loopAll(int x, int y, int z, int dirIn) {
    BlockVec3 obj = null;
    for (int dir = 0; dir < 6; dir++) {
        if (dir == dirIn) {
            continue;
        }
        switch(dir) {
            case 0:
                obj = new BlockVec3(x, y - 1, z);
                break;
            case 1:
                obj = new BlockVec3(x, y + 1, z);
                break;
            case 2:
                obj = new BlockVec3(x, y, z - 1);
                break;
            case 3:
                obj = new BlockVec3(x, y, z + 1);
                break;
            case 4:
                obj = new BlockVec3(x - 1, y, z);
                break;
            case 5:
                obj = new BlockVec3(x + 1, y, z);
                break;
        }
        if (!iterated.contains(obj)) {
            iterated.add(obj);
            TileEntity tileEntity = worldObj.getTileEntity(new BlockPos(obj.x, obj.y, obj.z));
            if (tileEntity instanceof IConductor) {
                found.add((IConductor) tileEntity);
                loopAll(obj.x, obj.y, obj.z, dir ^ 1);
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IConductor(micdoodle8.mods.galacticraft.api.transmission.tile.IConductor) BlockPos(net.minecraft.util.BlockPos) BlockVec3(micdoodle8.mods.galacticraft.api.vector.BlockVec3)

Aggregations

IConductor (micdoodle8.mods.galacticraft.api.transmission.tile.IConductor)11 TileEntity (net.minecraft.tileentity.TileEntity)10 BlockVec3 (micdoodle8.mods.galacticraft.api.vector.BlockVec3)6 EnumFacing (net.minecraft.util.EnumFacing)4 World (net.minecraft.world.World)4 BlockPos (net.minecraft.util.BlockPos)3 IEnergyConnection (cofh.api.energy.IEnergyConnection)2 IEnergyHandler (cofh.api.energy.IEnergyHandler)2 IEnergyReceiver (cofh.api.energy.IEnergyReceiver)2 IStrictEnergyAcceptor (mekanism.api.energy.IStrictEnergyAcceptor)2 IEnergyProvider (cofh.api.energy.IEnergyProvider)1 Field (java.lang.reflect.Field)1 ICableOutputter (mekanism.api.energy.ICableOutputter)1 IElectricityNetwork (micdoodle8.mods.galacticraft.api.transmission.grid.IElectricityNetwork)1 IConnector (micdoodle8.mods.galacticraft.api.transmission.tile.IConnector)1 IElectrical (micdoodle8.mods.galacticraft.api.transmission.tile.IElectrical)1 INetworkConnection (micdoodle8.mods.galacticraft.api.transmission.tile.INetworkConnection)1 EnergyStorageTile (micdoodle8.mods.galacticraft.core.energy.tile.EnergyStorageTile)1