use of mekanism.api.energy.ICableOutputter 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;
}
Aggregations