Jump to content

[1.18.2] [SOLVED] Draw string in gui above items


Robsutar

Recommended Posts

render method in my AbstractContainerScreen class

 

@Override
    protected void renderBg(PoseStack poseStack, float pPartialTick, int mouseX, int mouseY) {
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
        RenderSystem.setShaderTexture(0, TEXTURE);
        this.blit(poseStack,leftPos,topPos,0,0, imageWidth, imageHeight);

        ItemStack stack = new ItemStack(SItems.REDSTONE_SILICON,64);
        String count = String.valueOf(stack.getCount());
  		//render item model
        itemRenderer.renderGuiItem(stack,mouseX,mouseY);
  		//render my string (64 in iamge)
        font.draw(poseStack,count,mouseX+(-font.width(count)+20),mouseY+10,0xFFFFFF);

    }

@Override
    public void render(PoseStack poseStack, int mouseX, int mouseY, float delta) {
        renderBackground(poseStack);
        super.render(poseStack, mouseX, mouseY, delta);
        renderTooltip(poseStack, mouseX, mouseY);
    }


but the string is below the item:

imagem-2022-07-01-195053801.png

Edited by Robsutar
Link to comment
Share on other sites

I am not good at rendering, but it looks like you have depth test issues, probably caused by the blending used by the item render?

If you can't use ItemRenderer.renderGuiItemDecorations() for some reason, have it look at what it does.

You might also want to look at AbstractContainerScreen.renderSlot()

  • Thanks 1

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Taking a look at AbstractContainerScreen.renderSlot() I got the following code:

Method created in my AbstractContainerScreen class:

private void renderCustomSlot(PoseStack poseStack,ItemStack itemstack,int x, int y) {
        boolean visible = true;
        boolean hover = false;

        this.setBlitOffset(100);
        this.itemRenderer.blitOffset = 100.0F;

        if (visible) {
            if (hover) {
                //Draw a rectangle behind the item
                fill(poseStack, x, y, x + 16, y + 16, -2130706433);
            }

            //Render item icon
            RenderSystem.enableDepthTest();
            this.itemRenderer.renderAndDecorateItem(this.minecraft.player, itemstack, x, y, x + y * this.imageWidth);

            //Render item string count
            PoseStack posestack = new PoseStack();
            if (itemstack.getCount() != 1) {
                String s = String.valueOf(itemstack.getCount());
                posestack.translate(0.0D, 0.0D, itemRenderer.blitOffset + 200.0F);
                MultiBufferSource.BufferSource multibuffersource$buffersource = MultiBufferSource.immediate(Tesselator.getInstance().getBuilder());

                /*
                float scale;
                if (s.length()<3){
                    scale=1f;
                }else if (s.length()<4){
                    scale = 0.75f;
                }else {
                    scale=0.5f;
                    s = s.substring(0,s.length()-3)+"k";
                }

                float compensatingScale = 1/scale;

                float stringX = (x + 19f - 2 - font.width(s)*scale)*compensatingScale;
                float stringY =(y + 6f + 3)*compensatingScale;

                posestack.scale(scale,scale,scale);
				*/
				float stringX = (x + 19f - 2 - font.width(s));
                float stringY =(y + 6f + 3);
                font.drawInBatch(s,stringX,stringY,16777215, true, posestack.last().pose(), multibuffersource$buffersource, false, 0, 15728880);
                //posestack.scale(compensatingScale,compensatingScale,compensatingScale);

                multibuffersource$buffersource.endBatch();
            }
        }

        this.itemRenderer.blitOffset = 0.0F;
        this.setBlitOffset(0);
    }


Now it works correctly:

imagem-2022-07-02-122610834.png

Thanks to warjort for the tip!

Link to comment
Share on other sites

  • Robsutar changed the title to [1.18.2] [SOLVED] Draw string in gui above items

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.