Hud渲染时出现乱码

Chlamydomonos


版本信息
你使用的系统:Windows 64位
你用的JDK是: JDK 1.8.0_261 64位
你使用的IDE:IntelliJ IDEA
你使用的IDE版本: 2020.2.2
Forge版本: 1.16.3-34.1.35
Minecraft版本: 1.16.3
Mapping 文件版本: 20201028-1.16.3

出错图


错误情况简述
我正在实现一个San值条,但在上面使用Minecraft.getInstance().fontRenderer.drawString方法渲染文字时出现乱码,若删去该语句则不会出现乱码

报错日志

无报错

相关代码
SanityHud.java

package xyz.chlamydomonos.clmcore.gui.hud;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.LazyOptional;
import xyz.chlamydomonos.clmcore.api.capabilities.ClmcoreCapabilities;
import xyz.chlamydomonos.clmcore.api.capabilities.ISanityCapability;
import xyz.chlamydomonos.clmcore.core.ChlamydomonosiaCore;

public class SanityHud extends AbstractGui
{
    private abstract class Layer extends AbstractGui
    {
        abstract void render(MatrixStack stack);
    }
    private Layer layerSanity = new Layer()
    {
        private final ResourceLocation image = new ResourceLocation(ChlamydomonosiaCore.MODID, "textures/gui/sanity_hud/layer_sanity.png");
        public void render(MatrixStack stack)
        {
            RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
            Minecraft.getInstance().getTextureManager().bindTexture(image);
            LazyOptional<ISanityCapability> sanityCap = Minecraft.getInstance().player.getCapability(ClmcoreCapabilities.SANITY_CAPABILITY);
            sanityCap.ifPresent((s) -> {
                this.blit(stack, 4, 4, 0, 0, (int)((s.getSanity() / 99.0) * 128), 16, (int)((s.getSanity() / 99.0) * 128), 16);
            });
        }
    };
    private Layer layerMaxSanity = new Layer()
    {
        private final ResourceLocation image = new ResourceLocation(ChlamydomonosiaCore.MODID, "textures/gui/sanity_hud/layer_max_sanity.png");
        public void render(MatrixStack stack)
        {
            RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
            Minecraft.getInstance().getTextureManager().bindTexture(image);
            LazyOptional<ISanityCapability> sanityCap = Minecraft.getInstance().player.getCapability(ClmcoreCapabilities.SANITY_CAPABILITY);
            sanityCap.ifPresent((s) -> {
                this.blit(stack, 4, 4, 0, 0, (int)((s.getMaxSanity() / 99.0) * 128), 16, (int)((s.getMaxSanity() / 99.0) * 128), 16);
            });
        }
    };
    private MatrixStack matrixStack;
    private final ResourceLocation image = new ResourceLocation(ChlamydomonosiaCore.MODID, "textures/gui/sanity_hud/layer_background.png");

    public SanityHud(MatrixStack stack)
    {
        this.matrixStack = stack;
    }

    public void setMatrixStack(MatrixStack stack) {
        this.matrixStack = stack;
    }

    public void render()
    {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        Minecraft.getInstance().getTextureManager().bindTexture(image);
        this.layerMaxSanity.render(this.matrixStack);
        this.layerSanity.render(this.matrixStack);
        LazyOptional<ISanityCapability> sanityCap = Minecraft.getInstance().player.getCapability(ClmcoreCapabilities.SANITY_CAPABILITY);
        sanityCap.ifPresent((s) -> {
            Minecraft.getInstance().fontRenderer.drawString(this.matrixStack, "Sanity: " + s.getSanity() + "/" + s.getMaxSanity(), 24, 24, 0xe0e0e0);
        });
        this.blit(this.matrixStack,4, 4, 0, 0, 128, 16, 128, 16);
    }
}


FledgeXu


你试试不硬编码内容(顺便一提,任何时候硬编码内容都是不应该的),用I18n.format来在语言文件里写你的文字。


Chlamydomonos


问题解决了,只要把drawString放到blit之后就可以


system


该主题在最后一个回复创建后7天后自动关闭。不再允许新的回复。