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

[1.7.10] Show item durability bar


grand_mind1
 Share

Recommended Posts

Hi, I'm trying to make an item that can store "energy". I'd like the item to have a durability bar that displays how much energy it currently stores. I've tried to use showDurabilityBar() and getDurabilityForDisplay(), however it seems that the durability bar displays the opposite of what it should. (i.e. the bar is completely green when the item is empty and empty when the item is full)

Here is an example of what I mean:

http://i.imgur.com/U2byBbA.png

The item on the left is empty and the item on the right is full.

This is the code I have for the durability bar:

    @Override
    public boolean showDurabilityBar(ItemStack itemStack)
    {
        return true;
    }

    @Override
    public double getDurabilityForDisplay(ItemStack itemStack)
    {
        if (itemStack.getTagCompound() == null || !itemStack.getTagCompound().hasKey("Energy"))
        {
            itemStack.stackTagCompound = new NBTTagCompound();
            itemStack.getTagCompound().setInteger("Energy", 0);
        }

        return itemStack.getTagCompound().getInteger("Energy") / 80000;
    }

According to the documentation for getDurabilityForDisplay(), you are to pass 1 for 100% durability and 0 for 0%. I thought that this is what this would accomplish, but I must have done something wrong.

Thanks! :D

Link to comment
Share on other sites

Yeah, sorry, I meant to say that. 80k should be a full item. I got the items from the creative menu. This is the code I used to add the full item to the menu, if it is relevant:

    @Override
    public void getSubItems(Item item, CreativeTabs creativeTabs, List list)
    {
            list.add(new ItemStack(this, 1));
            ItemStack fullEnergy = new ItemStack(this, 1);
            this.setEnergyStored(fullEnergy, 80000);
            list.add(fullEnergy);
    }

setEnergyStored() just manipulates the NBT value used to store the amount of energy.

Link to comment
Share on other sites

Yes,

    /**
     * Queries the percentage of the 'Durability' bar that should be drawn.
     * 
     * @param stack The current ItemStack
     * @return 1.0 for 100% 0 for 0%
     */
    public double getDurabilityForDisplay(ItemStack stack)
    {
        return (double)stack.getItemDamageForDisplay() / (double)stack.getMaxDamage();
    }

  (default implementation of stack.getItemDamageForDisplay() just gives stack.itemDamage)

So this means that the javadoc is inverted.

 

And  how it is used:

                int j1 = (int)Math.round(13.0D - health * 13.0D);
                int k = (int)Math.round(255.0D - health * 255.0D);
                GL11.glDisable(GL11.GL_LIGHTING);
                GL11.glDisable(GL11.GL_DEPTH_TEST);
                GL11.glDisable(GL11.GL_TEXTURE_2D);
                GL11.glDisable(GL11.GL_ALPHA_TEST);
                GL11.glDisable(GL11.GL_BLEND);
                Tessellator tessellator = Tessellator.instance;
                int l = 255 - k << 16 | k << 8;
                int i1 = (255 - k) / 4 << 16 | 16128;
                this.renderQuad(tessellator, p_94148_4_ + 2, p_94148_5_ + 13, 13, 2, 0);
                this.renderQuad(tessellator, p_94148_4_ + 2, p_94148_5_ + 13, 12, 1, i1);
                this.renderQuad(tessellator, p_94148_4_ + 2, p_94148_5_ + 13, j1, 1, l);
                GL11.glEnable(GL11.GL_BLEND);
                GL11.glEnable(GL11.GL_ALPHA_TEST);
                GL11.glEnable(GL11.GL_TEXTURE_2D);
                GL11.glEnable(GL11.GL_LIGHTING);
                GL11.glEnable(GL11.GL_DEPTH_TEST);
                GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

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.
Note: Your post will require moderator approval before it will be visible.

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.