Skip to content

Commit

Permalink
fix some more phantom slot issues
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Jan 27, 2025
1 parent 9eafc58 commit b1e8013
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/cleanroommc/modularui/ModularUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ModularUI {

private static boolean blurLoaded = false;
private static boolean sorterLoaded = false;
private static boolean jeiLoaded = false;

static {
// confirm mXparser license
Expand All @@ -46,6 +47,7 @@ public class ModularUI {
public void preInit(FMLPreInitializationEvent event) {
blurLoaded = Loader.isModLoaded("blur");
sorterLoaded = Loader.isModLoaded(BOGO_SORT);
jeiLoaded = Loader.isModLoaded("jei");
proxy.preInit(event);
}

Expand All @@ -66,4 +68,8 @@ public static boolean isBlurLoaded() {
public static boolean isSortModLoaded() {
return sorterLoaded;
}

public static boolean isJeiLoaded() {
return jeiLoaded;
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/cleanroommc/modularui/core/LateMixins.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.cleanroommc.modularui.core;

import com.google.common.collect.ImmutableList;

import net.minecraftforge.fml.common.Loader;

import zone.rong.mixinbooter.ILateMixinLoader;

import java.util.List;
import java.util.stream.Collectors;

public class LateMixins implements ILateMixinLoader {

public static final List<String> modMixins = ImmutableList.of("jei");

@Override
public List<String> getMixinConfigs() {
return modMixins.stream().map(mod -> "mixin.modularui." + mod + ".json").collect(Collectors.toList());
}

@Override
public boolean shouldMixinConfigQueue(String mixinConfig) {
String[] parts = mixinConfig.split("\\.");
return parts.length != 4 || shouldEnableModMixin(parts[2]);
}

public boolean shouldEnableModMixin(String mod) {
return Loader.isModLoaded(mod);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cleanroommc.modularui.core.mixin.jei;

import mezz.jei.gui.ghost.GhostIngredientDrag;
import mezz.jei.gui.ghost.GhostIngredientDragManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(value = GhostIngredientDragManager.class, remap = false)
public interface GhostIngredientDragManagerAccessor {

@Accessor
GhostIngredientDrag<?> getGhostIngredientDrag();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public abstract class IngredientGridMixin {
at = @At(value = "INVOKE",
target = "Lmezz/jei/gui/overlay/IngredientGrid;drawTooltips(Lnet/minecraft/client/Minecraft;II)V"))
private void considerExclusions(IngredientGrid instance, Minecraft minecraft, int mouseX, int mouseY, Operation<Void> original) {
if (!guiScreenHelper.isInGuiExclusionArea(mouseX, mouseY))
if (!guiScreenHelper.isInGuiExclusionArea(mouseX, mouseY)) {
original.call(instance, minecraft, mouseX, mouseY);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cleanroommc.modularui.core.mixin.jei;

import mezz.jei.gui.ghost.GhostIngredientDragManager;
import mezz.jei.gui.overlay.IngredientListOverlay;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(value = IngredientListOverlay.class, remap = false)
public interface IngredientListOverlayAccessor {

@Accessor
GhostIngredientDragManager getGhostIngredientDragManager();
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.cleanroommc.modularui.integration.jei;

import com.cleanroommc.modularui.api.widget.IWidget;
import com.cleanroommc.modularui.drawable.GuiDraw;
import com.cleanroommc.modularui.utils.Color;
import com.cleanroommc.modularui.widget.Widget;

import mezz.jei.gui.ghost.GhostIngredientDrag;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;

/**
* An interface for compat with JEI's ghost slots.
* Implement this on any {@link IWidget}.
Expand Down Expand Up @@ -34,4 +40,18 @@ public interface JeiGhostIngredientSlot<I> {
*/
@Nullable
I castGhostIngredientIfValid(@NotNull Object ingredient);

default void drawHighlight(Rectangle area, boolean hovering) {
int color = hovering ? Color.argb(76, 201, 25, 128) : Color.argb(19, 201, 10, 64);
GuiDraw.drawRect(0, 0, area.width, area.height, color);
}

static <T> boolean insertGhostIngredient(GhostIngredientDrag<?> drag, JeiGhostIngredientSlot<T> slot) {
T t = slot.castGhostIngredientIfValid(drag.getIngredient());
if (t != null) {
slot.setGhostIngredient(t);
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public Object getIngredientUnderMouse(@NotNull T guiContainer, int mouseX, int m
@Override
public void onComplete() {}

@Override
public boolean shouldHighlightTargets() {
return false;
}

@Nullable
@Override
public IGuiProperties apply(@NotNull T guiScreen) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
package com.cleanroommc.modularui.integration.jei;

import com.cleanroommc.modularui.core.mixin.jei.GhostIngredientDragManagerAccessor;
import com.cleanroommc.modularui.core.mixin.jei.IngredientListOverlayAccessor;
import com.cleanroommc.modularui.screen.GuiContainerWrapper;

import mezz.jei.api.IJeiRuntime;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.IModRegistry;
import mezz.jei.api.JEIPlugin;

import mezz.jei.gui.ghost.GhostIngredientDrag;
import mezz.jei.gui.ghost.GhostIngredientDragManager;
import org.jetbrains.annotations.NotNull;

@JEIPlugin
public class ModularUIJeiPlugin implements IModPlugin {

private static IJeiRuntime runtime;

@Override
public void register(@NotNull IModRegistry registry) {
new ModularUIHandler<>(GuiContainerWrapper.class).register(registry);
}

@Override
public void onRuntimeAvailable(IJeiRuntime jeiRuntime) {
ModularUIJeiPlugin.runtime = jeiRuntime;
}

public static IJeiRuntime getRuntime() {
return runtime;
}

public static GhostIngredientDragManager getGhostDragManager() {
return ((IngredientListOverlayAccessor) runtime.getIngredientListOverlay()).getGhostIngredientDragManager();
}

public static GhostIngredientDrag<?> getGhostDrag() {
return ((GhostIngredientDragManagerAccessor) getGhostDragManager()).getGhostIngredientDrag();
}

public static boolean hasDraggingGhostIngredient() {
return getGhostDrag() != null;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/cleanroommc/modularui/screen/ModularPanel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.modularui.screen;

import com.cleanroommc.modularui.ModularUI;
import com.cleanroommc.modularui.api.IPanelHandler;
import com.cleanroommc.modularui.api.ITheme;
import com.cleanroommc.modularui.api.drawable.IDrawable;
Expand All @@ -8,6 +9,8 @@
import com.cleanroommc.modularui.api.widget.IFocusedWidget;
import com.cleanroommc.modularui.api.widget.IWidget;
import com.cleanroommc.modularui.api.widget.Interactable;
import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;
import com.cleanroommc.modularui.integration.jei.ModularUIJeiPlugin;
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
import com.cleanroommc.modularui.screen.viewport.GuiViewportStack;
import com.cleanroommc.modularui.screen.viewport.LocatedWidget;
Expand All @@ -20,6 +23,8 @@
import com.cleanroommc.modularui.widget.sizer.Area;
import com.cleanroommc.modularui.widgets.SlotGroupWidget;

import mezz.jei.gui.ghost.GhostIngredientDrag;

import net.minecraft.client.Minecraft;

import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -299,6 +304,17 @@ public boolean onMousePressed(int mouseButton) {
loop:
for (LocatedWidget widget : this.hovering) {
widget.applyMatrix(getContext());
// try inserting ghost ingredient
if (widget.getElement() instanceof JeiGhostIngredientSlot<?> ghostSlot && ModularUI.isJeiLoaded()) {
GhostIngredientDrag<?> drag = ModularUIJeiPlugin.getGhostDrag();
if (drag != null && JeiGhostIngredientSlot.insertGhostIngredient(drag, ghostSlot)) {
ModularUIJeiPlugin.getGhostDragManager().stopDrag();
pressed = LocatedWidget.EMPTY;
result = true;
widget.unapplyMatrix(getContext());
break;
}
}
// click widget and see how it reacts
if (widget.getElement() instanceof Interactable interactable) {
switch (interactable.onMousePressed(mouseButton)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void setGhostIngredient(@NotNull FluidStack ingredient) {

@Override
public @Nullable FluidStack castGhostIngredientIfValid(@NotNull Object ingredient) {
return this.syncHandler.isPhantom() && ingredient instanceof FluidStack fluidStack ? fluidStack : null;
return areAncestorsEnabled() && this.syncHandler.isPhantom() && ingredient instanceof FluidStack fluidStack ? fluidStack : null;
}

@Override
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/cleanroommc/modularui/widgets/ItemSlot.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.modularui.widgets;

import com.cleanroommc.modularui.ModularUI;
import com.cleanroommc.modularui.api.ITheme;
import com.cleanroommc.modularui.api.widget.IVanillaSlot;
import com.cleanroommc.modularui.api.widget.Interactable;
Expand All @@ -10,6 +11,7 @@
import com.cleanroommc.modularui.drawable.text.TextRenderer;
import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;
import com.cleanroommc.modularui.integration.jei.JeiIngredientProvider;
import com.cleanroommc.modularui.integration.jei.ModularUIJeiPlugin;
import com.cleanroommc.modularui.screen.ClientScreenHandler;
import com.cleanroommc.modularui.screen.ModularScreen;
import com.cleanroommc.modularui.screen.RichTooltip;
Expand Down Expand Up @@ -88,7 +90,11 @@ public void draw(ModularGuiContext context, WidgetTheme widgetTheme) {
drawSlot(getSlot());
RenderHelper.enableStandardItemLighting();
GlStateManager.disableLighting();
if (isHovering()) {
if (this.syncHandler.isPhantom() && ModularUI.isJeiLoaded() && ModularUIJeiPlugin.hasDraggingGhostIngredient()) {
GlStateManager.colorMask(true, true, true, false);
drawHighlight(getArea(), isHovering());
GlStateManager.colorMask(true, true, true, true);
} else if (isHovering()) {
GlStateManager.colorMask(true, true, true, false);
GuiDraw.drawRect(1, 1, 16, 16, getSlotHoverColor());
GlStateManager.colorMask(true, true, true, true);
Expand Down Expand Up @@ -289,7 +295,10 @@ public void setGhostIngredient(@NotNull ItemStack ingredient) {

@Override
public @Nullable ItemStack castGhostIngredientIfValid(@NotNull Object ingredient) {
return this.syncHandler.isPhantom() && ingredient instanceof ItemStack itemStack ? itemStack : null;
return areAncestorsEnabled() &&
this.syncHandler.isPhantom() &&
ingredient instanceof ItemStack itemStack &&
this.syncHandler.isItemValid(itemStack) ? itemStack : null;
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions src/main/resources/mixin.modularui.jei.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"package": "com.cleanroommc.modularui.core.mixin.jei",
"refmap": "mixins.modularui.refmap.json",
"target": "@env(DEFAULT)",
"minVersion": "0.8",
"compatibilityLevel": "JAVA_8",
"client": [
],
"mixins": [
"GhostIngredientDragManagerAccessor",
"IngredientGridMixin",
"IngredientListOverlayAccessor"
]
}

0 comments on commit b1e8013

Please sign in to comment.