From 2ff03573d9e4f4c9c40a0e504b6b0214d2f3555b Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Mon, 10 Aug 2015 19:28:52 +0100 Subject: [PATCH] Added logic to alloy furnace --- .../container/ContainerAlloyFurnace.java | 44 ++++ .../client/gui/GuiAlloyFurnace.java | 14 ++ .../techreborn/tiles/TileAlloyFurnace.java | 220 +++++++++++++++++- 3 files changed, 277 insertions(+), 1 deletion(-) diff --git a/src/main/java/techreborn/client/container/ContainerAlloyFurnace.java b/src/main/java/techreborn/client/container/ContainerAlloyFurnace.java index fe22b7d87..98720f866 100644 --- a/src/main/java/techreborn/client/container/ContainerAlloyFurnace.java +++ b/src/main/java/techreborn/client/container/ContainerAlloyFurnace.java @@ -1,6 +1,7 @@ package techreborn.client.container; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.ICrafting; import net.minecraft.inventory.Slot; import techreborn.client.SlotOutput; import techreborn.tiles.TileAlloyFurnace; @@ -46,4 +47,47 @@ public class ContainerAlloyFurnace extends TechRebornContainer { } } + int currentItemBurnTime; + int burnTime; + int cookTime; + + @Override + public void addCraftingToCrafters(ICrafting crafting) { + super.addCraftingToCrafters(crafting); + crafting.sendProgressBarUpdate(this, 0, tile.currentItemBurnTime); + crafting.sendProgressBarUpdate(this, 1, tile.burnTime); + crafting.sendProgressBarUpdate(this, 2, tile.cookTime); + } + + @Override + public void detectAndSendChanges() { + for (int i = 0; i < this.crafters.size(); i++) { + ICrafting crafting = (ICrafting) this.crafters.get(i); + if (this.currentItemBurnTime != tile.currentItemBurnTime) { + crafting.sendProgressBarUpdate(this, 0, tile.currentItemBurnTime); + } + if (this.burnTime != tile.burnTime) { + crafting.sendProgressBarUpdate(this, 1, tile.burnTime); + } + if (this.cookTime != tile.cookTime) { + crafting.sendProgressBarUpdate(this, 2, tile.cookTime); + } + } + super.detectAndSendChanges(); + } + + @Override + public void updateProgressBar(int id, int value) { + super.updateProgressBar(id, value); + if(id == 0){ + this.currentItemBurnTime = value; + } else if(id ==1){ + this.burnTime = value; + } else if(id == 2){ + this.cookTime = value; + } + this.tile.currentItemBurnTime = this.currentItemBurnTime; + this.tile.burnTime = this.burnTime; + this.tile.cookTime = this.cookTime; + } } diff --git a/src/main/java/techreborn/client/gui/GuiAlloyFurnace.java b/src/main/java/techreborn/client/gui/GuiAlloyFurnace.java index 1c4c7d350..b0507148e 100644 --- a/src/main/java/techreborn/client/gui/GuiAlloyFurnace.java +++ b/src/main/java/techreborn/client/gui/GuiAlloyFurnace.java @@ -15,11 +15,15 @@ public class GuiAlloyFurnace extends GuiContainer { TileAlloyFurnace alloyfurnace; + ContainerAlloyFurnace containerAlloyFurnace; + public GuiAlloyFurnace(EntityPlayer player, TileAlloyFurnace tileAlloyFurnace) { super(new ContainerAlloyFurnace(tileAlloyFurnace, player)); this.xSize = 176; this.ySize = 167; + this.alloyfurnace = tileAlloyFurnace; + this.containerAlloyFurnace = (ContainerAlloyFurnace) this.inventorySlots; } @Override @@ -36,8 +40,18 @@ public class GuiAlloyFurnace extends GuiContainer { int k = (this.width - this.xSize) / 2; int l = (this.height - this.ySize) / 2; this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize); + + if (this.alloyfurnace.isBurning()) + { + int i1 = this.alloyfurnace.getBurnTimeRemainingScaled(13); + this.drawTexturedModalRect(k + 56, l + 36 + 12 - i1, 176, 12 - i1, 14, i1 + 1); + i1 = this.alloyfurnace.getCookProgressScaled(24); + this.drawTexturedModalRect(k + 79, l + 34, 176, 14, i1 + 1, 16); + } } + + protected void drawGuiContainerForegroundLayer(int p_146979_1_, int p_146979_2_) { String name = StatCollector.translateToLocal("tile.techreborn.alloyfurnace.name"); diff --git a/src/main/java/techreborn/tiles/TileAlloyFurnace.java b/src/main/java/techreborn/tiles/TileAlloyFurnace.java index 5f0624f63..df3dcf151 100644 --- a/src/main/java/techreborn/tiles/TileAlloyFurnace.java +++ b/src/main/java/techreborn/tiles/TileAlloyFurnace.java @@ -1,18 +1,41 @@ package techreborn.tiles; +import cpw.mods.fml.common.registry.GameRegistry; import ic2.api.tile.IWrenchable; +import net.minecraft.block.Block; +import net.minecraft.block.material.Material; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; +import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; -import net.minecraft.item.ItemStack; +import net.minecraft.item.*; +import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.nbt.NBTTagCompound; +import techreborn.api.recipe.IBaseRecipeType; +import techreborn.api.recipe.RecipeHandler; import techreborn.init.ModBlocks; +import techreborn.lib.Reference; import techreborn.util.Inventory; +import techreborn.util.ItemUtils; public class TileAlloyFurnace extends TileMachineBase implements IWrenchable, IInventory { public int tickTime; public Inventory inventory = new Inventory(4, "TileAlloyFurnace", 64); + int input1 = 0; + int input2 = 1; + + int output = 2; + + int fuel = 3; + + + public int burnTime; + + public int currentItemBurnTime; + public int cookTime; + public TileAlloyFurnace() { } @@ -20,6 +43,201 @@ public class TileAlloyFurnace extends TileMachineBase implements IWrenchable, II @Override public void updateEntity() { super.updateEntity(); + + boolean flag = this.burnTime > 0; + boolean flag1 = false; + + if (this.burnTime > 0) + { + --this.burnTime; + } + + if (!this.worldObj.isRemote) + { + if (this.burnTime != 0 || getStackInSlot(input1) != null && getStackInSlot(fuel) != null) + { + if (this.burnTime == 0 && this.canSmelt()) + { + this.currentItemBurnTime = this.burnTime = getItemBurnTime(getStackInSlot(fuel)); + + if (this.burnTime > 0) + { + flag1 = true; + + if (this.getStackInSlot(fuel) != null) + { + decrStackSize(fuel, 1); + } + } + } + + if (this.isBurning() && this.canSmelt()) + { + ++this.cookTime; + + if (this.cookTime == 200) + { + this.cookTime = 0; + this.smeltItem(); + flag1 = true; + } + } + else + { + this.cookTime = 0; + } + } + + if (flag != this.burnTime > 0) + { + flag1 = true; + //TODO sync on/off + } + } + + if (flag1) + { + this.markDirty(); + } + + } + + private boolean canSmelt() + { + if (this.getStackInSlot(input1) == null || this.getStackInSlot(input2) == null) + { + return false; + } + else + { + ItemStack itemstack = null; + for(IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.alloySmelteRecipe)){ + for(ItemStack input : recipeType.getInputs()){ + if(ItemUtils.isItemEqual(input, getStackInSlot(input1), true, true, true) || ItemUtils.isItemEqual(input, getStackInSlot(input2), true, true, true)){ + itemstack = recipeType.getOutput(0); + break; + } + } + if(itemstack != null){ + break; + } + } + + if (itemstack == null) return false; + if (this.getStackInSlot(output) == null) return true; + if (!this.getStackInSlot(output).isItemEqual(itemstack)) return false; + int result = getStackInSlot(output).stackSize + itemstack.stackSize; + return result <= getInventoryStackLimit() && result <= this.getStackInSlot(output).getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly. + } + } + + /** + * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack + */ + public void smeltItem() + { + if (this.canSmelt()) + { + ItemStack itemstack = null; + for(IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.alloySmelteRecipe)){ + for(ItemStack input : recipeType.getInputs()){ + if(ItemUtils.isItemEqual(input, getStackInSlot(input1), true, true, true) || ItemUtils.isItemEqual(input, getStackInSlot(input2), true, true, true)){ + itemstack = recipeType.getOutput(0); + break; + } + } + if(itemstack != null){ + break; + } + } + + if (this.getStackInSlot(output) == null) + { + setInventorySlotContents(output, itemstack.copy()); + } + else if (this.getStackInSlot(output).getItem() == itemstack.getItem()) + { + decrStackSize(output, -itemstack.stackSize); + } + + this.decrStackSize(input1, 1); + + if (this.getStackInSlot(input1).stackSize <= 0) + { + setInventorySlotContents(0, null); + } + } + } + + /** + * Furnace isBurning + */ + public boolean isBurning() + { + return this.burnTime > 0; + } + + public int getBurnTimeRemainingScaled(int scale) + { + if (this.currentItemBurnTime == 0) + { + this.currentItemBurnTime = 200; + } + + return this.burnTime * scale / this.currentItemBurnTime; + } + + public int getCookProgressScaled(int scale) + { + return this.cookTime * scale / 200; + } + + + + /** + * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't + * fuel + */ + public static int getItemBurnTime(ItemStack stack) + { + if (stack == null) + { + return 0; + } + else + { + Item item = stack.getItem(); + + if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) + { + Block block = Block.getBlockFromItem(item); + + if (block == Blocks.wooden_slab) + { + return 150; + } + + if (block.getMaterial() == Material.wood) + { + return 300; + } + + if (block == Blocks.coal_block) + { + return 16000; + } + } + + if (item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD")) return 200; + if (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD")) return 200; + if (item instanceof ItemHoe && ((ItemHoe)item).getToolMaterialName().equals("WOOD")) return 200; + if (item == Items.stick) return 100; + if (item == Items.coal) return 1600; + if (item == Items.lava_bucket) return 20000; + if (item == Item.getItemFromBlock(Blocks.sapling)) return 100; + if (item == Items.blaze_rod) return 2400; + return GameRegistry.getFuelValue(stack); + } } @Override