From 8e34062f6e68224786839cc8f10174ed668dbc9a Mon Sep 17 00:00:00 2001 From: drcrazy Date: Wed, 13 Feb 2019 14:56:49 +0300 Subject: [PATCH] Iron alloy smelter should consume right amount of inputs. Closes #1667 --- .../tiles/tier0/TileIronAlloyFurnace.java | 318 +++++++++--------- 1 file changed, 153 insertions(+), 165 deletions(-) diff --git a/src/main/java/techreborn/tiles/tier0/TileIronAlloyFurnace.java b/src/main/java/techreborn/tiles/tier0/TileIronAlloyFurnace.java index 2f75357d1..7c2aaec85 100644 --- a/src/main/java/techreborn/tiles/tier0/TileIronAlloyFurnace.java +++ b/src/main/java/techreborn/tiles/tier0/TileIronAlloyFurnace.java @@ -24,14 +24,9 @@ package techreborn.tiles.tier0; -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.item.*; import net.minecraft.tileentity.TileEntityFurnace; -import net.minecraft.util.EnumFacing; import reborncore.api.IToolDrop; import reborncore.api.recipe.IBaseRecipeType; import reborncore.api.recipe.RecipeHandler; @@ -60,7 +55,7 @@ public class TileIronAlloyFurnace extends TileLegacyMachineBase public int cookTime; int input1 = 0; int input2 = 1; - int output = 2; + int outputSlot = 2; int fuel = 3; public TileIronAlloyFurnace() { @@ -79,47 +74,12 @@ public class TileIronAlloyFurnace extends TileLegacyMachineBase } return (int) (TileEntityFurnace.getItemBurnTime(stack) * 1.25); } - - @Override - public void update() { - super.update(); - final boolean flag = this.burnTime > 0; - boolean flag1 = false; - if (this.burnTime > 0) { - --this.burnTime; - } - if (!this.world.isRemote) { - if (this.burnTime != 0 || !this.getStackInSlot(this.input1).isEmpty()&& !this.getStackInSlot(this.fuel).isEmpty()) { - if (this.burnTime == 0 && this.canSmelt()) { - this.currentItemBurnTime = this.burnTime = TileIronAlloyFurnace.getItemBurnTime(this.getStackInSlot(this.fuel)); - if (this.burnTime > 0) { - flag1 = true; - if (!this.getStackInSlot(this.fuel).isEmpty()) { - this.decrStackSize(this.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(); - } - } - + + /** + * Checks if alloy furnace has all inputs for recipe + * @param recipeType IBaseRecipeType Alloy Smelter Recipe + * @return boolean True if we have all inputs necessery for recipe + */ public boolean hasAllInputs(final IBaseRecipeType recipeType) { if (recipeType == null) { return false; @@ -129,140 +89,190 @@ public class TileIronAlloyFurnace extends TileLegacyMachineBase boolean useOreDict = input instanceof String || recipeType.useOreDic(); boolean checkSize = input instanceof ItemStack; for (int inputslot = 0; inputslot < 2; inputslot++) { - if (ItemUtils.isInputEqual(input, inventory.getStackInSlot(inputslot), true, true, - useOreDict)) { - ItemStack stack = RecipeTranslator.getStackFromObject(input); - if (!checkSize || inventory.getStackInSlot(inputslot).getCount() >= stack.getCount()) { + if (ItemUtils.isInputEqual(input, inventory.getStackInSlot(inputslot), true, true, useOreDict)) { + ItemStack inputStack = RecipeTranslator.getStackFromObject(input); + if (!checkSize || inventory.getStackInSlot(inputslot).getCount() >= inputStack.getCount()) { hasItem = true; } } } - if (!hasItem) + if (!hasItem) { return false; + } } return true; } - - private boolean canSmelt() { - if (this.getStackInSlot(this.input1).isEmpty() || this.getStackInSlot(this.input2).isEmpty()) { - return false; - } else { - ItemStack itemstack = null; - for (final IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.ALLOY_SMELTER_RECIPE)) { - if (this.hasAllInputs(recipeType)) { - itemstack = recipeType.getOutput(0); - break; - } - } - - if (itemstack == null) - return false; - if (this.getStackInSlot(this.output).isEmpty()) - return true; - if (!this.getStackInSlot(this.output).isItemEqual(itemstack)) - return false; - final int result = this.getStackInSlot(this.output).getCount() + itemstack.getCount(); - return result <= this.getInventoryStackLimit() && result <= this.getStackInSlot(this.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 + * Checks if it has inputs and can fit recipe outputSlot to outputSlot slot + * @return boolean True if it can fit outputSlot itemstack into outputSlot slot */ - public void smeltItem() { - if (this.canSmelt()) { - ItemStack itemstack = ItemStack.EMPTY; - for (final IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.ALLOY_SMELTER_RECIPE)) { - if (this.hasAllInputs(recipeType)) { - itemstack = recipeType.getOutput(0); - break; - } - if (!itemstack.isEmpty()) { - break; - } + private boolean canSmelt() { + if (getStackInSlot(input1).isEmpty() || getStackInSlot(input2).isEmpty()) { + return false; + } + ItemStack outputStack = null; + for (final IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.ALLOY_SMELTER_RECIPE)) { + if (hasAllInputs(recipeType)) { + outputStack = recipeType.getOutput(0); + break; } - - if (this.getStackInSlot(this.output).isEmpty()) { - this.setInventorySlotContents(this.output, itemstack.copy()); - } else if (this.getStackInSlot(this.output).getItem() == itemstack.getItem()) { - this.decrStackSize(this.output, -itemstack.getCount()); - } - - for (final IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.ALLOY_SMELTER_RECIPE)) { - boolean hasAllRecipes = true; - if (this.hasAllInputs(recipeType)) { - - } else { - hasAllRecipes = false; - } - if (hasAllRecipes) { - for (Object input : recipeType.getInputs()) { - boolean useOreDict = input instanceof String || recipeType.useOreDic(); - for (int inputSlot = 0; inputSlot < 2; inputSlot++) { - if (ItemUtils.isInputEqual(input, this.inventory.getStackInSlot(inputSlot), true, true, useOreDict)) { - int count = 1; - if (input instanceof ItemStack) { - count = RecipeTranslator.getStackFromObject(input).getCount(); - } - inventory.decrStackSize(inputSlot, count); - break; - } - } - } - } - } - } - } + if (outputStack == null) { + return false; + } + ItemStack outputSlotStack = getStackInSlot(outputSlot); + if (outputSlotStack.isEmpty()) { + return true; + } + if (!outputSlotStack.isItemEqual(outputStack)) { + return false; + } + + final int result = outputSlotStack.getCount() + outputStack.getCount(); + return result <= getInventoryStackLimit() && result <= outputSlotStack.getMaxStackSize(); + } + /** - * Furnace isBurning - * @return Boolean True if furnace is burning + * Alloy Furnace isBurning + * @return Boolean True if alloy furnace is burning */ public boolean isBurning() { - return this.burnTime > 0; + return burnTime > 0; } public int getBurnTimeRemainingScaled(final int scale) { - if (this.currentItemBurnTime == 0) { - this.currentItemBurnTime = 200; + if (currentItemBurnTime == 0) { + currentItemBurnTime = 200; } - return this.burnTime * scale / this.currentItemBurnTime; + return burnTime * scale / currentItemBurnTime; } public int getCookProgressScaled(final int scale) { - return this.cookTime * scale / 200; + return cookTime * scale / 200; + } + + /** + * Turn inputs into the appropriate smelted item in the alloy furnace result stack + */ + public void smeltItem() { + if (!canSmelt()) { + return; + } + ItemStack outputStack = ItemStack.EMPTY; + IBaseRecipeType alloySmelterRecipe = null; + + for (final IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.ALLOY_SMELTER_RECIPE)) { + if (hasAllInputs(recipeType)) { + alloySmelterRecipe = recipeType; + outputStack = recipeType.getOutput(0); + break; + } + } + + if (alloySmelterRecipe == null || outputStack.isEmpty()) { + return; + } + + // Add recipe results + ItemStack outputSlotStack = getStackInSlot(outputSlot); + if (outputSlotStack.isEmpty()) { + setInventorySlotContents(outputSlot, outputStack.copy()); + } else if (getStackInSlot(outputSlot).getItem() == outputStack.getItem()) { + decrStackSize(outputSlot, -outputStack.getCount()); + } + + // Remove recipe ingredients + for (Object input : alloySmelterRecipe.getInputs()) { + boolean useOreDict = input instanceof String || alloySmelterRecipe.useOreDic(); + for (int inputSlot = 0; inputSlot < 2; inputSlot++) { + if (ItemUtils.isInputEqual(input, inventory.getStackInSlot(inputSlot), true, true, useOreDict)) { + int count = RecipeTranslator.getStackFromObject(input).getCount(); + inventory.decrStackSize(inputSlot, count); + } + } + } } + // TileLegacyMachineBase @Override - public EnumFacing getFacing() { - return this.getFacingEnum(); + public void update() { + super.update(); + final boolean burning = isBurning(); + boolean updateInventory = false; + if (burning) { + --burnTime; + } + if (world.isRemote) { + return; + } + ItemStack fuelStack = getStackInSlot(fuel); + if (burnTime != 0 || !getStackInSlot(input1).isEmpty() && !fuelStack.isEmpty()) { + if (burnTime == 0 && canSmelt()) { + currentItemBurnTime = burnTime = TileIronAlloyFurnace.getItemBurnTime(fuelStack); + if (burnTime > 0) { + updateInventory = true; + if (!fuelStack.isEmpty()) { + decrStackSize(fuel, 1); + } + } + } + if (isBurning() && canSmelt()) { + ++cookTime; + if (cookTime == 200) { + cookTime = 0; + smeltItem(); + updateInventory = true; + } + } else { + cookTime = 0; + } + } + if (burning != isBurning()) { + updateInventory = true; + } + if (updateInventory) { + markDirty(); + } + } + + @Override + public boolean canBeUpgraded() { + return false; } + // IToolDrop @Override public ItemStack getToolDrop(final EntityPlayer entityPlayer) { return new ItemStack(ModBlocks.IRON_ALLOY_FURNACE, 1); } - public boolean isComplete() { - return false; - } - + // IInventoryProvider @Override public Inventory getInventory() { - return this.inventory; + return inventory; } - + // IContainerProvider + @Override + public BuiltContainer createContainer(final EntityPlayer player) { + return new ContainerBuilder("alloyfurnace").player(player.inventory).inventory(8, 84).hotbar(8, 142) + .addInventory().tile(this) + .filterSlot(0, 47, 17, + stack -> RecipeHandler.recipeList.stream() + .anyMatch(recipe -> recipe instanceof AlloySmelterRecipe + && ItemUtils.isInputEqual(recipe.getInputs().get(0), stack, true, true, true))) + .filterSlot(1, 65, 17, + stack -> RecipeHandler.recipeList.stream() + .anyMatch(recipe -> recipe instanceof AlloySmelterRecipe + && ItemUtils.isInputEqual(recipe.getInputs().get(1), stack, true, true, true))) + .outputSlot(2, 116, 35).fuelSlot(3, 56, 53).syncIntegerValue(this::getBurnTime, this::setBurnTime) + .syncIntegerValue(this::getCookTime, this::setCookTime) + .syncIntegerValue(this::getCurrentItemBurnTime, this::setCurrentItemBurnTime).addInventory().create(this); + } + public int getBurnTime() { return this.burnTime; } @@ -286,26 +296,4 @@ public class TileIronAlloyFurnace extends TileLegacyMachineBase public void setCookTime(final int cookTime) { this.cookTime = cookTime; } - - @Override - public BuiltContainer createContainer(final EntityPlayer player) { - return new ContainerBuilder("alloyfurnace").player(player.inventory).inventory(8, 84).hotbar(8, 142) - .addInventory().tile(this) - .filterSlot(0, 47, 17, - stack -> RecipeHandler.recipeList.stream() - .anyMatch(recipe -> recipe instanceof AlloySmelterRecipe - && ItemUtils.isInputEqual(recipe.getInputs().get(0), stack, true, true, true))) - .filterSlot(1, 65, 17, - stack -> RecipeHandler.recipeList.stream() - .anyMatch(recipe -> recipe instanceof AlloySmelterRecipe - && ItemUtils.isInputEqual(recipe.getInputs().get(1), stack, true, true, true))) - .outputSlot(2, 116, 35).fuelSlot(3, 56, 53).syncIntegerValue(this::getBurnTime, this::setBurnTime) - .syncIntegerValue(this::getCookTime, this::setCookTime) - .syncIntegerValue(this::getCurrentItemBurnTime, this::setCurrentItemBurnTime).addInventory().create(this); - } - - @Override - public boolean canBeUpgraded() { - return false; - } }