/
CauldronRenderer.java
278 lines (249 loc) · 9.81 KB
/
CauldronRenderer.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package knightminer.inspirations.plugins.jei.cauldron;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents;
import knightminer.inspirations.recipes.RecipesClientEvents;
import mezz.jei.api.ingredients.IIngredientRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.inventory.container.PlayerContainer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.vector.Matrix4f;
import net.minecraft.util.text.IFormattableTextComponent;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fluids.FluidStack;
import org.lwjgl.opengl.GL11;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import static knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe.HALF;
import static knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe.MAX;
import static knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe.QUARTER;
import static knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe.SIXTH;
import static knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe.THIRD;
/** Renderer for the cauldron contents */
public class CauldronRenderer {
// translations
private static final String LEVEL = CauldronCategory.TRANSLATION_KEY + ".level";
private static final String LEVEL_HALF = LEVEL + ".half";
private static final String LEVEL_THIRD = LEVEL + ".third";
private static final String LEVEL_QUARTER = LEVEL + ".quarter";
private static final String LEVEL_SIXTH = LEVEL + ".sixth";
private static final String LEVEL_EMPTY = LEVEL + ".empty";
private static final String LEVEL_FULL = LEVEL + ".full";
private static final ITextComponent[] AMOUNT_TEXTS = new ITextComponent[MAX + 1];
/** Size of the cauldron in pixels */
public static final int CAULDRON_SIZE = 12;
private CauldronRenderer() {}
/**
* Rendering to use in the ingredient list
*/
public static final Contents LIST = new CauldronRenderer.Contents(16, MAX, true);
/**
* Rendering to use in recipes
*/
private static final Contents[] CONTENT_LEVEL = new Contents[MAX + 1];
private static final Fluids[] FLUID_LEVEL = new Fluids[MAX + 1];
/**
* Gets the content renderer for the given amount
* @param amount Amount to render
* @return Renderer
*/
public static Contents contentLevel(int amount) {
if (amount < 0) amount = 0;
else if (amount > MAX) amount = MAX;
if (CONTENT_LEVEL[amount] == null) {
CONTENT_LEVEL[amount] = new Contents(CAULDRON_SIZE, amount, false);
}
return CONTENT_LEVEL[amount];
}
/**
* Gets the fluid renderer for the given amount
* @param amount Amount to render
* @return Renderer
*/
public static Fluids fluidLevel(int amount) {
if (amount < 0) amount = 0;
else if (amount > MAX) amount = MAX;
if (FLUID_LEVEL[amount] == null) {
FLUID_LEVEL[amount] = new Fluids(amount);
}
return FLUID_LEVEL[amount];
}
public static ITextComponent getAmountText(int amount) {
if (amount < 0) amount = 0;
if (amount > MAX) amount = MAX;
if (AMOUNT_TEXTS[amount] == null) {
IFormattableTextComponent amountText;
// 0 is empty, display quarter and third as cleaner fractions
if (amount == 0) {
amountText = new TranslationTextComponent(LEVEL_EMPTY);
} else if (amount == MAX) {
amountText = new TranslationTextComponent(LEVEL_FULL);
} else if (amount % HALF == 0) {
amountText = new TranslationTextComponent(LEVEL_HALF);
} else if (amount % THIRD == 0) {
amountText = new TranslationTextComponent(LEVEL_THIRD, amount / THIRD);
} else if (amount % QUARTER == 0) {
amountText = new TranslationTextComponent(LEVEL_QUARTER, amount / QUARTER);
} else if (amount % SIXTH == 0) {
amountText = new TranslationTextComponent(LEVEL_SIXTH, amount / SIXTH);
} else {
// default to x/12 for odd cases
amountText = new TranslationTextComponent(LEVEL, amount);
}
AMOUNT_TEXTS[amount] = amountText.mergeStyle(TextFormatting.GRAY);
}
return AMOUNT_TEXTS[amount];
}
/**
* Shared cauldron render code
* @param matrices Matrix stack instance
* @param x X position
* @param y Y position
* @param width Width
* @param height Height
* @param amount Fluid amount
* @param texture Texture to render
* @param color Texture color
*/
private static void render(MatrixStack matrices, int x, int y, int width, int height, int amount, ResourceLocation texture, int color) {
if (amount == 0) {
return;
}
// set up renderer
RenderSystem.enableBlend();
RenderSystem.enableAlphaTest();
Minecraft minecraft = Minecraft.getInstance();
TextureAtlasSprite sprite = minecraft.getModelManager().getAtlasTexture(PlayerContainer.LOCATION_BLOCKS_TEXTURE).getSprite(texture);
minecraft.getTextureManager().bindTexture(PlayerContainer.LOCATION_BLOCKS_TEXTURE);
// draw
int scaled = amount * height / MAX;
Matrix4f matrix = matrices.getLast().getMatrix();
setGLColorFromInt(color);
drawSprite(matrix, x, (y + height - scaled), width, scaled, sprite);
// reset render system
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.disableAlphaTest();
RenderSystem.disableBlend();
}
/**
* Sets the color based on the given int
* @param color Int color
*/
@SuppressWarnings("deprecation")
private static void setGLColorFromInt(int color) {
float red = (color >> 16 & 255) / 255f;
float green = (color >> 8 & 255) / 255f;
float blue = (color & 255) / 255f;
int alphaI = (color >> 24 & 255);
float alpha = alphaI == 0 ? 1 : alphaI / 255f;
RenderSystem.color4f(red, green, blue, alpha);
}
/**
* Draws the actual sprite
* @param matrix Matrix
* @param x1 Sprite X start
* @param y1 Sprite Y start
* @param width Sprite width
* @param height Sprite height
* @param sprite Sprite to draw
*/
private static void drawSprite(Matrix4f matrix, float x1, float y1, float width, float height, TextureAtlasSprite sprite) {
// calculate missing numbers
final float z = 100.0f;
float u1 = sprite.getMinU();
float u2 = sprite.getInterpolatedU((16 * width) / sprite.getWidth());
float v1 = sprite.getMinV();
float v2 = sprite.getInterpolatedV((16 * height) / sprite.getHeight());
float x2 = x1 + width;
float y2 = y1 + height;
// start drawing
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder builder = tessellator.getBuffer();
builder.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
builder.pos(matrix, x1, y2, z).tex(u1, v2).endVertex();
builder.pos(matrix, x2, y2, z).tex(u2, v2).endVertex();
builder.pos(matrix, x2, y1, z).tex(u2, v1).endVertex();
builder.pos(matrix, x1, y1, z).tex(u1, v1).endVertex();
tessellator.draw();
}
/**
* Cauldron contents implementation
*/
public static class Contents implements IIngredientRenderer<ICauldronContents> {
// constants
private final int size;
private final int amount;
private final boolean isList;
/**
* Creates a new renderer instance
* @param size Renderer size in pixels
* @param amount Amount to render
* @param isList If true, is the ingredient list
*/
private Contents(int size, int amount, boolean isList) {
this.size = size;
this.amount = amount;
this.isList = isList;
}
@Override
public void render(MatrixStack matrices, int x, int y, @Nullable ICauldronContents contents) {
if (contents == null) {
return;
}
CauldronRenderer.render(matrices, x, y, size, size, amount, RecipesClientEvents.cauldronTextures.getTexture(contents.getTextureName()), contents.getTintColor());
}
@Override
public List<ITextComponent> getTooltip(ICauldronContents contents, ITooltipFlag flag) {
List<ITextComponent> list = new ArrayList<>();
if (!isList && amount == 0) {
list.add(getAmountText(0));
} else {
list.add(contents.getDisplayName());
contents.addInformation(list, flag);
if (!isList) {
list.add(getAmountText(amount));
}
}
return list;
}
}
/** Fluid renderer override for ingredient type matching */
public static class Fluids implements IIngredientRenderer<FluidStack> {
private final int amount;
/**
* Creates a new renderer instance
* @param amount Amount to render
*/
private Fluids(int amount) {
this.amount = amount;
}
@Override
public void render(MatrixStack matrices, int x, int y, @Nullable FluidStack fluid) {
if (fluid == null || fluid.isEmpty()) {
return;
}
FluidAttributes attributes = fluid.getFluid().getAttributes();
CauldronRenderer.render(matrices, x, y, CAULDRON_SIZE, CAULDRON_SIZE, amount, attributes.getStillTexture(fluid), attributes.getColor(fluid));
}
@Override
public List<ITextComponent> getTooltip(FluidStack fluidStack, ITooltipFlag iTooltipFlag) {
List<ITextComponent> list = new ArrayList<>();
if (amount == 0) {
list.add(getAmountText(0));
} else {
list.add(fluidStack.getDisplayName());
list.add(getAmountText(amount));
}
return list;
}
}
}