From b04c4336109024993420c02a339ba6ca52c9ddcb Mon Sep 17 00:00:00 2001 From: ImToggle <98242902+ImToggle@users.noreply.github.com> Date: Fri, 28 Jun 2024 14:05:39 +0800 Subject: [PATCH] fixed self nametag not showing in F1 --- .../polynametag/mixin/FontRendererMixin.java | 19 +++++ .../mixin/RendererLivingEntityMixin.java | 14 +++- .../mixin/oneconfig/TextRendererMixin.java | 20 ----- .../polynametag/render/NametagRendering.kt | 82 ++++--------------- src/main/resources/mixins.polynametag.json | 2 +- 5 files changed, 50 insertions(+), 87 deletions(-) create mode 100644 src/main/java/org/polyfrost/polynametag/mixin/FontRendererMixin.java delete mode 100644 src/main/java/org/polyfrost/polynametag/mixin/oneconfig/TextRendererMixin.java diff --git a/src/main/java/org/polyfrost/polynametag/mixin/FontRendererMixin.java b/src/main/java/org/polyfrost/polynametag/mixin/FontRendererMixin.java new file mode 100644 index 0000000..fd636e9 --- /dev/null +++ b/src/main/java/org/polyfrost/polynametag/mixin/FontRendererMixin.java @@ -0,0 +1,19 @@ +package org.polyfrost.polynametag.mixin; + +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.renderer.GlStateManager; +import org.polyfrost.polynametag.render.NametagRenderingKt; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(FontRenderer.class) +public class FontRendererMixin { + + @Inject(method = "drawString(Ljava/lang/String;FFIZ)I", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/FontRenderer;renderString(Ljava/lang/String;FFIZ)I", ordinal = 1)) + private void translate(String text, float x, float y, int color, boolean dropShadow, CallbackInfoReturnable cir) { + if (NametagRenderingKt.getDrawingText()) GlStateManager.translate(0f, 0f, -0.01f); + } + +} \ No newline at end of file diff --git a/src/main/java/org/polyfrost/polynametag/mixin/RendererLivingEntityMixin.java b/src/main/java/org/polyfrost/polynametag/mixin/RendererLivingEntityMixin.java index 78d2b9f..0d690c3 100644 --- a/src/main/java/org/polyfrost/polynametag/mixin/RendererLivingEntityMixin.java +++ b/src/main/java/org/polyfrost/polynametag/mixin/RendererLivingEntityMixin.java @@ -12,6 +12,7 @@ import org.polyfrost.polynametag.config.ModConfig; import org.polyfrost.polynametag.render.NametagRenderingKt; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.*; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -22,6 +23,18 @@ @Mixin(value = RendererLivingEntity.class, priority = 1001) public abstract class RendererLivingEntityMixin { + @Unique + private boolean shouldShowOwnNametag; + + @Redirect( + method = "canRenderName(Lnet/minecraft/entity/EntityLivingBase;)Z", + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;isGuiEnabled()Z") + ) + private boolean gui() { + shouldShowOwnNametag = ((ModConfig.INSTANCE.enabled && ModConfig.INSTANCE.getShowOwnNametag() && (ModConfig.INSTANCE.getShowInInventory() || !PolyNametag.INSTANCE.getDrawingInventory()) && (!PolyNametag.INSTANCE.getDrawingWorld() || Minecraft.getMinecraft().gameSettings.thirdPersonView != 0)) || ModConfig.INSTANCE.getNametagPreview().getDrawing()); + return shouldShowOwnNametag || Minecraft.isGuiEnabled(); + } + @Redirect( method = "canRenderName(Lnet/minecraft/entity/EntityLivingBase;)Z", at = @At( @@ -30,7 +43,6 @@ public abstract class RendererLivingEntityMixin { ) ) private Entity polyNametag$cancelSelfCheck(RenderManager renderManager) { - boolean shouldShowOwnNametag = ((ModConfig.INSTANCE.enabled && ModConfig.INSTANCE.getShowOwnNametag() && (ModConfig.INSTANCE.getShowInInventory() || !PolyNametag.INSTANCE.getDrawingInventory()) && (!PolyNametag.INSTANCE.getDrawingWorld() || Minecraft.getMinecraft().gameSettings.thirdPersonView != 0)) || ModConfig.INSTANCE.getNametagPreview().getDrawing()); return shouldShowOwnNametag ? null : renderManager.livingPlayer; } diff --git a/src/main/java/org/polyfrost/polynametag/mixin/oneconfig/TextRendererMixin.java b/src/main/java/org/polyfrost/polynametag/mixin/oneconfig/TextRendererMixin.java deleted file mode 100644 index 9925dae..0000000 --- a/src/main/java/org/polyfrost/polynametag/mixin/oneconfig/TextRendererMixin.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.polyfrost.polynametag.mixin.oneconfig; - -import org.polyfrost.polynametag.render.NametagRenderingKt; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Pseudo; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -@Pseudo -@Mixin(targets = "cc.polyfrost.oneconfig.renderer.TextRenderer", remap = false) -public class TextRendererMixin { - - @Inject(method = "isDrawingTextBorder", at = @At("HEAD"), cancellable = true) - private static void isDrawingTextBorder(CallbackInfoReturnable cir) { - if (NametagRenderingKt.isDrawingBorder()) { - cir.setReturnValue(true); - } - } -} diff --git a/src/main/kotlin/org/polyfrost/polynametag/render/NametagRendering.kt b/src/main/kotlin/org/polyfrost/polynametag/render/NametagRendering.kt index f3c3e3f..94ea336 100644 --- a/src/main/kotlin/org/polyfrost/polynametag/render/NametagRendering.kt +++ b/src/main/kotlin/org/polyfrost/polynametag/render/NametagRendering.kt @@ -1,5 +1,7 @@ package org.polyfrost.polynametag.render +import cc.polyfrost.oneconfig.renderer.TextRenderer +import cc.polyfrost.oneconfig.utils.dsl.getAlpha import cc.polyfrost.oneconfig.utils.dsl.mc import club.sk1er.patcher.config.PatcherConfig import gg.essential.Essential @@ -17,11 +19,10 @@ import org.polyfrost.polynametag.PolyNametag import org.polyfrost.polynametag.config.ModConfig import org.polyfrost.polynametag.mixin.FontRendererAccessor import java.awt.Color -import java.util.regex.Pattern import kotlin.math.cos -import kotlin.math.max import kotlin.math.sin -import net.minecraft.client.renderer.GlStateManager as GL + +var drawingText = false internal fun shouldDrawBackground() = ModConfig.background && (!PolyNametag.isPatcher || !PatcherConfig.disableNametagBoxes) @@ -98,28 +99,6 @@ fun drawBackground(xStart: Double, xEnd: Double, color: Color, entity: Entity) { GL11.glDisable(GL11.GL_LINE_SMOOTH) } -private val regex = Pattern.compile("(?i)\u00A7[0-9a-f]") -var isDrawingBorder = false -fun FontRenderer.drawBorderedText(text: String, x: Float, y: Float, opacity: Int): Int { - if (this !is FontRendererAccessor) return -1 - val noColors = regex.matcher(text).replaceAll("\u00A7r") - var yes = 0 - if (opacity / 4 > 3) { - for (xOff in -2..2) { - for (yOff in -2..2) { - if (xOff * xOff != yOff * yOff) { - yes = max( - invokeRenderString( - noColors, xOff / 2f + x, yOff / 2f + y, opacity / 4 shl 24, false - ), yes - ) - } - } - } - } - return yes -} - fun drawIndicator(entity: Entity, string: String) { if (entity !is EntityPlayer) return OnlineIndicator.drawNametagIndicator(UMatrixStack(), entity, string, 0) @@ -135,45 +114,18 @@ fun Entity.canDrawIndicator(): Boolean { return false } -internal fun FontRenderer.drawStringWithoutZFighting(text: String, x: Int, y: Float, color: Int): Int { - if (this !is FontRendererAccessor) return -1 - GL.pushMatrix() - GL.translate(0f, 0f, -0.01f) - - when (ModConfig.textType) { - 0 -> { - val i = drawString(text, x.toFloat(), y, color, false) - GL.popMatrix() - return i - } - - 1 -> { - GL.enableAlpha() - invokeResetStyles() - - val shadowX = invokeRenderString(text, x + 1f, y + 1f, color, true) - GL.translate(0f, 0f, -0.01f) - val normalX = drawString(text, x.toFloat(), y, color, false) - GL.popMatrix() - - return max(shadowX, normalX) - } - - 2 -> { - GL.enableAlpha() - invokeResetStyles() - - isDrawingBorder = true - - val shadowX = drawBorderedText(text, x.toFloat(), y, 255) - GL.translate(0f, 0f, -0.01f) - val normalX = drawString(text, x.toFloat(), y, color, false) - - isDrawingBorder = false - GL.popMatrix() - - return max(shadowX, normalX) - } +internal fun FontRenderer.drawStringWithoutZFighting(text: String, x: Float, y: Float, color: Int): Int { + if (this !is FontRendererAccessor) return 0 + GlStateManager.pushMatrix() + GlStateManager.translate(0f, 0f, -0.01f) + drawingText = true + return when (ModConfig.textType) { + 0 -> drawString(text, x, y, color, false) + 1 -> drawString(text, x, y, color, true) + 2 -> TextRenderer.drawBorderedText(text, x, y, color, color.getAlpha()) + else -> 0 + }.apply { + drawingText = false + GlStateManager.popMatrix() } - return -1 } diff --git a/src/main/resources/mixins.polynametag.json b/src/main/resources/mixins.polynametag.json index 77a432f..fac5aa6 100644 --- a/src/main/resources/mixins.polynametag.json +++ b/src/main/resources/mixins.polynametag.json @@ -9,6 +9,7 @@ "client": [ "EntityRendererMixin", "FontRendererAccessor", + "FontRendererMixin", "GuiInventoryMixin", "MinecraftAccessor", "RenderAccessor", @@ -17,7 +18,6 @@ "essential.OnlineIndicatorMixin", "levelhead.AboveHeadRenderMixin", "levelhead.AboveHeadRenderMixin_ModifyArgs", - "oneconfig.TextRendererMixin", "patcher.NameTagRenderingHooksMixin" ], "verbose": true