Optimise Iron furnace recipe lookup
This commit is contained in:
parent
af62a85677
commit
2ea118ec63
2 changed files with 39 additions and 16 deletions
|
@ -30,6 +30,7 @@ import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.recipe.AbstractCookingRecipe;
|
import net.minecraft.recipe.AbstractCookingRecipe;
|
||||||
|
import net.minecraft.recipe.Recipe;
|
||||||
import net.minecraft.recipe.RecipeType;
|
import net.minecraft.recipe.RecipeType;
|
||||||
import net.minecraft.recipe.SmeltingRecipe;
|
import net.minecraft.recipe.SmeltingRecipe;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
|
@ -49,6 +50,8 @@ public class IronFurnaceBlockEntity extends AbstractIronMachineBlockEntity imple
|
||||||
int outputSlot = 1;
|
int outputSlot = 1;
|
||||||
public float experience;
|
public float experience;
|
||||||
|
|
||||||
|
private Recipe<?> lastRecipe = null;
|
||||||
|
|
||||||
public IronFurnaceBlockEntity() {
|
public IronFurnaceBlockEntity() {
|
||||||
super(TRBlockEntities.IRON_FURNACE, 2, TRContent.Machine.IRON_FURNACE.block);
|
super(TRBlockEntities.IRON_FURNACE, 2, TRContent.Machine.IRON_FURNACE.block);
|
||||||
this.inventory = new RebornInventory<>(3, "IronFurnaceBlockEntity", 64, this);
|
this.inventory = new RebornInventory<>(3, "IronFurnaceBlockEntity", 64, this);
|
||||||
|
@ -68,10 +71,23 @@ public class IronFurnaceBlockEntity extends AbstractIronMachineBlockEntity imple
|
||||||
}
|
}
|
||||||
|
|
||||||
private ItemStack getResultFor(ItemStack stack) {
|
private ItemStack getResultFor(ItemStack stack) {
|
||||||
ItemStack result = RecipeUtils.getMatchingRecipes(world, RecipeType.SMELTING, stack);
|
if (stack.isEmpty()) {
|
||||||
if (!result.isEmpty()) {
|
// Fast fail if there is no input, no point checking the recipes if the machine is empty
|
||||||
return result.copy();
|
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);
|
||||||
|
|
||||||
|
if (matchingRecipe != null) {
|
||||||
|
lastRecipe = matchingRecipe;
|
||||||
|
return matchingRecipe.getOutput().copy();
|
||||||
|
}
|
||||||
|
|
||||||
return ItemStack.EMPTY;
|
return ItemStack.EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,17 +120,20 @@ public class IronFurnaceBlockEntity extends AbstractIronMachineBlockEntity imple
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean canSmelt() {
|
protected boolean canSmelt() {
|
||||||
if (inventory.getStack(inputSlot).isEmpty()) {
|
ItemStack inputStack = inventory.getStack(inputSlot);
|
||||||
|
if (inputStack.isEmpty())
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
ItemStack outputStack = getResultFor(inventory.getStack(inputSlot));
|
ItemStack outputStack = getResultFor(inputStack);
|
||||||
if (outputStack.isEmpty())
|
if (outputStack.isEmpty())
|
||||||
return false;
|
return false;
|
||||||
if (inventory.getStack(outputSlot).isEmpty())
|
|
||||||
|
ItemStack outputSlotStack = inventory.getStack(outputSlot);
|
||||||
|
if (outputSlotStack.isEmpty())
|
||||||
return true;
|
return true;
|
||||||
if (!inventory.getStack(outputSlot).isItemEqualIgnoreDamage(outputStack))
|
if (!outputSlotStack.isItemEqualIgnoreDamage(outputStack))
|
||||||
return false;
|
return false;
|
||||||
int result = inventory.getStack(outputSlot).getCount() + outputStack.getCount();
|
int result = outputSlotStack.getCount() + outputStack.getCount();
|
||||||
return result <= inventory.getStackLimit() && result <= outputStack.getMaxCount();
|
return result <= inventory.getStackLimit() && result <= outputStack.getMaxCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,22 +32,26 @@ import techreborn.items.DynamicCellItem;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class RecipeUtils {
|
public class RecipeUtils {
|
||||||
@NotNull
|
public static <T extends Recipe<?>> Optional<Recipe<?>> getMatchingRecipe(World world, RecipeType<T> type, ItemStack input) {
|
||||||
public static ItemStack getEmptyCell(int stackSize) {
|
return getRecipes(world, type).stream()
|
||||||
return DynamicCellItem.getEmptyCell(stackSize);
|
.filter(recipe -> matchesSingleInput(recipe, input))
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean matchesSingleInput(Recipe<?> recipe, ItemStack input) {
|
||||||
|
return recipe.getPreviewInputs().size() == 1 && recipe.getPreviewInputs().get(0).test(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to get the matching output of a recipe type that only has 1 input
|
* Used to get the matching output of a recipe type that only has 1 input
|
||||||
*/
|
*/
|
||||||
public static <T extends Recipe<?>> ItemStack getMatchingRecipes(World world, RecipeType<T> type, ItemStack input) {
|
public static <T extends Recipe<?>> ItemStack getMatchingRecipeOutput(World world, RecipeType<T> type, ItemStack input) {
|
||||||
return getRecipes(world, type).stream()
|
return getMatchingRecipe(world, type, input)
|
||||||
.filter(recipe -> recipe.getPreviewInputs().size() == 1 && recipe.getPreviewInputs().get(0).test(input))
|
|
||||||
.map(Recipe::getOutput)
|
.map(Recipe::getOutput)
|
||||||
.findFirst()
|
|
||||||
.orElse(ItemStack.EMPTY);
|
.orElse(ItemStack.EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue