2015-05-07 21:38:11 +02:00
|
|
|
package techreborn.api.recipe;
|
|
|
|
|
|
|
|
import ic2.api.energy.prefab.BasicSink;
|
2015-05-08 19:42:12 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-05-07 21:38:11 +02:00
|
|
|
import techreborn.tiles.TileMachineBase;
|
|
|
|
import techreborn.util.Inventory;
|
2015-05-07 22:12:07 +02:00
|
|
|
import techreborn.util.ItemUtils;
|
2015-05-07 21:38:11 +02:00
|
|
|
|
2015-05-08 19:42:12 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2015-05-07 21:38:11 +02:00
|
|
|
/**
|
|
|
|
* Use this in your tile entity to craft things
|
|
|
|
*/
|
|
|
|
public class RecipeCrafter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the recipe type to use
|
|
|
|
*/
|
|
|
|
public String recipeName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the parent tile
|
|
|
|
*/
|
|
|
|
public TileMachineBase parentTile;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the place to use the power from
|
|
|
|
*/
|
|
|
|
public BasicSink energy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the amount of inputs that the setRecipe has
|
|
|
|
*/
|
|
|
|
public int inputs;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the amount of outputs that the recipe has
|
|
|
|
*/
|
|
|
|
public int outputs;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the inventory to use for the crafting
|
|
|
|
*/
|
|
|
|
public Inventory inventory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the list of the slots that the crafting logic should look for the input item stacks.
|
|
|
|
*/
|
|
|
|
public int[] inputSlots;
|
|
|
|
|
|
|
|
/**
|
2015-05-07 22:12:07 +02:00
|
|
|
* This is the list for the slots that the crafting logic should look fot the output item stacks.
|
2015-05-07 21:38:11 +02:00
|
|
|
*/
|
|
|
|
public int[] outputSlots;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the constructor, not a lot to say here :P
|
2015-05-08 19:42:12 +02:00
|
|
|
*
|
2015-05-07 21:38:11 +02:00
|
|
|
* @param recipeName
|
|
|
|
* @param parentTile
|
|
|
|
* @param energy
|
|
|
|
* @param inputs
|
|
|
|
* @param outputs
|
|
|
|
* @param inventory
|
|
|
|
* @param inputSlots
|
|
|
|
* @param outputSlots
|
|
|
|
*/
|
|
|
|
public RecipeCrafter(String recipeName, TileMachineBase parentTile, BasicSink energy, int inputs, int outputs, Inventory inventory, int[] inputSlots, int[] outputSlots) {
|
|
|
|
this.recipeName = recipeName;
|
|
|
|
this.parentTile = parentTile;
|
|
|
|
this.energy = energy;
|
|
|
|
this.inputs = inputs;
|
|
|
|
this.outputs = outputs;
|
|
|
|
this.inventory = inventory;
|
|
|
|
this.inputSlots = inputSlots;
|
|
|
|
this.outputSlots = outputSlots;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-07 22:12:07 +02:00
|
|
|
IBaseRecipeType currentRecipe;
|
|
|
|
int currentTickTime = 0;
|
|
|
|
|
2015-05-07 21:38:11 +02:00
|
|
|
/**
|
|
|
|
* Call this on the tile tick
|
|
|
|
*/
|
2015-05-08 19:42:12 +02:00
|
|
|
public void updateEntity() {
|
|
|
|
if (parentTile.getWorldObj().isRemote) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (currentRecipe == null) {
|
|
|
|
for (IBaseRecipeType recipe : RecipeHanderer.getRecipeClassFromName(recipeName)) {
|
2015-05-07 22:12:07 +02:00
|
|
|
boolean isFullRecipe = false;
|
|
|
|
for (int i = 0; i < inputs; i++) {
|
2015-05-08 22:07:59 +02:00
|
|
|
if (ItemUtils.isItemEqual(inventory.getStackInSlot(inputSlots[i]), recipe.getInputs().get(i), true, true, true)) {
|
2015-05-07 22:12:07 +02:00
|
|
|
isFullRecipe = true;
|
|
|
|
} else {
|
|
|
|
isFullRecipe = false;
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 19:42:12 +02:00
|
|
|
if (isFullRecipe) {
|
2015-05-07 22:12:07 +02:00
|
|
|
currentRecipe = recipe;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < inputs; i++) {
|
2015-05-08 22:07:59 +02:00
|
|
|
if (!ItemUtils.isItemEqual(inventory.getStackInSlot(inputSlots[i]), currentRecipe.getInputs().get(i), true, true, true)) {
|
2015-05-07 22:12:07 +02:00
|
|
|
currentRecipe = null;
|
|
|
|
currentTickTime = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 19:42:12 +02:00
|
|
|
if (currentTickTime >= currentRecipe.tickTime()) {
|
|
|
|
boolean canGiveInvAll = true;
|
|
|
|
for (int i = 0; i < currentRecipe.getOutputs().size(); i++) {
|
|
|
|
if (!canFitStack(currentRecipe.getOutputs().get(i), outputSlots[i])) {
|
|
|
|
canGiveInvAll = false;
|
|
|
|
}
|
2015-05-07 22:12:07 +02:00
|
|
|
}
|
2015-05-08 19:42:12 +02:00
|
|
|
ArrayList<Integer> filledSlots = new ArrayList<Integer>();
|
|
|
|
if (canGiveInvAll) {
|
|
|
|
for (int i = 0; i < currentRecipe.getOutputs().size(); i++) {
|
|
|
|
if (!filledSlots.contains(outputSlots[i])) {
|
|
|
|
fitStack(currentRecipe.getOutputs().get(i), outputSlots[i]);
|
|
|
|
filledSlots.add(outputSlots[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < inputs; i++) {
|
|
|
|
if (!filledSlots.contains(inputSlots[i])) {
|
|
|
|
inventory.decrStackSize(inputSlots[i], currentRecipe.getInputs().get(i).stackSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
currentRecipe = null;
|
|
|
|
currentTickTime = 0;
|
|
|
|
parentTile.syncWithAll();
|
|
|
|
}
|
|
|
|
} else if (currentTickTime < currentRecipe.tickTime()) {
|
|
|
|
if (energy.useEnergy(currentRecipe.euPerTick())) {
|
|
|
|
currentTickTime++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean canFitStack(ItemStack stack, int slot) {
|
|
|
|
if (stack == null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (inventory.getStackInSlot(slot) == null) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-05-08 22:07:59 +02:00
|
|
|
if (ItemUtils.isItemEqual(inventory.getStackInSlot(slot), stack, true, true, true)) {
|
2015-05-08 19:42:12 +02:00
|
|
|
if (stack.stackSize + inventory.getStackInSlot(slot).stackSize <= stack.getMaxStackSize()) {
|
|
|
|
return true;
|
2015-05-07 22:12:07 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-08 19:42:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void fitStack(ItemStack stack, int slot) {
|
|
|
|
if (stack == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (inventory.getStackInSlot(slot) == null) {
|
|
|
|
inventory.setInventorySlotContents(slot, stack);
|
2015-05-08 20:03:09 +02:00
|
|
|
return;
|
2015-05-08 19:42:12 +02:00
|
|
|
}
|
2015-05-08 22:07:59 +02:00
|
|
|
if (ItemUtils.isItemEqual(inventory.getStackInSlot(slot), stack, true, true, true)) {
|
2015-05-08 19:42:12 +02:00
|
|
|
if (stack.stackSize + inventory.getStackInSlot(slot).stackSize <= stack.getMaxStackSize()) {
|
2015-05-08 20:03:09 +02:00
|
|
|
ItemStack newStack = stack.copy();
|
|
|
|
newStack.stackSize = inventory.getStackInSlot(slot).stackSize + stack.stackSize;
|
|
|
|
inventory.setInventorySlotContents(slot, newStack);
|
2015-05-08 19:42:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 21:43:28 +02:00
|
|
|
public void readFromNBT(NBTTagCompound tag) {
|
|
|
|
NBTTagCompound data = tag.getCompoundTag("Crater");
|
|
|
|
|
|
|
|
currentTickTime = data.getInteger("currentTickTime");
|
2015-05-08 19:42:12 +02:00
|
|
|
}
|
|
|
|
|
2015-05-08 21:43:28 +02:00
|
|
|
public void writeToNBT(NBTTagCompound tag) {
|
|
|
|
|
|
|
|
NBTTagCompound data = new NBTTagCompound();
|
|
|
|
|
|
|
|
data.setDouble("currentTickTime", currentTickTime);
|
2015-05-08 19:42:12 +02:00
|
|
|
|
2015-05-08 21:43:28 +02:00
|
|
|
tag.setTag("Crater", data);
|
2015-05-07 21:38:11 +02:00
|
|
|
}
|
|
|
|
}
|