<commit_before>package crazypants.enderio.machine.crafter;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.nbt.NBTTagCompound;
import crazypants.enderio.Config;
import crazypants.enderio.ModObject;
import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.enderio.machine.SlotDefinition;
import crazypants.enderio.power.Capacitors;

public class TileCrafter extends AbstractMachineEntity {

  DummyCraftingGrid craftingGrid = new DummyCraftingGrid();
  
  private List<ItemStack> containerItems;
  
  public TileCrafter() {
    super(new SlotDefinition(9, 1));
    
    containerItems = new ArrayList<ItemStack>();
  }

  @Override
  public String getMachineName() {
    return ModObject.blockCrafter.unlocalisedName;
  }

  @Override
  protected boolean isMachineItemValidForSlot(int slot, ItemStack itemstack) {
    if(!slotDefinition.isInputSlot(slot)) {
      return false;
    }
    return craftingGrid.inv[slot] != null && craftingGrid.inv[slot].isItemEqual(itemstack);
  }

  @Override
  public boolean isActive() {
    return false;
  }

  @Override
  public float getProgress() {
    return 0.5f;
  }

  @Override
  protected boolean processTasks(boolean redstoneCheckPassed) {  
    if(!redstoneCheckPassed || !craftingGrid.hasValidRecipe() || !canMergeOutput() || !hasPower()) {      
      return false;
    }
    if(capacitorType == Capacitors.BASIC_CAPACITOR && worldObj.getTotalWorldTime() % 20 != 0) {
      return false;
    } else if(capacitorType == Capacitors.ACTIVATED_CAPACITOR && worldObj.getTotalWorldTime() % 10 != 0) {
      return false;
    } else if(worldObj.getTotalWorldTime() % 2 != 0) {
      return false;
    }
    
    // process buffered container items
    if(!containerItems.isEmpty()) {
      for(int i = 0; i < containerItems.size(); i++) {
	ItemStack stack = containerItems.get(i);
	if(inventory[9] == null) {
	  inventory[9] = stack;
	  containerItems.remove(i);
	} else if(ItemStack.areItemStacksEqual(inventory[9], stack) && inventory[9].stackSize + stack.stackSize <= inventory[9].getMaxStackSize()) {
	  inventory[9].stackSize += stack.stackSize;
	  containerItems.remove(i);
	}
      }
      return false;
    }
    
    List<ItemStack> required = new ArrayList<ItemStack>();
    craftingGrid.copyRequiredInputs(required);
    if(hasRequiredInput(required)) {      
      craftRecipe();
      double used = Math.min(powerHandler.getEnergyStored(), Config.crafterMjPerCraft);    
      powerHandler.setEnergy(powerHandler.getEnergyStored() - used);
    }     
    return false;
  }

  private void craftRecipe() {    
    List<ItemStack> required = new ArrayList<ItemStack>();
    craftingGrid.copyRequiredInputs(required);
    for(ItemStack req : required) {
      for(int i=0;i<9 && req.stackSize > 0; i++) {
        ItemStack avail = inventory[i];
        if(avail != null && avail.stackSize > 0 && avail.isItemEqual(req)) {<commit_msg>Switch to iterator in Crafter to make MatthiasM2 happy<commit_after>package crazypants.enderio.machine.crafter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.nbt.NBTTagCompound;
import crazypants.enderio.Config;
import crazypants.enderio.ModObject;
import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.enderio.machine.SlotDefinition;
import crazypants.enderio.power.Capacitors;

public class TileCrafter extends AbstractMachineEntity {

  DummyCraftingGrid craftingGrid = new DummyCraftingGrid();
  
  private List<ItemStack> containerItems;
  
  public TileCrafter() {
    super(new SlotDefinition(9, 1));
    
    containerItems = new ArrayList<ItemStack>();
  }

  @Override
  public String getMachineName() {
    return ModObject.blockCrafter.unlocalisedName;
  }

  @Override
  protected boolean isMachineItemValidForSlot(int slot, ItemStack itemstack) {
    if(!slotDefinition.isInputSlot(slot)) {
      return false;
    }
    return craftingGrid.inv[slot] != null && craftingGrid.inv[slot].isItemEqual(itemstack);
  }

  @Override
  public boolean isActive() {
    return false;
  }

  @Override
  public float getProgress() {
    return 0.5f;
  }

  @Override
  protected boolean processTasks(boolean redstoneCheckPassed) {  
    if(!redstoneCheckPassed || !craftingGrid.hasValidRecipe() || !canMergeOutput() || !hasPower()) {      
      return false;
    }
    if(capacitorType == Capacitors.BASIC_CAPACITOR && worldObj.getTotalWorldTime() % 20 != 0) {
      return false;
    } else if(capacitorType == Capacitors.ACTIVATED_CAPACITOR && worldObj.getTotalWorldTime() % 10 != 0) {
      return false;
    } else if(worldObj.getTotalWorldTime() % 2 != 0) {
      return false;
    }
    
    // process buffered container items
    if(!containerItems.isEmpty()) {
      Iterator<ItemStack> iter = containerItems.iterator();
      while (iter.hasNext()) {
	ItemStack stack = iter.next();
	if(inventory[9] == null) {
	  inventory[9] = stack;
	  iter.remove();
	} else if(ItemStack.areItemStacksEqual(inventory[9], stack) && inventory[9].stackSize + stack.stackSize <= inventory[9].getMaxStackSize()) {
	  inventory[9].stackSize += stack.stackSize;
	  iter.remove();
	}
      }
      return false;
    }
    
    List<ItemStack> required = new ArrayList<ItemStack>();
    craftingGrid.copyRequiredInputs(required);
    if(hasRequiredInput(required)) {      
      craftRecipe();
      double used = Math.min(powerHandler.getEnergyStored(), Config.crafterMjPerCraft);    
      powerHandler.setEnergy(powerHandler.getEnergyStored() - used);
    }     
    return false;
  }

  private void craftRecipe() {    
    List<ItemStack> required = new ArrayList<ItemStack>();
    craftingGrid.copyRequiredInputs(required);
    for(ItemStack req : required) {
      for(int i=0;i<9 && req.stackSize > 0; i++) {
        ItemStack avail = inventory[i];
        if(avail != null && avail.stackSize > 0 && avail.isItemEqual(req)) {
{'fraction_non_alphanumeric': 0.11674491392801252, 'fraction_numerical': 0.007198748043818467, 'mean_word_length': 3.9619565217391304, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 20, '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': '828747', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1989, 'n_words': 371}