|
| 1 | +package gregtech.mixins.mui2; |
| 2 | + |
| 3 | +import gregtech.api.mui.InputAccessor; |
| 4 | +import gregtech.api.util.Mods; |
| 5 | +import gregtech.integration.jei.JustEnoughItemsModule; |
| 6 | +import gregtech.mixins.jei.DragManagerAccessor; |
| 7 | +import gregtech.mixins.jei.GhostDragAccessor; |
| 8 | + |
| 9 | +import net.minecraft.client.Minecraft; |
| 10 | + |
| 11 | +import com.cleanroommc.modularui.api.layout.IViewport; |
| 12 | +import com.cleanroommc.modularui.api.widget.IFocusedWidget; |
| 13 | +import com.cleanroommc.modularui.api.widget.Interactable; |
| 14 | +import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot; |
| 15 | +import com.cleanroommc.modularui.screen.ModularPanel; |
| 16 | +import com.cleanroommc.modularui.screen.viewport.LocatedWidget; |
| 17 | +import com.cleanroommc.modularui.utils.ObjectList; |
| 18 | +import com.cleanroommc.modularui.widget.ParentWidget; |
| 19 | +import mezz.jei.gui.ghost.GhostIngredientDrag; |
| 20 | +import mezz.jei.gui.ghost.GhostIngredientDragManager; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | +import org.spongepowered.asm.mixin.Final; |
| 23 | +import org.spongepowered.asm.mixin.Mixin; |
| 24 | +import org.spongepowered.asm.mixin.Overwrite; |
| 25 | +import org.spongepowered.asm.mixin.Shadow; |
| 26 | +import org.spongepowered.asm.mixin.Unique; |
| 27 | + |
| 28 | +@Mixin(value = ModularPanel.class, remap = false) |
| 29 | +public abstract class ModularPanelMixin extends ParentWidget<ModularPanel> implements IViewport { |
| 30 | + |
| 31 | + @Shadow |
| 32 | + @Final |
| 33 | + private ObjectList<LocatedWidget> hovering; |
| 34 | + |
| 35 | + @Shadow |
| 36 | + public abstract boolean closeOnOutOfBoundsClick(); |
| 37 | + |
| 38 | + @Shadow |
| 39 | + public abstract void animateClose(); |
| 40 | + |
| 41 | + @Shadow |
| 42 | + @Final |
| 43 | + private @NotNull String name; |
| 44 | + @Unique |
| 45 | + InputAccessor gregTech$mouse = null; |
| 46 | + |
| 47 | + /** |
| 48 | + * @author Ghzdude - GTCEu |
| 49 | + * @reason Implement fixes to phantom slot handling from Mui2 master |
| 50 | + */ |
| 51 | + // this looks really cursed in mixin.out, but it works |
| 52 | + @Overwrite |
| 53 | + private boolean lambda$onMousePressed$3(int mouseButton) { |
| 54 | + LocatedWidget pressed = LocatedWidget.EMPTY; |
| 55 | + boolean result = false; |
| 56 | + |
| 57 | + if (gregTech$mouse == null) { |
| 58 | + // reflection because the type is a private inner class |
| 59 | + try { |
| 60 | + gregTech$mouse = (InputAccessor) ModularPanel.class.getDeclaredField("mouse").get(this); |
| 61 | + } catch (IllegalAccessException | NoSuchFieldException e) { |
| 62 | + throw new RuntimeException(e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (this.hovering.isEmpty()) { |
| 67 | + if (closeOnOutOfBoundsClick()) { |
| 68 | + animateClose(); |
| 69 | + result = true; |
| 70 | + } |
| 71 | + } else { |
| 72 | + loop: |
| 73 | + for (LocatedWidget widget : this.hovering) { |
| 74 | + widget.applyMatrix(getContext()); |
| 75 | + if (widget.getElement() instanceof JeiGhostIngredientSlot<?>ghostSlot && |
| 76 | + Mods.JustEnoughItems.isModLoaded()) { |
| 77 | + GhostIngredientDrag<?> drag = gregTech$getGhostDrag(); |
| 78 | + if (drag != null && gregTech$insertGhostIngredient(drag, ghostSlot)) { |
| 79 | + gregTech$stopDrag(); |
| 80 | + pressed = LocatedWidget.EMPTY; |
| 81 | + result = true; |
| 82 | + widget.unapplyMatrix(getContext()); |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + if (widget.getElement() instanceof Interactable interactable) { |
| 87 | + switch (interactable.onMousePressed(mouseButton)) { |
| 88 | + case IGNORE: |
| 89 | + break; |
| 90 | + case ACCEPT: { |
| 91 | + if (!gregTech$mouse.held()) { |
| 92 | + gregTech$mouse.addInteractable(interactable); |
| 93 | + } |
| 94 | + pressed = widget; |
| 95 | + // result = false; |
| 96 | + break; |
| 97 | + } |
| 98 | + case STOP: { |
| 99 | + pressed = LocatedWidget.EMPTY; |
| 100 | + result = true; |
| 101 | + widget.unapplyMatrix(getContext()); |
| 102 | + break loop; |
| 103 | + } |
| 104 | + case SUCCESS: { |
| 105 | + if (!gregTech$mouse.held()) { |
| 106 | + gregTech$mouse.addInteractable(interactable); |
| 107 | + } |
| 108 | + pressed = widget; |
| 109 | + result = true; |
| 110 | + widget.unapplyMatrix(getContext()); |
| 111 | + break loop; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + if (getContext().onHoveredClick(mouseButton, widget)) { |
| 116 | + pressed = LocatedWidget.EMPTY; |
| 117 | + result = true; |
| 118 | + widget.unapplyMatrix(getContext()); |
| 119 | + break; |
| 120 | + } |
| 121 | + widget.unapplyMatrix(getContext()); |
| 122 | + if (widget.getElement().canHover()) { |
| 123 | + result = true; |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (result && pressed.getElement() instanceof IFocusedWidget) { |
| 130 | + getContext().focus(pressed); |
| 131 | + } else { |
| 132 | + getContext().removeFocus(); |
| 133 | + } |
| 134 | + if (!gregTech$mouse.held()) { |
| 135 | + gregTech$mouse.lastPressed(pressed); |
| 136 | + if (gregTech$mouse.lastPressed().getElement() != null) { |
| 137 | + gregTech$mouse.timeHeld(Minecraft.getSystemTime()); |
| 138 | + } |
| 139 | + gregTech$mouse.lastButton(mouseButton); |
| 140 | + gregTech$mouse.held(true); |
| 141 | + } |
| 142 | + return result; |
| 143 | + } |
| 144 | + |
| 145 | + @Unique |
| 146 | + private static GhostIngredientDrag<?> gregTech$getGhostDrag() { |
| 147 | + GhostIngredientDragManager manager = ((DragManagerAccessor) JustEnoughItemsModule.jeiRuntime |
| 148 | + .getIngredientListOverlay()).getManager(); |
| 149 | + return ((GhostDragAccessor) manager).getDrag(); |
| 150 | + } |
| 151 | + |
| 152 | + @Unique |
| 153 | + @SuppressWarnings("rawtypes") |
| 154 | + private static boolean gregTech$insertGhostIngredient(GhostIngredientDrag drag, |
| 155 | + JeiGhostIngredientSlot slot) { |
| 156 | + Object object = slot.castGhostIngredientIfValid(drag.getIngredient()); |
| 157 | + if (object != null) { |
| 158 | + // noinspection unchecked |
| 159 | + slot.setGhostIngredient(object); |
| 160 | + return true; |
| 161 | + } |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + @Unique |
| 166 | + private static void gregTech$stopDrag() { |
| 167 | + GhostIngredientDragManager manager = ((DragManagerAccessor) JustEnoughItemsModule.jeiRuntime |
| 168 | + .getIngredientListOverlay()).getManager(); |
| 169 | + manager.stopDrag(); |
| 170 | + } |
| 171 | +} |
0 commit comments