Skip to content

Commit 048f7b8

Browse files
DilithiumThoridedz894jurrejelleFourIsTheNumberBumperdo09
authored
Ranged Inputs (#3694)
Co-authored-by: dz894 <76979663+dz894@users.noreply.github.com> Co-authored-by: ApatheticLlama <76979663+ApatheticLlama@users.noreply.github.com> Co-authored-by: Jurre Groenendijk <jurre@jilles.com> Co-authored-by: FourIsTheNumber <33456283+FourIsTheNumber@users.noreply.github.com> Co-authored-by: Bumperdo09 <45916709+Bumperdo09@users.noreply.github.com> Co-authored-by: LeoDreamer <110815661+LeoDreamer2004@users.noreply.github.com> Co-authored-by: screret <68943070+screret@users.noreply.github.com> Co-authored-by: 楓 <r0yalist@outlook.com> Co-authored-by: YoungOnion <39562198+YoungOnionMC@users.noreply.github.com> Co-authored-by: WinExp <55543397+WinExp@users.noreply.github.com> Co-authored-by: Ghostipedia / Caitlynn <46772882+Ghostipedia@users.noreply.github.com> Co-authored-by: Maya <10861407+serenibyss@users.noreply.github.com> Co-authored-by: Gustavo <77560533+gustovafing@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent abac467 commit 048f7b8

11 files changed

Lines changed: 111 additions & 51 deletions

File tree

docs/content/Modpacks/Other-Topics/Adding-and-Removing-Recipes.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ ServerEvents.recipes(event => {
8686
- `.itemInput()`
8787
- `.itemInputs()`
8888
- `.chancedInput()`
89+
- `.itemInputsRanged()`
8990
- `.notConsumable()`
9091
- Fluids:
9192
- `.inputFluids()`
9293
- `.chancedFluidInput()`
94+
- `.inputFluidsRanged()`
9395
- `.notConsumableFluid()`
9496
- Misc:
9597
- `.circuit()`
@@ -98,7 +100,7 @@ ServerEvents.recipes(event => {
98100
- `.itemOutput()`
99101
- `.itemOutputs()`
100102
- `.chancedOutput()`
101-
- `.outputItemsRanged()`
103+
- `.itemOutputsRanged()`
102104
- Fluids:
103105
- `.outputFluids()`
104106
- `.chancedFluidOutput()`
@@ -126,8 +128,8 @@ ServerEvents.recipes(event => {
126128
Behavior was changed in 7.0.0.
127129
- `first` - Makes a chance roll for each item/fluid, in order of registration. Only the first item which succeeds
128130
on its roll is returned. Prior to 7.0.0, this was the behavior of `xor` logic.
129-
- Ranged Outputs:
130-
- Item or Fluid outputs that will produce a random amount within a `min, max` range (inclusive).
131+
- Ranged Ingredients:
132+
- Item or Fluid ingredients that will be consumed or produced in a random amount within a `min, max` range (inclusive).
131133
- Circuits
132134
- Many GT recipes use a `Programmed Circuit` item with a Configuration value of `1-32` as a `Non-Consumed` input,
133135
to distinguish them from other recipes in the same machine with similar ingredients. `.circuit()` adds one to a recipe.

src/main/java/com/gregtechceu/gtceu/api/machine/multiblock/MultiblockDisplayText.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,11 @@ public Builder addOutputLines(GTRecipe recipe) {
358358
for (var item : itemOutputs) {
359359
boolean rounded = false;
360360
ItemStack stack;
361+
// number of items output by a non-ranged ingredient
361362
int count = 0;
363+
// number of items output, but stored as a double. Used for accurate items/second display.
362364
double countD = 1;
365+
// number of items output which is actually displayed. Can be either a number, or a range.
363366
Component displaycount;
364367
if (item.content instanceof IntProviderIngredient provider) {
365368
rounded = true;
@@ -383,7 +386,7 @@ public Builder addOutputLines(GTRecipe recipe) {
383386
countD = countD * runs * function.getBoostedChance(item, recipeTier, chanceTier) /
384387
item.maxChance;
385388
}
386-
count = countD < 1 ? 1 : (int) Math.round(countD);
389+
count = Math.max(1, (int) Math.round(countD));
387390
displaycount = Component.literal(String.valueOf(count));
388391
}
389392
if (countD < maxDurationSec) {
@@ -399,8 +402,11 @@ public Builder addOutputLines(GTRecipe recipe) {
399402
for (var fluid : fluidOutputs) {
400403
boolean rounded = false;
401404
FluidStack stack;
405+
// amount of fluid output by a non-ranged ingredient
402406
int amount = 0;
407+
// amount of fluid output, but stored as a double. Used for accurate fluid/second display.
403408
double amountD = 1;
409+
// amount of fluid output which is actually displayed. Can be either a number, or a range.
404410
Component displaycount;
405411
if (fluid.content instanceof IntProviderFluidIngredient provider) {
406412
rounded = true;
@@ -424,7 +430,7 @@ public Builder addOutputLines(GTRecipe recipe) {
424430
amountD = amountD * runs * function.getBoostedChance(fluid, recipeTier, chanceTier) /
425431
fluid.maxChance;
426432
}
427-
amount = amountD < 1 ? 1 : (int) Math.round(amountD);
433+
amount = Math.max(1, (int) Math.round(amountD));
428434
displaycount = Component.literal(String.valueOf(amount));
429435
}
430436
if (amountD < maxDurationSec) {

src/main/java/com/gregtechceu/gtceu/api/machine/trait/NotifiableFluidTank.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public List<FluidIngredient> handleRecipeInner(IO io, GTRecipe recipe, List<Flui
119119

120120
FluidStack[] fluids;
121121

122-
if (io == IO.OUT && ingredient instanceof IntProviderFluidIngredient provider) {
122+
if (ingredient instanceof IntProviderFluidIngredient provider) {
123123
provider.setFluidStacks(null);
124124
provider.setSampledCount(-1);
125125

src/main/java/com/gregtechceu/gtceu/api/machine/trait/NotifiableItemStackHandler.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static List<Ingredient> handleRecipe(IO io, GTRecipe recipe, List<Ingredi
113113

114114
ItemStack[] items;
115115
int amount;
116-
if (io == IO.OUT && ingredient instanceof IntProviderIngredient provider) {
116+
if (ingredient instanceof IntProviderIngredient provider) {
117117
provider.setItemStacks(null);
118118
provider.setSampledCount(-1);
119119

@@ -129,21 +129,23 @@ public static List<Ingredient> handleRecipe(IO io, GTRecipe recipe, List<Ingredi
129129
}
130130
output = items[0];
131131
}
132-
133-
int outputStorageLimit = 0;
134-
for (int slot = 0; slot < storage.getSlots(); ++slot) {
135-
ItemStack stack = storage.getStackInSlot(slot);
136-
if (stack.isEmpty() || ItemStack.isSameItemSameTags(stack, output)) {
137-
outputStorageLimit += storage.getSlotLimit(slot) - stack.getCount();
132+
amount = output.getCount();
133+
if (io == IO.OUT) {
134+
int outputStorageLimit = 0;
135+
for (int slot = 0; slot < storage.getSlots(); ++slot) {
136+
ItemStack stack = storage.getStackInSlot(slot);
137+
if (stack.isEmpty() || ItemStack.isSameItemSameTags(stack, output)) {
138+
outputStorageLimit += storage.getSlotLimit(slot) - stack.getCount();
139+
}
140+
}
141+
if (provider.getCountProvider().getMinValue() > outputStorageLimit) {
142+
it.remove();
143+
continue;
144+
} else if (simulate) {
145+
amount = provider.getCountProvider().getMaxValue();
146+
} else {
147+
amount = Math.min(output.getCount(), outputStorageLimit);
138148
}
139-
}
140-
if (provider.getCountProvider().getMinValue() > outputStorageLimit) {
141-
it.remove();
142-
continue;
143-
} else if (simulate) {
144-
amount = provider.getCountProvider().getMaxValue();
145-
} else {
146-
amount = Math.min(output.getCount(), outputStorageLimit);
147149
}
148150
} else {
149151
items = ingredient.getItems();

src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/IntProviderFluidIngredient.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.gregtechceu.gtceu.GTCEu;
44
import com.gregtechceu.gtceu.api.GTValues;
5+
import com.gregtechceu.gtceu.config.ConfigHolder;
56

67
import net.minecraft.nbt.CompoundTag;
78
import net.minecraft.nbt.NbtOps;
@@ -12,7 +13,6 @@
1213
import net.minecraft.util.valueproviders.UniformInt;
1314
import net.minecraftforge.fluids.FluidStack;
1415

15-
import com.google.errorprone.annotations.DoNotCall;
1616
import com.google.gson.*;
1717
import com.mojang.serialization.Codec;
1818
import com.mojang.serialization.JsonOps;
@@ -22,8 +22,7 @@
2222

2323
/**
2424
* Allows a {@link FluidIngredient} to be created with a ranged {@code amount}, which will be randomly rolled upon
25-
* recipe completion.
26-
* Only valid as a recipe fluid {@code output}.
25+
* recipe start (input) / completion (output).
2726
* Instantiated using {@link IntProviderFluidIngredient#of()}, with a {@link FluidIngredient}
2827
* and either an {@link IntProvider} or {@code int, int} range bounds (inclusive).
2928
* Functions similarly to {@link IntProviderIngredient}.
@@ -66,9 +65,12 @@ public IntProviderFluidIngredient copy() {
6665
* You probably want either {@link IntProviderFluidIngredient#getStacks()} or
6766
* {@link IntProviderFluidIngredient#getMaxSizeStack()}.
6867
*/
69-
@DoNotCall
68+
@Deprecated
7069
@Override
7170
public int getAmount() {
71+
if (ConfigHolder.INSTANCE.dev.debug) {
72+
throw new IllegalCallerException("An IPFI should never have getAmount() called on it!");
73+
}
7274
return -1;
7375
}
7476

src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/IntProviderIngredient.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828

2929
/**
3030
* Allows an {@link Ingredient} to be created with a ranged {@code count}, which will be randomly rolled upon recipe
31-
* completion.
32-
* Only valid as a recipe item {@code output}.
31+
* start (input) / completion (output).
3332
* Instantiated using {@link IntProviderIngredient#of()}, with a {@link Ingredient} or {@link ItemStack},
3433
* and an {@link IntProvider}.
3534
* Functions similarly to {@link IntProviderFluidIngredient}.

src/main/java/com/gregtechceu/gtceu/api/recipe/lookup/ingredient/fluid/FluidStackMapIngredient.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.gregtechceu.gtceu.api.recipe.lookup.ingredient.fluid;
22

33
import com.gregtechceu.gtceu.api.recipe.ingredient.FluidIngredient;
4+
import com.gregtechceu.gtceu.api.recipe.ingredient.IntProviderFluidIngredient;
45
import com.gregtechceu.gtceu.api.recipe.lookup.ingredient.AbstractMapIngredient;
56

67
import net.minecraftforge.fluids.FluidStack;
@@ -30,7 +31,12 @@ public static List<AbstractMapIngredient> from(@NotNull FluidIngredient ingredie
3031
List<AbstractMapIngredient> ingredients = new ObjectArrayList<>();
3132
for (FluidIngredient.Value value : ingredient.values) {
3233
if (value instanceof FluidIngredient.FluidValue fluidValue) {
33-
FluidStack stack = new FluidStack(fluidValue.fluid(), ingredient.getAmount(), ingredient.getNbt());
34+
FluidStack stack = new FluidStack(fluidValue.fluid(),
35+
// wait. that's illegal.
36+
(ingredient instanceof IntProviderFluidIngredient provider ?
37+
provider.getCountProvider().getMaxValue() :
38+
ingredient.getAmount()),
39+
ingredient.getNbt());
3440
ingredients.add(new FluidStackMapIngredient(stack, ingredient));
3541
}
3642
}

src/main/java/com/gregtechceu/gtceu/data/recipe/builder/GTRecipeBuilder.java

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,7 @@ public GTRecipeBuilder inputItems(Object input, int count) {
337337
}
338338

339339
public GTRecipeBuilder inputItems(Ingredient inputs) {
340-
if (intProviderInputError(inputs, 0)) {
341-
return this;
342-
} else if (missingIngredientError(0, true, ItemRecipeCapability.CAP, inputs::isEmpty)) {
340+
if (missingIngredientError(0, true, ItemRecipeCapability.CAP, inputs::isEmpty)) {
343341
return this;
344342
}
345343
return input(ItemRecipeCapability.CAP, inputs);
@@ -349,9 +347,7 @@ public GTRecipeBuilder inputItems(Ingredient... inputs) {
349347
List<Ingredient> ingredients = new ArrayList<>();
350348
for (int i = 0; i < inputs.length; i++) {
351349
var ingredient = inputs[i];
352-
if (intProviderInputError(ingredient, i)) {
353-
return this;
354-
} else if (missingIngredientError(i, true, ItemRecipeCapability.CAP, ingredient::isEmpty)) {
350+
if (missingIngredientError(i, true, ItemRecipeCapability.CAP, ingredient::isEmpty)) {
355351
return this;
356352
} else {
357353
ingredients.add(ingredient);
@@ -361,9 +357,7 @@ public GTRecipeBuilder inputItems(Ingredient... inputs) {
361357
}
362358

363359
public GTRecipeBuilder inputItems(Ingredient inputs, int count) {
364-
if (intProviderInputError(inputs, 0)) {
365-
return this;
366-
} else if (missingIngredientError(0, true, ItemRecipeCapability.CAP, inputs::isEmpty)) {
360+
if (missingIngredientError(0, true, ItemRecipeCapability.CAP, inputs::isEmpty)) {
367361
return this;
368362
}
369363
return input(ItemRecipeCapability.CAP, SizedIngredient.create(inputs, count));
@@ -483,6 +477,31 @@ public GTRecipeBuilder inputItems(MachineDefinition machine, int count) {
483477
return inputItems(machine.asStack(count));
484478
}
485479

480+
public GTRecipeBuilder inputItemsRanged(ItemStack input, IntProvider intProvider) {
481+
return inputItems(IntProviderIngredient.of(input, intProvider));
482+
}
483+
484+
public GTRecipeBuilder inputItemsRanged(Item input, IntProvider intProvider) {
485+
return inputItemsRanged(new ItemStack(input), intProvider);
486+
}
487+
488+
public GTRecipeBuilder inputItemsRanged(Supplier<? extends ItemLike> input, IntProvider intProvider) {
489+
return inputItemsRanged(new ItemStack(input.get().asItem()), intProvider);
490+
}
491+
492+
public GTRecipeBuilder inputItemsRanged(TagPrefix orePrefix, Material material, IntProvider intProvider) {
493+
var item = ChemicalHelper.get(orePrefix, material, 1);
494+
if (item.isEmpty()) {
495+
GTCEu.LOGGER.error("Tried to set input ranged item stack that doesn't exist, TagPrefix: {}, Material: {}",
496+
orePrefix, material);
497+
}
498+
return inputItemsRanged(item, intProvider);
499+
}
500+
501+
public GTRecipeBuilder inputItemsRanged(MachineDefinition machine, IntProvider intProvider) {
502+
return inputItemsRanged(machine.asStack(), intProvider);
503+
}
504+
486505
public GTRecipeBuilder outputItems(Object output) {
487506
if (output instanceof Item item) {
488507
return outputItems(item);
@@ -960,6 +979,14 @@ public GTRecipeBuilder inputFluids(FluidStack... inputs) {
960979
return input(FluidRecipeCapability.CAP, ingredients.toArray(FluidIngredient[]::new));
961980
}
962981

982+
public GTRecipeBuilder inputFluidsRanged(FluidStack input, IntProvider intProvider) {
983+
return inputFluidsRanged(FluidIngredient.of(input), intProvider);
984+
}
985+
986+
protected GTRecipeBuilder inputFluidsRanged(FluidIngredient input, IntProvider intProvider) {
987+
return inputFluids(IntProviderFluidIngredient.of(input, intProvider));
988+
}
989+
963990
public GTRecipeBuilder inputFluids(FluidIngredient... inputs) {
964991
return input(FluidRecipeCapability.CAP, inputs);
965992
}
@@ -1564,16 +1591,6 @@ protected void warnTooManyIngredients(RecipeCapability<?> capability,
15641591
}
15651592
}
15661593

1567-
protected boolean intProviderInputError(Ingredient ingredient, int index) {
1568-
if (ingredient instanceof IntProviderIngredient) {
1569-
int size = (perTick ? tickOutput : output).getOrDefault(ItemRecipeCapability.CAP, List.of()).size();
1570-
GTCEu.LOGGER.error("Using int provider ingredients as inputs is not supported!" +
1571-
"Input {} in recipe {} will be skipped.", size + index, id);
1572-
return true;
1573-
}
1574-
return false;
1575-
}
1576-
15771594
protected boolean missingIngredientError(int index, boolean isInput,
15781595
RecipeCapability<?> cap, BooleanSupplier empty) {
15791596
if (empty.getAsBoolean()) {

src/main/java/com/gregtechceu/gtceu/integration/jade/provider/RecipeOutputProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected void write(CompoundTag data, RecipeLogic recipeLogic) {
9292
int count = stack.getCount();
9393
double countD = (double) count * runs *
9494
function.getBoostedChance(item, recipeTier, chanceTier) / item.maxChance;
95-
count = countD < 1 ? 1 : (int) Math.round(countD);
95+
count = Math.max(1, (int) Math.round(countD));
9696
itemTag.putInt("Count", count);
9797
}
9898
}
@@ -128,7 +128,7 @@ protected void write(CompoundTag data, RecipeLogic recipeLogic) {
128128
int amount = stacks[0].getAmount();
129129
double amountD = (double) amount * runs *
130130
function.getBoostedChance(fluid, recipeTier, chanceTier) / fluid.maxChance;
131-
amount = amountD < 1 ? 1 : (int) Math.round(amountD);
131+
amount = Math.max(1, (int) Math.round(amountD));
132132
fluidTag.putInt("Amount", amount);
133133
}
134134
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,22 @@ public GTRecipeJS inputItems(MachineDefinition machine, int count) {
336336
return inputItems(machine.asStack(count));
337337
}
338338

339+
public GTRecipeJS itemInputsRanged(ExtendedOutputItem ingredient, int min, int max) {
340+
return inputItemsRanged(ingredient.ingredient.getInner(), min, max);
341+
}
342+
343+
public GTRecipeJS inputItemsRanged(Ingredient ingredient, int min, int max) {
344+
return input(ItemRecipeCapability.CAP, new ExtendedOutputItem(ingredient, 1, UniformInt.of(min, max)));
345+
}
346+
347+
public GTRecipeJS inputItemsRanged(ItemStack stack, int min, int max) {
348+
return input(ItemRecipeCapability.CAP, new ExtendedOutputItem(stack, UniformInt.of(min, max)));
349+
}
350+
351+
public GTRecipeJS itemInputsRanged(TagPrefix orePrefix, Material material, int min, int max) {
352+
return inputItemsRanged(ChemicalHelper.get(orePrefix, material), min, max);
353+
}
354+
339355
public GTRecipeJS itemOutputs(ExtendedOutputItem... outputs) {
340356
return outputItems(outputs);
341357
}
@@ -696,6 +712,16 @@ public GTRecipeJS inputFluids(GTRecipeComponents.FluidIngredientJS... inputs) {
696712
return input(FluidRecipeCapability.CAP, (Object[]) inputs);
697713
}
698714

715+
public GTRecipeJS inputFluidsRanged(FluidStackJS input, int min, int max) {
716+
return inputFluidsRanged(input, UniformInt.of(min, max));
717+
}
718+
719+
public GTRecipeJS inputFluidsRanged(FluidStackJS input, IntProvider range) {
720+
FluidStack stack = new FluidStack(input.getFluid(), (int) input.getAmount(), input.getNbt());
721+
return input(FluidRecipeCapability.CAP,
722+
IntProviderFluidIngredient.of(FluidIngredient.of(stack), range));
723+
}
724+
699725
public GTRecipeJS outputFluids(FluidStackJS... outputs) {
700726
return output(FluidRecipeCapability.CAP, (Object[]) outputs);
701727
}

0 commit comments

Comments
 (0)