/
FluidStackRenderer.java
172 lines (143 loc) · 5.84 KB
/
FluidStackRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package mezz.jei.gui.ingredients;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.b3d.B3DModel;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import mezz.jei.api.gui.IDrawable;
public class FluidStackRenderer implements IIngredientRenderer<FluidStack> {
private static final int TEX_WIDTH = 16;
private static final int TEX_HEIGHT = 16;
private static final int MIN_FLUID_HEIGHT = 1; // ensure tiny amounts of fluid are still visible
private final int capacityMb;
private final int width;
private final int height;
@Nullable
private final IDrawable overlay;
public FluidStackRenderer(int capacityMb, int width, int height, @Nullable IDrawable overlay) {
this.capacityMb = capacityMb;
this.width = width;
this.height = height;
this.overlay = overlay;
}
@Override
public void draw(@Nonnull Minecraft minecraft, final int xPosition, final int yPosition, @Nullable FluidStack fluidStack) {
GlStateManager.pushAttrib();
{
GlStateManager.disableBlend();
drawFluid(minecraft, xPosition, yPosition, fluidStack);
GlStateManager.resetColor();
if (overlay != null) {
GlStateManager.pushAttrib();
GlStateManager.pushMatrix();
GlStateManager.enableAlpha();
GlStateManager.enableBlend();
GlStateManager.translate(0, 0, 200);
overlay.draw(minecraft, xPosition, yPosition);
GlStateManager.enableDepth();
GlStateManager.popMatrix();
GlStateManager.popAttrib();
}
GlStateManager.enableBlend();
}
GlStateManager.popAttrib();
}
private void drawFluid(@Nonnull Minecraft minecraft, final int xPosition, final int yPosition, @Nullable FluidStack fluidStack) {
if (fluidStack == null) {
return;
}
Fluid fluid = fluidStack.getFluid();
if (fluid == null) {
return;
}
TextureMap textureMapBlocks = minecraft.getTextureMapBlocks();
ResourceLocation fluidStill = fluid.getStill();
TextureAtlasSprite fluidStillSprite = null;
if (fluidStill != null) {
fluidStillSprite = textureMapBlocks.getTextureExtry(fluidStill.toString());
}
if (fluidStillSprite == null) {
fluidStillSprite = textureMapBlocks.getMissingSprite();
}
int fluidColor = fluid.getColor(fluidStack);
int scaledAmount = (fluidStack.amount * height) / capacityMb;
if (fluidStack.amount > 0 && scaledAmount < MIN_FLUID_HEIGHT) {
scaledAmount = MIN_FLUID_HEIGHT;
}
if (scaledAmount > height) {
scaledAmount = height;
}
minecraft.renderEngine.bindTexture(TextureMap.locationBlocksTexture);
setGLColorFromInt(fluidColor);
final int xTileCount = width / TEX_WIDTH;
final int xRemainder = width - (xTileCount * TEX_WIDTH);
final int yTileCount = scaledAmount / TEX_HEIGHT;
final int yRemainder = scaledAmount - (yTileCount * TEX_HEIGHT);
final int yStart = yPosition + height;
for (int xTile = 0; xTile <= xTileCount; xTile++) {
for (int yTile = 0; yTile <= yTileCount; yTile++) {
int width = (xTile == xTileCount) ? xRemainder : TEX_WIDTH;
int height = (yTile == yTileCount) ? yRemainder : TEX_HEIGHT;
int x = xPosition + (xTile * TEX_WIDTH);
int y = yStart - ((yTile + 1) * TEX_HEIGHT);
if (width > 0 && height > 0) {
int maskTop = TEX_HEIGHT - height;
int maskRight = TEX_WIDTH - width;
drawFluidTexture(x, y, fluidStillSprite, maskTop, maskRight, 100);
}
}
}
}
private static void setGLColorFromInt(int color) {
float red = (color >> 16 & 0xFF) / 255.0F;
float green = (color >> 8 & 0xFF) / 255.0F;
float blue = (color & 0xFF) / 255.0F;
GlStateManager.color(red, green, blue, 1.0F);
}
private static void drawFluidTexture(double xCoord, double yCoord, TextureAtlasSprite textureSprite, int maskTop, int maskRight, double zLevel) {
double uMin = (double) textureSprite.getMinU();
double uMax = (double) textureSprite.getMaxU();
double vMin = (double) textureSprite.getMinV();
double vMax = (double) textureSprite.getMaxV();
uMax = uMax - (maskRight / 16.0 * (uMax - uMin));
vMax = vMax - (maskTop / 16.0 * (vMax - vMin));
Tessellator tessellator = Tessellator.getInstance();
WorldRenderer worldrenderer = tessellator.getWorldRenderer();
worldrenderer.startDrawingQuads();
worldrenderer.addVertexWithUV(xCoord, yCoord + 16, zLevel, uMin, vMax);
worldrenderer.addVertexWithUV(xCoord + 16 - maskRight, yCoord + 16, zLevel, uMax, vMax);
worldrenderer.addVertexWithUV(xCoord + 16 - maskRight, yCoord + maskTop, zLevel, uMax, vMin);
worldrenderer.addVertexWithUV(xCoord, yCoord + maskTop, zLevel, uMin, vMin);
tessellator.draw();
}
@Nonnull
@Override
public List<String> getTooltip(@Nonnull Minecraft minecraft, @Nonnull FluidStack fluidStack) {
List<String> tooltip = new ArrayList<>();
Fluid fluidType = fluidStack.getFluid();
if (fluidType == null) {
return tooltip;
}
String fluidName = fluidType.getLocalizedName(fluidStack);
tooltip.add(fluidName);
String amount = String.format(Locale.ENGLISH, EnumChatFormatting.GRAY + "%,d / %,d", fluidStack.amount, capacityMb);
tooltip.add(amount);
return tooltip;
}
@Override
public FontRenderer getFontRenderer(@Nonnull Minecraft minecraft, @Nonnull FluidStack fluidStack) {
return minecraft.fontRendererObj;
}
}