Skip to content

Commit dd400b1

Browse files
committed
fix phantom slots not accepting jei ingredients
1 parent ee106c8 commit dd400b1

3 files changed

Lines changed: 64 additions & 26 deletions

File tree

src/main/java/com/cleanroommc/modularui/screen/ClientScreenHandler.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.cleanroommc.modularui.core.mixins.early.minecraft.GuiScreenAccessor;
1515
import com.cleanroommc.modularui.drawable.GuiDraw;
1616
import com.cleanroommc.modularui.drawable.Stencil;
17+
import com.cleanroommc.modularui.integration.jei.ModularUIJeiPlugin;
1718
import com.cleanroommc.modularui.network.ModularNetwork;
1819
import com.cleanroommc.modularui.overlay.OverlayManager;
1920
import com.cleanroommc.modularui.overlay.OverlayStack;
@@ -49,13 +50,15 @@
4950
import net.minecraftforge.client.event.GuiOpenEvent;
5051
import net.minecraftforge.client.event.GuiScreenEvent;
5152
import net.minecraftforge.common.MinecraftForge;
53+
import net.minecraftforge.fml.common.Optional;
5254
import net.minecraftforge.fml.common.eventhandler.EventPriority;
5355
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
5456
import net.minecraftforge.fml.common.gameevent.TickEvent;
5557
import net.minecraftforge.fml.relauncher.Side;
5658
import net.minecraftforge.fml.relauncher.SideOnly;
5759

5860
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
61+
import mezz.jei.gui.ghost.GhostIngredientDrag;
5962
import org.jetbrains.annotations.ApiStatus;
6063
import org.jetbrains.annotations.Nullable;
6164
import org.jetbrains.annotations.UnmodifiableView;
@@ -67,6 +70,7 @@
6770
import java.io.IOException;
6871
import java.util.Collections;
6972
import java.util.List;
73+
import java.util.function.Consumer;
7074
import 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
*/

src/main/java/com/cleanroommc/modularui/screen/ModularPanel.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.cleanroommc.neverenoughanimations.NEAConfig;
3434

3535
import net.minecraft.client.Minecraft;
36+
import net.minecraftforge.fml.common.Optional;
3637

3738
import mezz.jei.gui.ghost.GhostIngredientDrag;
3839
import org.jetbrains.annotations.ApiStatus;
@@ -318,8 +319,6 @@ public boolean onMousePressed(int mouseButton) {
318319
closeIfOpen();
319320
result = true;
320321
}
321-
} else if (checkRecipeViewerGhostIngredient(mouseButton)) {
322-
return true;
323322
} else {
324323
for (LocatedWidget widget : this.hovering) {
325324
if (widget.getElement() == null || !widget.getElement().isValid()) continue;
@@ -380,32 +379,26 @@ public boolean onMousePressed(int mouseButton) {
380379
});
381380
}
382381

383-
private boolean checkRecipeViewerGhostIngredient(int mouseButton) {
384-
if (ModularUI.Mods.JEI.isLoaded() && ModularUIJeiPlugin.getGhostDrag() != null) {
385-
// try inserting ghost ingredient
386-
GhostIngredientDrag<?> drag = ModularUIJeiPlugin.getGhostDrag();
387-
for (LocatedWidget widget : this.hovering) {
388-
if (widget.getElement() instanceof RecipeViewerGhostIngredientSlot<?> ghostSlot && RecipeViewerGhostIngredientSlot.insertGhostIngredient(drag, ghostSlot)) {
389-
ModularUIJeiPlugin.getGhostDragManager().stopDrag();
390-
this.mouse.pressed(widget, mouseButton);
391-
this.mouse.doRelease = false;
392-
getContext().removeFocus();
393-
return true;
394-
}
395-
// we can't really predict if the interactable would stop further interaction
396-
// so we assume worst
397-
if (widget.getElement() instanceof Interactable || !widget.getElement().canClickThrough()) {
398-
break;
399-
}
382+
@Optional.Method(modid = ModularUI.ModIds.JEI)
383+
boolean checkRecipeViewerGhostIngredient(int mouseButton, GhostIngredientDrag<?> drag) {
384+
if (this.hovering.isEmpty()) return false;
385+
// try inserting ghost ingredient
386+
for (LocatedWidget widget : this.hovering) {
387+
if (widget.getElement() instanceof RecipeViewerGhostIngredientSlot<?> ghostSlot && RecipeViewerGhostIngredientSlot.insertGhostIngredient(drag, ghostSlot)) {
388+
ModularUIJeiPlugin.getGhostDragManager().stopDrag();
389+
this.mouse.pressed(widget, mouseButton);
390+
this.mouse.doRelease = false;
391+
getContext().removeFocus();
392+
return true;
393+
}
394+
// we can't really predict if the interactable would stop further interaction
395+
// so we assume worst
396+
if (widget.getElement() instanceof Interactable || !widget.getElement().canClickThrough()) {
397+
break;
400398
}
401-
// no target found -> tell jei to drop the ghost ingredient
402-
// stop all further interaction since dropping the ingredient counts as an interaction
403-
ModularUIJeiPlugin.getGhostDragManager().stopDrag();
404-
this.mouse.pressed(LocatedWidget.EMPTY, mouseButton);
405-
this.mouse.doRelease = false;
406-
getContext().removeFocus();
407-
return true;
408399
}
400+
this.mouse.pressed(LocatedWidget.EMPTY, mouseButton);
401+
this.mouse.doRelease = false;
409402
return false;
410403
}
411404

src/main/java/com/cleanroommc/modularui/screen/ModularScreen.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
import net.minecraft.client.renderer.GlStateManager;
2323
import net.minecraft.client.renderer.RenderHelper;
2424
import net.minecraft.util.ResourceLocation;
25+
import net.minecraftforge.fml.common.Optional;
2526
import net.minecraftforge.fml.relauncher.Side;
2627
import net.minecraftforge.fml.relauncher.SideOnly;
2728

2829
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
2930
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
3031
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
3132
import it.unimi.dsi.fastutil.objects.ObjectIterator;
33+
import mezz.jei.gui.ghost.GhostIngredientDrag;
3234
import org.jetbrains.annotations.ApiStatus;
3335
import org.jetbrains.annotations.MustBeInvokedByOverriders;
3436
import org.jetbrains.annotations.NotNull;
@@ -381,6 +383,17 @@ public boolean onMousePressed(int mouseButton) {
381383
return false;
382384
}
383385

386+
@Optional.Method(modid = ModularUI.ModIds.JEI)
387+
boolean checkRecipeViewerGhostDrag(int mouseButton, GhostIngredientDrag<?> drag) {
388+
for (ModularPanel panel : this.panelManager.getOpenPanels()) {
389+
if (panel.checkRecipeViewerGhostIngredient(mouseButton, drag)) {
390+
return true;
391+
}
392+
}
393+
getContext().removeFocus();
394+
return false;
395+
}
396+
384397
/**
385398
* Called when a mouse button is released. Tries to invoke
386399
* {@link com.cleanroommc.modularui.api.widget.Interactable#onMouseRelease(int) Interactable#onMouseRelease(int)} on every widget under

0 commit comments

Comments
 (0)