/
GuiMelter.java
298 lines (257 loc) · 9.7 KB
/
GuiMelter.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package knightminer.tcomplement.melter.client;
import java.util.List;
import java.util.function.Consumer;
import com.google.common.collect.Lists;
import knightminer.tcomplement.library.Util;
import knightminer.tcomplement.melter.inventory.ContainerMelter;
import knightminer.tcomplement.melter.tileentity.TileMelter;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.inventory.Slot;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fluids.FluidStack;
import slimeknights.mantle.client.gui.GuiElement;
import slimeknights.mantle.client.gui.GuiElementScalable;
import slimeknights.tconstruct.library.client.GuiUtil;
import slimeknights.tconstruct.library.fluid.FluidTankAnimated;
import slimeknights.tconstruct.smeltery.client.GuiHeatingStructureFuelTank;
import slimeknights.tconstruct.smeltery.client.IGuiLiquidTank;
public class GuiMelter extends GuiHeatingStructureFuelTank implements IGuiLiquidTank {
public static final ResourceLocation BACKGROUND = Util.getResource("textures/gui/melter.png");
public static final ResourceLocation BACKGROUND_SOLID = Util.getResource("textures/gui/melter_solid.png");
protected GuiElement scala = new GuiElement(176, 0, 52, 52, 256, 256);
protected GuiElement progressBar = new GuiElementScalable(176, 150, 3, 16, 256, 256);
protected GuiElement unprogressBar = new GuiElementScalable(179, 150, 3, 16);
protected GuiElement uberHeatBar = new GuiElementScalable(182, 150, 3, 16);
protected GuiElement noMeltBar = new GuiElementScalable(185, 150, 3, 16);
protected GuiElement flame = new GuiElementScalable(176, 52, 14, 14);
public TileMelter melter;
private boolean solidFuel;
private ResourceLocation background;
public GuiMelter(ContainerMelter container, TileMelter melter) {
super(container);
this.melter = melter;
this.background = BACKGROUND;
// for the heater, adjust a few things
this.solidFuel = melter.getSolidHeater() != null;
if(solidFuel) {
this.background = BACKGROUND_SOLID;
}
}
// this is the same for both structures, but the superclass does not have (nor need) access to the side inventory
@Override
public void updateScreen() {
super.updateScreen();
// if the solif fuel status changes
if((melter.getSolidHeater() != null) != solidFuel) {
// close screen
this.mc.player.closeScreen();
}
}
@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
// we don't need to add the corner since the mouse is already reletive to the corner
super.drawGuiContainerForegroundLayer(mouseX, mouseY);
// draw the scale
this.mc.getTextureManager().bindTexture(background);
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
scala.draw(90, 16);
// draw the tooltips, if any
// subtract the corner of the main module so the mouse location is relative to just the center, rather than the side inventory
mouseX -= cornerX;
mouseY -= cornerY;
// slot status display
RenderHelper.disableStandardItemLighting();
String tooltipText = null;
// draw the "heat" bars for each slot
if(this.inventorySlots instanceof ContainerMelter) {
// skip the fourth slot if it exists
for(Slot slot : ((ContainerMelter)this.inventorySlots).getInventorySlots()) {
if(slot.getHasStack()) {
float progress = melter.getHeatingProgress(slot.getSlotIndex());
String tooltip = null;
GuiElement bar = progressBar;
if(Float.isNaN(progress)) {
progress = 1f;
bar = noMeltBar;
tooltip = "gui.smeltery.progress.no_recipe";
}
else if(progress < 0) {
bar = unprogressBar;
progress = 1f;
tooltip = "gui.smeltery.progress.no_heat";
}
else if((progress > 1f && progress < 2f) || progress == Float.POSITIVE_INFINITY) {
progress = 1f;
}
else if(progress >= 2f && progress < 3f) {
bar = uberHeatBar;
progress = 1f;
tooltip = "gui.tcomplement.melter.progress.no_space";
}
else if(progress >= 3f) {
bar = uberHeatBar;
progress = 1f;
tooltip = "gui.tcomplement.melter.progress.wrong_fluid";
}
int height = 1 + Math.round(progress * (bar.h - 1));
int x = slot.xPos - 4;
int y = slot.yPos + bar.h - height;
if(tooltip != null &&
x <= mouseX && x + bar.w > mouseX &&
y <= mouseY && y + bar.h > mouseY) {
tooltipText = tooltip;
}
GuiScreen.drawModalRectWithCustomSizedTexture(x, y, bar.x, bar.y + bar.h - height, bar.w, height, bar.texW, bar.texH);
}
}
}
if(tooltipText != null) {
drawHoveringText(this.fontRenderer.listFormattedStringToWidth(Util.translate(tooltipText), 100), mouseX, mouseY);
}
drawTankTooltip(mouseX, mouseY);
// adjust coords based on fuel type
int xMin = 153;
int xMax = solidFuel ? 167 : 165;
int yMin = solidFuel ? 36 : 16;
int yMax = solidFuel ? 50 : 68;
// Fuel tank rendering
if(xMin <= mouseX && mouseX < xMax && yMin <= mouseY && mouseY < yMax) {
if(melter.currentFuel != null) {
drawFuelTooltip(mouseX, mouseY);
}
else {
drawSolidFuelTooltip(mouseX, mouseY);
}
}
}
@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
drawBackground(background);
super.drawGuiContainerBackgroundLayer(partialTicks, mouseX, mouseY);
// draw liquids
drawTank(90, 16, 52, 52);
// update fuel info
fuelInfo = melter.getFuelDisplay();
if(solidFuel) {
float fuel = melter.getFuelPercentage();
if(fuel > 0) {
this.mc.getTextureManager().bindTexture(background);
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
GuiElement flame = this.flame;
int height = 1 + Math.round(fuel * (flame.h - 1));
int x = 153 + cornerX;
int y = 36 + cornerY + flame.h - height;
GuiScreen.drawModalRectWithCustomSizedTexture(x, y, flame.x, flame.y + flame.h - height, flame.w, height, flame.texW, flame.texH);
}
} else {
drawFuel(153, 16, 12, 52);
}
}
protected FluidStack getHoveredFluidStack(int x, int y) {
FluidTankAnimated tank = melter.getTank();
FluidStack fluid = tank.getFluid();
if(fluid != null && fluid.amount > 0) {
int h = 52 - (52 * fluid.amount / tank.getCapacity());
// within fluid
if(x >= 90 && x < 142 && y >= (h + 16) && y < 68) {
return fluid;
}
}
return null;
}
// used by JEI, factor in the modules
@Override
public FluidStack getFluidStackAtPosition(int x, int y) {
return getHoveredFluidStack(x - cornerX, y - cornerY);
}
/**
* Draws the fluid at the specified location
*
* @param displayX Display X location, excluding cornerX
* @param displayY Display Y location, excluding cornerY
* @param width Width
* @param height Height of the whole area, note that displayed size may differ due to how full the fuel is
*/
protected void drawTank(int displayX, int displayY, int width, int height) {
FluidTankAnimated tank = melter.getTank();
FluidStack fluid = tank.getFluid();
if(fluid != null && fluid.amount > 0) {
int x = displayX + cornerX;
int y = displayY + cornerY + height;
int w = width;
int h = height * fluid.amount / tank.getCapacity();
GuiUtil.renderTiledFluid(x, y - h, w, h, this.zLevel, fluid);
}
}
/**
* Draws the fluid tooltip at the specified location
*
* @param x Tooltip X location
* @param y Tooltip Y location
* @param width Width
* @param height Height of the whole area, note that displayed size may differ due to how full the fuel is
*/
protected void drawTankTooltip(int x, int y) {
List<String> tooltip = getTankTooltip(x, y);
if(tooltip != null) {
this.drawHoveringText(tooltip, x, y);
}
}
/**
* Called by drawTankTooltip to generate the full tank tooltip
*
* @param x Tooltip X location
* @param y Tooltip Y location
* @return List of the strings for the tooltip
*/
protected List<String> getTankTooltip(int x, int y) {
if(x >= 90 && x < 142 && y >= 16 && y < 68) {
FluidTankAnimated tank = melter.getTank();
FluidStack hovered = getHoveredFluidStack(x, y);
List<String> text = Lists.newArrayList();
Consumer<Integer> stringFn = Util.isShiftKeyDown() ? (i) -> GuiUtil.amountToString(i, text) : (i) -> GuiUtil.amountToIngotString(i, text);
if(hovered == null) {
int usedCap = tank.getFluidAmount();
int maxCap = tank.getCapacity();
text.add(TextFormatting.WHITE + Util.translate("gui.smeltery.capacity"));
stringFn.accept(maxCap);
text.add(Util.translateFormatted("gui.smeltery.capacity_available"));
stringFn.accept(maxCap - usedCap);
text.add(Util.translateFormatted("gui.smeltery.capacity_used"));
stringFn.accept(usedCap);
if(!Util.isShiftKeyDown()) {
text.add("");
text.add(Util.translate("tooltip.tank.holdShift"));
}
}
else {
text.add(TextFormatting.WHITE + hovered.getLocalizedName());
GuiUtil.liquidToString(hovered, text);
}
return text;
}
return null;
}
/**
* Draws the tooltip for solid fuel
* @param mouseX X position of the mouse
* @param mouseY Y position of the mouse
*/
protected void drawSolidFuelTooltip(int mouseX, int mouseY) {
List<String> text = Lists.newArrayList();
text.add(TextFormatting.WHITE + Util.translate("gui.smeltery.fuel"));
if(melter.hasFuel()) {
text.add(Util.translate("gui.tcomplement.melter.solid_fuel"));
text.add(TextFormatting.GRAY + Util.translateFormatted("gui.tcomplement.melter.solid_fuel.amount", melter.getFuel() / 5));
// melter stores temperature in Kelvin, but we display in Celsius
text.add(Util.translateFormatted("gui.smeltery.fuel.heat", melter.getTemperature() + 300));
}
else {
text.add(Util.translate("gui.smeltery.fuel.empty"));
}
this.drawHoveringText(text, mouseX, mouseY);
}
}