Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Help displaying tooltips to the player's screen


skip999
 Share

Recommended Posts

Hello,

I have been working on a custom tool. One of it's properties is that it has a temperature. I have this displayed in the item's tooltip, but I would also like to display it to the player's screen for ease of use. I was recommended to look into the RenderTooltipEvent, but I was unable to find anything in the event or it's sub classes that would actually enable me to accomplish this. Am I looking at the wrong event, or am I simply missing something.

 

 

Thanks in advance!

Link to comment
Share on other sites

Okay taking a look at that, that's more along the lines of what I need. However, I now have a problem in that I have no way of getting at the item in the player's inventory. What would I need to call in order to accomplish that?

Link to comment
Share on other sites

Okay this is working perfectly. However, I get this cursed image when I run my code:

image.thumb.png.3334e2c1de030d0e94f9989901c45637.png

I've tried calling different drawString variants, but to no avail.

Here is the code I am using:

    @SuppressWarnings("resource")
    @SubscribeEvent
    public static void renderRailgunTooltip(RenderGameOverlayEvent event) {
        
        ItemStack gunStackMainHand = Minecraft.getInstance().player.getItemStackFromSlot(EquipmentSlotType.MAINHAND);
        ItemStack gunStackOffHand = Minecraft.getInstance().player.getItemStackFromSlot(EquipmentSlotType.OFFHAND);
        
        if(gunStackMainHand.getItem() instanceof AbstractRailgun) {
            renderHeatToolTip(event, gunStackMainHand);
        }else if(gunStackOffHand.getItem() instanceof AbstractRailgun) {
            renderHeatToolTip(event, gunStackOffHand);
        }
        
    }

    @SuppressWarnings("static-access")
    private static void renderHeatToolTip(RenderGameOverlayEvent event, ItemStack stack) {
        Minecraft minecraft = Minecraft.getInstance();
        ItemElectric railgun = (ItemElectric)stack.getItem();
        
        ITextComponent text =
            new StringTextComponent("Temperature: " + railgun.getTemperatureStored(stack) + " C").mergeStyle(TextFormatting.YELLOW);
        
        minecraft.currentScreen.
        drawString(event.getMatrixStack(),
            minecraft.fontRenderer, text, text.getString().length(),0,0);
    }

Link to comment
Share on other sites

  • Do not use the raw RenderGameOverlayEvent. It has two subclasses, choose Post to draw additional things on the screen. Additionally RenderGameOverlayEvent has an EventType, you want to check for ALL when drawing new things. With your current code you are drawing to the screen many many times every frame.
  • 9 hours ago, skip999 said:

    ItemElectric railgun = (ItemElectric)stack.getItem();

    This cast can fail.

  • 9 hours ago, skip999 said:

    @SuppressWarnings("static-access")

    Why?

  • When changing the bound texture in RenderGameOverlayEvent (which you implicitly do by using the FontRenderer) you have to use

    minecraft.getTextureManager().bind(AbstractGui.GUI_ICONS_LOCATION);

    afterwards to rebind the default texture.

  • 9 hours ago, skip999 said:

    ITextComponent text =
                new StringTextComponent("Temperature: " + railgun.getTemperatureStored(stack) + " C").mergeStyle(TextFormatting.YELLOW);

    This should be in your language file.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.