<commit_before>package crazypants.enderio.conduit.liquid;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidHandler;
import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent;
import crazypants.enderio.conduit.ConduitNetworkTickHandler;
import crazypants.enderio.conduit.ConduitNetworkTickHandler.TickListener;
import crazypants.enderio.conduit.ConduitUtil;
import crazypants.enderio.conduit.IConduit;
import crazypants.util.BlockCoord;

public class LiquidConduitNetwork extends AbstractTankConduitNetwork<LiquidConduit> {

  public LiquidConduitNetwork() {
    super(LiquidConduit.class);
  }

  private long timeAtLastApply;

  private int maxFlowsPerTick = 10;
  private int lastFlowIndex = 0;

  private boolean printFlowTiming = false;

  private int pushToken = 0;

  private int inputVolume;

  private int outputVolume;

  private boolean inputLocked = false;

  private final InnerTickHandler tickHandler = new InnerTickHandler();

  public boolean lockNetworkForFill() {
    if(inputLocked) {
      return false;
    }
    inputLocked = true;
    return true;
  }

  public void unlockNetworkFromFill() {
    inputLocked = false;
  }

  @Override
  public void onUpdateEntity(IConduit conduit) {
    World world = conduit.getBundle().getEntity().getWorldObj();
    if(world == null) {
      return;
    }
    if(world.isRemote || liquidType == null) {
      return;
    }

    long curTime = world.getTotalWorldTime();
    if(curTime > 0 && curTime != timeAtLastApply) {
      timeAtLastApply = curTime;
      ConduitNetworkTickHandler.instance.addListener(tickHandler);
      //doTick(curTime);
    }
  }

  private void doTick() {

    List<LiquidConduit> cons = getConduits();
    if(cons == null || cons.isEmpty()) {
      return;
    }

    long curTime = cons.get(0).getBundle().getEntity().getWorldObj().getTotalWorldTime();

    // 1000 water, 6000 lava
    if(liquidType != null && liquidType.getFluid() != null) {
      int visc = Math.max(1000, liquidType.getFluid().getViscosity());
      if(curTime % (visc / 500) == 0) {
        long start = System.nanoTime();
        if(doFlow() && printFlowTiming) {
          long took = System.nanoTime() - start;
          double secs = took / 1000000000.0;
          System.out.println("LiquidConduitNetwork.onUpdateEntity: took " + secs + " secs, " + (secs * 1000) + " millis");
        }
      }
    }
    if(!fluidTypeLocked && isEmpty()) {
      setFluidType(null);
    }
  }

  void addedFromExternal(int res) {
    inputVolume += res;
  }

  void outputedToExternal(int filled) {
    outputVolume += filled;
  }

  int getNextPushToken() {
    return ++pushToken;
  }

  private boolean doFlow() {

    int pushToken = getNextPushToken();
    List<FlowAction> actions = new ArrayList<FlowAction>();
    for (int i = 0; i < Math.min(maxFlowsPerTick, conduits.size()); i++) {

      if(lastFlowIndex >= conduits.size()) {<commit_msg>#852 Basic Fluid Conduit causes performance issues with small fluid volumes.<commit_after>package crazypants.enderio.conduit.liquid;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidHandler;
import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent;
import crazypants.enderio.conduit.ConduitNetworkTickHandler;
import crazypants.enderio.conduit.ConduitNetworkTickHandler.TickListener;
import crazypants.enderio.conduit.ConduitUtil;
import crazypants.enderio.conduit.IConduit;
import crazypants.util.BlockCoord;

public class LiquidConduitNetwork extends AbstractTankConduitNetwork<LiquidConduit> {

  public LiquidConduitNetwork() {
    super(LiquidConduit.class);
  }

  private long timeAtLastApply;

  private int ticksEmpty = 0;

  private int maxFlowsPerTick = 10;
  private int lastFlowIndex = 0;

  private boolean printFlowTiming = false;

  private int pushToken = 0;

  private int inputVolume;

  private int outputVolume;

  private boolean inputLocked = false;

  private final InnerTickHandler tickHandler = new InnerTickHandler();

  public boolean lockNetworkForFill() {
    if(inputLocked) {
      return false;
    }
    inputLocked = true;
    return true;
  }

  public void unlockNetworkFromFill() {
    inputLocked = false;
  }

  @Override
  public void onUpdateEntity(IConduit conduit) {
    World world = conduit.getBundle().getEntity().getWorldObj();
    if(world == null) {
      return;
    }
    if(world.isRemote || liquidType == null) {
      return;
    }

    long curTime = world.getTotalWorldTime();
    if(curTime > 0 && curTime != timeAtLastApply) {
      timeAtLastApply = curTime;
      ConduitNetworkTickHandler.instance.addListener(tickHandler);
    }
  }

  private void doTick() {

    List<LiquidConduit> cons = getConduits();
    if(cons == null || cons.isEmpty()) {
      return;
    }

    if(isEmpty()) {
      if(!fluidTypeLocked && liquidType != null) {
        ticksEmpty++;
        if(ticksEmpty > 40) {
          setFluidType(null);
          ticksEmpty = 0;
        }
      }
      return;
    }

    ticksEmpty = 0;
    long curTime = cons.get(0).getBundle().getEntity().getWorldObj().getTotalWorldTime();

    // 1000 water, 6000 lava
    if(liquidType != null && liquidType.getFluid() != null && !isEmpty()) {
      int visc = Math.max(1000, liquidType.getFluid().getViscosity());
      if(curTime % (visc / 500) == 0) {
        long start = System.nanoTime();
        if(doFlow() && printFlowTiming) {
          long took = System.nanoTime() - start;
          double secs = took / 1000000000.0;
          System.out.println("LiquidConduitNetwork.onUpdateEntity: took " + secs + " secs, " + (secs * 1000) + " millis");
        }
      }
    }
  }

  void addedFromExternal(int res) {
    inputVolume += res;
  }

  void outputedToExternal(int filled) {
    outputVolume += filled;
  }

  int getNextPushToken() {
    return ++pushToken;
  }

  private boolean doFlow() {

    int pushToken = getNextPushToken();
    List<FlowAction> actions = new ArrayList<FlowAction>();
    for (int i = 0; i < Math.min(maxFlowsPerTick, conduits.size()); i++) {

      if(lastFlowIndex >= conduits.size()) {
{'fraction_non_alphanumeric': 0.11435149654643131, 'fraction_numerical': 0.012893323100537223, 'mean_word_length': 3.426630434782609, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '3635448', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2069, 'n_words': 424}