From 921ae36cd20785fa146163c02377bc0bd7e3eab8 Mon Sep 17 00:00:00 2001 From: Alexdoru <57050655+Alexdoru@users.noreply.github.com> Date: Tue, 6 Sep 2022 19:38:37 +0200 Subject: [PATCH] Move ASM transformer to mixin potion render fix (#101) * Add a debug message in the chat when toggling vanilla debug options * update dependencies and use GTNH lib to print chat message * update dependencies to get NEI fix that causes mixins to crash * move the potion rendering over NEI fix from ASM to mixins * changed the mixin to center the inventory * applys spotless * use the same name for potion offset * Fix the vanilla bug that doesn't render the potion effects that you get while your inventory is opened --- dependencies.gradle | 1 + .../hodgepodge/asm/HodgePodgeASMLoader.java | 4 - .../InventoryEffectRendererTransformer.java | 158 ------------------ .../mitchej123/hodgepodge/mixins/Mixins.java | 9 +- .../MixinInventoryEffectRenderer.java | 20 --- ...yEffectRenderer_PotionEffectRendering.java | 42 +++++ ...nInventoryEffectRenderer_PotionOffset.java | 29 ++++ 7 files changed, 79 insertions(+), 184 deletions(-) delete mode 100644 src/main/java/com/mitchej123/hodgepodge/asm/InventoryEffectRendererTransformer.java delete mode 100644 src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer.java create mode 100644 src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer_PotionEffectRendering.java create mode 100644 src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer_PotionOffset.java diff --git a/dependencies.gradle b/dependencies.gradle index 01e0273d..c36f0247 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,4 +1,5 @@ dependencies { + compile("com.github.GTNewHorizons:NotEnoughItems:2.3.1-GTNH:dev") compile("com.github.GTNewHorizons:GT5-Unofficial:5.09.41.18:dev") compile("com.github.GTNewHorizons:StructureLib:1.0.15:dev") compile("com.github.GTNewHorizons:GTNHLib:0.0.3:dev") diff --git a/src/main/java/com/mitchej123/hodgepodge/asm/HodgePodgeASMLoader.java b/src/main/java/com/mitchej123/hodgepodge/asm/HodgePodgeASMLoader.java index da0bc432..a80f6b11 100644 --- a/src/main/java/com/mitchej123/hodgepodge/asm/HodgePodgeASMLoader.java +++ b/src/main/java/com/mitchej123/hodgepodge/asm/HodgePodgeASMLoader.java @@ -38,10 +38,6 @@ public enum AsmTransformers { "Speed up Progress Bar by speeding up stripSpecialCharacters", () -> Hodgepodge.config.speedupProgressBar, Collections.singletonList("com.mitchej123.hodgepodge.asm.SpeedupProgressBarTransformer")), - FIX_POTION_EFFECT_RENDERING( - "Fix vanilla potion effects rendering above the NEI tooltips in the inventory", - () -> Hodgepodge.config.fixPotionEffectRender, - Collections.singletonList("com.mitchej123.hodgepodge.asm.InventoryEffectRendererTransformer")), FIX_TINKER_POTION_EFFECT_OFFSET( "Prevents the inventory from shifting when the player has active potion effects", () -> Hodgepodge.config.fixPotionRenderOffset, diff --git a/src/main/java/com/mitchej123/hodgepodge/asm/InventoryEffectRendererTransformer.java b/src/main/java/com/mitchej123/hodgepodge/asm/InventoryEffectRendererTransformer.java deleted file mode 100644 index b2c5d4fe..00000000 --- a/src/main/java/com/mitchej123/hodgepodge/asm/InventoryEffectRendererTransformer.java +++ /dev/null @@ -1,158 +0,0 @@ -package com.mitchej123.hodgepodge.asm; - -import com.mitchej123.hodgepodge.asm.util.AbstractClassTransformer; -import com.mitchej123.hodgepodge.asm.util.AbstractMethodTransformer; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.tree.*; - -public class InventoryEffectRendererTransformer extends AbstractClassTransformer { - public InventoryEffectRendererTransformer() { - addMethodTransformer( - References.cEffectRenderer.getMethod("drawScreen"), - ClassWriter.COMPUTE_FRAMES, - new AbstractMethodTransformer() { - @Override - public void transform() { - - final String NEIClientConfigClasspath = "codechicken/nei/NEIClientConfig"; - try { - if (this.getClass().getClassLoader().getResource(NEIClientConfigClasspath + ".class") - == null) { - log.info("Skip reordering InventoryEffectRenderer.drawScreen since NEI is not loaded"); - return; - } - } catch (Exception ignored) { - } - - /* - ============ORIGINAL CODE========= - - super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); - if (this.field_147045_u) { - this.func_147044_g(); - } - - ============TARGET CODE========== - - boolean bookmarkPanelHidden = NEIClientConfig.isHidden(); - if (bookmarkPanelHidden) { - super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); - } - if (this.field_147045_u) { - this.func_147044_g(); - } - if (bookmarkPanelHidden) { - return; - } - super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); - - ==========TARGET BYTECODE======== - - L0 - INVOKESTATIC codechicken/nei/NEIClientConfig.isHidden ()Z - DUP - ISTORE 4 - IFEQ L5 - LINENUMBER 44 L0 - ALOAD 0 - ILOAD 1 - ILOAD 2 - FLOAD 3 - INVOKESPECIAL net/minecraft/client/gui/inventory/GuiContainer.drawScreen (IIF)V - L5 - L1 - LINENUMBER 46 L1 - ALOAD 0 - GETFIELD net/minecraft/client/renderer/InventoryEffectRenderer.field_147045_u : Z - IFEQ L2 - L3 - LINENUMBER 48 L3 - ALOAD 0 - INVOKESPECIAL net/minecraft/client/renderer/InventoryEffectRenderer.func_147044_g ()V - L2 - ILOAD 4 - IFEQ L6 - RETURN - L6 - ALOAD 0 - ILOAD 1 - ILOAD 2 - FLOAD 3 - INVOKESPECIAL net/minecraft/client/gui/inventory/GuiContainer.drawScreen (IIF)V - LINENUMBER 50 L2 - FRAME SAME - RETURN - L4 - */ - - AbstractInsnNode startNodeSuperCall = - findNext(currentMethod.instructions.getFirst(), matchOpcode(Opcodes.ALOAD)); - InsnList superCallInsnList = new InsnList(); - for (int i = 0; i < 5; ++i) { - superCallInsnList.add(startNodeSuperCall.clone(null)); - startNodeSuperCall = startNodeSuperCall.getNext(); - } - - int ordinal = 0; - AbstractInsnNode drawScreenMethodeInsnNode = null; - AbstractInsnNode drawPotionEffectsNode = null; - for (AbstractInsnNode insnNode : currentMethod.instructions.toArray()) { - if (insnNode instanceof MethodInsnNode && insnNode.getOpcode() == Opcodes.INVOKESPECIAL) { - if (ordinal == 0) { - drawScreenMethodeInsnNode = insnNode; - ordinal++; - continue; - } - if (ordinal == 1) { - drawPotionEffectsNode = insnNode; - } - } - } - - if (drawScreenMethodeInsnNode == null || drawPotionEffectsNode == null) { - log.info("Failed reordering InventoryEffectRenderer.drawScreen"); - return; - } - - /* - * Injects before super call : - * boolean bookmarkPanelHidden = NEIClientConfig.isHidden(); - * if (bookmarkPanelHidden) { - */ - LabelNode skipFirstIfLabel = new LabelNode(); - InsnList headInjectionList = new InsnList(); - headInjectionList.add(new MethodInsnNode( - Opcodes.INVOKESTATIC, NEIClientConfigClasspath, "isHidden", "()Z", false)); - headInjectionList.add(new InsnNode(Opcodes.DUP)); - headInjectionList.add(new VarInsnNode(Opcodes.ISTORE, 4)); - headInjectionList.add(new JumpInsnNode(Opcodes.IFEQ, skipFirstIfLabel)); - currentMethod.instructions.insert(currentMethod.instructions.getFirst(), headInjectionList); - - /* - * Injects end of first if after the drawScreen super call - */ - currentMethod.instructions.insert(drawScreenMethodeInsnNode, skipFirstIfLabel); - - /* - * Injects at the end of the method : - * if(bookmarkPanelHidden){ - * return; - * } - * super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); - */ - InsnList secondDrawScreenInsnList = new InsnList(); - LabelNode skipSecondIfLabel = new LabelNode(); - secondDrawScreenInsnList.add(new VarInsnNode(Opcodes.ILOAD, 4)); - secondDrawScreenInsnList.add(new JumpInsnNode(Opcodes.IFEQ, skipSecondIfLabel)); - secondDrawScreenInsnList.add(new InsnNode(Opcodes.RETURN)); - secondDrawScreenInsnList.add(skipSecondIfLabel); - secondDrawScreenInsnList.add(superCallInsnList); - currentMethod.instructions.insert(drawPotionEffectsNode.getNext(), secondDrawScreenInsnList); - - currentMethod.maxLocals = 5; - log.info("Reordering InventoryEffectRenderer.drawScreen"); - } - }); - } -} diff --git a/src/main/java/com/mitchej123/hodgepodge/mixins/Mixins.java b/src/main/java/com/mitchej123/hodgepodge/mixins/Mixins.java index 54c7125e..4448232d 100644 --- a/src/main/java/com/mitchej123/hodgepodge/mixins/Mixins.java +++ b/src/main/java/com/mitchej123/hodgepodge/mixins/Mixins.java @@ -14,10 +14,15 @@ public enum Mixins { FENCE_CONNECTIONS_FIX( "minecraft.MixinBlockFence", () -> Hodgepodge.config.fixFenceConnections, TargetedMod.VANILLA), FIX_INVENTORY_OFFSET_WITH_POTIONS( - "minecraft.MixinInventoryEffectRenderer", + "minecraft.MixinInventoryEffectRenderer_PotionOffset", Side.CLIENT, () -> Hodgepodge.config.fixPotionRenderOffset, TargetedMod.VANILLA), + FIX_POTION_EFFECT_RENDERING( + "minecraft.MixinInventoryEffectRenderer_PotionEffectRendering", + Side.CLIENT, + () -> Hodgepodge.config.fixPotionEffectRender, + TargetedMod.VANILLA), CHUNK_COORDINATES_HASHCODE( "minecraft.MixinChunkCoordinates", () -> Hodgepodge.config.speedupChunkCoordinatesHashCode, @@ -293,6 +298,6 @@ public boolean shouldLoad(List loadedMods) { enum Side { BOTH, CLIENT, - SERVER; + SERVER } } diff --git a/src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer.java b/src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer.java deleted file mode 100644 index 3d18d805..00000000 --- a/src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.mitchej123.hodgepodge.mixins.minecraft; - -import net.minecraft.client.renderer.InventoryEffectRenderer; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.Constant; -import org.spongepowered.asm.mixin.injection.ModifyConstant; - -@Mixin(InventoryEffectRenderer.class) -public class MixinInventoryEffectRenderer { - - @ModifyConstant(method = "initGui", constant = @Constant(intValue = 160, ordinal = 0)) - public int hodgepodge$fixPotionOffset1(int i) { - return 0; - } - - @ModifyConstant(method = "initGui", constant = @Constant(intValue = 200, ordinal = 0)) - public int hodgepodge$fixPotionOffset2(int i) { - return 0; - } -} diff --git a/src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer_PotionEffectRendering.java b/src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer_PotionEffectRendering.java new file mode 100644 index 00000000..05ba1118 --- /dev/null +++ b/src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer_PotionEffectRendering.java @@ -0,0 +1,42 @@ +package com.mitchej123.hodgepodge.mixins.minecraft; + +import codechicken.nei.NEIClientConfig; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.renderer.InventoryEffectRenderer; +import net.minecraft.inventory.Container; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(InventoryEffectRenderer.class) +public abstract class MixinInventoryEffectRenderer_PotionEffectRendering extends GuiContainer { + + @Shadow + private void func_147044_g() {} + + /** + * @author Alexdoru + * @reason Fix the bug that renders the potion effects above the tooltips from items in NEI + * Fix the vanilla bug that doesn't render the potion effects that you get while your inventory + * is opened + */ + @Overwrite + public void drawScreen(int p_73863_1_, int p_73863_2_, float p_73863_3_) { + boolean bookmarkPanelHidden = NEIClientConfig.isHidden(); + if (bookmarkPanelHidden) { + super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); + } + if (!this.mc.thePlayer.getActivePotionEffects().isEmpty()) { + this.func_147044_g(); + } + if (bookmarkPanelHidden) { + return; + } + super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); + } + + /*Forced to have constructor matching super*/ + public MixinInventoryEffectRenderer_PotionEffectRendering(Container p_i1089_1_) { + super(p_i1089_1_); + } +} diff --git a/src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer_PotionOffset.java b/src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer_PotionOffset.java new file mode 100644 index 00000000..a0070bb8 --- /dev/null +++ b/src/main/java/com/mitchej123/hodgepodge/mixins/minecraft/MixinInventoryEffectRenderer_PotionOffset.java @@ -0,0 +1,29 @@ +package com.mitchej123.hodgepodge.mixins.minecraft; + +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.renderer.InventoryEffectRenderer; +import net.minecraft.inventory.Container; +import org.objectweb.asm.Opcodes; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(InventoryEffectRenderer.class) +public abstract class MixinInventoryEffectRenderer_PotionOffset extends GuiContainer { + + @Redirect( + method = "initGui", + at = + @At( + value = "FIELD", + target = "Lnet/minecraft/client/renderer/InventoryEffectRenderer;guiLeft:I", + opcode = Opcodes.PUTFIELD)) + public void hodgepodge$fixPotionOffset(InventoryEffectRenderer instance, int value) { + this.guiLeft = (this.width - this.xSize) / 2; + } + + /*Forced to have constructor matching super*/ + public MixinInventoryEffectRenderer_PotionOffset(Container p_i1072_1_) { + super(p_i1072_1_); + } +}