Skip to content

Commit fc21963

Browse files
committed
Fix #402: Combat Armor Decreases Frame Rate When Equiped
Re: #402
1 parent eb890f5 commit fc21963

File tree

3 files changed

+261
-181
lines changed

3 files changed

+261
-181
lines changed

src/main/java/electrodynamics/client/event/guipost/HandlerArmorData.java

Lines changed: 146 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -30,176 +30,198 @@
3030

3131
public class HandlerArmorData extends AbstractPostGuiOverlayHandler {
3232

33-
private final Component statusGogglesOn = ElectroTextUtils.tooltip("nightvisiongoggles.status").withStyle(ChatFormatting.GRAY).append(ElectroTextUtils.tooltip("nightvisiongoggles.on").withStyle(ChatFormatting.GREEN));
34-
private final Component statusGogglesOff = ElectroTextUtils.tooltip("nightvisiongoggles.status").withStyle(ChatFormatting.GRAY).append(ElectroTextUtils.tooltip("nightvisiongoggles.off").withStyle(ChatFormatting.RED));
33+
private static final float SMALL_SCALE = 0.8F;
34+
private static final int ICON_X = 10;
35+
private static final int TEXT_X = 35;
36+
37+
private final Component statusGogglesOn = ElectroTextUtils.tooltip("nightvisiongoggles.status")
38+
.withStyle(ChatFormatting.GRAY)
39+
.append(ElectroTextUtils.tooltip("nightvisiongoggles.on").withStyle(ChatFormatting.GREEN));
40+
private final Component statusGogglesOff = ElectroTextUtils.tooltip("nightvisiongoggles.status")
41+
.withStyle(ChatFormatting.GRAY)
42+
.append(ElectroTextUtils.tooltip("nightvisiongoggles.off").withStyle(ChatFormatting.RED));
43+
44+
private final CachedComponent<Integer> jetpackModeCached = new CachedComponent<>(ItemJetpack::getModeText);
45+
private final CachedComponent<Integer> servoModeCached = new CachedComponent<>(ItemServoLeggings::getModeText);
46+
private final CachedComponent<Integer> ceramicPlateCountCached = new CachedComponent<>(plates -> ElectroTextUtils
47+
.tooltip("ceramicplatecount", Component.literal(Integer.toString(plates))).withStyle(ChatFormatting.AQUA));
48+
49+
private final CachedComponent<Double> joulesStorageCached = new CachedComponent<>(
50+
joules -> ChatFormatter.getChatDisplayShort(joules, DisplayUnits.JOULES));
3551

3652
@Override
3753
public void renderToScreen(GuiGraphics graphics, DeltaTracker tracker, Minecraft minecraft) {
3854

39-
if (!ElectrodynamicsConfig.INSTANCE.RENDER_COMBAT_ARMOR_STATUS.get()) {
40-
return;
41-
}
55+
if (!ElectrodynamicsConfig.INSTANCE.RENDER_COMBAT_ARMOR_STATUS.get() || minecraft.player == null) {
56+
return;
57+
}
4258

43-
List<ItemStack> armor = minecraft.player.getInventory().armor;
59+
List<ItemStack> armor = minecraft.player.getInventory().armor;
4460

45-
graphics.pose().pushPose();
61+
graphics.pose().pushPose();
4662

47-
int heightOffset = graphics.guiHeight();
63+
int heightOffset = graphics.guiHeight();
4864

49-
if (!armor.get(0).isEmpty() && handleBoots(armor.get(0), graphics, minecraft, heightOffset)) {
50-
heightOffset -= 30;
51-
}
65+
if (!armor.get(0).isEmpty() && handleBoots(armor.get(0), graphics, minecraft, heightOffset)) {
66+
heightOffset -= 30;
67+
}
5268

53-
if (!armor.get(1).isEmpty() && handleLeggings(armor.get(1), graphics, minecraft, heightOffset)) {
54-
heightOffset -= 30;
55-
}
69+
if (!armor.get(1).isEmpty() && handleLeggings(armor.get(1), graphics, minecraft, heightOffset)) {
70+
heightOffset -= 30;
71+
}
5672

57-
if (!armor.get(2).isEmpty() && handleChestplate(armor.get(2), graphics, minecraft, heightOffset)) {
58-
heightOffset -= 30;
59-
}
73+
if (!armor.get(2).isEmpty() && handleChestplate(armor.get(2), graphics, minecraft, heightOffset)) {
74+
heightOffset -= 30;
75+
}
6076

61-
if (!armor.get(3).isEmpty() && handleHelmet(armor.get(3), graphics, minecraft, heightOffset)) {
62-
heightOffset -= 30;
63-
}
77+
if (!armor.get(3).isEmpty() && handleHelmet(armor.get(3), graphics, minecraft, heightOffset)) {
78+
heightOffset -= 30;
79+
}
6480

65-
graphics.pose().popPose();
81+
graphics.pose().popPose();
6682

6783
}
6884

6985
private boolean handleHelmet(ItemStack helmet, GuiGraphics graphics, Minecraft minecraft, int heightOffset) {
7086

71-
boolean renderItem = false;
87+
if (!ItemUtils.testItems(helmet.getItem(), ElectrodynamicsItems.ITEM_NIGHTVISIONGOGGLES.get(),
88+
ElectrodynamicsItems.ITEM_COMBATHELMET.get())) {
89+
return false;
90+
}
7291

73-
if (ItemUtils.testItems(helmet.getItem(), ElectrodynamicsItems.ITEM_NIGHTVISIONGOGGLES.get(), ElectrodynamicsItems.ITEM_COMBATHELMET.get())) {
74-
renderItem = true;
75-
Component mode;
76-
if (helmet.getOrDefault(VoltaicDataComponentTypes.ON, false)) {
77-
mode = statusGogglesOn;
78-
} else {
79-
mode = statusGogglesOff;
80-
}
81-
graphics.drawString(minecraft.font, mode, 35, heightOffset - 30, Color.BLACK.color());
82-
graphics.drawString(minecraft.font, ChatFormatter.getChatDisplayShort(((IItemElectric)helmet.getItem()).getJoulesStored(helmet), DisplayUnits.JOULES), 35, heightOffset - 20, Color.WHITE.color(), false);
83-
}
92+
Component status = helmet.getOrDefault(VoltaicDataComponentTypes.ON, false) ? statusGogglesOn
93+
: statusGogglesOff;
94+
double joules = ((IItemElectric) helmet.getItem()).getJoulesStored(helmet);
8495

85-
if (renderItem) {
86-
RenderingUtils.renderItemScaled(graphics, helmet.getItem(), 10, heightOffset - 30, 1.5F);
87-
}
96+
graphics.drawString(minecraft.font, status, TEXT_X, heightOffset - 30, Color.BLACK.color());
97+
graphics.drawString(minecraft.font, joulesStorageCached.get(joules), TEXT_X, heightOffset - 20,
98+
Color.WHITE.color(), false);
8899

89-
return renderItem;
100+
RenderingUtils.renderItemScaled(graphics, helmet.getItem(), ICON_X, heightOffset - 30, 1.5F);
101+
return true;
90102

91103
}
92104

93-
private static boolean handleChestplate(ItemStack chestplate, GuiGraphics graphics, Minecraft minecraft, int heightOffset) {
105+
private boolean handleChestplate(ItemStack chestplate, GuiGraphics graphics, Minecraft minecraft,
106+
int heightOffset) {
107+
108+
// Jetpack
109+
if (ItemUtils.testItems(chestplate.getItem(), ElectrodynamicsItems.ITEM_JETPACK.get())) {
110+
111+
Component mode = jetpackModeCached.get(chestplate.getOrDefault(VoltaicDataComponentTypes.MODE, 0));
112+
IGasHandlerItem handler = chestplate.getCapability(VoltaicCapabilities.CAPABILITY_GASHANDLER_ITEM);
113+
114+
if (handler != null) {
115+
GasStack gas = handler.getGasInTank(0);
116+
Component gasText = gas.isEmpty()
117+
? VoltaicTextUtils.ratio(Component.literal("0"),
118+
ChatFormatter.formatFluidMilibuckets(ItemJetpack.MAX_CAPACITY))
119+
: VoltaicTextUtils.ratio(ChatFormatter.formatFluidMilibuckets(gas.getAmount()),
120+
ChatFormatter.formatFluidMilibuckets(ItemJetpack.MAX_CAPACITY));
94121

95-
boolean renderItem = false;
122+
graphics.drawString(minecraft.font, mode, TEXT_X, heightOffset - 30, 0);
123+
graphics.drawString(minecraft.font, gasText, TEXT_X, heightOffset - 20, Color.WHITE.color(), false);
124+
}
96125

97-
if (ItemUtils.testItems(chestplate.getItem(), ElectrodynamicsItems.ITEM_JETPACK.get())) {
98-
renderItem = true;
99-
Component mode = ItemJetpack.getModeText(chestplate.getOrDefault(VoltaicDataComponentTypes.MODE, 0));
126+
RenderingUtils.renderItemScaled(graphics, chestplate.getItem(), ICON_X, heightOffset - 30, 1.5F);
127+
return true;
100128

101-
IGasHandlerItem handler = chestplate.getCapability(VoltaicCapabilities.CAPABILITY_GASHANDLER_ITEM);
129+
}
102130

103-
graphics.pose().pushPose();
131+
// Combat chestplate (scaled)
132+
if (ItemUtils.testItems(chestplate.getItem(), ElectrodynamicsItems.ITEM_COMBATCHESTPLATE.get())) {
104133

105-
if (handler != null) {
106-
GasStack gas = handler.getGasInTank(0);
107-
if (gas.isEmpty()) {
108-
graphics.drawString(minecraft.font, mode, 35, heightOffset - 30, 0);
109-
graphics.drawString(minecraft.font, VoltaicTextUtils.ratio(Component.literal("0"), ChatFormatter.formatFluidMilibuckets(ItemJetpack.MAX_CAPACITY)), 35, heightOffset - 20, Color.WHITE.color());
110-
} else {
111-
graphics.drawString(minecraft.font, mode, 35, heightOffset - 30, 0);
112-
graphics.drawString(minecraft.font, VoltaicTextUtils.ratio(ChatFormatter.formatFluidMilibuckets(gas.getAmount()), ChatFormatter.formatFluidMilibuckets(ItemJetpack.MAX_CAPACITY)), 35, heightOffset - 20, Color.WHITE.color(), false);
113-
}
114-
}
134+
graphics.pose().pushPose();
135+
graphics.pose().scale(SMALL_SCALE, SMALL_SCALE, SMALL_SCALE);
115136

116-
graphics.pose().popPose();
117-
}
137+
int scaledX = (int) (TEXT_X / SMALL_SCALE);
138+
int scaledY0 = (int) ((heightOffset - 34) / SMALL_SCALE);
139+
int scaledY1 = (int) ((heightOffset - 25) / SMALL_SCALE);
140+
int scaledY2 = (int) ((heightOffset - 16) / SMALL_SCALE);
118141

119-
if (ItemUtils.testItems(chestplate.getItem(), ElectrodynamicsItems.ITEM_COMBATCHESTPLATE.get())) {
142+
Component mode = jetpackModeCached.get(chestplate.getOrDefault(VoltaicDataComponentTypes.MODE, 0));
143+
IGasHandlerItem handler = chestplate.getCapability(VoltaicCapabilities.CAPABILITY_GASHANDLER_ITEM);
120144

121-
graphics.pose().pushPose();
145+
if (handler != null) {
146+
GasStack gas = handler.getGasInTank(0);
147+
Component gasText = gas.isEmpty()
148+
? VoltaicTextUtils.ratio(Component.literal("0"),
149+
ChatFormatter.formatFluidMilibuckets(ItemJetpack.MAX_CAPACITY))
150+
: VoltaicTextUtils.ratio(ChatFormatter.formatFluidMilibuckets(gas.getAmount()),
151+
ChatFormatter.formatFluidMilibuckets(ItemJetpack.MAX_CAPACITY));
122152

123-
graphics.pose().scale(0.8F, 0.8F, 0.8F);
124-
renderItem = true;
125-
Component mode = ItemJetpack.getModeText(chestplate.getOrDefault(VoltaicDataComponentTypes.MODE, 0));
153+
int plates = chestplate.getOrDefault(VoltaicDataComponentTypes.PLATES, 0);
126154

127-
IGasHandlerItem handler = chestplate.getCapability(VoltaicCapabilities.CAPABILITY_GASHANDLER_ITEM);
155+
graphics.drawString(minecraft.font, mode, scaledX, scaledY0, 0);
156+
graphics.drawString(minecraft.font, gasText, scaledX, scaledY1, -1, false);
157+
graphics.drawString(minecraft.font, ceramicPlateCountCached.get(plates), scaledX, scaledY2,
158+
Color.WHITE.color());
159+
}
128160

129-
if (handler != null) {
130-
GasStack gas = handler.getGasInTank(0);
131-
int x = (int) (35 / 0.8F);
132-
if (gas.isEmpty()) {
133-
graphics.drawString(minecraft.font, mode, x, (int) ((heightOffset - 34) / 0.8F), 0);
134-
graphics.drawString(minecraft.font, VoltaicTextUtils.ratio(Component.literal("0"), ChatFormatter.formatFluidMilibuckets(ItemJetpack.MAX_CAPACITY)), x, (int) ((heightOffset - 25) / 0.8F), -1);
135-
graphics.drawString(minecraft.font, ElectroTextUtils.tooltip("ceramicplatecount", Component.literal(chestplate.getOrDefault(VoltaicDataComponentTypes.PLATES, 0) + "")).withStyle(ChatFormatting.AQUA), x, (int) ((heightOffset - 16) / 0.8F), Color.WHITE.color());
136-
} else {
137-
graphics.drawString(minecraft.font, mode, x, (int) ((heightOffset - 34) / 0.8F), 0);
138-
graphics.drawString(minecraft.font, VoltaicTextUtils.ratio(ChatFormatter.formatFluidMilibuckets(gas.getAmount()), ChatFormatter.formatFluidMilibuckets(ItemJetpack.MAX_CAPACITY)), x, (int) ((heightOffset - 25) / 0.8F), -1);
139-
graphics.drawString(minecraft.font, ElectroTextUtils.tooltip("ceramicplatecount", Component.literal(chestplate.getOrDefault(VoltaicDataComponentTypes.PLATES, 0) + "")).withStyle(ChatFormatting.AQUA), x, (int) ((heightOffset - 16) / 0.8F), Color.WHITE.color());
140-
}
141-
}
161+
graphics.pose().popPose();
162+
RenderingUtils.renderItemScaled(graphics, chestplate.getItem(), ICON_X, heightOffset - 30, 1.5F);
163+
return true;
142164

143-
graphics.pose().popPose();
144-
}
165+
}
145166

146-
if (ItemUtils.testItems(chestplate.getItem(), ElectrodynamicsItems.ITEM_COMPOSITECHESTPLATE.get())) {
147-
renderItem = true;
148-
graphics.drawString(minecraft.font, ElectroTextUtils.tooltip("ceramicplatecount", Component.literal(chestplate.getOrDefault(VoltaicDataComponentTypes.PLATES, 0) + "")).withStyle(ChatFormatting.AQUA), 35, heightOffset - 25, Color.WHITE.color(), false);
149-
}
167+
// Composite chestplate
168+
if (ItemUtils.testItems(chestplate.getItem(), ElectrodynamicsItems.ITEM_COMPOSITECHESTPLATE.get())) {
169+
int plates = chestplate.getOrDefault(VoltaicDataComponentTypes.PLATES, 0);
170+
graphics.drawString(minecraft.font, ceramicPlateCountCached.get(plates), TEXT_X, heightOffset - 25,
171+
Color.WHITE.color(), false);
172+
RenderingUtils.renderItemScaled(graphics, chestplate.getItem(), ICON_X, heightOffset - 30, 1.5F);
173+
return true;
174+
}
150175

151-
if (renderItem) {
152-
RenderingUtils.renderItemScaled(graphics, chestplate.getItem(), 10, heightOffset - 30, 1.5F);
153-
}
176+
return false;
154177

155-
return renderItem;
156178
}
157179

158-
private static boolean handleLeggings(ItemStack leggings, GuiGraphics graphics, Minecraft minecraft, int heightOffset) {
159-
160-
boolean renderItem = false;
161-
162-
if (ItemUtils.testItems(leggings.getItem(), ElectrodynamicsItems.ITEM_SERVOLEGGINGS.get(), ElectrodynamicsItems.ITEM_COMBATLEGGINGS.get())) {
163-
renderItem = true;
164-
Component on;
165-
if (leggings.getOrDefault(VoltaicDataComponentTypes.ON, false)) {
166-
on = ElectroTextUtils.tooltip("nightvisiongoggles.status").withStyle(ChatFormatting.GRAY).append(ElectroTextUtils.tooltip("nightvisiongoggles.on").withStyle(ChatFormatting.GREEN));
167-
} else {
168-
on = ElectroTextUtils.tooltip("nightvisiongoggles.status").withStyle(ChatFormatting.GRAY).append(ElectroTextUtils.tooltip("nightvisiongoggles.off").withStyle(ChatFormatting.RED));
169-
}
170-
int x = (int) (35 / 0.8F);
171-
graphics.pose().pushPose();
172-
graphics.pose().scale(0.8F, 0.8F, 0.8F);
173-
graphics.drawString(minecraft.font, on, x, (int) ((heightOffset - 34) / 0.8F), 0);
174-
graphics.drawString(minecraft.font, ItemServoLeggings.getModeText(leggings.getOrDefault(VoltaicDataComponentTypes.MODE, -1)), x, (int) ((heightOffset - 25) / 0.8F), Color.BLACK.color(), false);
175-
graphics.drawString(minecraft.font, ChatFormatter.getChatDisplayShort(((IItemElectric)leggings.getItem()).getJoulesStored(leggings), DisplayUnits.JOULES), x, (int) ((heightOffset - 16) / 0.8F), Color.WHITE.color(), false);
176-
graphics.pose().popPose();
177-
}
178-
179-
if (renderItem) {
180-
RenderingUtils.renderItemScaled(graphics, leggings.getItem(), 10, heightOffset - 30, 1.5F);
181-
}
182-
183-
return renderItem;
180+
private boolean handleLeggings(ItemStack leggings, GuiGraphics graphics, Minecraft minecraft, int heightOffset) {
181+
182+
if (!ItemUtils.testItems(leggings.getItem(), ElectrodynamicsItems.ITEM_SERVOLEGGINGS.get(),
183+
ElectrodynamicsItems.ITEM_COMBATLEGGINGS.get())) {
184+
return false;
185+
}
186+
187+
Component status = leggings.getOrDefault(VoltaicDataComponentTypes.ON, false) ? statusGogglesOn
188+
: statusGogglesOff;
189+
Component mode = servoModeCached.get(leggings.getOrDefault(VoltaicDataComponentTypes.MODE, -1));
190+
double joules = ((IItemElectric) leggings.getItem()).getJoulesStored(leggings);
191+
192+
int x = (int) (TEXT_X / SMALL_SCALE);
193+
194+
graphics.pose().pushPose();
195+
graphics.pose().scale(SMALL_SCALE, SMALL_SCALE, SMALL_SCALE);
196+
graphics.drawString(minecraft.font, status, x, (int) ((heightOffset - 34) / SMALL_SCALE), 0);
197+
graphics.drawString(minecraft.font, mode, x, (int) ((heightOffset - 25) / SMALL_SCALE), Color.BLACK.color(),
198+
false);
199+
graphics.drawString(minecraft.font, joulesStorageCached.get(joules), x,
200+
(int) ((heightOffset - 16) / SMALL_SCALE), Color.WHITE.color(), false);
201+
graphics.pose().popPose();
202+
203+
RenderingUtils.renderItemScaled(graphics, leggings.getItem(), ICON_X, heightOffset - 30, 1.5F);
204+
return true;
205+
184206
}
185207

186-
private static boolean handleBoots(ItemStack boots, GuiGraphics graphics, Minecraft minecraft, int heightOffset) {
208+
private boolean handleBoots(ItemStack boots, GuiGraphics graphics, Minecraft minecraft, int heightOffset) {
187209

188-
boolean renderItem = false;
210+
if (!ItemUtils.testItems(boots.getItem(), ElectrodynamicsItems.ITEM_HYDRAULICBOOTS.get(),
211+
ElectrodynamicsItems.ITEM_COMBATBOOTS.get())) {
212+
return false;
213+
}
189214

190-
if (ItemUtils.testItems(boots.getItem(), ElectrodynamicsItems.ITEM_HYDRAULICBOOTS.get(), ElectrodynamicsItems.ITEM_COMBATBOOTS.get())) {
191-
renderItem = true;
192-
IFluidHandlerItem handler = boots.getCapability(Capabilities.FluidHandler.ITEM);
193-
if (handler != null) {
194-
graphics.drawString(minecraft.font, ChatFormatter.formatFluidMilibuckets(handler.getFluidInTank(0).getAmount()), 35, heightOffset - 25, Color.WHITE.color());
195-
}
196-
}
215+
IFluidHandlerItem handler = boots.getCapability(Capabilities.FluidHandler.ITEM);
216+
if (handler != null) {
217+
graphics.drawString(minecraft.font,
218+
ChatFormatter.formatFluidMilibuckets(handler.getFluidInTank(0).getAmount()), TEXT_X,
219+
heightOffset - 25, Color.WHITE.color());
220+
}
197221

198-
if (renderItem) {
199-
RenderingUtils.renderItemScaled(graphics, boots.getItem(), 10, heightOffset - 30, 1.5F);
200-
}
222+
RenderingUtils.renderItemScaled(graphics, boots.getItem(), ICON_X, heightOffset - 30, 1.5F);
223+
return true;
201224

202-
return renderItem;
203225
}
204226

205227
}

0 commit comments

Comments
 (0)