11package net .swedz .tesseract .neoforge .compat .mi .helper ;
22
33import aztech .modern_industrialization .MI ;
4+ import aztech .modern_industrialization .compat .almostunified .AlmostUnifiedFacade ;
45import aztech .modern_industrialization .inventory .AbstractConfigurableStack ;
56import aztech .modern_industrialization .inventory .ConfigurableFluidStack ;
67import aztech .modern_industrialization .inventory .ConfigurableItemStack ;
910import aztech .modern_industrialization .stats .PlayerStatistics ;
1011import aztech .modern_industrialization .thirdparty .fabrictransfer .api .fluid .FluidVariant ;
1112import aztech .modern_industrialization .thirdparty .fabrictransfer .api .item .ItemVariant ;
13+ import aztech .modern_industrialization .thirdparty .fabrictransfer .api .storage .TransferVariant ;
14+ import com .google .common .base .Predicate ;
15+ import com .google .common .base .Supplier ;
1216import net .minecraft .core .registries .BuiltInRegistries ;
1317import net .minecraft .resources .ResourceLocation ;
1418import net .minecraft .server .level .ServerLevel ;
@@ -49,7 +53,7 @@ private static boolean takeItemInputs(
4953 int remainingAmount = input .amount () * (input .probability () == 0 ? 1 : multiplier );
5054 for (ConfigurableItemStack stack : stacks )
5155 {
52- if (stack .getAmount () > 0 && input . matches ( stack .getResource ().toStack ()))
56+ if (stack .getAmount () > 0 && stack .getResource ().test ( input . ingredient ()))
5357 {
5458 int taken = Math .min ((int ) stack .getAmount (), remainingAmount );
5559 if (taken > 0 && !simulate )
@@ -371,127 +375,140 @@ public static boolean putFluidOutputs(
371375 return putFluidOutputs (recipe , simulate , toggleLock , CommonBehavior .from (behavior ), inventory , multiplier );
372376 }
373377
374- public static void lockRecipe (MachineRecipe recipe , Inventory playerInventory , CrafterComponent .Inventory inventory )
378+ private static <T , K extends TransferVariant <T >, S extends AbstractConfigurableStack <T , K >> void handleLocking (
379+ List <S > stacks ,
380+ Predicate <T > matchesRecipe ,
381+ long requiredAmount ,
382+ Supplier <T > lockTarget
383+ )
375384 {
376- // ITEM INPUTS
377- outer :
378- for (MachineRecipe .ItemInput input : recipe .itemInputs )
385+ for (S stack : stacks )
379386 {
380- for (ConfigurableItemStack stack : inventory .getItemInputs ())
381- {
382- if (stack .getLockedInstance () != null && input .matches (new ItemStack (stack .getLockedInstance ())))
383- {
384- continue outer ;
385- }
386- }
387- Item targetItem = null ;
388- // Find the first match in the player inventory (useful for logs for example)
389- for (int i = 0 ; i < playerInventory .getContainerSize (); i ++)
387+ if (stack .getLockedInstance () != null && matchesRecipe .apply (stack .getLockedInstance ()))
390388 {
391- ItemStack playerStack = playerInventory . getItem ( i );
392- if (! playerStack . isEmpty () && input . matches ( new ItemStack ( playerStack . getItem ())) )
389+ requiredAmount -= stack . getTotalCapacityFor ( stack . getLockedInstance () );
390+ if (requiredAmount <= 0 )
393391 {
394- targetItem = playerStack . getItem ();
395- break ;
392+ // We have all we need already
393+ return ;
396394 }
397395 }
398- if (targetItem == null )
399- {
400- // Find the first match that is an item from MI (useful for ingots for example)
401- for (Item item : input .getInputItems ())
402- {
403- ResourceLocation id = BuiltInRegistries .ITEM .getKey (item );
404- if (id .getNamespace ().equals (MI .ID ))
396+ }
397+ var newLockedInstance = lockTarget .get ();
398+ if (newLockedInstance == null )
399+ {
400+ return ;
401+ }
402+ AbstractConfigurableStack .playerLockNoOverride (newLockedInstance , requiredAmount , stacks );
403+ }
404+
405+ public static void lockRecipe (MachineRecipe recipe , Inventory playerInventory , CrafterComponent .Inventory inventory )
406+ {
407+ // ITEM INPUTS
408+ for (var input : recipe .itemInputs )
409+ {
410+ handleLocking (
411+ inventory .getItemInputs (),
412+ (item ) -> input .matches (new ItemStack (item )),
413+ input .amount (),
414+ () ->
405415 {
406- targetItem = item ;
407- break ;
416+ // Find the first match in the player inventory (useful for logs for example)
417+ for (int i = 0 ; i < playerInventory .getContainerSize (); i ++)
418+ {
419+ ItemStack playerStack = playerInventory .getItem (i );
420+ if (!playerStack .isEmpty () && input .matches (new ItemStack (playerStack .getItem ())))
421+ {
422+ return playerStack .getItem ();
423+ }
424+ }
425+ List <Item > inputItems = input .getInputItems ();
426+ // Find the preferred item with Almost Unified if possible
427+ if (!inputItems .isEmpty ())
428+ {
429+ var targetItem = AlmostUnifiedFacade .INSTANCE .getTargetItem (inputItems .getFirst ());
430+ if (targetItem != null )
431+ {
432+ return targetItem ;
433+ }
434+ }
435+ // Find the first match that is an item from MI (useful for ingots for example)
436+ for (Item item : inputItems )
437+ {
438+ ResourceLocation id = BuiltInRegistries .ITEM .getKey (item );
439+ if (id .getNamespace ().equals (MI .ID ))
440+ {
441+ return item ;
442+ }
443+ }
444+ // If there is only one value in the tag, pick that one
445+ if (inputItems .size () == 1 )
446+ {
447+ return inputItems .getFirst ();
448+ }
449+ return null ;
408450 }
409- }
410- }
411- if (targetItem == null )
412- {
413- // If there is only one value in the tag, pick that one
414- if (input .getInputItems ().size () == 1 )
415- {
416- targetItem = input .getInputItems ().getFirst ();
417- }
418- }
419-
420- if (targetItem != null )
421- {
422- AbstractConfigurableStack .playerLockNoOverride (targetItem , inventory .getItemInputs ());
423- }
451+ );
424452 }
453+
425454 // ITEM OUTPUTS
426- outer :
427- for (MachineRecipe .ItemOutput output : recipe .itemOutputs )
455+ for (var output : recipe .itemOutputs )
428456 {
429- for ( ConfigurableItemStack stack : inventory . getItemOutputs ())
430- {
431- if ( stack . getLockedInstance () == output .variant ().getItem ())
432- continue outer ;
433- }
434- AbstractConfigurableStack . playerLockNoOverride ( output . variant (). getItem (), inventory . getItemOutputs () );
457+ handleLocking (
458+ inventory . getItemOutputs (),
459+ ( item ) -> output .variant ().isOf ( item ),
460+ output . amount (),
461+ output . variant ():: getItem
462+ );
435463 }
436464
437465 // FLUID INPUTS
438- outer :
439- for (MachineRecipe .FluidInput input : recipe .fluidInputs )
466+ for (var input : recipe .fluidInputs )
440467 {
441- for (ConfigurableFluidStack stack : inventory .getFluidInputs ())
442- {
443- if (stack .getLockedInstance () != null && input .fluid ().test (new FluidStack (stack .getLockedInstance (), 1 )))
444- {
445- continue outer ;
446- }
447- }
448- Fluid targetFluid = null ;
449- // Find the first match in the player inventory
450- for (int i = 0 ; i < playerInventory .getContainerSize (); i ++)
451- {
452- var playerStack = FluidUtil .getFluidContained (playerInventory .getItem (i )).orElse (FluidStack .EMPTY );
453- if (!playerStack .isEmpty () && input .fluid ().test (new FluidStack (playerStack .getFluid (), 1 )))
454- {
455- targetFluid = playerStack .getFluid ();
456- break ;
457- }
458- }
459- if (targetFluid == null )
460- {
461- // Find the first match that is an item from MI
462- for (Fluid fluid : input .getInputFluids ())
463- {
464- ResourceLocation id = BuiltInRegistries .FLUID .getKey (fluid );
465- if (id .getNamespace ().equals (MI .ID ))
468+ handleLocking (
469+ inventory .getFluidInputs (),
470+ (fluid ) -> input .fluid ().test (new FluidStack (fluid , 1 )),
471+ input .amount (),
472+ () ->
466473 {
467- targetFluid = fluid ;
468- break ;
474+ // Find the first match in the player inventory
475+ for (int i = 0 ; i < playerInventory .getContainerSize (); i ++)
476+ {
477+ var playerStack = FluidUtil .getFluidContained (playerInventory .getItem (i )).orElse (FluidStack .EMPTY );
478+ if (!playerStack .isEmpty () && input .fluid ().test (new FluidStack (playerStack .getFluid (), 1 )))
479+ {
480+ return playerStack .getFluid ();
481+ }
482+ }
483+ List <Fluid > inputFluids = input .getInputFluids ();
484+ // Find the first match that is an item from MI
485+ for (Fluid fluid : inputFluids )
486+ {
487+ ResourceLocation id = BuiltInRegistries .FLUID .getKey (fluid );
488+ if (id .getNamespace ().equals (MI .ID ))
489+ {
490+ return fluid ;
491+ }
492+ }
493+ // If there is only one value in the tag, pick that one
494+ if (inputFluids .size () == 1 )
495+ {
496+ return inputFluids .getFirst ();
497+ }
498+ return null ;
469499 }
470- }
471- }
472- if (targetFluid == null )
473- {
474- // If there is only one value in the tag, pick that one
475- if (input .getInputFluids ().size () == 1 )
476- {
477- targetFluid = input .getInputFluids ().getFirst ();
478- }
479- }
480- if (targetFluid != null )
481- {
482- AbstractConfigurableStack .playerLockNoOverride (targetFluid , inventory .getFluidInputs ());
483- }
500+ );
484501 }
502+
485503 // FLUID OUTPUTS
486- outer :
487- for (MachineRecipe .FluidOutput output : recipe .fluidOutputs )
504+ for (var output : recipe .fluidOutputs )
488505 {
489- for ( ConfigurableFluidStack stack : inventory . getFluidOutputs ())
490- {
491- if ( stack . isLockedTo ( output .fluid ()))
492- continue outer ;
493- }
494- AbstractConfigurableStack . playerLockNoOverride ( output . fluid (), inventory . getFluidOutputs () );
506+ handleLocking (
507+ inventory . getFluidOutputs (),
508+ ( fluid ) -> output .fluid () == fluid ,
509+ output . amount (),
510+ output :: fluid
511+ );
495512 }
496513
497514 // LOCK ITEMS
@@ -500,6 +517,7 @@ public static void lockRecipe(MachineRecipe recipe, Inventory playerInventory, C
500517 lockAll (inventory .getItemInputs ());
501518 lockAll (inventory .getItemOutputs ());
502519 }
520+
503521 // LOCK FLUIDS
504522 if (!recipe .fluidInputs .isEmpty () || !recipe .fluidOutputs .isEmpty ())
505523 {
0 commit comments