11package io .github .linkfgfgui .emi_patternizer ;
22
3+ import appeng .client .gui .AEBaseScreen ;
34import appeng .client .gui .me .items .PatternEncodingTermScreen ;
5+ import appeng .menu .AEBaseMenu ;
46import appeng .menu .SlotSemantics ;
57import appeng .menu .me .items .PatternEncodingTermMenu ;
68import com .mojang .blaze3d .platform .InputConstants ;
1820import net .minecraft .client .resources .sounds .SimpleSoundInstance ;
1921import net .minecraft .sounds .SoundEvents ;
2022import net .minecraft .world .inventory .ClickType ;
23+ import net .minecraft .world .inventory .Slot ;
2124import net .minecraftforge .client .event .ScreenEvent ;
2225import org .slf4j .Logger ;
2326
27+ import java .lang .reflect .InvocationTargetException ;
28+ import java .lang .reflect .Method ;
2429import java .util .Comparator ;
2530import java .util .HashSet ;
2631import java .util .List ;
32+ import java .util .concurrent .Callable ;
2733import java .util .concurrent .CompletableFuture ;
2834import java .util .concurrent .TimeUnit ;
2935import java .util .concurrent .atomic .AtomicLong ;
@@ -37,6 +43,8 @@ public class Patternize {
3743 static long delayPerOperation ;
3844 static long delayAdditionalPerPattern ;
3945 static boolean isPlaySound ;
46+ static Method encodeMethod = null ;
47+ static Runnable callEncode = null ;
4048
4149 public static boolean containsAllItems (EmiRecipe r ) {
4250 return containsAllItems (r .getOutputs ());
@@ -46,7 +54,7 @@ public static boolean containsAllItems(List<EmiStack> stackList) {
4654 return (stackList .stream ().allMatch (emiStack -> EncodedItems .contains (emiStack .getId ().toString ())));
4755 }
4856
49- public static void Encode (long initDelay , Minecraft minecraft , EmiRecipe recipe , PatternEncodingTermScreen <?> screen , PatternEncodingTermMenu menu , LocalPlayer player , MultiPlayerGameMode gameMode , int encodedPatternSlot ) {
57+ public static void Encode (long initDelay , Minecraft minecraft , EmiRecipe recipe , AEBaseScreen <?> screen , AEBaseMenu menu , LocalPlayer player , MultiPlayerGameMode gameMode , Integer encodedPatternSlot ) {
5058 CompletableFuture .delayedExecutor (initDelay , TimeUnit .MILLISECONDS ).execute (() -> {
5159 minecraft .execute (() -> {
5260 EmiRecipeFiller .performFill (recipe , screen , EmiCraftContext .Type .FILL_BUTTON , EmiCraftContext .Destination .NONE , 1 );
@@ -56,17 +64,22 @@ public static void Encode(long initDelay, Minecraft minecraft, EmiRecipe recipe,
5664 });
5765 CompletableFuture .delayedExecutor (delayPerOperation , TimeUnit .MILLISECONDS ).execute (() -> {
5866 minecraft .execute (() -> {
59- menu .encode ();
67+ if (encodeMethod !=null ){
68+ callEncode .run ();
69+ }
6070 });
6171 CompletableFuture .delayedExecutor (delayPerOperation , TimeUnit .MILLISECONDS ).execute (() ->
62- minecraft .execute (() ->
63- gameMode .handleInventoryMouseClick (
64- menu .containerId ,
65- encodedPatternSlot ,
66- 0 , // 0 = Left, 1 = Right
67- ClickType .QUICK_MOVE ,
68- player
69- )
72+ minecraft .execute (() -> {
73+ if (encodedPatternSlot != null ) {
74+ gameMode .handleInventoryMouseClick (
75+ menu .containerId ,
76+ encodedPatternSlot ,
77+ 0 , // 0 = Left, 1 = Right
78+ ClickType .QUICK_MOVE ,
79+ player
80+ );
81+ }
82+ }
7083 ));
7184 });
7285 });
@@ -89,19 +102,47 @@ public static void LoadConfig() {
89102
90103 public static void onKeyPressed (ScreenEvent .KeyPressed event ) {
91104 if (!operating && Emi_patternizer .PATTERNIZE_MAPPING .get ().isActiveAndMatches (InputConstants .getKey (event .getKeyCode (), event .getScanCode ()))) {
92- if (event .getScreen () instanceof PatternEncodingTermScreen <?> screen ) {
93- PatternEncodingTermMenu menu = screen .getMenu ();
105+ if (event .getScreen () instanceof AEBaseScreen <?> screen ) {
106+ AEBaseMenu menu = screen .getMenu ();
94107// int blankPatternSlot = ((AEBaseMenuAccessor) menu).getSlotsBySemantic().get(SlotSemantics.BLANK_PATTERN).getFirst().index;
95- int encodedPatternSlot = ((AEBaseMenuAccessor ) menu ).getSlotsBySemantic ().get (SlotSemantics .ENCODED_PATTERN ).get (0 ).index ;
108+ List <Slot > slots = ((AEBaseMenuAccessor ) menu ).getSlotsBySemantic ().get (SlotSemantics .ENCODED_PATTERN );
109+ Integer encodedPatternSlot = slots .isEmpty () ? null : slots .get (0 ).index ;
96110 if (BoM .craftingMode && !operating ) {
97111 LoadConfig ();
98112 Minecraft minecraft = Minecraft .getInstance ();
99113 LocalPlayer player = minecraft .player ;
100114 MultiPlayerGameMode gameMode = minecraft .gameMode ;
101115 MaterialNode goal = BoM .tree .goal ;
116+ encodeMethod =null ;
117+ try {
118+ // AE2 Pattern Workstation(1462173) Compatibility
119+ encodeMethod =menu .getClass ().getMethod ("encode" , Long .class );
120+ encodedPatternSlot = null ;
121+ callEncode =()->{
122+ try {
123+ encodeMethod .invoke (menu ,0L );
124+ } catch (IllegalAccessException | InvocationTargetException e01 ) {
125+ LOGGER .error (e01 .getMessage ());
126+ }
127+ };
128+ } catch (NoSuchMethodException e1 ) {
129+ try {
130+ encodeMethod =menu .getClass ().getMethod ("encode" );
131+ callEncode =()->{
132+ try {
133+ encodeMethod .invoke (menu );
134+ } catch (IllegalAccessException | InvocationTargetException e11 ) {
135+ LOGGER .error (e11 .getMessage ());
136+ }
137+ };
138+ } catch (NoSuchMethodException e2 ) {
139+ LOGGER .error (e2 .getMessage ());
140+ }
141+ }
102142 operating = true ;
103143 LOGGER .debug ("operating started" );
104144 AtomicLong maxDelay = new AtomicLong (0 );
145+ Integer finalEncodedPatternSlot = encodedPatternSlot ;
105146 Stream .of (goal )
106147 .flatMap (Patternize ::streamTree )
107148 .sorted (Comparator .comparing (node -> {
@@ -113,7 +154,7 @@ public static void onKeyPressed(ScreenEvent.KeyPressed event) {
113154 }))
114155 .forEachOrdered (node -> {
115156 if (node .recipe != null && node .recipe .getId () != null && !containsAllItems (node .recipe )) {
116- Encode (maxDelay .get (), minecraft , node .recipe , screen , menu , player , gameMode , encodedPatternSlot );
157+ Encode (maxDelay .get (), minecraft , node .recipe , screen , menu , player , gameMode , finalEncodedPatternSlot );
117158 node .recipe .getOutputs ().forEach (emiStack -> EncodedItems .add (emiStack .getId ().toString ()));
118159 maxDelay .addAndGet (3 * delayPerOperation + delayAdditionalPerPattern );
119160 }
0 commit comments