Jump to content

Render Fluid in GUI


dyno

Recommended Posts

How should I do to render fluids in a bar in a GUI?

I read about TextureAtlasSprite on this forum, but it was on 1.15.2.

 

I suppose to have to:

get the amount of fluid and I did it with FluidTank#getFluidAmount(),

get the still texture of fluid and I did it with Fluid#getAttributes()#getStill().

 

Now how can I do to render in the bar? 

 

package com.olivemod.blocks.machine.energy.fluid_transporter;

import com.mojang.blaze3d.platform.GlStateManager;
import com.olivemod.utils.ModUtils;
import com.olivemod.utils.Reference.Reference;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.renderer.BlockRendererDispatcher;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureAtlasSpriteStitcher;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.fluid.IFluidState;
import net.minecraft.inventory.container.PlayerContainer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.energy.EnergyStorage;
import net.minecraftforge.fluids.FluidStack;

public class FluidTransporterScreen extends ContainerScreen<FluidTransporterContainer>{
	
	private static final ResourceLocation guiLocation = new ResourceLocation(Reference.MOD_ID, "textures/gui/machine/fluid_transporter_gui.png");

	public FluidTransporterScreen(FluidTransporterContainer screenContainer, PlayerInventory inv, ITextComponent titleIn) {
		super(screenContainer, inv, titleIn);
		
	}
	
	@Override
	public void render(final int mouseX, final int mouseY, final float partialTick) {
		
		this.renderBackground();
		super.render(mouseX, mouseY, partialTick);
		this.renderHoveredToolTip(mouseX, mouseY);
		
		int relMouseX = mouseX - this.guiLeft;
		int relMouseY = mouseY - this.guiTop;
		
		final TileEntityFluidTransporter tileEntityFluidTransporter = this.container.tileEntityFluidTransporter;
		boolean energyBarHovered = relMouseX > 150 && relMouseX < 170 && relMouseY > 4 && relMouseY < 81;
		if(energyBarHovered)
		{
			String toolTip = new TranslationTextComponent("gui." + Reference.MOD_ID + ".energy", String.valueOf(tileEntityFluidTransporter.energyStorage.getEnergyStored())).getFormattedText();
			this.renderTooltip(toolTip, mouseX, mouseY);
		}
	}
	
	@Override
	protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
		
		super.drawGuiContainerForegroundLayer(mouseX, mouseY);
		String string = this.title.getFormattedText();
		this.font.drawString(string, (float)(this.xSize / 2 - this.font.getStringWidth(string) / 2), 6.0F, 0x404040);
		this.font.drawString(this.playerInventory.getDisplayName().getFormattedText(), 8.0F, (this.ySize - 96 + 2), 0x404040);
	}

	@Override
	protected void drawGuiContainerBackgroundLayer(final float partialTicks, final int mouseX, final int mouseY) {	
		GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
		this.minecraft.getTextureManager().bindTexture(guiLocation);
		
		int startX = this.guiLeft;
		int startY = this.guiTop;
		
		// Screen #blit draws a part of the current texture (assumed to be 256x256) to the screen
		// The parameters are (x, y, u, v, width, height)		
		this.blit(startX, startY, 0, 0, this.xSize, this.ySize);
		
		final TileEntityFluidTransporter tileEntityFluidTransporter = container.tileEntityFluidTransporter;
		if(tileEntityFluidTransporter.energyStorage.getEnergyStored() > 0)
		{
			this.blit(startX + 151, startY + 4 + 74 - getEnergyProgressScaled(), 177, 36, 17,getEnergyProgressScaled());
		}
		if(tileEntityFluidTransporter.cookTime > 0)
		{
			this.blit(startX + 78, startY + 17, 175, 0, getCookProgressScaled(), 36);
		}
		
		if(!tileEntityFluidTransporter.tank.isEmpty())
		{ 
			int fluidStored = getFluidInTank(tileEntityFluidTransporter);
			FluidStack fluidStack = tileEntityFluidTransporter.tank.getFluid();
			if(fluidStack.getFluid() != Fluids.EMPTY)
			{
				ResourceLocation stillLocation = fluidStack.getFluid().getAttributes().getStill(fluidStack);
				TextureAtlasSprite textureAtlasSprite = 
				int color = fluidStack.getFluid().getAttributes().getColor();
				GlStateManager.color4f(1.0f, 1.0f, 1.0f, color);
			}
		}
	}

	private int getFluidInTank(TileEntityFluidTransporter tileEntityFluidTransporter2) {

		/*
		 * First getFluid() -> returns the fluidStack
		 * Second getFluid() -> returns the fluid
		*/
		if(tileEntityFluidTransporter2.tank.getFluid().isEmpty())
			return 0;
		
		return tileEntityFluidTransporter2.tank.getFluidAmount()/tileEntityFluidTransporter2.tank.getCapacity() * 74;
	}

	final TileEntityFluidTransporter tileEntityFluidTransporter = container.tileEntityFluidTransporter;
	
	private int getCookProgressScaled() {
		
		int i = this.tileEntityFluidTransporter.cookTime;
		return i != 0 ? i*24/ModUtils.getCookTime(tileEntityFluidTransporter.inventory.getStackInSlot(2), tileEntityFluidTransporter.inventory.getStackInSlot(3), tileEntityFluidTransporter.inventory.getStackInSlot(4)) : 0;
	}

	private int getEnergyProgressScaled() {

		final EnergyStorage storage = tileEntityFluidTransporter.energyStorage;
		return Math.round((float)storage.getEnergyStored() / (float)storage.getMaxEnergyStored() * 74);
	}

}

 

Link to comment
Share on other sites

I haven't tried doing what you are doing or something similar yet, but I would say probably take a look at the furnace, and see how it renders the progress bar. I know your thing isn't a progress bar, but it should give an idea about rending an image based on a % of completeness (or, fullness).

Link to comment
Share on other sites

1 hour ago, Ugdhar said:

I haven't tried doing what you are doing or something similar yet, but I would say probably take a look at the furnace, and see how it renders the progress bar. I know your thing isn't a progress bar, but it should give an idea about rending an image based on a % of completeness (or, fullness).

It Is almost useless for me because I know how to fill the bar based on a %.

I am trying to render each fluid in a bar

i.e. 

If It contains water, bar will be blue, if lava, will be red... 

Of course furnace doesn't do it.

This is what I would do.

 

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

Announcements



×
×
  • Create New...

Important Information

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