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

@ -28,7 +28,7 @@ import net.minecraft.item.ItemConvertible
import net.minecraft.item.ItemStack
import net.minecraft.test.TestContext
import net.minecraft.util.math.BlockPos
import techreborn.blockentity.machine.GenericMachineBlockEntity
import reborncore.common.blockentity.MachineBaseBlockEntity
import techreborn.init.TRContent
class TRTestContext extends TestContext {
@ -55,6 +55,20 @@ class TRTestContext extends TestContext {
}
}
def machine(TRContent.Machine machine, @DelegatesTo(MachineContext) Closure machineContextClosure) {
def machinePos = new BlockPos(0, 1, 0)
setBlockState(machinePos, machine.block)
waitAndRun(5) {
try {
new MachineContext(machinePos).with(machineContextClosure)
} catch (e) {
e.printStackTrace()
throw e
}
}
}
class MachineContext {
final BlockPos machinePos
@ -100,14 +114,14 @@ class TRTestContext extends TestContext {
}
}
GenericMachineBlockEntity getBlockEntity() {
MachineBaseBlockEntity getBlockEntity() {
def be = getBlockEntity(machinePos)
if (!be) {
throwPositionedException("Failed to get machine block entity", machinePos)
}
return be as GenericMachineBlockEntity
return be as MachineBaseBlockEntity
}
}
}

View file

@ -0,0 +1,51 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2020 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
//file:noinspection GrMethodMayBeStatic
package techreborn.test.machine
import net.minecraft.item.Items
import net.minecraft.test.GameTest
import techreborn.blockentity.machine.iron.IronAlloyFurnaceBlockEntity
import techreborn.config.TechRebornConfig
import techreborn.init.TRContent
import techreborn.test.TRGameTest
import techreborn.test.TRTestContext
class IronAlloyFurnaceTest extends TRGameTest {
@GameTest(structureName = "fabric-gametest-api-v1:empty", tickLimit = 2000)
def testIronAlloyFurnaceElectrumAlloyIngot(TRTestContext context) {
/**
* Test that the Iron Alloy Furnace smelts a gold ingot and a silver ingot into an electrum alloy ingot in 200
* Verifies: Issue #2850
*/
context.machine(TRContent.Machine.IRON_ALLOY_FURNACE) {
input(Items.COAL_BLOCK, IronAlloyFurnaceBlockEntity.FUEL_SLOT)
input(Items.GOLD_INGOT, IronAlloyFurnaceBlockEntity.INPUT_SLOT_1)
input(TRContent.Ingots.SILVER, IronAlloyFurnaceBlockEntity.INPUT_SLOT_2)
expectOutput(TRContent.Ingots.ELECTRUM, (int) (200 / TechRebornConfig.cookingScale), IronAlloyFurnaceBlockEntity.OUTPUT_SLOT)
}
}
}

View file

@ -0,0 +1,65 @@
/*
* This file is part of TechReborn, licensed under the MIT License (MIT).
*
* Copyright (c) 2020 TechReborn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
//file:noinspection GrMethodMayBeStatic
package techreborn.test.machine
import net.minecraft.item.Items
import net.minecraft.test.GameTest
import techreborn.blockentity.machine.iron.IronFurnaceBlockEntity
import techreborn.config.TechRebornConfig
import techreborn.init.TRContent
import techreborn.test.TRGameTest
import techreborn.test.TRTestContext
class IronFurnaceTest extends TRGameTest {
@GameTest(structureName = "fabric-gametest-api-v1:empty", tickLimit = 2000)
def testIronFurnaceSmeltRawIron(TRTestContext context) {
/**
* Test that the Iron Furnace smelts a raw iron ore into an iron ingot in 200 ticks
* Verifies: Issue #2850
*/
context.machine(TRContent.Machine.IRON_FURNACE) {
input(Items.COAL_BLOCK, IronFurnaceBlockEntity.FUEL_SLOT)
input(Items.RAW_IRON, IronFurnaceBlockEntity.INPUT_SLOT)
expectOutput(Items.IRON_INGOT, (int) (200 / TechRebornConfig.cookingScale), IronFurnaceBlockEntity.OUTPUT_SLOT)
}
}
@GameTest(structureName = "fabric-gametest-api-v1:empty", tickLimit = 2000)
def testIronFurnaceSmeltRawIronBlock(TRTestContext context) {
/**
* Test that the Iron Furnace smelts a raw iron block into an iron block in 1500 ticks instead of 200
* Verifies: Issue #2850
*/
context.machine(TRContent.Machine.IRON_FURNACE) {
input(Items.COAL_BLOCK, IronFurnaceBlockEntity.FUEL_SLOT)
input(Items.RAW_IRON_BLOCK, IronFurnaceBlockEntity.INPUT_SLOT)
expectOutput(Items.IRON_BLOCK, (int) (1500 / TechRebornConfig.cookingScale), IronFurnaceBlockEntity.OUTPUT_SLOT)
}
}
}

View file

@ -7,6 +7,8 @@
"entrypoints": {
"fabric-gametest" : [
"techreborn.test.machine.GrinderTest",
"techreborn.test.machine.IronFurnaceTest",
"techreborn.test.machine.IronAlloyFurnaceTest",
"techreborn.test.recipe.RecipeSyncTests"
]
}

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