1414import com .cleanroommc .modularui .core .mixins .early .minecraft .GuiScreenAccessor ;
1515import com .cleanroommc .modularui .drawable .GuiDraw ;
1616import com .cleanroommc .modularui .drawable .Stencil ;
17+ import com .cleanroommc .modularui .integration .jei .ModularUIJeiPlugin ;
1718import com .cleanroommc .modularui .network .ModularNetwork ;
1819import com .cleanroommc .modularui .overlay .OverlayManager ;
1920import com .cleanroommc .modularui .overlay .OverlayStack ;
4950import net .minecraftforge .client .event .GuiOpenEvent ;
5051import net .minecraftforge .client .event .GuiScreenEvent ;
5152import net .minecraftforge .common .MinecraftForge ;
53+ import net .minecraftforge .fml .common .Optional ;
5254import net .minecraftforge .fml .common .eventhandler .EventPriority ;
5355import net .minecraftforge .fml .common .eventhandler .SubscribeEvent ;
5456import net .minecraftforge .fml .common .gameevent .TickEvent ;
5557import net .minecraftforge .fml .relauncher .Side ;
5658import net .minecraftforge .fml .relauncher .SideOnly ;
5759
5860import it .unimi .dsi .fastutil .objects .ObjectArrayList ;
61+ import mezz .jei .gui .ghost .GhostIngredientDrag ;
5962import org .jetbrains .annotations .ApiStatus ;
6063import org .jetbrains .annotations .Nullable ;
6164import org .jetbrains .annotations .UnmodifiableView ;
6770import java .io .IOException ;
6871import java .util .Collections ;
6972import java .util .List ;
73+ import java .util .function .Consumer ;
7074import java .util .function .Predicate ;
7175
7276@ ApiStatus .Internal
@@ -245,6 +249,11 @@ private static boolean doAction(@Nullable ModularScreen muiScreen, Predicate<Mod
245249 return OverlayStack .interact (action , true ) || (muiScreen != null && action .test (muiScreen ));
246250 }
247251
252+ private static void foreach (@ Nullable ModularScreen muiScreen , Consumer <ModularScreen > action ) {
253+ OverlayStack .foreach (action , true );
254+ if (muiScreen != null ) action .accept (muiScreen );
255+ }
256+
248257 private static boolean handleMouseInput (int button , @ Nullable ModularScreen muiScreen , GuiScreen mcScreen ) throws IOException {
249258 GameSettings gameSettings = Minecraft .getMinecraft ().gameSettings ;
250259 GuiScreenAccessor acc = (GuiScreenAccessor ) mcScreen ;
@@ -261,6 +270,18 @@ private static boolean handleMouseInput(int button, @Nullable ModularScreen muiS
261270 acc .setEventButton (button );
262271 acc .setLastMouseEvent (Minecraft .getSystemTime ());
263272 if (muiScreen != null && muiScreen .onMouseInputPre (button , true )) return true ;
273+ if (ModularUI .Mods .JEI .isLoaded ()) {
274+ GhostIngredientDrag <?> drag = ModularUIJeiPlugin .getGhostDrag ();
275+ if (drag != null ) {
276+ if (!RecipeViewerGhostHandler .checkRecipeViewerGhostDrag (muiScreen , button , drag )) {
277+ // no target found -> tell jei to drop the ghost ingredient
278+ // stop all further interaction since dropping the ingredient counts as an interaction
279+ ModularUIJeiPlugin .getGhostDragManager ().stopDrag ();
280+ }
281+ // in both cases we want to stop further click processing
282+ return true ;
283+ }
284+ }
264285 return doAction (muiScreen , ms -> ms .onMousePressed (button ));
265286 }
266287 if (button != -1 ) {
@@ -284,6 +305,17 @@ private static boolean handleMouseInput(int button, @Nullable ModularScreen muiS
284305 return false ;
285306 }
286307
308+ /**
309+ * We need to have a separate class for this since {@link Optional.Method} does not remove its contained lambdas.
310+ * So we have this class which must only be loaded when recipe viewer mod is loaded.
311+ */
312+ private static class RecipeViewerGhostHandler {
313+
314+ private static boolean checkRecipeViewerGhostDrag (ModularScreen muiScreen , int button , GhostIngredientDrag <?> drag ) {
315+ return doAction (muiScreen , ms -> ms .checkRecipeViewerGhostDrag (button , drag ));
316+ }
317+ }
318+
287319 /**
288320 * This replicates vanilla behavior while also injecting custom behavior for consistency
289321 */
0 commit comments