Fix #2850 Fetching cooking time based on recipe. Thanks to SimonFlapse

* Added new method cookingTime(), replacing totalCookingTime
* Added GameTest for validating smelting times
This commit is contained in:
Simon 2022-03-27 15:15:22 +02:00 committed by GitHub
parent d2c146b257
commit c61398c950
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 244 additions and 58 deletions

View file

@ -47,15 +47,12 @@ public abstract class AbstractIronMachineBlockEntity extends MachineBaseBlockEnt
public int burnTime;
public int totalBurnTime;
public int progress;
public int totalCookingTime;
int fuelSlot;
Block toolDrop;
public AbstractIronMachineBlockEntity(BlockEntityType<?> blockEntityTypeIn, BlockPos pos, BlockState state, int fuelSlot, Block toolDrop) {
super(blockEntityTypeIn, pos, state);
this.fuelSlot = fuelSlot;
// default value for vanilla smelting recipes is 200
this.totalCookingTime = (int) (200 / TechRebornConfig.cookingScale);
this.toolDrop = toolDrop;
}
@ -71,6 +68,12 @@ public abstract class AbstractIronMachineBlockEntity extends MachineBaseBlockEnt
*/
protected abstract void smelt();
/**
* Get the current recipe's cooking time
*
*/
protected abstract int cookingTime();
/**
* Returns the number of ticks that the supplied fuel item will keep the
* furnace burning, or 0 if the item isn't fuel
@ -106,8 +109,8 @@ public abstract class AbstractIronMachineBlockEntity extends MachineBaseBlockEnt
* @return {@code int} Scaled crafting progress
*/
public int getProgressScaled(int scale) {
if (totalCookingTime > 0) {
return progress * scale / totalCookingTime;
if (cookingTime() > 0) {
return progress * scale / cookingTime();
}
return 0;
}
@ -174,7 +177,7 @@ public abstract class AbstractIronMachineBlockEntity extends MachineBaseBlockEnt
if (isBurning() && canSmelt()) {
++progress;
if (progress == totalCookingTime) {
if (progress == cookingTime()) {
progress = 0;
smelt();
}
@ -231,6 +234,4 @@ public abstract class AbstractIronMachineBlockEntity extends MachineBaseBlockEnt
public void setProgress(int progress) {
this.progress = progress;
}
}

View file

@ -28,12 +28,13 @@ import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import reborncore.common.screen.BuiltScreenHandlerProvider;
import reborncore.common.screen.BuiltScreenHandler;
import reborncore.client.screen.builder.ScreenHandlerBuilder;
import reborncore.common.crafting.RebornRecipe;
import reborncore.common.crafting.ingredient.RebornIngredient;
import reborncore.common.screen.BuiltScreenHandler;
import reborncore.common.screen.BuiltScreenHandlerProvider;
import reborncore.common.util.RebornInventory;
import techreborn.config.TechRebornConfig;
import techreborn.init.ModRecipes;
import techreborn.init.TRBlockEntities;
import techreborn.init.TRContent;
@ -42,12 +43,13 @@ import java.util.List;
public class IronAlloyFurnaceBlockEntity extends AbstractIronMachineBlockEntity implements BuiltScreenHandlerProvider {
int input1 = 0;
int input2 = 1;
int output = 2;
public final static int INPUT_SLOT_1 = 0;
public final static int INPUT_SLOT_2 = 1;
public final static int OUTPUT_SLOT = 2;
public final static int FUEL_SLOT = 3;
public IronAlloyFurnaceBlockEntity(BlockPos pos, BlockState state) {
super(TRBlockEntities.IRON_ALLOY_FURNACE, pos, state, 3, TRContent.Machine.IRON_ALLOY_FURNACE.block);
super(TRBlockEntities.IRON_ALLOY_FURNACE, pos, state, FUEL_SLOT, TRContent.Machine.IRON_ALLOY_FURNACE.block);
this.inventory = new RebornInventory<>(4, "IronAlloyFurnaceBlockEntity", 64, this);
}
@ -68,9 +70,19 @@ public class IronAlloyFurnaceBlockEntity extends AbstractIronMachineBlockEntity
return true;
}
private RebornRecipe getRecipe() {
for (RebornRecipe recipeType : ModRecipes.ALLOY_SMELTER.getRecipes(world)) {
if (hasAllInputs(recipeType)) {
return recipeType;
}
}
return null;
}
@Override
protected boolean canSmelt() {
if (inventory.getStack(input1).isEmpty() || inventory.getStack(input2).isEmpty()) {
if (inventory.getStack(INPUT_SLOT_1).isEmpty() || inventory.getStack(INPUT_SLOT_2).isEmpty()) {
return false;
}
ItemStack itemstack = null;
@ -90,12 +102,12 @@ public class IronAlloyFurnaceBlockEntity extends AbstractIronMachineBlockEntity
if (itemstack == null)
return false;
if (inventory.getStack(output).isEmpty())
if (inventory.getStack(OUTPUT_SLOT).isEmpty())
return true;
if (!inventory.getStack(output).isItemEqualIgnoreDamage(itemstack))
if (!inventory.getStack(OUTPUT_SLOT).isItemEqualIgnoreDamage(itemstack))
return false;
int result = inventory.getStack(output).getCount() + itemstack.getCount();
return result <= inventory.getStackLimit() && result <= inventory.getStack(output).getMaxCount();
int result = inventory.getStack(OUTPUT_SLOT).getCount() + itemstack.getCount();
return result <= inventory.getStackLimit() && result <= inventory.getStack(OUTPUT_SLOT).getMaxCount();
}
@Override
@ -104,24 +116,19 @@ public class IronAlloyFurnaceBlockEntity extends AbstractIronMachineBlockEntity
return;
}
RebornRecipe currentRecipe = null;
for (RebornRecipe recipeType : ModRecipes.ALLOY_SMELTER.getRecipes(world)) {
if (hasAllInputs(recipeType)) {
currentRecipe = recipeType;
break;
}
}
RebornRecipe currentRecipe = getRecipe();
if (currentRecipe == null) {
return;
}
ItemStack outputStack = currentRecipe.getOutputs().get(0);
if (outputStack.isEmpty()) {
return;
}
if (inventory.getStack(output).isEmpty()) {
inventory.setStack(output, outputStack.copy());
} else if (inventory.getStack(output).getItem() == outputStack.getItem()) {
inventory.shrinkSlot(output, -outputStack.getCount());
if (inventory.getStack(OUTPUT_SLOT).isEmpty()) {
inventory.setStack(OUTPUT_SLOT, outputStack.copy());
} else if (inventory.getStack(OUTPUT_SLOT).getItem() == outputStack.getItem()) {
inventory.shrinkSlot(OUTPUT_SLOT, -outputStack.getCount());
}
for (RebornIngredient ingredient : currentRecipe.getRebornIngredients()) {
@ -134,6 +141,19 @@ public class IronAlloyFurnaceBlockEntity extends AbstractIronMachineBlockEntity
}
}
@Override
protected int cookingTime() {
// default value for vanilla smelting recipes is 200
int cookingTime = 200;
RebornRecipe recipe = getRecipe();
if (recipe != null) {
cookingTime = recipe.getTime();
}
return (int) (cookingTime / TechRebornConfig.cookingScale);
}
@Override
public BuiltScreenHandler createScreenHandler(int syncID, final PlayerEntity player) {
return new ScreenHandlerBuilder("alloyfurnace").player(player.getInventory()).inventory().hotbar()
@ -157,6 +177,6 @@ public class IronAlloyFurnaceBlockEntity extends AbstractIronMachineBlockEntity
@Override
public int[] getInputSlots() {
return new int[]{input1, input2};
return new int[]{INPUT_SLOT_1, INPUT_SLOT_2};
}
}

View file

@ -35,27 +35,31 @@ import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.SmeltingRecipe;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import reborncore.common.screen.BuiltScreenHandlerProvider;
import reborncore.common.screen.BuiltScreenHandler;
import reborncore.client.screen.builder.ScreenHandlerBuilder;
import reborncore.common.screen.BuiltScreenHandler;
import reborncore.common.screen.BuiltScreenHandlerProvider;
import reborncore.common.util.RebornInventory;
import techreborn.config.TechRebornConfig;
import techreborn.init.TRBlockEntities;
import techreborn.init.TRContent;
import techreborn.utils.RecipeUtils;
import javax.annotation.Nullable;
import java.util.Optional;
public class IronFurnaceBlockEntity extends AbstractIronMachineBlockEntity implements BuiltScreenHandlerProvider {
int inputSlot = 0;
int outputSlot = 1;
public final static int INPUT_SLOT = 0;
public final static int OUTPUT_SLOT = 1;
public final static int FUEL_SLOT = 2;
public float experience;
private boolean previousValid = false;
private ItemStack previousStack = ItemStack.EMPTY;
private Recipe<?> lastRecipe = null;
public IronFurnaceBlockEntity(BlockPos pos, BlockState state) {
super(TRBlockEntities.IRON_FURNACE, pos, state, 2, TRContent.Machine.IRON_FURNACE.block);
super(TRBlockEntities.IRON_FURNACE, pos, state, FUEL_SLOT, TRContent.Machine.IRON_FURNACE.block);
this.inventory = new RebornInventory<>(3, "IronFurnaceBlockEntity", 64, this);
}
@ -71,6 +75,22 @@ public class IronFurnaceBlockEntity extends AbstractIronMachineBlockEntity imple
experience = 0;
}
@Nullable
private Recipe<?> refreshRecipe(ItemStack stack) {
// Check the previous recipe to see if it still applies to the current inv, saves rechecking the whole recipe list
if (lastRecipe != null && RecipeUtils.matchesSingleInput(lastRecipe, stack)) {
return lastRecipe;
} else {
Recipe<?> matchingRecipe = RecipeUtils.getMatchingRecipe(world, RecipeType.SMELTING, stack).orElse(null);
if (matchingRecipe != null) {
lastRecipe = matchingRecipe;
}
}
return lastRecipe;
}
private ItemStack getResultFor(ItemStack stack) {
if (stack.isEmpty()) {
// Fast fail if there is no input, no point checking the recipes if the machine is empty
@ -80,15 +100,9 @@ public class IronFurnaceBlockEntity extends AbstractIronMachineBlockEntity imple
return ItemStack.EMPTY;
}
// Check the previous recipe to see if it still applies to the current inv, saves rechecking the whole recipe list
if (lastRecipe != null && RecipeUtils.matchesSingleInput(lastRecipe, stack)) {
return lastRecipe.getOutput();
}
Recipe<?> matchingRecipe = RecipeUtils.getMatchingRecipe(world, RecipeType.SMELTING, stack).orElse(null);
Recipe<?> matchingRecipe = refreshRecipe(stack);
if (matchingRecipe != null) {
lastRecipe = matchingRecipe;
return matchingRecipe.getOutput().copy();
}
@ -106,25 +120,25 @@ public class IronFurnaceBlockEntity extends AbstractIronMachineBlockEntity imple
if (!canSmelt()) {
return;
}
ItemStack inputStack = inventory.getStack(inputSlot);
ItemStack inputStack = inventory.getStack(INPUT_SLOT);
ItemStack resultStack = getResultFor(inputStack);
if (inventory.getStack(outputSlot).isEmpty()) {
inventory.setStack(outputSlot, resultStack.copy());
} else if (inventory.getStack(outputSlot).isItemEqualIgnoreDamage(resultStack)) {
inventory.getStack(outputSlot).increment(resultStack.getCount());
if (inventory.getStack(OUTPUT_SLOT).isEmpty()) {
inventory.setStack(OUTPUT_SLOT, resultStack.copy());
} else if (inventory.getStack(OUTPUT_SLOT).isItemEqualIgnoreDamage(resultStack)) {
inventory.getStack(OUTPUT_SLOT).increment(resultStack.getCount());
}
experience += getExperienceFor();
if (inputStack.getCount() > 1) {
inventory.shrinkSlot(inputSlot, 1);
inventory.shrinkSlot(INPUT_SLOT, 1);
} else {
inventory.setStack(inputSlot, ItemStack.EMPTY);
inventory.setStack(INPUT_SLOT, ItemStack.EMPTY);
}
}
@Override
protected boolean canSmelt() {
ItemStack inputStack = inventory.getStack(inputSlot);
ItemStack inputStack = inventory.getStack(INPUT_SLOT);
if (inputStack.isEmpty())
return false;
if (previousStack != inputStack) {
@ -139,7 +153,7 @@ public class IronFurnaceBlockEntity extends AbstractIronMachineBlockEntity imple
else {
previousValid = true;
}
ItemStack outputSlotStack = inventory.getStack(outputSlot);
ItemStack outputSlotStack = inventory.getStack(OUTPUT_SLOT);
if (outputSlotStack.isEmpty())
return true;
if (!outputSlotStack.isItemEqualIgnoreDamage(outputStack))
@ -148,6 +162,25 @@ public class IronFurnaceBlockEntity extends AbstractIronMachineBlockEntity imple
return result <= inventory.getStackLimit() && result <= outputStack.getMaxCount();
}
@Override
protected int cookingTime() {
// default value for vanilla smelting recipes is 200
int cookingTime = 200;
Recipe<?> recipe = refreshRecipe(inventory.getStack(INPUT_SLOT));
if (recipe != null) {
try {
cookingTime = ((SmeltingRecipe) recipe).getCookTime();
} catch (ClassCastException ex) {
// Intentionally ignored
System.out.println("Not a smelting recipe!");
}
}
return (int) (cookingTime / TechRebornConfig.cookingScale);
}
@Override
public boolean isStackValid(int slotID, ItemStack stack) {
return !getResultFor(stack).isEmpty();
@ -176,7 +209,7 @@ public class IronFurnaceBlockEntity extends AbstractIronMachineBlockEntity imple
@Override
public int[] getInputSlots() {
return new int[]{inputSlot};
return new int[]{INPUT_SLOT};
}
@Override