Package vazkii.botania.api.recipe

Source Code of vazkii.botania.api.recipe.RecipeBrew

/**
* This class was created by <Vazkii>. It's distributed as
* part of the Botania Mod. Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under a
* Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
* (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB)
*
* File Created @ [Nov 1, 2014, 8:52:00 PM (GMT)]
*/
package vazkii.botania.api.recipe;

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

import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
import vazkii.botania.api.brew.Brew;
import vazkii.botania.api.brew.IBrewContainer;

public class RecipeBrew {

  Brew brew;
  List<Object> inputs;

  public RecipeBrew(Brew brew, Object... inputs) {
    this.brew = brew;
   
    List<Object> inputsToSet = new ArrayList();
    for(Object obj : inputs) {
      if(obj instanceof String || obj instanceof ItemStack)
        inputsToSet.add(obj);
      else throw new IllegalArgumentException("Invalid input");
    }

    this.inputs = inputsToSet;
  }

  public boolean matches(IInventory inv) {
    List<Object> inputsMissing = new ArrayList(inputs);

    for(int i = 0; i < inv.getSizeInventory(); i++) {
      ItemStack stack = inv.getStackInSlot(i);
      if(stack == null)
        break;

      if(stack.getItem() instanceof IBrewContainer)
        continue;
       
      int stackIndex = -1, oredictIndex = -1;

      for(int j = 0; j < inputsMissing.size(); j++) {
        Object input = inputsMissing.get(j);
        if(input instanceof String) {
          List<ItemStack> validStacks = OreDictionary.getOres((String) input);
          boolean found = false;
          for(ItemStack ostack : validStacks) {
            ItemStack cstack = ostack.copy();
            if(cstack.getItemDamage() == Short.MAX_VALUE)
              cstack.setItemDamage(stack.getItemDamage());

            if(stack.isItemEqual(cstack)) {
              oredictIndex = j;
              found = true;
              break;
            }
          }


          if(found)
            break;
        } else if(input instanceof ItemStack && simpleAreStacksEqual((ItemStack) input, stack)) {
          stackIndex = j;
          break;
        }
      }

      if(stackIndex != -1)
        inputsMissing.remove(stackIndex);
      else if(oredictIndex != -1)
        inputsMissing.remove(oredictIndex);
      else return false;
    }

    return inputsMissing.isEmpty();
  }

  boolean simpleAreStacksEqual(ItemStack stack, ItemStack stack2) {
    return stack.getItem() == stack2.getItem() && stack.getItemDamage() == stack2.getItemDamage();
  }

  public List<Object> getInputs() {
    return new ArrayList(inputs);
  }
 
  public Brew getBrew() {
    return brew;
  }
 
  public int getManaUsage() {
    return brew.getManaCost();
  }

  public ItemStack getOutput(ItemStack stack) {
    if(stack == null || !(stack.getItem() instanceof IBrewContainer))
      return new ItemStack(Items.glass_bottle); // Fallback...
    IBrewContainer container = (IBrewContainer) stack.getItem();
   
    return container.getItemForBrew(brew, stack);
  }

}
TOP

Related Classes of vazkii.botania.api.recipe.RecipeBrew

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.