<commit_before>package crazypants.enderio.power;

import net.minecraftforge.common.ForgeDirection;
import buildcraft.api.power.IPowerEmitter;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerHandler.PowerReceiver;
import buildcraft.api.power.PowerHandler.Type;
import cofh.api.energy.IEnergyHandler;

public class PowerInterface {

  public static PowerInterface create(Object o) {
    if(o instanceof IEnergyHandler) {
      return new PowerInterface((IEnergyHandler) o);
    } else if(o instanceof IPowerReceptor) {
      return new PowerInterface((IPowerReceptor) o);
    }
    return null;
  }

  private IPowerReceptor bcPower;

  private IEnergyHandler rfPower;

  public PowerInterface(IPowerReceptor powerReceptor) {
    bcPower = powerReceptor;
  }

  public PowerInterface(IEnergyHandler powerReceptor) {
    rfPower = powerReceptor;
  }

  public Object getDelegate() {
    if(bcPower != null) {
      return bcPower;
    }
    return rfPower;
  }

  public boolean canConduitConnect(ForgeDirection direction) {
    if(rfPower != null) {
      return rfPower.canInterface(direction.getOpposite());
    }

    // bc
    if(bcPower != null) {
      if(bcPower instanceof IPowerEmitter) {
        return ((IPowerEmitter) bcPower).canEmitPowerFrom(direction.getOpposite());
      }
      return PowerHandlerUtil.canConnectRecievePower(bcPower);
    }
    return false;
  }

  public float getEnergyStored(ForgeDirection dir) {
    if(rfPower != null) {
      return rfPower.getEnergyStored(dir) / 10;      
    }

    // bc
    float result = 0;
    if(bcPower instanceof IInternalPowerReceptor) {
      result = ((IInternalPowerReceptor) bcPower).getPowerHandler().getEnergyStored();
    } else if(bcPower != null && !(bcPower instanceof IPowerEmitter)) {
      PowerReceiver pr = bcPower.getPowerReceiver(dir);
      if(pr != null) {
        result += pr.getEnergyStored();
      }
    }
    return result;
  }

  public float getMaxEnergyStored(ForgeDirection dir) {
    if(rfPower != null) {
      return rfPower.getMaxEnergyStored(dir) / 10;      
    }

    // bc
    float result = 0;
    if(bcPower instanceof IInternalPowerReceptor) {
      result = ((IInternalPowerReceptor) bcPower).getPowerHandler().getMaxEnergyStored();
    } else if(bcPower != null && !(bcPower instanceof IPowerEmitter)) {
      PowerReceiver pr = bcPower.getPowerReceiver(dir);
      if(pr != null) {
        result += pr.getMaxEnergyStored();
      }
    }
    return result;
  }

  public float getPowerRequest(ForgeDirection dir) {
    if(rfPower != null) {
      if(rfPower.canInterface(dir)) {
        return rfPower.receiveEnergy(dir, rfPower.getMaxEnergyStored(dir), true) / 10;        
      }
      return 0;
    }

    // bc
    if(bcPower != null) {
      PowerReceiver pr = bcPower.getPowerReceiver(dir);
      if(pr == null || pr.getType() == Type.ENGINE) {
        return 0;
      }
      return pr.powerRequest();
    }

    return 0;
  }

  public float getMinEnergyReceived(ForgeDirection dir) {
    if(rfPower != null) {
      return 0;
    }

    if(bcPower != null) {
      PowerReceiver pr = bcPower.getPowerReceiver(dir);
      if(pr == null) {
        return 0;
      }
      return pr.getMinEnergyReceived();
    }
    return 0;
  }

