diff --git a/src/main/java/techreborn/api/recipe/RecipeCrafter.java b/src/main/java/techreborn/api/recipe/RecipeCrafter.java index d5fd88371..c58dd3273 100644 --- a/src/main/java/techreborn/api/recipe/RecipeCrafter.java +++ b/src/main/java/techreborn/api/recipe/RecipeCrafter.java @@ -1,10 +1,14 @@ package techreborn.api.recipe; import ic2.api.energy.prefab.BasicSink; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import techreborn.tiles.TileMachineBase; import techreborn.util.Inventory; import techreborn.util.ItemUtils; +import java.util.ArrayList; + /** * Use this in your tile entity to craft things */ @@ -52,6 +56,7 @@ public class RecipeCrafter { /** * This is the constructor, not a lot to say here :P + * * @param recipeName * @param parentTile * @param energy @@ -79,38 +84,98 @@ public class RecipeCrafter { /** * Call this on the tile tick */ - public void updateEntity(){ - if(currentRecipe == null){ - for(IBaseRecipeType recipe : RecipeHanderer.getRecipeClassFromName(recipeName)){ + public void updateEntity() { + if (parentTile.getWorldObj().isRemote) { + return; + } + if (currentRecipe == null) { + for (IBaseRecipeType recipe : RecipeHanderer.getRecipeClassFromName(recipeName)) { boolean isFullRecipe = false; for (int i = 0; i < inputs; i++) { - if(ItemUtils.isItemEqual(inventory.getStackInSlot(inputSlots[i]), recipe.getInputs().get(i), true, true)){ + if (ItemUtils.isItemEqual(inventory.getStackInSlot(inputSlots[i]), recipe.getInputs().get(i), true, true)) { isFullRecipe = true; } else { isFullRecipe = false; } } - if(isFullRecipe){ + if (isFullRecipe) { currentRecipe = recipe; return; } } } else { for (int i = 0; i < inputs; i++) { - if(!ItemUtils.isItemEqual(inventory.getStackInSlot(inputSlots[i]), currentRecipe.getInputs().get(i), true, true)){ + if (!ItemUtils.isItemEqual(inventory.getStackInSlot(inputSlots[i]), currentRecipe.getInputs().get(i), true, true)) { currentRecipe = null; currentTickTime = 0; return; } } - if(currentTickTime >= currentRecipe.tickTime()){ - //TODO give the player the goodies :) - //Need some nicer way of added the crafting things. - } else { - if(energy.useEnergy(currentRecipe.euPerTick())){ - currentTickTime ++; + 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; + } + } + ArrayList filledSlots = new ArrayList(); + 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; + } + if (ItemUtils.isItemEqual(inventory.getStackInSlot(slot), stack, true, true)) { + if (stack.stackSize + inventory.getStackInSlot(slot).stackSize <= stack.getMaxStackSize()) { + return true; + } + } + return false; + } + + public void fitStack(ItemStack stack, int slot) { + if (stack == null) { + return; + } + if (inventory.getStackInSlot(slot) == null) { + inventory.setInventorySlotContents(slot, stack); + } + if (ItemUtils.isItemEqual(inventory.getStackInSlot(slot), stack, true, true)) { + if (stack.stackSize + inventory.getStackInSlot(slot).stackSize <= stack.getMaxStackSize()) { + inventory.decrStackSize(slot, -stack.stackSize); + } + } + } + + public void readFromNBT(NBTTagCompound tagCompound) { + } + + public void writeToNBT(NBTTagCompound tagCompound) { + + } } diff --git a/src/main/java/techreborn/tiles/TileBlastFurnace.java b/src/main/java/techreborn/tiles/TileBlastFurnace.java index 1c3cf66bc..e99c0a78d 100644 --- a/src/main/java/techreborn/tiles/TileBlastFurnace.java +++ b/src/main/java/techreborn/tiles/TileBlastFurnace.java @@ -265,12 +265,14 @@ public class TileBlastFurnace extends TileMachineBase implements IWrenchable, II super.readFromNBT(tagCompound); inventory.readFromNBT(tagCompound); tickTime = tagCompound.getInteger("tickTime"); + energy.readFromNBT(tagCompound); } @Override public void writeToNBT(NBTTagCompound tagCompound) { super.writeToNBT(tagCompound); inventory.writeToNBT(tagCompound); + energy.writeToNBT(tagCompound); writeUpdateToNBT(tagCompound); } diff --git a/src/main/java/techreborn/tiles/TileCentrifuge.java b/src/main/java/techreborn/tiles/TileCentrifuge.java index 4030cf3ae..6a3db8ed9 100644 --- a/src/main/java/techreborn/tiles/TileCentrifuge.java +++ b/src/main/java/techreborn/tiles/TileCentrifuge.java @@ -255,6 +255,7 @@ public class TileCentrifuge extends TileMachineBase implements IInventory, { super.readFromNBT(tagCompound); inventory.readFromNBT(tagCompound); + energy.readFromNBT(tagCompound); String recipeName = tagCompound.getString("recipe"); for (CentrifugeRecipie recipie : TechRebornAPI.centrifugeRecipies) { @@ -274,6 +275,7 @@ public class TileCentrifuge extends TileMachineBase implements IInventory, { super.writeToNBT(tagCompound); inventory.writeToNBT(tagCompound); + energy.writeToNBT(tagCompound); writeUpdateToNBT(tagCompound); } diff --git a/src/main/java/techreborn/tiles/TileImplosionCompressor.java b/src/main/java/techreborn/tiles/TileImplosionCompressor.java index b4f3d8a02..1d3b7e86e 100644 --- a/src/main/java/techreborn/tiles/TileImplosionCompressor.java +++ b/src/main/java/techreborn/tiles/TileImplosionCompressor.java @@ -2,6 +2,9 @@ package techreborn.tiles; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import techreborn.api.CentrifugeRecipie; +import techreborn.api.TechRebornAPI; import techreborn.api.recipe.RecipeCrafter; import techreborn.init.ModBlocks; import techreborn.util.Inventory; @@ -66,5 +69,23 @@ public class TileImplosionCompressor extends TileMachineBase implements IWrencha public void updateEntity() { super.updateEntity(); crafter.updateEntity(); + energy.updateEntity(); } + + @Override + public void readFromNBT(NBTTagCompound tagCompound) + { + super.readFromNBT(tagCompound); + inventory.readFromNBT(tagCompound); + energy.readFromNBT(tagCompound); + } + + @Override + public void writeToNBT(NBTTagCompound tagCompound) + { + super.writeToNBT(tagCompound); + inventory.writeToNBT(tagCompound); + energy.writeToNBT(tagCompound); + } + } diff --git a/src/main/java/techreborn/tiles/TileRollingMachine.java b/src/main/java/techreborn/tiles/TileRollingMachine.java index b2b7f3e11..715b82224 100644 --- a/src/main/java/techreborn/tiles/TileRollingMachine.java +++ b/src/main/java/techreborn/tiles/TileRollingMachine.java @@ -166,6 +166,7 @@ public class TileRollingMachine extends TileMachineBase implements IWrenchable { { super.readFromNBT(tagCompound); inventory.readFromNBT(tagCompound); + energy.readFromNBT(tagCompound); ItemUtils.readInvFromNBT(craftMatrix, "Crafting", tagCompound); isRunning = tagCompound.getBoolean("isRunning"); tickTime = tagCompound.getInteger("tickTime"); @@ -176,6 +177,7 @@ public class TileRollingMachine extends TileMachineBase implements IWrenchable { { super.writeToNBT(tagCompound); inventory.writeToNBT(tagCompound); + energy.writeToNBT(tagCompound); ItemUtils.writeInvToNBT(craftMatrix, "Crafting", tagCompound); writeUpdateToNBT(tagCompound); }