Skip to content

Commit dbe468f

Browse files
Ranged Ingredient backported fixes (#4270)
1 parent 4a81c44 commit dbe468f

File tree

5 files changed

+75
-60
lines changed

5 files changed

+75
-60
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.gregtechceu.gtceu.api.recipe.ingredient;
2+
3+
import com.gregtechceu.gtceu.api.GTValues;
4+
5+
import net.minecraft.util.RandomSource;
6+
import net.minecraft.util.valueproviders.IntProvider;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public interface IRangedIngredient {
11+
12+
IntProvider getCountProvider();
13+
14+
int getSampledCount();
15+
16+
void setSampledCount(int count);
17+
18+
/**
19+
* If this ingredient has not yet had its count rolled, rolls it and returns the roll.
20+
* If it has, returns the existing roll.
21+
* Passthrough method, invokes {@code rollSampledCount()} using the threadsafe {@link GTValues#RNG}.
22+
*
23+
* @return the amount rolled
24+
*/
25+
default int rollSampledCount() {
26+
return rollSampledCount(GTValues.RNG);
27+
}
28+
29+
int rollSampledCount(@NotNull RandomSource random);
30+
31+
/**
32+
* @return the average roll of this ranged amount
33+
*/
34+
default double getMidRoll() {
35+
return ((getCountProvider().getMaxValue() + getCountProvider().getMinValue()) / 2.0);
36+
}
37+
38+
default boolean isRolled() {
39+
return getSampledCount() != -1;
40+
}
41+
42+
void reset();
43+
}

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* and either an {@link IntProvider} or {@code int, int} range bounds (inclusive).
3030
* Functions similarly to {@link IntProviderIngredient}.
3131
*/
32-
public class IntProviderFluidIngredient extends FluidIngredient {
32+
public class IntProviderFluidIngredient extends FluidIngredient implements IRangedIngredient {
3333

3434
public static final Codec<IntProviderFluidIngredient> CODEC = ExtraCodecs.JSON
3535
.xmap(IntProviderFluidIngredient::fromJson, IntProviderFluidIngredient::toJson);
@@ -39,6 +39,7 @@ public class IntProviderFluidIngredient extends FluidIngredient {
3939
/**
4040
* The last result of {@link IntProviderFluidIngredient#getSampledCount()}. -1 if not rolled.
4141
*/
42+
@Getter
4243
@Setter
4344
protected int sampledCount = -1;
4445
/**
@@ -92,7 +93,7 @@ public int getAmount() {
9293
@Override
9394
public FluidStack[] getStacks() {
9495
if (fluidStacks == null) {
95-
int cachedAmount = getSampledCount(GTValues.RNG);
96+
int cachedAmount = rollSampledCount(GTValues.RNG);
9697
if (cachedAmount == 0) {
9798
return EMPTY_STACK_ARRAY;
9899
}
@@ -119,19 +120,6 @@ public FluidStack[] getStacks() {
119120
return new FluidStack(in[0], countProvider.getMaxValue());
120121
}
121122

122-
/**
123-
* If this ingredient has not yet had its {@link IntProviderFluidIngredient#sampledCount} rolled, rolls it and
124-
* returns the roll.
125-
* If it has, returns the existing roll.
126-
* Passthrough method, invokes {@link IntProviderFluidIngredient#getSampledCount(RandomSource)} using the threadsafe
127-
* {@link GTValues#RNG}.
128-
*
129-
* @return the amount rolled
130-
*/
131-
public int getSampledCount() {
132-
return getSampledCount(GTValues.RNG);
133-
}
134-
135123
/**
136124
* If this ingredient has not yet had its {@link IntProviderFluidIngredient#sampledCount} rolled, rolls it and
137125
* returns the roll.
@@ -140,7 +128,7 @@ public int getSampledCount() {
140128
* @param random {@link RandomSource}, must be threadsafe, usually called using {@link GTValues#RNG}.
141129
* @return the amount rolled
142130
*/
143-
public int getSampledCount(@NotNull RandomSource random) {
131+
public int rollSampledCount(@NotNull RandomSource random) {
144132
if (sampledCount == -1) {
145133
sampledCount = countProvider.sample(random);
146134
}
@@ -162,7 +150,7 @@ public boolean isEmpty() {
162150
/**
163151
* Resets the random roll on this ingredient
164152
*/
165-
public void reroll() {
153+
public void reset() {
166154
sampledCount = -1;
167155
fluidStacks = null;
168156
}

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

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,17 @@
3333
* and an {@link IntProvider}.
3434
* Functions similarly to {@link IntProviderFluidIngredient}.
3535
*/
36-
public class IntProviderIngredient extends Ingredient {
36+
public class IntProviderIngredient extends Ingredient implements IRangedIngredient {
3737

3838
public static final ResourceLocation TYPE = GTCEu.id("int_provider");
3939
public static final ItemStack[] EMPTY_STACK_ARRAY = new ItemStack[0];
4040

4141
@Getter
4242
protected final IntProvider countProvider;
4343
/**
44-
* The last result of {@link IntProviderIngredient#getSampledCount(RandomSource)}. -1 if not rolled.
44+
* The last result of {@link IntProviderIngredient#rollSampledCount(RandomSource)}. -1 if not rolled.
4545
*/
46+
@Getter
4647
@Setter
4748
protected int sampledCount = -1;
4849
/**
@@ -99,7 +100,7 @@ public boolean test(@Nullable ItemStack stack) {
99100
@Override
100101
public ItemStack @NotNull [] getItems() {
101102
if (itemStacks == null) {
102-
int cachedCount = getSampledCount();
103+
int cachedCount = rollSampledCount();
103104
if (cachedCount == 0) {
104105
return EMPTY_STACK_ARRAY;
105106
}
@@ -124,19 +125,6 @@ public boolean test(@Nullable ItemStack stack) {
124125
else return inner.getItems()[0].copyWithCount(countProvider.getMaxValue());
125126
}
126127

127-
/**
128-
* If this ingredient has not yet had its {@link IntProviderIngredient#sampledCount} rolled, rolls it and
129-
* returns the roll.
130-
* If it has, returns the existing roll.
131-
* Passthrough method, invokes {@link IntProviderIngredient#getSampledCount(RandomSource)} using the threadsafe
132-
* {@link GTValues#RNG}.
133-
*
134-
* @return the amount rolled
135-
*/
136-
public int getSampledCount() {
137-
return getSampledCount(GTValues.RNG);
138-
}
139-
140128
/**
141129
* If this ingredient has not yet had its {@link IntProviderIngredient#sampledCount} rolled, rolls it and returns
142130
* the roll.
@@ -145,24 +133,17 @@ public int getSampledCount() {
145133
* @param random {@link RandomSource}, must be threadsafe, usually called using {@link GTValues#RNG}.
146134
* @return the count rolled
147135
*/
148-
public int getSampledCount(@NotNull RandomSource random) {
136+
public int rollSampledCount(@NotNull RandomSource random) {
149137
if (sampledCount == -1) {
150138
sampledCount = countProvider.sample(random);
151139
}
152140
return sampledCount;
153141
}
154142

155-
/**
156-
* @return the average roll of this ranged amount
157-
*/
158-
public double getMidRoll() {
159-
return ((countProvider.getMaxValue() + countProvider.getMinValue()) / 2.0);
160-
}
161-
162143
/**
163144
* Resets the random roll on this ingredient
164145
*/
165-
public void reroll() {
146+
public void reset() {
166147
sampledCount = -1;
167148
itemStacks = null;
168149
}

src/test/java/com/gregtechceu/gtceu/api/recipe/ingredient/IntProviderFluidIngredientTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public static void rangedFluidIngredientGetStacksTest(GameTestHelper helper) {
215215
"IntProviderFluidIngredient should have fluid equal to what it was made with");
216216
helper.assertTrue(stacks[0].isFluidStackIdentical(ingredient.getStacks()[0]),
217217
"IntProviderFluidIngredient.getStacks shouldn't change between getStacks calls");
218-
ingredient.reroll();
218+
ingredient.reset();
219219
helper.assertFalse(stacks[0].isFluidStackIdentical(ingredient.getStacks()[0]),
220220
"IntProviderFluidIngredient.getStacks should have changed after rerolling");
221221
helper.succeed();
@@ -272,7 +272,7 @@ public static void singleblockRangedFluidOutputSabotaged(GameTestHelper helper)
272272
// get the result of each roll independently
273273
int[] addedRolls = new int[runs];
274274

275-
helper.runAfterDelay(4, () -> {
275+
helper.runAfterDelay(2, () -> {
276276
if (machine.getRecipeLogic().getLastRecipe().getOutputContents(FluidRecipeCapability.CAP).get(0)
277277
.getContent() instanceof IntProviderFluidIngredient ingredient) {
278278
ingredient.setSampledCount(0);
@@ -295,14 +295,14 @@ public static void singleblockRangedFluidOutputSabotaged(GameTestHelper helper)
295295
// check the results of all rolls together
296296
helper.runAfterDelay(runs * 2 + 1, () -> {
297297
FluidStack results = fluidOut.getFluidInTank(0);
298-
helper.assertTrue(TestUtils.isFluidWithinRange(results, runs, runs * 9),
299-
"Sabotaged Singleblock CR didn't produce correct number of fluids, produced [" +
300-
results.getAmount() + "] not [" + runs + "-" + (runs * 9) + "]");
301-
helper.assertFalse((results.getAmount() == runs * 9),
302-
"Sabotaged Singleblock CR rolled max value on every roll (how??)");
303298
helper.assertFalse((results.getAmount() == runs * 0),
304299
"Sabotaged Singleblock CR rolled min value on every roll! " +
305300
"This is the failure this sabotage was intended to induce.");
301+
helper.assertFalse((results.getAmount() == runs * 9),
302+
"Sabotaged Singleblock CR rolled max value on every roll (how??)");
303+
helper.assertTrue(TestUtils.isFluidWithinRange(results, runs, runs * 9),
304+
"Sabotaged Singleblock CR didn't produce correct number of fluids, produced [" +
305+
results.getAmount() + "] not [" + runs + "-" + (runs * 9) + "]");
306306

307307
// check if all the rolls were equal, but not min/max
308308
int[] rolls = new int[runs];

src/test/java/com/gregtechceu/gtceu/api/recipe/ingredient/IntProviderIngredientTest.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.gregtechceu.gtceu.api.machine.multiblock.WorkableMultiblockMachine;
1212
import com.gregtechceu.gtceu.api.machine.trait.NotifiableItemStackHandler;
1313
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
14+
import com.gregtechceu.gtceu.common.data.GTRecipeTypes;
1415
import com.gregtechceu.gtceu.common.machine.multiblock.part.FluidHatchPartMachine;
1516
import com.gregtechceu.gtceu.common.machine.multiblock.part.ItemBusPartMachine;
1617
import com.gregtechceu.gtceu.common.machine.multiblock.part.ParallelHatchPartMachine;
@@ -66,10 +67,11 @@ public class IntProviderIngredientTest {
6667

6768
@BeforeBatch(batch = "RangedIngredients")
6869
public static void prepare(ServerLevel level) {
69-
CR_RECIPE_TYPE = TestUtils.createRecipeType("ranged_ingredient_cr_tests", 2, 2, 3, 2);
70-
LCR_RECIPE_TYPE = TestUtils.createRecipeType("ranged_ingredient_lcr_tests", 3, 3, 5, 4);
71-
CENTRIFUGE_RECIPE_TYPE = TestUtils.createRecipeType("ranged_ingredient_centrifuge_tests", 2, 6, 1, 6);
72-
70+
CR_RECIPE_TYPE = TestUtils.createRecipeType("ranged_ingredient_cr_tests", GTRecipeTypes.CHEMICAL_RECIPES);
71+
LCR_RECIPE_TYPE = TestUtils.createRecipeType("ranged_ingredient_lcr_tests",
72+
GTRecipeTypes.LARGE_CHEMICAL_RECIPES);
73+
CENTRIFUGE_RECIPE_TYPE = TestUtils.createRecipeType("ranged_ingredient_centrifuge_tests",
74+
GTRecipeTypes.CENTRIFUGE_RECIPES);
7375
CR_RECIPE_TYPE.getLookup().addRecipe(CR_RECIPE_TYPE
7476
.recipeBuilder(GTCEu.id("test_ranged_input_item_cr"))
7577
.inputItemsRanged(CR_IN, UniformInt.of(0, 9))
@@ -206,7 +208,7 @@ public static void rangedIngredientGetStacksTest(GameTestHelper helper) {
206208
"IntProviderIngredient should have item equal to what it was made with");
207209
helper.assertTrue(TestUtils.areItemStacksEqual(stacks, ingredient.getItems()),
208210
"IntProviderIngredient.getItems shouldn't change between getStacks calls");
209-
ingredient.reroll();
211+
ingredient.reset();
210212
helper.assertFalse(TestUtils.areItemStacksEqual(stacks, ingredient.getItems()),
211213
"IntProviderIngredient.getItems should have changed after rerolling");
212214
helper.succeed();
@@ -263,7 +265,7 @@ public static void singleblockRangedItemOutputSabotaged(GameTestHelper helper) {
263265
// get the result of each roll independently
264266
int[] addedRolls = new int[runs];
265267

266-
helper.runAfterDelay(4, () -> {
268+
helper.runAfterDelay(2, () -> {
267269
if (machine.getRecipeLogic().getLastRecipe().getOutputContents(ItemRecipeCapability.CAP).get(0)
268270
.getContent() instanceof IntProviderIngredient ingredient) {
269271
ingredient.setSampledCount(0);
@@ -286,14 +288,15 @@ public static void singleblockRangedItemOutputSabotaged(GameTestHelper helper) {
286288
// check the results of all rolls together
287289
helper.runAfterDelay(runs * 2 + 1, () -> {
288290
ItemStack results = itemOut.getStackInSlot(0);
289-
helper.assertTrue(TestUtils.isItemWithinRange(results, runs, runs * 9),
290-
"Sabotaged Singleblock CR didn't produce correct number of items, produced [" +
291-
results.getCount() + "] not [" + runs + "-" + (runs * 9) + "]");
292-
helper.assertFalse((results.getCount() == runs * 9),
293-
"Sabotaged Singleblock CR rolled max value on every roll (how??)");
294291
helper.assertFalse((results.getCount() == runs * 0),
295292
"Sabotaged Singleblock CR rolled min value on every roll! " +
296293
"This is the failure this sabotage was intended to induce.");
294+
helper.assertFalse((results.getCount() == runs * 9),
295+
"Sabotaged Singleblock CR rolled max value on every roll (how??)");
296+
297+
helper.assertTrue(TestUtils.isItemWithinRange(results, runs, runs * 9),
298+
"Sabotaged Singleblock CR didn't produce correct number of items, produced [" +
299+
results.getCount() + "] not [" + runs + "-" + (runs * 9) + "]");
297300

298301
// check if all the rolls were equal, but not min/max
299302
int[] rolls = new int[runs];

0 commit comments

Comments
 (0)