ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/inventory/gui/FluidDisplay.java

108 lines
4 KiB
Java
Raw Normal View History

2020-09-09 16:49:01 +02:00
package de.ellpeck.actuallyadditions.common.inventory.gui;
2016-07-21 01:49:01 +02:00
2019-05-02 09:10:29 +02:00
import java.text.NumberFormat;
import java.util.Collections;
2020-09-09 16:49:01 +02:00
import de.ellpeck.actuallyadditions.common.util.AssetUtil;
import de.ellpeck.actuallyadditions.common.util.StringUtil;
2016-07-21 01:49:01 +02:00
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fml.client.config.GuiUtils;
2016-11-26 15:09:15 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2016-07-21 01:49:01 +02:00
2016-11-26 15:09:15 +01:00
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public class FluidDisplay extends Gui {
2016-07-21 01:49:01 +02:00
private FluidTank fluidReference;
2016-07-21 01:49:01 +02:00
private Fluid oldFluid;
private int x;
private int y;
private boolean outline;
2016-07-21 01:49:01 +02:00
private ResourceLocation resLoc;
private boolean drawTextNextTo;
2019-05-02 09:10:29 +02:00
public FluidDisplay(int x, int y, FluidTank fluidReference, boolean outline, boolean drawTextNextTo) {
this.setData(x, y, fluidReference, outline, drawTextNextTo);
}
2019-05-02 09:10:29 +02:00
public FluidDisplay(int x, int y, FluidTank fluidReference) {
this(x, y, fluidReference, false, false);
}
2019-05-02 09:10:29 +02:00
public void setData(int x, int y, FluidTank fluidReference, boolean outline, boolean drawTextNextTo) {
2016-07-21 01:49:01 +02:00
this.x = x;
this.y = y;
this.fluidReference = fluidReference;
this.outline = outline;
this.drawTextNextTo = drawTextNextTo;
2016-07-21 01:49:01 +02:00
}
2019-05-02 09:10:29 +02:00
public void draw() {
2016-07-21 01:49:01 +02:00
Minecraft mc = Minecraft.getMinecraft();
mc.getTextureManager().bindTexture(AssetUtil.GUI_INVENTORY_LOCATION);
int barX = this.x;
int barY = this.y;
2019-05-02 09:10:29 +02:00
if (this.outline) {
2016-07-21 01:49:01 +02:00
this.drawTexturedModalRect(this.x, this.y, 52, 163, 26, 93);
barX += 4;
barY += 4;
}
this.drawTexturedModalRect(barX, barY, 0, 171, 18, 85);
FluidStack stack = this.fluidReference.getFluid();
Fluid fluid = stack == null ? null : stack.getFluid();
2019-05-02 09:10:29 +02:00
if (this.resLoc == null || this.oldFluid != fluid) {
2016-07-21 01:49:01 +02:00
this.oldFluid = fluid;
2019-05-02 09:10:29 +02:00
if (fluid != null && fluid.getStill() != null) {
this.resLoc = new ResourceLocation(fluid.getStill().getNamespace(), "textures/" + fluid.getStill().getPath() + ".png");
2016-07-21 01:49:01 +02:00
}
}
2019-05-02 09:10:29 +02:00
if (stack != null && fluid != null && this.resLoc != null) {
2016-07-21 01:49:01 +02:00
mc.getTextureManager().bindTexture(this.resLoc);
2016-11-17 18:03:37 +01:00
GlStateManager.pushMatrix();
2016-07-21 01:49:01 +02:00
GlStateManager.enableBlend();
GlStateManager.disableAlpha();
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
2019-05-02 09:10:29 +02:00
int i = this.fluidReference.getFluidAmount() * 83 / this.fluidReference.getCapacity();
Gui.drawModalRectWithCustomSizedTexture(barX + 1, barY + 84 - i, 36, 172, 16, i, 16, 512);
2016-07-21 01:49:01 +02:00
GlStateManager.disableBlend();
GlStateManager.enableAlpha();
2016-11-17 18:03:37 +01:00
GlStateManager.popMatrix();
2016-07-21 01:49:01 +02:00
}
2019-05-02 09:10:29 +02:00
if (this.drawTextNextTo) {
this.drawString(mc.fontRenderer, this.getOverlayText(), barX + 25, barY + 78, StringUtil.DECIMAL_COLOR_WHITE);
}
2016-07-21 01:49:01 +02:00
}
2019-05-02 09:10:29 +02:00
public void drawOverlay(int mouseX, int mouseY) {
if (mouseX >= this.x && mouseY >= this.y && mouseX < this.x + (this.outline ? 26 : 18) && mouseY < this.y + (this.outline ? 93 : 85)) {
2016-07-21 01:49:01 +02:00
Minecraft mc = Minecraft.getMinecraft();
GuiUtils.drawHoveringText(Collections.singletonList(this.getOverlayText()), mouseX, mouseY, mc.displayWidth, mc.displayHeight, -1, mc.fontRenderer);
2016-07-21 01:49:01 +02:00
}
}
2019-05-02 09:10:29 +02:00
private String getOverlayText() {
NumberFormat format = NumberFormat.getInstance();
FluidStack stack = this.fluidReference.getFluid();
String cap = format.format(this.fluidReference.getCapacity());
2019-05-02 09:10:29 +02:00
return stack == null || stack.getFluid() == null ? "0/" + cap + " mB" : format.format(this.fluidReference.getFluidAmount()) + "/" + cap + " mB " + stack.getLocalizedName();
}
2016-07-21 01:49:01 +02:00
}