Skip to content

Commit

Permalink
chore: simplify canProcess and extract isIngredient
Browse files Browse the repository at this point in the history
  • Loading branch information
klikli-dev committed Aug 20, 2024
1 parent 5c5bdd8 commit 0b8a717
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public CalcinationCraftingBehaviour(BlockEntity blockEntity, Supplier<IItemHandl
}

@Override
public boolean canProcess(ItemStack stack) {
if (this.alreadyHasInput(stack))
return true; //early out if we are already processing this type of item

public boolean isIngredient(ItemStack stack) {
return this.recipeCachedCheck.getRecipeFor(stack, Objects.requireNonNull(this.blockEntity.getLevel())).isPresent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public DigestionCraftingBehaviour(BlockEntity blockEntity, Supplier<IItemHandler
}

@Override
public boolean canProcess(ItemStack stack) {
if (ItemStack.isSameItemSameComponents(stack, this.inputInventorySupplier.get().getStackInSlot(0)))
return true; //early out if we are already processing this type of item


public boolean isIngredient(ItemStack stack) {
return this.recipeCachedCheck.getRecipeFor(stack, this.blockEntity.getLevel()).isPresent();
}

Expand All @@ -49,6 +45,11 @@ public boolean canProcess(FluidStack stack) {
return true; //early out if we are already processing this type of fluid

//now we use our custom cached check that checks only liquids:
return this.isIngredient(stack);
}

@Override
public boolean isIngredient(FluidStack stack) {
return this.recipeCachedCheck.getRecipeFor(stack, this.blockEntity.getLevel()).isPresent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public DistillationCraftingBehaviour(BlockEntity blockEntity, Supplier<IItemHand
}

@Override
public boolean canProcess(ItemStack stack) {
if (this.alreadyHasInput(stack))
return true; //early out if we are already processing this type of item

public boolean isIngredient(ItemStack stack) {
return this.recipeCachedCheck.getRecipeFor(stack, this.blockEntity.getLevel()).isPresent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import net.neoforged.neoforge.items.ItemHandlerHelper;

import java.util.function.Supplier;
import java.util.stream.IntStream;

public class FermentationCraftingBehaviour extends CraftingBehaviour<ItemHandlerWithFluidRecipeInput, FermentationRecipe, FermentationCachedCheck> {

Expand All @@ -35,10 +34,7 @@ public FermentationCraftingBehaviour(BlockEntity blockEntity, Supplier<IItemHand
}

@Override
public boolean canProcess(ItemStack stack) {
if (this.alreadyHasInput(stack))
return true; //early out if we are already processing this type of item

public boolean isIngredient(ItemStack stack) {
return this.recipeCachedCheck.getRecipeFor(stack, this.blockEntity.getLevel()).isPresent();
}

Expand All @@ -48,6 +44,11 @@ public boolean canProcess(FluidStack stack) {
return true; //early out if we are already processing this type of fluid

//now we use our custom cached check that checks only liquids:
return this.isIngredient(stack);
}

@Override
public boolean isIngredient(FluidStack stack) {
return this.recipeCachedCheck.getRecipeFor(stack, this.blockEntity.getLevel()).isPresent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public boolean canProcess(ItemStack stack) {
return true; //not used because the vessels handle their input on their own.
}

@Override
public boolean isIngredient(ItemStack stack) {
return true; //not used because the vessels handle their input on their own.
}

@Override
protected boolean craft(RecipeHolder<IncubationRecipe> pRecipe) {
var ItemHandlerRecipeInput = this.recipeWrapperSupplier.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ public LiquefactionCraftingBehaviour(BlockEntity blockEntity, Supplier<IItemHand
}

@Override
public boolean canProcess(ItemStack stack) {
if (this.alreadyHasInput(stack))
return true; //early out if we are already processing this type of item


public boolean isIngredient(ItemStack stack) {
return this.recipeCachedCheck.getRecipeFor(stack, this.blockEntity.getLevel()).isPresent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public MercuryCatalystCraftingBehaviour(BlockEntity blockEntity, Supplier<IItemH
}

@Override
public boolean canProcess(ItemStack stack) {
if (this.alreadyHasInput(stack))
return true; //early out if we are already processing this type of item

public boolean isIngredient(ItemStack stack) {
var tempInv = new ItemStackHandler(NonNullList.of(ItemStack.EMPTY, stack));
var tempRecipeWrapper = new ItemHandlerRecipeInput(tempInv);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ public ReformationArrayCraftingBehaviour(BlockEntity blockEntity, Supplier<Refor
}

@Override
public boolean canProcess(ItemStack stack) {
if (this.alreadyHasInput(stack))
return true; //early out if we are already processing this type of item

public boolean isIngredient(ItemStack stack) {
return this.recipeCachedCheck.getRecipeFor(this.recipeWrapperSupplier.get(), this.blockEntity.getLevel()).isPresent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ public SalAmmoniacAccumulatorCraftingBehaviour(BlockEntity blockEntity, Supplier
}

@Override
public boolean canProcess(ItemStack stack) {
if (this.alreadyHasInput(stack))
return true; //early out if we are already processing this type of item

public boolean isIngredient(ItemStack stack) {
return this.recipeCachedCheck.getRecipeFor(stack, this.blockEntity.getLevel()).isPresent();
}

Expand All @@ -49,6 +46,11 @@ public boolean canProcess(FluidStack stack) {
return true; //early out if we are already processing this type of fluid

//now we use our custom cached check that checks only liquids:
return this.isIngredient(stack);
}

@Override
public boolean isIngredient(FluidStack stack) {
return this.recipeCachedCheck.getRecipeFor(stack, this.blockEntity.getLevel()).isPresent();
}

Expand Down Expand Up @@ -106,7 +108,7 @@ protected boolean craft(RecipeHolder<AccumulationRecipe> pRecipe) {
this.inputInventorySupplier.get().extractItem(0, this.getIngredientCount(pRecipe), false);
}

if(pRecipe.value().hasEvaporant()){
if (pRecipe.value().hasEvaporant()) {
this.waterTankSupplier.get().drain(pRecipe.value().getEvaporantAmount(), IFluidHandler.FluidAction.EXECUTE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,41 @@ public void onInputItemChanged(ItemStack oldStack, ItemStack newStack) {
this.progress = 0;
}

public abstract boolean canProcess(ItemStack stack);
/**
* Returns true if the crafting behaviour can currently process the given stack.
* It might return false even for a valid ingredient, e.g. if another recipe is already being processed.
*/
public boolean canProcess(ItemStack stack) {
if (this.alreadyHasInput(stack))
return true; //early out if we are already processing this type of item

return this.isIngredient(stack);
}

/**
* Returns true if the crafting behaviour can *generally* process the given stack.
* This is used for visual indicators, e.g. to highlight fitting items.
* Returning true does not mean that the item can be processed right now, as it might be blocked by other factors.
*/
public abstract boolean isIngredient(ItemStack stack);

/**
* Returns true if the crafting behaviour can currently process the given fluid stack.
* It might return false even for a valid ingredient, e.g. if another recipe is already being processed.
*/
public boolean canProcess(FluidStack stack) {
return false;
}

/**
* Returns true if the crafting behaviour can *generally* process the given fluid stack.
* This is used for visual indicators, e.g. to highlight fitting items.
* Returning true does not mean that the fluid can be processed right now, as it might be blocked by other factors.
*/
public boolean isIngredient(FluidStack stack) {
return false;
}

/**
* Returns true if the input inventory already contains a stack matching the given stack.
* This allows us to early out of canProcess.
Expand Down

0 comments on commit 0b8a717

Please sign in to comment.