  public float recieveEnergy(ForgeDirection opposite, float canOffer) {
    if(rfPower != null) {
      return rfPower.receiveEnergy(opposite, (int) (canOffer * 10), false) / 10;
    }

    if(bcPower != null) {
      if(bcPower instanceof IInternalPowerReceptor) {
        return PowerHandlerUtil.transmitInternal((IInternalPowerReceptor) bcPower, bcPower.getPowerReceiver(opposite), canOffer, Type.PIPE, opposite);
      }
      PowerReceiver pr = bcPower.getPowerReceiver(opposite);<commit_msg>#306 In some cases infinite energy could be produced when combing MJ generation and RF consumption.<commit_after>package crazypants.enderio.power;

import net.minecraftforge.common.ForgeDirection;
import buildcraft.api.power.IPowerEmitter;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerHandler.PowerReceiver;
import buildcraft.api.power.PowerHandler.Type;
import cofh.api.energy.IEnergyHandler;

public class PowerInterface {

  public static PowerInterface create(Object o) {
    if(o instanceof IEnergyHandler) {
      return new PowerInterface((IEnergyHandler) o);
    } else if(o instanceof IPowerReceptor) {
      return new PowerInterface((IPowerReceptor) o);
    }
    return null;
  }

  private IPowerReceptor bcPower;

  private IEnergyHandler rfPower;

  public PowerInterface(IPowerReceptor powerReceptor) {
    bcPower = powerReceptor;
  }

  public PowerInterface(IEnergyHandler powerReceptor) {
    rfPower = powerReceptor;
  }

  public Object getDelegate() {
    if(bcPower != null) {
      return bcPower;
    }
    return rfPower;
  }

  public boolean canConduitConnect(ForgeDirection direction) {
    if(rfPower != null) {
      return rfPower.canInterface(direction.getOpposite());
    }

    // bc
    if(bcPower != null) {
      if(bcPower instanceof IPowerEmitter) {
        return ((IPowerEmitter) bcPower).canEmitPowerFrom(direction.getOpposite());
      }
      return PowerHandlerUtil.canConnectRecievePower(bcPower);
    }
    return false;
  }

  public float getEnergyStored(ForgeDirection dir) {
    if(rfPower != null) {
      return rfPower.getEnergyStored(dir) / 10f;      
    }

    // bc
    float result = 0;
    if(bcPower instanceof IInternalPowerReceptor) {
      result = ((IInternalPowerReceptor) bcPower).getPowerHandler().getEnergyStored();
    } else if(bcPower != null && !(bcPower instanceof IPowerEmitter)) {
      PowerReceiver pr = bcPower.getPowerReceiver(dir);
      if(pr != null) {
        result += pr.getEnergyStored();
      }
    }
    return result;
  }

  public float getMaxEnergyStored(ForgeDirection dir) {
    if(rfPower != null) {
      return rfPower.getMaxEnergyStored(dir) / 10f;      
    }

    // bc
    float result = 0;
    if(bcPower instanceof IInternalPowerReceptor) {
      result = ((IInternalPowerReceptor) bcPower).getPowerHandler().getMaxEnergyStored();
    } else if(bcPower != null && !(bcPower instanceof IPowerEmitter)) {
      PowerReceiver pr = bcPower.getPowerReceiver(dir);
      if(pr != null) {
        result += pr.getMaxEnergyStored();
      }
    }
    return result;
  }

  public float getPowerRequest(ForgeDirection dir) {
    if(rfPower != null) {
      if(rfPower.canInterface(dir)) {
        return rfPower.receiveEnergy(dir, rfPower.getMaxEnergyStored(dir), true) / 10f;        
      }
      return 0;
    }

    // bc
    if(bcPower != null) {
      PowerReceiver pr = bcPower.getPowerReceiver(dir);
      if(pr == null || pr.getType() == Type.ENGINE) {
        return 0;
      }
      return pr.powerRequest();
    }

    return 0;
  }

  public float getMinEnergyReceived(ForgeDirection dir) {
    if(rfPower != null) {
      return 0;
    }

    if(bcPower != null) {
      PowerReceiver pr = bcPower.getPowerReceiver(dir);
      if(pr == null) {
        return 0;
      }
      return pr.getMinEnergyReceived();
    }
    return 0;
  }

  public float recieveEnergy(ForgeDirection opposite, float canOffer) {
    if(rfPower != null) {
      return rfPower.receiveEnergy(opposite, (int)(canOffer * 10), false) / 10f;
    }

    if(bcPower != null) {
      if(bcPower instanceof IInternalPowerReceptor) {
        return PowerHandlerUtil.transmitInternal((IInternalPowerReceptor) bcPower, bcPower.getPowerReceiver(opposite), canOffer, Type.PIPE, opposite);
      }
      PowerReceiver pr = bcPower.getPowerReceiver(opposite);
{'fraction_non_alphanumeric': 0.10501505826895378, 'fraction_numerical': 0.0051067172973680765, 'mean_word_length': 3.1020408163265305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6074938', 'n_tokens_mistral': 2426, 'n_tokens_neox': 2321, 'n_words': 481}