Skip to content

Commit 6319779

Browse files
authored
Validate KubeJS recipes to ensure no invalid/missing ingredients (#4170)
1 parent 846eeb7 commit 6319779

File tree

1 file changed

+98
-3
lines changed

1 file changed

+98
-3
lines changed

src/main/java/com/gregtechceu/gtceu/integration/kjs/recipe/GTRecipeSchema.java

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ public GTRecipeJS itemInput(MaterialEntry input, int count) {
264264
}
265265

266266
public GTRecipeJS inputItems(InputItem... inputs) {
267+
validateItems("input", inputs);
268+
267269
for (var stack : inputs) {
268270
// test simple item that have pure singular material stack
269271
var matStack = ChemicalHelper.getMaterialStack(stack.ingredient.getItems()[0].getItem());
@@ -286,6 +288,8 @@ public GTRecipeJS inputItems(InputItem... inputs) {
286288
}
287289

288290
public GTRecipeJS inputItems(ItemStack... inputs) {
291+
validateItems("input", inputs);
292+
289293
for (ItemStack itemStack : inputs) {
290294
// test simple item that have pure singular material stack
291295
var matStack = ChemicalHelper.getMaterialStack(itemStack);
@@ -365,10 +369,12 @@ public GTRecipeJS itemInputsRanged(ExtendedOutputItem ingredient, int min, int m
365369
}
366370

367371
public GTRecipeJS inputItemsRanged(Ingredient ingredient, int min, int max) {
372+
validateItems("ranged input", ingredient);
368373
return input(ItemRecipeCapability.CAP, new ExtendedOutputItem(ingredient, 1, UniformInt.of(min, max)));
369374
}
370375

371376
public GTRecipeJS inputItemsRanged(ItemStack stack, int min, int max) {
377+
validateItems("ranged input", stack);
372378
return input(ItemRecipeCapability.CAP, new ExtendedOutputItem(stack, UniformInt.of(min, max)));
373379
}
374380

@@ -389,6 +395,7 @@ public GTRecipeJS itemOutput(MaterialEntry materialEntry, int count) {
389395
}
390396

391397
public GTRecipeJS outputItems(ExtendedOutputItem... outputs) {
398+
validateItems("output", outputs);
392399
for (ExtendedOutputItem itemStack : outputs) {
393400
if (itemStack.isEmpty()) {
394401
throw new RecipeExceptionJS(String.format("Output items is empty, id: %s", id));
@@ -426,10 +433,12 @@ public GTRecipeJS itemOutputsRanged(ExtendedOutputItem ingredient, int min, int
426433
}
427434

428435
public GTRecipeJS outputItemsRanged(Ingredient ingredient, int min, int max) {
436+
validateItems("ranged output", ingredient);
429437
return output(ItemRecipeCapability.CAP, new ExtendedOutputItem(ingredient, 1, UniformInt.of(min, max)));
430438
}
431439

432440
public GTRecipeJS outputItemsRanged(ItemStack stack, int min, int max) {
441+
validateItems("ranged output", stack);
433442
return output(ItemRecipeCapability.CAP, new ExtendedOutputItem(stack, UniformInt.of(min, max)));
434443
}
435444

@@ -438,6 +447,8 @@ public GTRecipeJS outputItemsRanged(TagPrefix orePrefix, Material material, int
438447
}
439448

440449
public GTRecipeJS notConsumable(InputItem itemStack) {
450+
validateItems("not consumable", itemStack);
451+
441452
int lastChance = this.chance;
442453
this.chance = 0;
443454
inputItems(itemStack);
@@ -446,6 +457,8 @@ public GTRecipeJS notConsumable(InputItem itemStack) {
446457
}
447458

448459
public GTRecipeJS notConsumable(TagPrefix orePrefix, Material material) {
460+
validateItems("not consumable", orePrefix);
461+
449462
int lastChance = this.chance;
450463
this.chance = 0;
451464
inputItems(orePrefix, material);
@@ -454,6 +467,8 @@ public GTRecipeJS notConsumable(TagPrefix orePrefix, Material material) {
454467
}
455468

456469
public GTRecipeJS notConsumableFluid(GTRecipeComponents.FluidIngredientJS fluid) {
470+
validateFluids("not consumable", fluid);
471+
457472
int lastChance = this.chance;
458473
this.chance = 0;
459474
inputFluids(fluid);
@@ -469,6 +484,8 @@ public GTRecipeJS circuit(int configuration) {
469484
}
470485

471486
public GTRecipeJS chancedInput(InputItem stack, int chance, int tierChanceBoost) {
487+
validateItems("chanced input", stack);
488+
472489
if (0 >= chance || chance > ChanceLogic.getMaxChancedValue()) {
473490
throw new RecipeExceptionJS(
474491
String.format("Chance cannot be less or equal to 0 or more than %s, Actual: %s, id: %s",
@@ -486,6 +503,8 @@ public GTRecipeJS chancedInput(InputItem stack, int chance, int tierChanceBoost)
486503

487504
public GTRecipeJS chancedFluidInput(GTRecipeComponents.FluidIngredientJS stack, int chance,
488505
int tierChanceBoost) {
506+
validateFluids("chanced input", stack);
507+
489508
if (0 >= chance || chance > ChanceLogic.getMaxChancedValue()) {
490509
throw new RecipeExceptionJS(
491510
String.format("Chance cannot be less or equal to 0 or more than %s, Actual: %s, id: %s",
@@ -502,6 +521,8 @@ public GTRecipeJS chancedFluidInput(GTRecipeComponents.FluidIngredientJS stack,
502521
}
503522

504523
public GTRecipeJS chancedOutput(ExtendedOutputItem stack, int chance, int tierChanceBoost) {
524+
validateItems("chanced output", stack);
525+
505526
if (0 >= chance || chance > ChanceLogic.getMaxChancedValue()) {
506527
throw new RecipeExceptionJS(
507528
String.format("Chance cannot be less or equal to 0 or more than %s, Actual: %s, id: %s",
@@ -527,9 +548,7 @@ public GTRecipeJS chancedOutput(TagPrefix tag, Material mat, int count, int chan
527548
}
528549

529550
public GTRecipeJS chancedOutput(ExtendedOutputItem stack, String fraction, int tierChanceBoost) {
530-
if (stack.isEmpty()) {
531-
return this;
532-
}
551+
validateItems("chanced output", stack);
533552

534553
String[] split = fraction.split("/");
535554
if (split.length > 2) {
@@ -600,6 +619,8 @@ public GTRecipeJS chancedOutput(TagPrefix prefix, Material material, String frac
600619
}
601620

602621
public GTRecipeJS chancedFluidOutput(FluidStackJS stack, int chance, int tierChanceBoost) {
622+
validateFluids("chanced output", stack);
623+
603624
if (0 >= chance || chance > ChanceLogic.getMaxChancedValue()) {
604625
throw new RecipeExceptionJS(
605626
String.format("Chance cannot be less or equal to 0 or more than %s, Actual: %s, id: %s",
@@ -616,6 +637,8 @@ public GTRecipeJS chancedFluidOutput(FluidStackJS stack, int chance, int tierCha
616637
}
617638

618639
public GTRecipeJS chancedFluidOutput(FluidStackJS stack, String fraction, int tierChanceBoost) {
640+
validateFluids("chanced output", stack);
641+
619642
if (stack.getAmount() == 0) {
620643
return this;
621644
}
@@ -724,6 +747,8 @@ public GTRecipeJS chancedTickInputLogic(RecipeCapability<?> cap, ChanceLogic log
724747
}
725748

726749
public GTRecipeJS inputFluids(GTRecipeComponents.FluidIngredientJS... inputs) {
750+
validateFluids("input", inputs);
751+
727752
for (var fluidIng : inputs) {
728753
for (var stack : fluidIng.ingredient().getStacks()) {
729754
var mat = ChemicalHelper.getMaterial(stack.getFluid());
@@ -741,12 +766,14 @@ public GTRecipeJS inputFluidsRanged(FluidStackJS input, int min, int max) {
741766
}
742767

743768
public GTRecipeJS inputFluidsRanged(FluidStackJS input, IntProvider range) {
769+
validateFluids("ranged input", input);
744770
FluidStack stack = new FluidStack(input.getFluid(), (int) input.getAmount(), input.getNbt());
745771
return input(FluidRecipeCapability.CAP,
746772
IntProviderFluidIngredient.of(FluidIngredient.of(stack), range));
747773
}
748774

749775
public GTRecipeJS outputFluids(FluidStackJS... outputs) {
776+
validateFluids("output", outputs);
750777
return output(FluidRecipeCapability.CAP, (Object[]) outputs);
751778
}
752779

@@ -755,11 +782,79 @@ public GTRecipeJS outputFluidsRanged(FluidStackJS output, int min, int max) {
755782
}
756783

757784
public GTRecipeJS outputFluidsRanged(FluidStackJS output, IntProvider range) {
785+
validateFluids("ranged output", output);
758786
FluidStack stack = new FluidStack(output.getFluid(), (int) output.getAmount(), output.getNbt());
759787
return output(FluidRecipeCapability.CAP,
760788
IntProviderFluidIngredient.of(FluidIngredient.of(stack), range));
761789
}
762790

791+
//////////////////////////////////////
792+
// ********** VALIDATION ***********//
793+
//////////////////////////////////////
794+
795+
private void validateItems(@NotNull String type, InputItem... items) {
796+
for (var stack : items) {
797+
if (stack == null || stack.isEmpty()) {
798+
throw new RecipeExceptionJS(String.format("Invalid or empty %s item (recipe ID: %s)", type, id));
799+
}
800+
}
801+
}
802+
803+
private void validateItems(@NotNull String type, ItemStack... stacks) {
804+
for (var stack : stacks) {
805+
if (stack == null || stack.isEmpty()) {
806+
throw new RecipeExceptionJS(String.format("Invalid or empty %s item (recipe ID: %s)", type, id));
807+
}
808+
}
809+
}
810+
811+
private void validateItems(@NotNull String type, Ingredient... ingredients) {
812+
for (var ingredient : ingredients) {
813+
if (ingredient == null || ingredient.isEmpty()) {
814+
throw new RecipeExceptionJS(String.format("Invalid or empty %s item (recipe ID: %s)", type, id));
815+
}
816+
}
817+
}
818+
819+
private void validateItems(@NotNull String type, OutputItem... items) {
820+
for (var item : items) {
821+
if (item == null || item.item == null || item.item.isEmpty()) {
822+
throw new RecipeExceptionJS(String.format("Invalid or empty %s item (recipe ID: %s)", type, id));
823+
}
824+
}
825+
}
826+
827+
private void validateItems(@NotNull String type, TagPrefix... items) {
828+
for (var item : items) {
829+
if (item == null || item.isEmpty()) {
830+
throw new RecipeExceptionJS(String.format("Invalid or empty %s item (recipe ID: %s)", type, id));
831+
}
832+
}
833+
}
834+
835+
private void validateFluids(@NotNull String type, GTRecipeComponents.FluidIngredientJS... fluids) {
836+
for (var fluid : fluids) {
837+
if (fluid == null || fluid.ingredient() == null || fluid.ingredient().getStacks() == null) {
838+
throw new RecipeExceptionJS(String.format("Invalid or empty %s fluid (recipe ID: %s)", type, id));
839+
}
840+
841+
for (var stack : fluid.ingredient().getStacks()) {
842+
if (stack == null || stack.isEmpty()) {
843+
throw new RecipeExceptionJS(
844+
String.format("Invalid or empty %s fluid (recipe ID: %s)", type, id));
845+
}
846+
}
847+
}
848+
}
849+
850+
private void validateFluids(@NotNull String type, FluidStackJS... stacks) {
851+
for (var stack : stacks) {
852+
if (stack == null || stack.getFluidStack() == null || stack.getFluidStack().isEmpty()) {
853+
throw new RecipeExceptionJS(String.format("Invalid or empty %s fluid (recipe ID: %s)", type, id));
854+
}
855+
}
856+
}
857+
763858
//////////////////////////////////////
764859
// ********** DATA ***********//
765860
//////////////////////////////////////

0 commit comments

Comments
 (0)