|
1 | 1 | package com.gregtechceu.gtceu.api.recipe.ingredient; |
2 | 2 |
|
3 | 3 | import com.gregtechceu.gtceu.GTCEu; |
| 4 | +import com.gregtechceu.gtceu.api.GTValues; |
| 5 | +import com.gregtechceu.gtceu.api.capability.recipe.IO; |
| 6 | +import com.gregtechceu.gtceu.api.capability.recipe.ItemRecipeCapability; |
| 7 | +import com.gregtechceu.gtceu.api.machine.SimpleTieredMachine; |
| 8 | +import com.gregtechceu.gtceu.api.machine.trait.NotifiableItemStackHandler; |
| 9 | +import com.gregtechceu.gtceu.api.recipe.GTRecipeType; |
4 | 10 | import com.gregtechceu.gtceu.api.recipe.ingredient.nbtpredicate.NBTPredicate; |
| 11 | +import com.gregtechceu.gtceu.gametest.util.TestUtils; |
5 | 12 |
|
| 13 | +import net.minecraft.core.BlockPos; |
6 | 14 | import net.minecraft.gametest.framework.BeforeBatch; |
7 | 15 | import net.minecraft.gametest.framework.GameTest; |
8 | 16 | import net.minecraft.gametest.framework.GameTestHelper; |
9 | 17 | import net.minecraft.nbt.CompoundTag; |
10 | 18 | import net.minecraft.server.level.ServerLevel; |
| 19 | +import net.minecraft.util.valueproviders.UniformInt; |
| 20 | +import net.minecraft.world.item.ItemStack; |
| 21 | +import net.minecraft.world.item.Items; |
11 | 22 | import net.minecraftforge.gametest.GameTestHolder; |
12 | 23 | import net.minecraftforge.gametest.PrefixGameTestTemplate; |
13 | 24 |
|
14 | 25 | import static com.gregtechceu.gtceu.api.recipe.ingredient.nbtpredicate.NBTPredicates.*; |
15 | | -import static com.gregtechceu.gtceu.api.recipe.ingredient.nbtpredicate.NBTPredicates.eq; |
| 26 | +import static com.gregtechceu.gtceu.common.data.GTRecipeTypes.CHEMICAL_RECIPES; |
| 27 | +import static com.gregtechceu.gtceu.gametest.util.TestUtils.getMetaMachine; |
16 | 28 |
|
17 | 29 | @PrefixGameTestTemplate(false) |
18 | 30 | @GameTestHolder(GTCEu.MOD_ID) |
19 | 31 | public class NBTPredicateTest { |
20 | 32 |
|
| 33 | + private static GTRecipeType CR_RECIPE_TYPE; |
| 34 | + |
21 | 35 | @BeforeBatch(batch = "NBTPredicateTest") |
22 | | - public static void prepare(ServerLevel level) {} |
| 36 | + public static void prepare(ServerLevel level) { |
| 37 | + CR_RECIPE_TYPE = TestUtils.createRecipeType("nbt_predicate_ingredient_cr_tests", CHEMICAL_RECIPES); |
| 38 | + CR_RECIPE_TYPE.getLookup().addRecipe( |
| 39 | + CR_RECIPE_TYPE.recipeBuilder("nbt_predicate_test") |
| 40 | + .inputItemNbtPredicate(new ItemStack(Items.FEATHER), eq("foo", "bar")) |
| 41 | + .outputItems(new ItemStack(Items.COAL)) |
| 42 | + .EUt(GTValues.V[GTValues.HV]) |
| 43 | + .duration(5) |
| 44 | + .buildRawRecipe()); |
| 45 | + |
| 46 | + CR_RECIPE_TYPE.getLookup().addRecipe( |
| 47 | + CR_RECIPE_TYPE.recipeBuilder("nbt_predicate_test_chanced") |
| 48 | + .chance(4000) |
| 49 | + .inputItemNbtPredicate(new ItemStack(Items.FEATHER), eq("bin", "bar")) |
| 50 | + .chance(10000) |
| 51 | + .outputItems(new ItemStack(Items.CHARCOAL)) |
| 52 | + .EUt(GTValues.V[GTValues.HV]) |
| 53 | + .duration(4) |
| 54 | + .buildRawRecipe()); |
| 55 | + |
| 56 | + CR_RECIPE_TYPE.getLookup().addRecipe( |
| 57 | + CR_RECIPE_TYPE.recipeBuilder("nbt_predicate_test_ranged") |
| 58 | + .inputItemRanged(new IntProviderIngredient(new NBTPredicateIngredient( |
| 59 | + new ItemStack(Items.FEATHER), eq("bash", "bar")), UniformInt.of(0, 4))) |
| 60 | + .outputItems(new ItemStack(Items.COBBLESTONE)) |
| 61 | + .EUt(GTValues.V[GTValues.HV]) |
| 62 | + .duration(4) |
| 63 | + .buildRawRecipe()); |
| 64 | + |
| 65 | + CR_RECIPE_TYPE.getLookup().addRecipe( |
| 66 | + CR_RECIPE_TYPE.recipeBuilder("nbt_predicate_test_chanced_ranged") |
| 67 | + .chance(4000) |
| 68 | + .inputItemRanged(new IntProviderIngredient(new NBTPredicateIngredient( |
| 69 | + new ItemStack(Items.FEATHER), eq("bash", "botch")), UniformInt.of(0, 4))) |
| 70 | + .chance(10000) |
| 71 | + .outputItems(new ItemStack(Items.DEEPSLATE)) |
| 72 | + .EUt(GTValues.V[GTValues.HV]) |
| 73 | + .duration(4) |
| 74 | + .buildRawRecipe()); |
| 75 | + } |
23 | 76 |
|
24 | 77 | @GameTest(template = "empty", batch = "NBTPredicateTest") |
25 | 78 | public static void NBTPredicateEqualsTest(GameTestHelper helper) { |
@@ -176,4 +229,130 @@ public static void NBTPredicateEmptyTest(GameTestHelper helper) { |
176 | 229 | "Less-or-equal NBTPredicate succeeded with empty tag"); |
177 | 230 | helper.succeed(); |
178 | 231 | } |
| 232 | + |
| 233 | + @GameTest(template = "singleblock_chem_reactor", batch = "NBTPredicateTest") |
| 234 | + public static void NBTPredicateMachineCRTestSucceeds(GameTestHelper helper) { |
| 235 | + SimpleTieredMachine machine = (SimpleTieredMachine) getMetaMachine( |
| 236 | + helper.getBlockEntity(new BlockPos(0, 1, 0))); |
| 237 | + |
| 238 | + machine.setRecipeType(CR_RECIPE_TYPE); |
| 239 | + NotifiableItemStackHandler itemIn = (NotifiableItemStackHandler) machine |
| 240 | + .getCapabilitiesFlat(IO.IN, ItemRecipeCapability.CAP).get(0); |
| 241 | + NotifiableItemStackHandler itemOut = (NotifiableItemStackHandler) machine |
| 242 | + .getCapabilitiesFlat(IO.OUT, ItemRecipeCapability.CAP).get(0); |
| 243 | + |
| 244 | + var inputStack = new ItemStack(Items.FEATHER); |
| 245 | + inputStack.getOrCreateTag().putString("foo", "bar"); |
| 246 | + itemIn.setStackInSlot(0, inputStack); |
| 247 | + helper.runAfterDelay(10, () -> { |
| 248 | + helper.assertTrue(ItemStack.isSameItemSameTags(itemOut.getStackInSlot(0), new ItemStack(Items.COAL)), |
| 249 | + "NBT Predicate test didn't run when it should have."); |
| 250 | + helper.succeed(); |
| 251 | + }); |
| 252 | + } |
| 253 | + |
| 254 | + @GameTest(template = "singleblock_chem_reactor", batch = "NBTPredicateTest") |
| 255 | + public static void NBTPredicateMachineCRTestDoesntSucceed(GameTestHelper helper) { |
| 256 | + SimpleTieredMachine machine = (SimpleTieredMachine) getMetaMachine( |
| 257 | + helper.getBlockEntity(new BlockPos(0, 1, 0))); |
| 258 | + |
| 259 | + machine.setRecipeType(CR_RECIPE_TYPE); |
| 260 | + NotifiableItemStackHandler itemIn = (NotifiableItemStackHandler) machine |
| 261 | + .getCapabilitiesFlat(IO.IN, ItemRecipeCapability.CAP).get(0); |
| 262 | + NotifiableItemStackHandler itemOut = (NotifiableItemStackHandler) machine |
| 263 | + .getCapabilitiesFlat(IO.OUT, ItemRecipeCapability.CAP).get(0); |
| 264 | + |
| 265 | + var inputStack = new ItemStack(Items.FEATHER); |
| 266 | + inputStack.getOrCreateTag().putString("foo", "baz"); |
| 267 | + itemIn.setStackInSlot(0, inputStack); |
| 268 | + helper.runAfterDelay(10, () -> { |
| 269 | + helper.assertFalse(ItemStack.isSameItemSameTags(itemOut.getStackInSlot(0), new ItemStack(Items.COAL)), |
| 270 | + "NBT Predicate test ran when it shouldn't have."); |
| 271 | + helper.succeed(); |
| 272 | + }); |
| 273 | + } |
| 274 | + |
| 275 | + @GameTest(template = "singleblock_chem_reactor", batch = "NBTPredicateTest") |
| 276 | + public static void NBTPredicateMachineCRTestChanced(GameTestHelper helper) { |
| 277 | + SimpleTieredMachine machine = (SimpleTieredMachine) getMetaMachine( |
| 278 | + helper.getBlockEntity(new BlockPos(0, 1, 0))); |
| 279 | + |
| 280 | + machine.setRecipeType(CR_RECIPE_TYPE); |
| 281 | + NotifiableItemStackHandler itemIn = (NotifiableItemStackHandler) machine |
| 282 | + .getCapabilitiesFlat(IO.IN, ItemRecipeCapability.CAP).get(0); |
| 283 | + NotifiableItemStackHandler itemOut = (NotifiableItemStackHandler) machine |
| 284 | + .getCapabilitiesFlat(IO.OUT, ItemRecipeCapability.CAP).get(0); |
| 285 | + |
| 286 | + var inputStack = new ItemStack(Items.FEATHER, 15); // one short, a chance roll needs to fail |
| 287 | + inputStack.getOrCreateTag().putString("bin", "bar"); |
| 288 | + itemIn.setStackInSlot(0, inputStack); |
| 289 | + helper.runAfterDelay(4 * 16 + 1, () -> { |
| 290 | + helper.assertTrue(ItemStack.isSameItemSameTags(itemOut.getStackInSlot(0), new ItemStack(Items.CHARCOAL)), |
| 291 | + "NBT Predicate Chanced test ran the wrong recipe!"); |
| 292 | + helper.assertTrue(itemOut.getStackInSlot(0).getCount() == 16, |
| 293 | + "NBT Predicate Chanced test didn't complete enough recipe runs, completed [" + |
| 294 | + itemOut.getStackInSlot(0).getCount() + "], not [16]"); |
| 295 | + helper.assertFalse(itemIn.getStackInSlot(0).getCount() == 15, |
| 296 | + "NBT Predicate Chanced test didn't consume items"); |
| 297 | + helper.assertFalse(itemIn.getStackInSlot(0).isEmpty(), |
| 298 | + "NBT Predicate Chanced test consumed too many items"); |
| 299 | + helper.succeed(); |
| 300 | + }); |
| 301 | + } |
| 302 | + |
| 303 | + @GameTest(template = "singleblock_chem_reactor", batch = "NBTPredicateTest") |
| 304 | + public static void NBTPredicateMachineCRTestRanged(GameTestHelper helper) { |
| 305 | + SimpleTieredMachine machine = (SimpleTieredMachine) getMetaMachine( |
| 306 | + helper.getBlockEntity(new BlockPos(0, 1, 0))); |
| 307 | + |
| 308 | + machine.setRecipeType(CR_RECIPE_TYPE); |
| 309 | + NotifiableItemStackHandler itemIn = (NotifiableItemStackHandler) machine |
| 310 | + .getCapabilitiesFlat(IO.IN, ItemRecipeCapability.CAP).get(0); |
| 311 | + NotifiableItemStackHandler itemOut = (NotifiableItemStackHandler) machine |
| 312 | + .getCapabilitiesFlat(IO.OUT, ItemRecipeCapability.CAP).get(0); |
| 313 | + |
| 314 | + var inputStack = new ItemStack(Items.FEATHER, 63); // one short, a range needs to roll not max |
| 315 | + inputStack.getOrCreateTag().putString("bash", "bar"); |
| 316 | + itemIn.setStackInSlot(0, inputStack); |
| 317 | + helper.runAfterDelay(4 * 16 + 1, () -> { |
| 318 | + helper.assertTrue(ItemStack.isSameItemSameTags(itemOut.getStackInSlot(0), new ItemStack(Items.COBBLESTONE)), |
| 319 | + "NBT Predicate Ranged test ran the wrong recipe!"); |
| 320 | + helper.assertTrue(itemOut.getStackInSlot(0).getCount() == 16, |
| 321 | + "NBT Predicate Ranged test didn't complete enough recipe runs, completed [" + |
| 322 | + itemOut.getStackInSlot(0).getCount() + "], not [16]"); |
| 323 | + helper.assertFalse(itemIn.getStackInSlot(0).getCount() == 15, |
| 324 | + "NBT Predicate Ranged test didn't consume items"); |
| 325 | + helper.assertFalse(itemIn.getStackInSlot(0).isEmpty(), |
| 326 | + "NBT Predicate Ranged test consumed too many items"); |
| 327 | + helper.succeed(); |
| 328 | + }); |
| 329 | + } |
| 330 | + |
| 331 | + @GameTest(template = "singleblock_chem_reactor", batch = "NBTPredicateTest") |
| 332 | + public static void NBTPredicateMachineCRTestChancedRanged(GameTestHelper helper) { |
| 333 | + SimpleTieredMachine machine = (SimpleTieredMachine) getMetaMachine( |
| 334 | + helper.getBlockEntity(new BlockPos(0, 1, 0))); |
| 335 | + |
| 336 | + machine.setRecipeType(CR_RECIPE_TYPE); |
| 337 | + NotifiableItemStackHandler itemIn = (NotifiableItemStackHandler) machine |
| 338 | + .getCapabilitiesFlat(IO.IN, ItemRecipeCapability.CAP).get(0); |
| 339 | + NotifiableItemStackHandler itemOut = (NotifiableItemStackHandler) machine |
| 340 | + .getCapabilitiesFlat(IO.OUT, ItemRecipeCapability.CAP).get(0); |
| 341 | + |
| 342 | + var inputStack = new ItemStack(Items.FEATHER, 63); // one short, a chance or range needs to not max |
| 343 | + inputStack.getOrCreateTag().putString("bash", "botch"); |
| 344 | + itemIn.setStackInSlot(0, inputStack); |
| 345 | + helper.runAfterDelay(4 * 16 + 1, () -> { |
| 346 | + helper.assertTrue(ItemStack.isSameItemSameTags(itemOut.getStackInSlot(0), new ItemStack(Items.DEEPSLATE)), |
| 347 | + "NBT Predicate Chanced Ranged test ran the wrong recipe!"); |
| 348 | + helper.assertTrue(itemOut.getStackInSlot(0).getCount() == 16, |
| 349 | + "NBT Predicate Chanced Ranged test didn't complete enough recipe runs, completed [" + |
| 350 | + itemOut.getStackInSlot(0).getCount() + "], not [16]"); |
| 351 | + helper.assertFalse(itemIn.getStackInSlot(0).getCount() == 15, |
| 352 | + "NBT Predicate Chanced Ranged test didn't consume items"); |
| 353 | + helper.assertFalse(itemIn.getStackInSlot(0).isEmpty(), |
| 354 | + "NBT Predicate Chanced Ranged test consumed too many items"); |
| 355 | + helper.succeed(); |
| 356 | + }); |
| 357 | + } |
179 | 358 | } |
0 commit comments