Skip to content

Commit 7b0d46d

Browse files
authored
Fix the JEI green overlay appearing in cheat mode on phantom slots and fix the crafting station slots not having a hover overlay (#2817)
1 parent ff6de03 commit 7b0d46d

File tree

9 files changed

+162
-36
lines changed

9 files changed

+162
-36
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package gregtech.api.util;
2+
3+
import net.minecraft.enchantment.EnchantmentData;
4+
5+
import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;
6+
import com.cleanroommc.modularui.integration.jei.ModularUIJeiPlugin;
7+
import mezz.jei.Internal;
8+
9+
public class JEIUtil {
10+
11+
/**
12+
* Check if the player is currently hovering over a valid ingredient for this slot. <br/>
13+
* Will always return false is JEI is not installed.
14+
*/
15+
public static boolean hoveringOverIngredient(JeiGhostIngredientSlot<?> jeiGhostIngredientSlot) {
16+
if (!Mods.JustEnoughItems.isModLoaded()) return false;
17+
return ModularUIJeiPlugin.hoveringOverIngredient(jeiGhostIngredientSlot);
18+
}
19+
20+
public static Object getBookStackIfEnchantment(Object ingredient) {
21+
if (ingredient instanceof EnchantmentData enchantmentData) {
22+
return Internal.getIngredientRegistry()
23+
.getIngredientHelper(enchantmentData)
24+
.getCheatItemStack(enchantmentData);
25+
}
26+
27+
return ingredient;
28+
}
29+
}

src/main/java/gregtech/client/utils/RenderUtil.java

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package gregtech.client.utils;
22

33
import gregtech.api.gui.resources.TextureArea;
4-
import gregtech.api.util.Mods;
4+
import gregtech.api.util.JEIUtil;
55

66
import net.minecraft.client.Minecraft;
77
import net.minecraft.client.gui.FontRenderer;
@@ -29,8 +29,11 @@
2929
import codechicken.lib.vec.Matrix4;
3030
import com.cleanroommc.modularui.api.MCHelper;
3131
import com.cleanroommc.modularui.api.widget.IWidget;
32+
import com.cleanroommc.modularui.drawable.GuiDraw;
3233
import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;
33-
import com.cleanroommc.modularui.integration.jei.ModularUIJeiPlugin;
34+
import com.cleanroommc.modularui.theme.WidgetSlotTheme;
35+
import com.cleanroommc.modularui.theme.WidgetTheme;
36+
import com.cleanroommc.modularui.utils.Color;
3437
import org.jetbrains.annotations.NotNull;
3538
import org.jetbrains.annotations.Nullable;
3639
import org.lwjgl.opengl.GL11;
@@ -43,6 +46,7 @@
4346
public class RenderUtil {
4447

4548
private static final Deque<int[]> scissorFrameStack = new ArrayDeque<>();
49+
public static final int defaultSlotHoverColor = Color.withAlpha(Color.WHITE.main, 0x60);
4650

4751
public static void useScissor(int x, int y, int width, int height, Runnable codeBlock) {
4852
pushScissorFrame(x, y, width, height);
@@ -716,14 +720,39 @@ public void put(int element, float @NotNull... data) {
716720
return getTextureMap().getMissingSprite();
717721
}
718722

719-
public static void handleJeiGhostHighlight(IWidget slot) {
720-
if (!Mods.JustEnoughItems.isModLoaded()) return;
721-
if (!(slot instanceof JeiGhostIngredientSlot<?>ingredientSlot)) return;
722-
if (ModularUIJeiPlugin.hasDraggingGhostIngredient() ||
723-
ModularUIJeiPlugin.hoveringOverIngredient(ingredientSlot)) {
724-
GlStateManager.colorMask(true, true, true, false);
725-
ingredientSlot.drawHighlight(slot.getArea(), slot.isHovering());
726-
GlStateManager.colorMask(true, true, true, true);
723+
public static void drawSlotOverlay(@NotNull IWidget slot, int overlayColor) {
724+
GlStateManager.colorMask(true, true, true, false);
725+
GuiDraw.drawRect(1, 1, slot.getArea().w() - 2, slot.getArea().h() - 2, overlayColor);
726+
GlStateManager.colorMask(true, true, true, true);
727+
}
728+
729+
public static void drawSlotOverlay(@NotNull IWidget slot, WidgetTheme widgetTheme) {
730+
drawSlotOverlay(slot, widgetTheme instanceof WidgetSlotTheme slotTheme ? slotTheme.getSlotHoverColor() :
731+
defaultSlotHoverColor);
732+
}
733+
734+
public static void handleSlotOverlay(@NotNull IWidget slot, @NotNull WidgetTheme widgetTheme) {
735+
if (slot.isHovering()) {
736+
drawSlotOverlay(slot, widgetTheme);
737+
}
738+
}
739+
740+
public static <
741+
T extends IWidget & JeiGhostIngredientSlot<?>> void drawJEIGhostSlotOverlay(@NotNull T jeiGhostIngredientSlot) {
742+
GlStateManager.colorMask(true, true, true, false);
743+
jeiGhostIngredientSlot.drawHighlight(jeiGhostIngredientSlot.getArea(), jeiGhostIngredientSlot.isHovering());
744+
GlStateManager.colorMask(true, true, true, true);
745+
}
746+
747+
public static <
748+
T extends IWidget & JeiGhostIngredientSlot<?>> boolean handleJEIGhostSlotOverlay(@NotNull T jeiGhostIngredientSlot,
749+
@NotNull WidgetTheme widgetTheme) {
750+
if (JEIUtil.hoveringOverIngredient(jeiGhostIngredientSlot)) {
751+
drawJEIGhostSlotOverlay(jeiGhostIngredientSlot);
752+
return true;
727753
}
754+
755+
handleSlotOverlay(jeiGhostIngredientSlot, widgetTheme);
756+
return false;
728757
}
729758
}

src/main/java/gregtech/common/mui/widget/GTFluidSlot.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import gregtech.api.util.GTUtility;
55
import gregtech.client.utils.RenderUtil;
66

7-
import net.minecraft.client.renderer.GlStateManager;
87
import net.minecraft.item.ItemStack;
98
import net.minecraftforge.fluids.FluidStack;
109
import net.minecraftforge.fluids.IFluidTank;
@@ -116,13 +115,7 @@ public void draw(ModularGuiContext context, WidgetSlotTheme widgetTheme) {
116115
this.textRenderer.draw(amount);
117116
}
118117

119-
if (isHovering()) {
120-
GlStateManager.colorMask(true, true, true, false);
121-
GuiDraw.drawRect(1, 1, getArea().w() - 2, getArea().h() - 2, widgetTheme.getSlotHoverColor());
122-
GlStateManager.colorMask(true, true, true, true);
123-
}
124-
125-
RenderUtil.handleJeiGhostHighlight(this);
118+
RenderUtil.handleJEIGhostSlotOverlay(this, widgetTheme);
126119
}
127120

128121
@Override

src/main/java/gregtech/common/mui/widget/workbench/CraftingInputSlot.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gregtech.common.mui.widget.workbench;
22

33
import gregtech.api.util.GTUtility;
4+
import gregtech.api.util.JEIUtil;
45
import gregtech.client.utils.RenderUtil;
56
import gregtech.common.metatileentities.storage.CraftingRecipeLogic;
67

@@ -102,15 +103,21 @@ public void onMouseDrag(int mouseButton, long timeSinceClick) {
102103
@Override
103104
public void draw(ModularGuiContext context, WidgetTheme widgetTheme) {
104105
ItemStack itemstack = this.syncHandler.getStack();
106+
boolean jeiIngredientBeingHovered = JEIUtil.hoveringOverIngredient(this);
107+
105108
if (!itemstack.isEmpty()) {
106-
if (!this.hasIngredients) {
109+
if (!jeiIngredientBeingHovered && !this.hasIngredients) {
107110
RenderUtil.renderRect(0, 0, 18, 18, 200, 0x80FF0000);
108111
}
109112

110113
RenderUtil.renderItem(itemstack, 1, 1, 16, 16);
111114
}
112115

113-
RenderUtil.handleJeiGhostHighlight(this);
116+
if (jeiIngredientBeingHovered) {
117+
RenderUtil.drawJEIGhostSlotOverlay(this);
118+
} else {
119+
RenderUtil.handleSlotOverlay(this, widgetTheme);
120+
}
114121
}
115122

116123
@Override
@@ -128,6 +135,7 @@ public void setGhostIngredient(@NotNull ItemStack ingredient) {
128135

129136
@Override
130137
public @Nullable ItemStack castGhostIngredientIfValid(@NotNull Object ingredient) {
138+
ingredient = JEIUtil.getBookStackIfEnchantment(ingredient);
131139
return areAncestorsEnabled() && ingredient instanceof ItemStack ? (ItemStack) ingredient : null;
132140
}
133141

src/main/java/gregtech/common/mui/widget/workbench/CraftingOutputSlot.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ public boolean isValidSyncHandler(SyncHandler syncHandler) {
7272
@Override
7373
public void draw(ModularGuiContext context, WidgetTheme widgetTheme) {
7474
ItemStack itemstack = this.syncHandler.getOutputStack();
75-
if (itemstack.isEmpty()) return;
76-
7775
RenderUtil.renderItem(itemstack, 1, 1, 16, 16);
76+
RenderUtil.handleSlotOverlay(this, widgetTheme);
7877
}
7978

8079
@Override

src/main/java/gregtech/common/mui/widget/workbench/RecipeMemorySlot.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,23 @@ public RecipeMemorySlot(CraftingRecipeMemory memory, int index) {
4545

4646
@Override
4747
public void draw(ModularGuiContext context, WidgetTheme widgetTheme) {
48-
ItemStack itemstack = this.memory.getRecipeOutputAtIndex(this.index);
49-
if (itemstack.isEmpty()) return;
50-
51-
int cachedCount = itemstack.getCount();
52-
itemstack.setCount(1); // required to not render the amount overlay
53-
RenderUtil.renderItem(itemstack, 1, 1, 16, 16);
54-
itemstack.setCount(cachedCount);
55-
56-
// noinspection DataFlowIssue
57-
if (this.memory.getRecipeAtIndex(this.index).isRecipeLocked()) {
58-
GlStateManager.disableDepth();
59-
GTGuiTextures.RECIPE_LOCK.draw(context, 10, 1, 8, 8, widgetTheme);
60-
GlStateManager.enableDepth();
48+
ItemStack itemStack = this.memory.getRecipeOutputAtIndex(this.index);
49+
50+
if (!itemStack.isEmpty()) {
51+
int cachedCount = itemStack.getCount();
52+
itemStack.setCount(1); // required to not render the amount overlay
53+
RenderUtil.renderItem(itemStack, 1, 1, 16, 16);
54+
itemStack.setCount(cachedCount);
55+
56+
// noinspection DataFlowIssue
57+
if (this.memory.getRecipeAtIndex(this.index).isRecipeLocked()) {
58+
GlStateManager.disableDepth();
59+
GTGuiTextures.RECIPE_LOCK.draw(context, 10, 1, 8, 8, widgetTheme);
60+
GlStateManager.enableDepth();
61+
}
6162
}
63+
64+
RenderUtil.handleSlotOverlay(this, widgetTheme);
6265
}
6366

6467
@Override
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package gregtech.mixins.mui2;
2+
3+
import gregtech.api.util.JEIUtil;
4+
5+
import net.minecraft.item.ItemStack;
6+
7+
import com.cleanroommc.modularui.integration.jei.ModularUIJeiPlugin;
8+
import com.cleanroommc.modularui.widgets.ItemSlot;
9+
import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod;
10+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
11+
import mezz.jei.gui.ghost.GhostIngredientDrag;
12+
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
14+
import org.spongepowered.asm.mixin.Mixin;
15+
import org.spongepowered.asm.mixin.Shadow;
16+
import org.spongepowered.asm.mixin.injection.At;
17+
import org.spongepowered.asm.mixin.injection.Redirect;
18+
19+
// TODO: remove once MUI PR 146 merges into a release we use
20+
@Mixin(value = ItemSlot.class, remap = false)
21+
public abstract class ItemSlotMixin {
22+
23+
@Shadow
24+
public abstract @Nullable ItemStack castGhostIngredientIfValid(@NotNull Object ingredient);
25+
26+
@Redirect(method = "draw",
27+
at = @At(value = "INVOKE",
28+
target = "Lcom/cleanroommc/modularui/integration/jei/ModularUIJeiPlugin;hasDraggingGhostIngredient()Z"))
29+
private boolean onlyHighlightOnValidDrag() {
30+
GhostIngredientDrag<?> ingredientDrag = ModularUIJeiPlugin.getGhostDrag();
31+
if (ingredientDrag == null) return false;
32+
Object ingredient = ingredientDrag.getIngredient();
33+
if (ingredient == null) return false;
34+
return castGhostIngredientIfValid(ingredient) != null;
35+
}
36+
37+
@WrapMethod(method = "castGhostIngredientIfValid(Ljava/lang/Object;)Lnet/minecraft/item/ItemStack;")
38+
public @Nullable ItemStack addSupportForEnchantedBooks(Object ingredient, Operation<ItemStack> original) {
39+
return original.call(JEIUtil.getBookStackIfEnchantment(ingredient));
40+
}
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package gregtech.mixins.mui2;
2+
3+
import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;
4+
import com.cleanroommc.modularui.integration.jei.ModularUIJeiPlugin;
5+
import mezz.jei.config.Config;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.injection.At;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
10+
11+
@Mixin(value = ModularUIJeiPlugin.class)
12+
public class ModularUIJeiPluginMixin {
13+
14+
// TODO: remove this mixin when the fix from Brachy makes it into a release we use
15+
@Inject(method = "hoveringOverIngredient", at = @At(value = "HEAD"), remap = false, cancellable = true)
16+
private static void cancelIfCheatsOn(JeiGhostIngredientSlot<?> ingredientSlot,
17+
CallbackInfoReturnable<Boolean> cir) {
18+
if (Config.isCheatItemsEnabled()) {
19+
cir.setReturnValue(false);
20+
}
21+
}
22+
}

src/main/resources/mixins.gregtech.mui2.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"RichTextCompilerMixin"
1616
],
1717
"client": [
18-
"LangKeyMixin"
18+
"LangKeyMixin",
19+
"ModularUIJeiPluginMixin",
20+
"ItemSlotMixin"
1921
],
2022
"server": []
2123
}

0 commit comments

Comments
 (0)