Iron alloy smelter should consume right amount of inputs. Closes #1667

This commit is contained in:
drcrazy 2019-02-13 14:56:49 +03:00
parent fbcab3bf2d
commit 8e34062f6e

View file

@ -24,14 +24,9 @@
package techreborn.tiles.tier0; package techreborn.tiles.tier0;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.*; import net.minecraft.item.*;
import net.minecraft.tileentity.TileEntityFurnace; import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.EnumFacing;
import reborncore.api.IToolDrop; import reborncore.api.IToolDrop;
import reborncore.api.recipe.IBaseRecipeType; import reborncore.api.recipe.IBaseRecipeType;
import reborncore.api.recipe.RecipeHandler; import reborncore.api.recipe.RecipeHandler;
@ -60,7 +55,7 @@ public class TileIronAlloyFurnace extends TileLegacyMachineBase
public int cookTime; public int cookTime;
int input1 = 0; int input1 = 0;
int input2 = 1; int input2 = 1;
int output = 2; int outputSlot = 2;
int fuel = 3; int fuel = 3;
public TileIronAlloyFurnace() { public TileIronAlloyFurnace() {
@ -80,46 +75,11 @@ public class TileIronAlloyFurnace extends TileLegacyMachineBase
return (int) (TileEntityFurnace.getItemBurnTime(stack) * 1.25); return (int) (TileEntityFurnace.getItemBurnTime(stack) * 1.25);
} }
@Override /**
public void update() { * Checks if alloy furnace has all inputs for recipe
super.update(); * @param recipeType IBaseRecipeType Alloy Smelter Recipe
final boolean flag = this.burnTime > 0; * @return boolean True if we have all inputs necessery for recipe
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();
}
}
public boolean hasAllInputs(final IBaseRecipeType recipeType) { public boolean hasAllInputs(final IBaseRecipeType recipeType) {
if (recipeType == null) { if (recipeType == null) {
return false; return false;
@ -129,139 +89,189 @@ public class TileIronAlloyFurnace extends TileLegacyMachineBase
boolean useOreDict = input instanceof String || recipeType.useOreDic(); boolean useOreDict = input instanceof String || recipeType.useOreDic();
boolean checkSize = input instanceof ItemStack; boolean checkSize = input instanceof ItemStack;
for (int inputslot = 0; inputslot < 2; inputslot++) { for (int inputslot = 0; inputslot < 2; inputslot++) {
if (ItemUtils.isInputEqual(input, inventory.getStackInSlot(inputslot), true, true, if (ItemUtils.isInputEqual(input, inventory.getStackInSlot(inputslot), true, true, useOreDict)) {
useOreDict)) { ItemStack inputStack = RecipeTranslator.getStackFromObject(input);
ItemStack stack = RecipeTranslator.getStackFromObject(input); if (!checkSize || inventory.getStackInSlot(inputslot).getCount() >= inputStack.getCount()) {
if (!checkSize || inventory.getStackInSlot(inputslot).getCount() >= stack.getCount()) {
hasItem = true; hasItem = true;
} }
} }
} }
if (!hasItem) if (!hasItem) {
return false; return false;
} }
}
return true; 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 * Checks if it has inputs and can fit recipe outputSlot to outputSlot slot
* item in the furnace result stack * @return boolean True if it can fit outputSlot itemstack into outputSlot slot
*/ */
public void smeltItem() { private boolean canSmelt() {
if (this.canSmelt()) { if (getStackInSlot(input1).isEmpty() || getStackInSlot(input2).isEmpty()) {
ItemStack itemstack = ItemStack.EMPTY; return false;
}
ItemStack outputStack = null;
for (final IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.ALLOY_SMELTER_RECIPE)) { for (final IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.ALLOY_SMELTER_RECIPE)) {
if (this.hasAllInputs(recipeType)) { if (hasAllInputs(recipeType)) {
itemstack = recipeType.getOutput(0); outputStack = recipeType.getOutput(0);
break;
}
if (!itemstack.isEmpty()) {
break; break;
} }
} }
if (this.getStackInSlot(this.output).isEmpty()) { if (outputStack == null) {
this.setInventorySlotContents(this.output, itemstack.copy()); return false;
} else if (this.getStackInSlot(this.output).getItem() == itemstack.getItem()) { }
this.decrStackSize(this.output, -itemstack.getCount()); ItemStack outputSlotStack = getStackInSlot(outputSlot);
if (outputSlotStack.isEmpty()) {
return true;
}
if (!outputSlotStack.isItemEqual(outputStack)) {
return false;
} }
for (final IBaseRecipeType recipeType : RecipeHandler.getRecipeClassFromName(Reference.ALLOY_SMELTER_RECIPE)) { final int result = outputSlotStack.getCount() + outputStack.getCount();
boolean hasAllRecipes = true; return result <= getInventoryStackLimit() && result <= outputSlotStack.getMaxStackSize();
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;
}
}
}
}
}
}
} }
/** /**
* Furnace isBurning * Alloy Furnace isBurning
* @return Boolean True if furnace is burning * @return Boolean True if alloy furnace is burning
*/ */
public boolean isBurning() { public boolean isBurning() {
return this.burnTime > 0; return burnTime > 0;
} }
public int getBurnTimeRemainingScaled(final int scale) { public int getBurnTimeRemainingScaled(final int scale) {
if (this.currentItemBurnTime == 0) { if (currentItemBurnTime == 0) {
this.currentItemBurnTime = 200; currentItemBurnTime = 200;
} }
return this.burnTime * scale / this.currentItemBurnTime; return burnTime * scale / currentItemBurnTime;
} }
public int getCookProgressScaled(final int scale) { 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 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 @Override
public EnumFacing getFacing() { public boolean canBeUpgraded() {
return this.getFacingEnum(); return false;
} }
// IToolDrop
@Override @Override
public ItemStack getToolDrop(final EntityPlayer entityPlayer) { public ItemStack getToolDrop(final EntityPlayer entityPlayer) {
return new ItemStack(ModBlocks.IRON_ALLOY_FURNACE, 1); return new ItemStack(ModBlocks.IRON_ALLOY_FURNACE, 1);
} }
public boolean isComplete() { // IInventoryProvider
return false;
}
@Override @Override
public Inventory getInventory() { 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() { public int getBurnTime() {
return this.burnTime; return this.burnTime;
@ -286,26 +296,4 @@ public class TileIronAlloyFurnace extends TileLegacyMachineBase
public void setCookTime(final int cookTime) { public void setCookTime(final int cookTime) {
this.cookTime = 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;
}
} }