Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework GuiScreenMixin #31

Merged
merged 4 commits into from
Oct 13, 2024
Merged

Rework GuiScreenMixin #31

merged 4 commits into from
Oct 13, 2024

Conversation

glowredman
Copy link
Member

Motivation

At the moment GTNewHorizons/GTNHLib#81 breaks this Mixin because drawHoveringText() is no longer called in many cases. By making the mixin a bit more lenient, the mixin in GTNHLib can be converted to an @Overwrite mixin, with these two requirements:

  • The number and order of local int variables until if (textLines.size() > 1) must be the same as in the vanilla implementation to ensure the correct variables are captured
  • From this.zLevel = 300.0f; to this.drawGradientRect(j2 - 3, k2 - 4, j2 + k + 3, k2 - 3, j1, j1); k, k2, j2 and i1 must not be modified to ensure that the values of the captured variables are correct

Removal of ObfuscationReflectionHelper.remapFieldNames()

The previous usage was incorrect. ReflectionHelper.findField() takes multiple field names and returns the field of the first name it finds. ObfuscationReflectionHelper.remapFieldNames() remaps all passed in names to the currently used names. In this case it would return an array of three times the same name (the correct one). Even though this works, it is not necessary.

Code comparison

Before

    protected void drawHoveringText(List<String> textLines, int x, int y, FontRenderer font) {
        if (!textLines.isEmpty()) {
            GL11.glDisable(32826);
            RenderHelper.disableStandardItemLighting();
            GL11.glDisable(2896);
            GL11.glDisable(2929);
            int k = 0;
            Iterator iterator = textLines.iterator();

            int k2;
            while(iterator.hasNext()) {
                String s = (String)iterator.next();
                k2 = font.getStringWidth(s);
                if (k2 > k) {
                    k = k2;
                }
            }

            int j2 = x + 12;
            k2 = y - 12;
            int i1 = 8;
            if (textLines.size() > 1) {
                i1 += 2 + (textLines.size() - 1) * 10;
            }

            if (j2 + k > this.width) {
                j2 -= 28 + k;
            }

            if (k2 + i1 + 6 > this.height) {
                k2 = this.height - i1 - 6;
            }

            this.zLevel = 300.0F;
            itemRender.zLevel = 300.0F;
            int j1 = -267386864;
            this.drawGradientRect(j2 - 3, k2 - 4, j2 + k + 3, k2 - 3, j1, j1);
            this.drawGradientRect(j2 - 3, k2 + i1 + 3, j2 + k + 3, k2 + i1 + 4, j1, j1);
            this.drawGradientRect(j2 - 3, k2 - 3, j2 + k + 3, k2 + i1 + 3, j1, j1);
            this.drawGradientRect(j2 - 4, k2 - 3, j2 - 3, k2 + i1 + 3, j1, j1);
            this.drawGradientRect(j2 + k + 3, k2 - 3, j2 + k + 4, k2 + i1 + 3, j1, j1);
            int k1 = 1347420415;
            int l1 = (k1 & 16711422) >> 1 | k1 & -16777216;
            this.drawGradientRect(j2 - 3, k2 - 3 + 1, j2 - 3 + 1, k2 + i1 + 3 - 1, k1, l1);
            this.drawGradientRect(j2 + k + 2, k2 - 3 + 1, j2 + k + 3, k2 + i1 + 3 - 1, k1, l1);
            this.drawGradientRect(j2 - 3, k2 - 3, j2 + k + 3, k2 - 3 + 1, k1, k1);
            this.drawGradientRect(j2 - 3, k2 + i1 + 2, j2 + k + 3, k2 + i1 + 3, l1, l1);
            int i2 = 0;

            while(true) {
                this.handler$zzo000$AppleCore$onDrawHoveringText(textLines, x, y, font, (CallbackInfo)null, k, iterator, j2, k2, i1, j1, k1, l1, i2); // <--- INJECT
                if (i2 >= textLines.size()) {
                    this.zLevel = 0.0F;
                    itemRender.zLevel = 0.0F;
                    GL11.glEnable(2896);
                    GL11.glEnable(2929);
                    RenderHelper.enableStandardItemLighting();
                    GL11.glEnable(32826);
                    break;
                }

                String s1 = (String)textLines.get(i2);
                font.drawStringWithShadow(s1, j2, k2, -1);
                if (i2 == 0) {
                    k2 += 2;
                }

                k2 += 10;
                ++i2;
            }
        }

    }

