Skip to content

Commit

Permalink
Reduce allocations in RecipeBuilder (GregTechCEu#2434)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechLord22 authored Mar 31, 2024
1 parent e6cc493 commit 456c1ae
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions src/main/java/gregtech/api/recipes/RecipeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @see Recipe
Expand Down Expand Up @@ -388,10 +387,21 @@ public R inputNBT(@NotNull ItemStack stack, NBTMatcher matcher, NBTCondition con
return inputNBT(new GTRecipeItemInput(stack), matcher, condition);
}

public R inputs(ItemStack input) {
if (input == null || input.isEmpty()) {
GTLog.logger.error("Input cannot be null or empty. Input: {}", input);
GTLog.logger.error("Stacktrace:", new IllegalArgumentException());
recipeStatus = EnumValidationResult.INVALID;
} else {
this.inputs.add(new GTRecipeItemInput(input));
}
return (R) this;
}

public R inputs(ItemStack... inputs) {
for (ItemStack input : inputs) {
if (input == null || input.isEmpty()) {
GTLog.logger.error("Input cannot contain null or empty ItemStacks. Inputs: {}", input);
GTLog.logger.error("Inputs cannot contain null or empty ItemStacks. Inputs: {}", input);
GTLog.logger.error("Stacktrace:", new IllegalArgumentException());
recipeStatus = EnumValidationResult.INVALID;
continue;
Expand All @@ -414,10 +424,21 @@ public R inputStacks(Collection<ItemStack> inputs) {
return (R) this;
}

public R inputs(GTRecipeInput input) {
if (input.getAmount() < 0) {
GTLog.logger.error("Input count cannot be less than 0. Actual: {}.", input.getAmount());
GTLog.logger.error("Stacktrace:", new IllegalArgumentException());
recipeStatus = EnumValidationResult.INVALID;
} else {
this.inputs.add(input);
}
return (R) this;
}

public R inputs(GTRecipeInput... inputs) {
for (GTRecipeInput input : inputs) {
if (input.getAmount() < 0) {
GTLog.logger.error("Count cannot be less than 0. Actual: {}.", input.getAmount());
GTLog.logger.error("Input count cannot be less than 0. Actual: {}.", input.getAmount());
GTLog.logger.error("Stacktrace:", new IllegalArgumentException());
recipeStatus = EnumValidationResult.INVALID;
continue;
Expand Down Expand Up @@ -532,6 +553,13 @@ public R output(MetaTileEntity mte, int amount) {
return outputs(mte.getStackForm(amount));
}

public R outputs(ItemStack output) {
if (output != null && !output.isEmpty()) {
this.outputs.add(output);
}
return (R) this;
}

public R outputs(ItemStack... outputs) {
return outputs(Arrays.asList(outputs));
}
Expand All @@ -558,13 +586,25 @@ public R fluidInputs(GTRecipeInput fluidIngredient) {
return (R) this;
}

public R fluidInputs(FluidStack input) {
if (input != null && input.amount > 0) {
this.fluidInputs.add(new GTRecipeFluidInput(input));
} else if (input != null) {
GTLog.logger.error("Fluid Input count cannot be less than 0. Actual: {}.", input.amount);
GTLog.logger.error("Stacktrace:", new IllegalArgumentException());
} else {
GTLog.logger.error("FluidStack cannot be null.");
}
return (R) this;
}

public R fluidInputs(FluidStack... fluidStacks) {
ArrayList<GTRecipeInput> fluidIngredients = new ArrayList<>();
for (FluidStack fluidStack : fluidStacks) {
if (fluidStack != null && fluidStack.amount > 0) {
fluidIngredients.add(new GTRecipeFluidInput(fluidStack));
} else if (fluidStack != null) {
GTLog.logger.error("Count cannot be less than 0. Actual: {}.", fluidStack.amount);
GTLog.logger.error("Fluid Input count cannot be less than 0. Actual: {}.", fluidStack.amount);
GTLog.logger.error("Stacktrace:", new IllegalArgumentException());
} else {
GTLog.logger.error("FluidStack cannot be null.");
Expand All @@ -579,13 +619,20 @@ public R clearFluidInputs() {
return (R) this;
}

public R fluidOutputs(FluidStack output) {
if (output != null && output.amount > 0) {
this.fluidOutputs.add(output);
}
return (R) this;
}

public R fluidOutputs(FluidStack... outputs) {
return fluidOutputs(Arrays.asList(outputs));
}

public R fluidOutputs(Collection<FluidStack> outputs) {
outputs = new ArrayList<>(outputs);
outputs.removeIf(Objects::isNull);
outputs.removeIf(o -> o == null || o.amount <= 0);
this.fluidOutputs.addAll(outputs);
return (R) this;
}
Expand Down

0 comments on commit 456c1ae

Please sign in to comment.