diff --git a/src/main/java/techreborn/api/RollingMachineRecipe.java b/src/main/java/techreborn/api/RollingMachineRecipe.java new file mode 100644 index 000000000..723160a02 --- /dev/null +++ b/src/main/java/techreborn/api/RollingMachineRecipe.java @@ -0,0 +1,112 @@ +package techreborn.api; + +import net.minecraft.block.Block; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.item.crafting.ShapedRecipes; +import net.minecraft.item.crafting.ShapelessRecipes; +import net.minecraft.world.World; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class RollingMachineRecipe { + + private final List recipes = new ArrayList(); + + public static final RollingMachineRecipe instance = new RollingMachineRecipe(); + + public void addRecipe(ItemStack output, Object... components) { + String s = ""; + int i = 0; + int j = 0; + int k = 0; + if(components[i] instanceof String[]) { + String as[] = (String[])components[i++]; + for(int l = 0; l < as.length; l++) { + String s2 = as[l]; + k++; + j = s2.length(); + s = (new StringBuilder()).append(s).append(s2).toString(); + } + } else { + while(components[i] instanceof String) { + String s1 = (String)components[i++]; + k++; + j = s1.length(); + s = (new StringBuilder()).append(s).append(s1).toString(); + } + } + HashMap hashmap = new HashMap(); + for(; i < components.length; i += 2) { + Character character = (Character)components[i]; + ItemStack itemstack1 = null; + if(components[i + 1] instanceof Item) { + itemstack1 = new ItemStack((Item)components[i + 1]); + } else if(components[i + 1] instanceof Block) { + itemstack1 = new ItemStack((Block)components[i + 1], 1, -1); + } else if(components[i + 1] instanceof ItemStack) { + itemstack1 = (ItemStack)components[i + 1]; + } + hashmap.put(character, itemstack1); + } + + ItemStack recipeArray[] = new ItemStack[j * k]; + for(int i1 = 0; i1 < j * k; i1++) { + char c = s.charAt(i1); + if(hashmap.containsKey(Character.valueOf(c))) { + recipeArray[i1] = ((ItemStack)hashmap.get(Character.valueOf(c))).copy(); + } else { + recipeArray[i1] = null; + } + } + + recipes.add(new ShapedRecipes(j, k, recipeArray, output)); + } + + public void addShapelessRecipe(ItemStack output, Object... components) { + List ingredients = new ArrayList(); + for(int j = 0; j < components.length; j++) { + Object obj = components[j]; + if(obj instanceof ItemStack) { + ingredients.add(((ItemStack)obj).copy()); + continue; + } + if(obj instanceof Item) { + ingredients.add(new ItemStack((Item)obj)); + continue; + } + if(obj instanceof Block) { + ingredients.add(new ItemStack((Block)obj)); + } else { + throw new RuntimeException("Invalid shapeless recipe!"); + } + } + + recipes.add(new ShapelessRecipes(output, ingredients)); + } + + + public ItemStack findMatchingRecipe(InventoryCrafting inv, World world) { + for(int k = 0; k < recipes.size(); k++) { + IRecipe irecipe = (IRecipe)recipes.get(k); + if(irecipe.matches(inv, world)) { + return irecipe.getCraftingResult(inv); + } + } + + return null; + } + + + public List getRecipeList() { + return recipes; + } + + +} + + diff --git a/src/main/java/techreborn/api/RollingMachineRecipie.java b/src/main/java/techreborn/api/RollingMachineRecipie.java deleted file mode 100644 index 0355a02b0..000000000 --- a/src/main/java/techreborn/api/RollingMachineRecipie.java +++ /dev/null @@ -1,119 +0,0 @@ -package techreborn.api; - -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; - -public class RollingMachineRecipie { - ItemStack inputItem1, inputItem2, inputItem3, inputItem4, inputItem5, inputItem6, - inputItem7, inputItem8, inputItem9; - ItemStack output1; - int tickTime; - - - public RollingMachineRecipie(ItemStack inputItem1, ItemStack inputItem2, ItemStack inputItem3, ItemStack inputItem4, - ItemStack inputItem5, ItemStack inputItem6, ItemStack inputItem7, ItemStack inputItem8, ItemStack inputItem9, - ItemStack output1, int tickTime) { - this.inputItem1 = inputItem1; - this.inputItem2 = inputItem2; - this.inputItem3 = inputItem3; - this.inputItem4 = inputItem4; - this.inputItem5 = inputItem5; - this.inputItem6 = inputItem6; - this.inputItem7 = inputItem7; - this.inputItem8 = inputItem8; - this.inputItem9 = inputItem9; - this.output1 = output1; - this.tickTime = tickTime; - - } - - public RollingMachineRecipie(Item inputItem1, Item inputItem2, Item inputItem3, Item inputItem4, Item inputItem5, - Item inputItem6, Item inputItem7, Item inputItem8, Item inputItem9, int inputAmount, - Item output1, int tickTime) { - if (inputItem1 != null) - this.inputItem1 = new ItemStack(inputItem1, inputAmount); - if (inputItem2 != null) - this.inputItem2 = new ItemStack(inputItem2, inputAmount); - if (inputItem3 != null) - this.inputItem3 = new ItemStack(inputItem3, inputAmount); - if (inputItem4 != null) - this.inputItem4 = new ItemStack(inputItem4, inputAmount); - if (inputItem5 != null) - this.inputItem5 = new ItemStack(inputItem5, inputAmount); - if (inputItem6 != null) - this.inputItem6 = new ItemStack(inputItem6, inputAmount); - if (inputItem7 != null) - this.inputItem7 = new ItemStack(inputItem7, inputAmount); - if (inputItem8 != null) - this.inputItem8 = new ItemStack(inputItem8, inputAmount); - if (inputItem9 != null) - this.inputItem9 = new ItemStack(inputItem9, inputAmount); - - this.output1 = new ItemStack(output1); - this.tickTime = tickTime; - ; - } - - public RollingMachineRecipie(RollingMachineRecipie rollingmachineRecipie) { - this.inputItem1 = rollingmachineRecipie.getInputItem1(); - this.inputItem2 = rollingmachineRecipie.getInputItem2(); - this.inputItem3 = rollingmachineRecipie.getInputItem3(); - this.inputItem4 = rollingmachineRecipie.getInputItem4(); - this.inputItem5 = rollingmachineRecipie.getInputItem5(); - this.inputItem6 = rollingmachineRecipie.getInputItem6(); - this.inputItem7 = rollingmachineRecipie.getInputItem7(); - this.inputItem8 = rollingmachineRecipie.getInputItem8(); - this.inputItem9 = rollingmachineRecipie.getInputItem9(); - - this.output1 = rollingmachineRecipie.getOutput1(); - this.tickTime = rollingmachineRecipie.getTickTime(); - } - - public ItemStack getInputItem1() { - return inputItem1; - } - - public ItemStack getInputItem2() { - return inputItem2; - } - - public ItemStack getInputItem3() { - return inputItem3; - } - - public ItemStack getInputItem4() { - return inputItem4; - } - - public ItemStack getInputItem5() { - return inputItem5; - } - - public ItemStack getInputItem6() { - return inputItem6; - } - - public ItemStack getInputItem7() { - return inputItem7; - } - - public ItemStack getInputItem8() { - return inputItem8; - } - - public ItemStack getInputItem9() { - return inputItem9; - } - - public ItemStack getOutput1() { - return output1; - } - - public int getTickTime() { - return tickTime; - } - - -} - - diff --git a/src/main/java/techreborn/api/TechRebornAPI.java b/src/main/java/techreborn/api/TechRebornAPI.java index f2aece986..99c163b96 100644 --- a/src/main/java/techreborn/api/TechRebornAPI.java +++ b/src/main/java/techreborn/api/TechRebornAPI.java @@ -1,5 +1,6 @@ package techreborn.api; +import net.minecraft.item.ItemStack; import techreborn.util.ItemUtils; import java.util.ArrayList; @@ -7,7 +8,7 @@ import java.util.ArrayList; public final class TechRebornAPI { public static ArrayList centrifugeRecipies = new ArrayList(); - public static ArrayList rollingmachineRecipes = new ArrayList(); + public static ArrayList rollingmachineRecipes = new ArrayList(); public static void registerCentrifugeRecipe(CentrifugeRecipie recipie) { @@ -26,21 +27,14 @@ public final class TechRebornAPI { centrifugeRecipies.add(recipie); } - public static void registerRollingMachineRecipe(RollingMachineRecipie recipie) { - boolean shouldAdd = true; - for (CentrifugeRecipie centrifugeRecipie : centrifugeRecipies) { - if (ItemUtils.isItemEqual(centrifugeRecipie.getInputItem(), recipie.getInputItem1(), false, true)) { - try { - throw new RegisteredItemRecipe("Item " + recipie.getInputItem1().getUnlocalizedName() + " is already being used in a recipe for the RollingMachine"); - } catch (RegisteredItemRecipe registeredItemRecipe) { - registeredItemRecipe.printStackTrace(); - shouldAdd = false; - } - } - } - if (shouldAdd) - rollingmachineRecipes.add(recipie); + public static void addRollingMachinceRecipe(ItemStack output, Object... components) { + RollingMachineRecipe.instance.addRecipe(output, components); } + + public void addShapelessRollingMachinceRecipe(ItemStack output, Object... components) { + RollingMachineRecipe.instance.addShapelessRecipe(output, components); + } + } diff --git a/src/main/java/techreborn/client/container/ContainerRollingMachine.java b/src/main/java/techreborn/client/container/ContainerRollingMachine.java index e1ea5ff6e..73c014ad4 100644 --- a/src/main/java/techreborn/client/container/ContainerRollingMachine.java +++ b/src/main/java/techreborn/client/container/ContainerRollingMachine.java @@ -1,7 +1,11 @@ package techreborn.client.container; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import techreborn.api.RollingMachineRecipe; +import techreborn.client.SlotFake; import techreborn.client.SlotOutput; import techreborn.tiles.TileRollingMachine; @@ -14,18 +18,18 @@ public class ContainerRollingMachine extends TechRebornContainer { tile = tileRollingmachine; this.player = player; - //input - this.addSlotToContainer(new Slot(tileRollingmachine.inventory, 0, 30, 17)); - this.addSlotToContainer(new Slot(tileRollingmachine.inventory, 1, 30, 35)); - this.addSlotToContainer(new Slot(tileRollingmachine.inventory, 2, 30, 53)); - this.addSlotToContainer(new Slot(tileRollingmachine.inventory, 3, 48, 17)); - this.addSlotToContainer(new Slot(tileRollingmachine.inventory, 4, 48, 35)); - this.addSlotToContainer(new Slot(tileRollingmachine.inventory, 5, 48, 53)); - this.addSlotToContainer(new Slot(tileRollingmachine.inventory, 6, 66, 17)); - this.addSlotToContainer(new Slot(tileRollingmachine.inventory, 7, 66, 35)); - this.addSlotToContainer(new Slot(tileRollingmachine.inventory, 8, 66, 53)); - //outputs - this.addSlotToContainer(new SlotOutput(tileRollingmachine.inventory, 9, 124, 35)); + + for (int l = 0; l < 3; l++) { + for (int k1 = 0; k1 < 3; k1++) { + this.addSlotToContainer(new Slot(tileRollingmachine.craftMatrix, k1 + l * 3, 30 + k1 * 18, 17 + l * 18)); + } + } + + //output + this.addSlotToContainer(new SlotOutput(tileRollingmachine.inventory, 0, 124, 35)); + + //fakeOutput + this.addSlotToContainer(new SlotFake(tileRollingmachine.inventory, 1, 124, 10, false, false, 1)); int i; @@ -46,4 +50,11 @@ public class ContainerRollingMachine extends TechRebornContainer { return true; } + @Override + public final void onCraftMatrixChanged(IInventory inv) { + ItemStack output = RollingMachineRecipe.instance.findMatchingRecipe(tile.craftMatrix, tile.getWorldObj()); + tile.inventory.setInventorySlotContents(1, output); + } + + } diff --git a/src/main/java/techreborn/init/ModRecipes.java b/src/main/java/techreborn/init/ModRecipes.java index 069c410e6..803fb1ed3 100644 --- a/src/main/java/techreborn/init/ModRecipes.java +++ b/src/main/java/techreborn/init/ModRecipes.java @@ -6,7 +6,6 @@ import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import techreborn.api.CentrifugeRecipie; -import techreborn.api.RollingMachineRecipie; import techreborn.api.TechRebornAPI; import techreborn.config.ConfigTechReborn; import techreborn.util.CraftingHelper; @@ -237,7 +236,7 @@ public class ModRecipes { public static void addMachineRecipes() { TechRebornAPI.registerCentrifugeRecipe(new CentrifugeRecipie(Items.apple, 4, Items.beef, Items.baked_potato, null, null, 120, 4)); TechRebornAPI.registerCentrifugeRecipe(new CentrifugeRecipie(Items.nether_star, 1, Items.diamond, Items.emerald, Items.bed, Items.cake, 500, 8)); - TechRebornAPI.registerRollingMachineRecipe(new RollingMachineRecipie(new ItemStack(Blocks.cobblestone), new ItemStack(Blocks.cobblestone), new ItemStack(Blocks.cobblestone), new ItemStack(Blocks.cobblestone), new ItemStack(Blocks.cobblestone), new ItemStack(Blocks.cobblestone), new ItemStack(Blocks.cobblestone), new ItemStack(Blocks.cobblestone), new ItemStack(Blocks.cobblestone), new ItemStack(Blocks.furnace), 4)); + TechRebornAPI.addRollingMachinceRecipe(new ItemStack(Blocks.furnace, 4), "ccc", "c c", "ccc", 'c', Blocks.cobblestone); LogHelper.info("Machine Recipes Added"); } diff --git a/src/main/java/techreborn/tiles/TileRollingMachine.java b/src/main/java/techreborn/tiles/TileRollingMachine.java index 63f2f714d..c10c0c705 100644 --- a/src/main/java/techreborn/tiles/TileRollingMachine.java +++ b/src/main/java/techreborn/tiles/TileRollingMachine.java @@ -3,22 +3,38 @@ package techreborn.tiles; import ic2.api.energy.prefab.BasicSink; import ic2.api.tile.IWrenchable; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; -import techreborn.api.RollingMachineRecipie; +import net.minecraft.nbt.NBTTagCompound; +import techreborn.api.RollingMachineRecipe; import techreborn.init.ModBlocks; import techreborn.util.Inventory; +import techreborn.util.ItemUtils; +//TODO add tick and power bars. public class TileRollingMachine extends TileMachineBase implements IWrenchable { public BasicSink energy; - public Inventory inventory = new Inventory(10, "TileRollingMachine", 64); + public Inventory inventory = new Inventory(2, "TileRollingMachine", 64); + public final InventoryCrafting craftMatrix = new InventoryCrafting(new RollingTileContainer(), 3, 3); public boolean isRunning; public int tickTime; - public RollingMachineRecipie currentRecipe; + public int runTime = 250; + public ItemStack currentRecipe; public int euTick = 5; + private static class RollingTileContainer extends Container { + + @Override + public boolean canInteractWith(EntityPlayer entityplayer) { + return true; + } + + } + public TileRollingMachine() { energy = new BasicSink(this, 100000, 1); } @@ -27,6 +43,58 @@ public class TileRollingMachine extends TileMachineBase implements IWrenchable { public void updateEntity() { super.updateEntity(); energy.updateEntity(); + if(!worldObj.isRemote){ + currentRecipe = RollingMachineRecipe.instance.findMatchingRecipe(craftMatrix, worldObj); + if(currentRecipe != null && canMake()){ + if(tickTime >= runTime){ + currentRecipe = RollingMachineRecipe.instance.findMatchingRecipe(craftMatrix, worldObj); + if (currentRecipe != null) { + boolean hasCrafted = false; + if(inventory.getStackInSlot(0) == null){ + inventory.setInventorySlotContents(0, currentRecipe); + tickTime = 0; + hasCrafted = true; + } else { + if(inventory.getStackInSlot(0).stackSize + currentRecipe.stackSize <= currentRecipe.getMaxStackSize()){ + ItemStack stack = inventory.getStackInSlot(0); + stack.stackSize = stack.stackSize + currentRecipe.stackSize; + inventory.setInventorySlotContents(0, stack); + tickTime = 0; + hasCrafted = true; + } + } + if(hasCrafted){ + for (int i = 0; i < craftMatrix.getSizeInventory(); i++) { + craftMatrix.decrStackSize(i, 1); + } + currentRecipe = null; + } + } + } + } + if(currentRecipe != null) { + if(energy.canUseEnergy(euTick) && tickTime < runTime){ + tickTime ++; + } + } + if(currentRecipe == null){ + tickTime = 0; + } + } else { + currentRecipe = RollingMachineRecipe.instance.findMatchingRecipe(craftMatrix, worldObj); + if(currentRecipe != null){ + inventory.setInventorySlotContents(1, currentRecipe); + } else { + inventory.setInventorySlotContents(1, null); + } + } + } + + public boolean canMake() { + if (RollingMachineRecipe.instance.findMatchingRecipe(craftMatrix, worldObj) == null){ + return false; + } + return true; } @Override @@ -58,4 +126,25 @@ public class TileRollingMachine extends TileMachineBase implements IWrenchable { return new ItemStack(ModBlocks.RollingMachine, 1); } + @Override + public void readFromNBT(NBTTagCompound tagCompound) { + super.readFromNBT(tagCompound); + inventory.readFromNBT(tagCompound); + ItemUtils.readInvFromNBT(craftMatrix, "Crafting", tagCompound); + isRunning = tagCompound.getBoolean("isRunning"); + tickTime = tagCompound.getInteger("tickTime"); + } + + @Override + public void writeToNBT(NBTTagCompound tagCompound) { + super.writeToNBT(tagCompound); + inventory.writeToNBT(tagCompound); + ItemUtils.writeInvToNBT(craftMatrix, "Crafting", tagCompound); + writeUpdateToNBT(tagCompound); + } + + public void writeUpdateToNBT(NBTTagCompound tagCompound) { + tagCompound.setBoolean("isRunning", isRunning); + tagCompound.setInteger("tickTime", tickTime); + } } diff --git a/src/main/java/techreborn/util/ItemUtils.java b/src/main/java/techreborn/util/ItemUtils.java index 9e9330880..cefea15ec 100644 --- a/src/main/java/techreborn/util/ItemUtils.java +++ b/src/main/java/techreborn/util/ItemUtils.java @@ -1,6 +1,9 @@ package techreborn.util; +import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraftforge.oredict.OreDictionary; /** @@ -30,4 +33,42 @@ public class ItemUtils { public static boolean isWildcard(int damage) { return damage == -1 || damage == OreDictionary.WILDCARD_VALUE; } + + public static void writeInvToNBT(IInventory inv, String tag, NBTTagCompound data) { + NBTTagList list = new NBTTagList(); + for (byte slot = 0; slot < inv.getSizeInventory(); slot++) { + ItemStack stack = inv.getStackInSlot(slot); + if (stack != null) { + NBTTagCompound itemTag = new NBTTagCompound(); + itemTag.setByte("Slot", slot); + writeItemToNBT(stack, itemTag); + list.appendTag(itemTag); + } + } + data.setTag(tag, list); + } + + public static void readInvFromNBT(IInventory inv, String tag, NBTTagCompound data) { + NBTTagList list = data.getTagList(tag, 10); + for (byte entry = 0; entry < list.tagCount(); entry++) { + NBTTagCompound itemTag = list.getCompoundTagAt(entry); + int slot = itemTag.getByte("Slot"); + if (slot >= 0 && slot < inv.getSizeInventory()) { + ItemStack stack = readItemFromNBT(itemTag); + inv.setInventorySlotContents(slot, stack); + } + } + } + + public static void writeItemToNBT(ItemStack stack, NBTTagCompound data) { + if (stack == null || stack.stackSize <= 0) + return; + if (stack.stackSize > 127) + stack.stackSize = 127; + stack.writeToNBT(data); + } + + public static ItemStack readItemFromNBT(NBTTagCompound data) { + return ItemStack.loadItemStackFromNBT(data); + } }