After

    protected void drawHoveringText(List<String> textLines, int x, int y, FontRenderer font) {
        if (!textLines.isEmpty()) {
            GL11.glDisable(32826);
            RenderHelper.disableStandardItemLighting();
            GL11.glDisable(2896);
            GL11.glDisable(2929);
            int k = 0;
            Iterator iterator = textLines.iterator();

            int k2;
            while(iterator.hasNext()) {
                String s = (String)iterator.next();
                k2 = font.getStringWidth(s);
                if (k2 > k) {
                    k = k2;
                }
            }

            int j2 = x + 12;
            k2 = y - 12;
            int i1 = 8;
            if (textLines.size() > 1) {
                i1 += 2 + (textLines.size() - 1) * 10;
            }

            if (j2 + k > this.width) {
                j2 -= 28 + k;
            }

            if (k2 + i1 + 6 > this.height) {
                k2 = this.height - i1 - 6;
            }

            this.handler$zzo000$AppleCore$onDrawHoveringText((CallbackInfo)null, j2, k2, k, i1); // <--- INJECT
            this.zLevel = 300.0F;
            itemRender.zLevel = 300.0F;
            int j1 = -267386864;
            this.drawGradientRect(j2 - 3, k2 - 4, j2 + k + 3, k2 - 3, j1, j1);
            this.drawGradientRect(j2 - 3, k2 + i1 + 3, j2 + k + 3, k2 + i1 + 4, j1, j1);
            this.drawGradientRect(j2 - 3, k2 - 3, j2 + k + 3, k2 + i1 + 3, j1, j1);
            this.drawGradientRect(j2 - 4, k2 - 3, j2 - 3, k2 + i1 + 3, j1, j1);
            this.drawGradientRect(j2 + k + 3, k2 - 3, j2 + k + 4, k2 + i1 + 3, j1, j1);
            int k1 = 1347420415;
            int l1 = (k1 & 16711422) >> 1 | k1 & -16777216;
            this.drawGradientRect(j2 - 3, k2 - 3 + 1, j2 - 3 + 1, k2 + i1 + 3 - 1, k1, l1);
            this.drawGradientRect(j2 + k + 2, k2 - 3 + 1, j2 + k + 3, k2 + i1 + 3 - 1, k1, l1);
            this.drawGradientRect(j2 - 3, k2 - 3, j2 + k + 3, k2 - 3 + 1, k1, k1);
            this.drawGradientRect(j2 - 3, k2 + i1 + 2, j2 + k + 3, k2 + i1 + 3, l1, l1);

            for(int i2 = 0; i2 < textLines.size(); ++i2) {
                String s1 = (String)textLines.get(i2);
                font.drawStringWithShadow(s1, j2, k2, -1);
                if (i2 == 0) {
                    k2 += 2;
                }

                k2 += 10;
            }

            this.zLevel = 0.0F;
            itemRender.zLevel = 0.0F;
            GL11.glEnable(2896);
            GL11.glEnable(2929);
            RenderHelper.enableStandardItemLighting();
            GL11.glEnable(32826);
        }

    }

Diff

https://www.diffchecker.com/qVdFcRxx/

@glowredman glowredman requested a review from a team October 12, 2024 23:22
@Dream-Master Dream-Master merged commit cd8a951 into master Oct 13, 2024
1 check passed
@Dream-Master Dream-Master deleted the guiscreenmixin branch October 13, 2024 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants