1818import net .minecraftforge .api .distmarker .Dist ;
1919import net .minecraftforge .api .distmarker .OnlyIn ;
2020import org .apache .commons .lang3 .tuple .Pair ;
21+ import org .jetbrains .annotations .NotNull ;
2122
2223@ OnlyIn (Dist .CLIENT )
2324public class PatternEncodingModifierService {
@@ -45,50 +46,80 @@ public static void encode(
4546 boolean maxTransfer ) {
4647 List <List <GenericStack >> inputs = GenericEntryStackHelper .ofInputs (slotsView );
4748 List <GenericStack > outputs = GenericEntryStackHelper .ofOutputs (slotsView );
49+
4850 PatternEncodingModifier .ModificationResult modificationResult =
49- new PatternEncodingModifier .ModificationResult (inputs , outputs , false );
51+ new PatternEncodingModifier .ModificationResult (
52+ inputs , outputs , PatternEncodingModifier .MergeMode .GLOBAL );
53+
5054 PatternEncodingModifier .ModificationContext modificationContext =
5155 new PatternEncodingModifier .ModificationContext (recipeBase , slotsView , player , maxTransfer );
5256
5357 for (var modifier : Modifiers ) {
54- modificationResult = modifier . modify (modificationResult , modificationContext );
58+ modificationResult = getModificationResult (modificationResult , modificationContext , modifier );
5559 }
56- modificationResult = GlobalModifier .INSTANCE .modify (modificationResult , modificationContext );
5760
58- if ( modificationResult . mergeAdjacently ()) {
59- PatternEncodingModifierService . encodeProcessingRecipeAdjacentMerge (
60- menu , modificationResult . inputs (), modificationResult . outputs ());
61- } else {
61+ modificationResult =
62+ getModificationResult ( modificationResult , modificationContext , GlobalModifier . INSTANCE );
63+
64+ if ( modificationResult . mergeMode () == PatternEncodingModifier . MergeMode . GLOBAL ) {
6265 EncodingHelper .encodeProcessingRecipe (
6366 menu , modificationResult .inputs (), modificationResult .outputs ());
67+ } else {
68+ PatternEncodingModifierService .encodeProcessingRecipeCustom (
69+ menu ,
70+ modificationResult .inputs (),
71+ modificationResult .outputs (),
72+ modificationResult .mergeMode ());
6473 }
6574 }
6675
67- public static void encodeProcessingRecipeAdjacentMerge (
76+ private static PatternEncodingModifier .@ NotNull ModificationResult getModificationResult (
77+ PatternEncodingModifier .ModificationResult modificationResult ,
78+ PatternEncodingModifier .ModificationContext modificationContext ,
79+ PatternEncodingModifier <?> modifier ) {
80+ var beforeGlobalMode = modificationResult .mergeMode ();
81+ var afterGlobalResult = modifier .modify (modificationResult , modificationContext );
82+
83+ if (afterGlobalResult .mergeMode ().ordinal () < beforeGlobalMode .ordinal ()) {
84+ modificationResult =
85+ new PatternEncodingModifier .ModificationResult (
86+ afterGlobalResult .inputs (), afterGlobalResult .outputs (), beforeGlobalMode );
87+ } else {
88+ modificationResult = afterGlobalResult ;
89+ }
90+ return modificationResult ;
91+ }
92+
93+ public static void encodeProcessingRecipeCustom (
6894 PatternEncodingTermMenu menu ,
6995 List <List <GenericStack >> genericIngredients ,
70- List <GenericStack > genericResults ) {
96+ List <GenericStack > genericResults ,
97+ PatternEncodingModifier .MergeMode mode ) {
98+
7199 menu .setMode (EncodingMode .PROCESSING );
72100 Map <AEKey , Integer > ingredientPriorities =
73101 EncodingHelper .getIngredientPriorities (menu , ENTRY_COMPARATOR );
74- encodeBestMatchingStacksIntoSlotsAdjacent (
75- genericIngredients , ingredientPriorities , menu .getProcessingInputSlots ());
76- encodeBestMatchingStacksIntoSlotsAdjacent (
102+
103+ encodeBestMatchingStacksIntoSlots (
104+ genericIngredients , ingredientPriorities , menu .getProcessingInputSlots (), mode );
105+ encodeBestMatchingStacksIntoSlots (
77106 genericResults .stream ().map (List ::of ).toList (),
78107 ingredientPriorities ,
79- menu .getProcessingOutputSlots ());
108+ menu .getProcessingOutputSlots (),
109+ mode );
80110 }
81111
82- private static void encodeBestMatchingStacksIntoSlotsAdjacent (
112+ private static void encodeBestMatchingStacksIntoSlots (
83113 List <List <GenericStack >> possibleInputsBySlot ,
84114 Map <AEKey , Integer > ingredientPriorities ,
85- FakeSlot [] slots ) {
115+ FakeSlot [] slots ,
116+ PatternEncodingModifier .MergeMode mode ) {
117+
86118 ArrayList <GenericStack > encodedInputs = new ArrayList <>();
87119
88120 for (List <GenericStack > genericIngredient : possibleInputsBySlot ) {
89121 if (!genericIngredient .isEmpty ()) {
90- addOrMergeAdjacent (
91- encodedInputs , findBestIngredient (ingredientPriorities , genericIngredient ));
122+ addStack (encodedInputs , findBestIngredient (ingredientPriorities , genericIngredient ), mode );
92123 }
93124 }
94125
@@ -111,8 +142,9 @@ private static GenericStack findBestIngredient(
111142 .orElseThrow ();
112143 }
113144
114- private static void addOrMergeAdjacent (List <GenericStack > stacks , GenericStack newStack ) {
115- if (!stacks .isEmpty ()) {
145+ private static void addStack (
146+ List <GenericStack > stacks , GenericStack newStack , PatternEncodingModifier .MergeMode mode ) {
147+ if (mode == PatternEncodingModifier .MergeMode .ADJACENT && !stacks .isEmpty ()) {
116148 int lastIndex = stacks .size () - 1 ;
117149 GenericStack lastStack = stacks .get (lastIndex );
118150
0 commit comments