Skip to content

Commit bda0caa

Browse files
committed
part 2
1 parent 1386ff7 commit bda0caa

16 files changed

Lines changed: 707 additions & 624 deletions
Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
package dsns.betterhud;
22

3+
import dsns.betterhud.mods.*;
4+
import dsns.betterhud.util.BaseMod;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
37
import net.fabricmc.api.ClientModInitializer;
48
import net.fabricmc.api.EnvType;
59
import net.fabricmc.api.Environment;
610
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
711
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
812
import net.minecraft.util.Identifier;
9-
1013
import org.slf4j.Logger;
1114
import org.slf4j.LoggerFactory;
1215

1316
@Environment(EnvType.CLIENT)
1417
public class BetterHUD implements ClientModInitializer {
15-
// This logger is used to write text to the console and the log file.
16-
// It is considered best practice to use your mod id as the logger's name.
17-
// That way, it's clear which mod wrote info, warnings, and errors.
18-
public static final Logger LOGGER = LoggerFactory.getLogger("betterhud");
1918

20-
@Override
21-
public void onInitializeClient() {
22-
Config.configure();
19+
// This logger is used to write text to the console and the log file.
20+
// It is considered best practice to use your mod id as the logger's name.
21+
// That way, it's clear which mod wrote info, warnings, and errors.
22+
public static final Logger LOGGER = LoggerFactory.getLogger("betterhud");
23+
24+
ArrayList<BaseMod> mods = new ArrayList<>(
25+
Arrays.asList(
26+
new FPS(),
27+
new Ping(),
28+
new Momentum(),
29+
new Coordinates(),
30+
new Biome(),
31+
new Facing(),
32+
new Time()
33+
)
34+
);
35+
36+
@Override
37+
public void onInitializeClient() {
38+
Config.configure(mods);
2339

24-
BetterHUDGUI betterHUDGUI = new BetterHUDGUI();
25-
HudElementRegistry.addLast(Identifier.of("betterhud", "hud"), betterHUDGUI::onHudRender);
26-
ClientTickEvents.START_CLIENT_TICK.register(betterHUDGUI);
27-
}
28-
}
40+
BetterHUDGUI betterHUDGUI = new BetterHUDGUI();
41+
HudElementRegistry.addLast(
42+
Identifier.of("betterhud", "hud"),
43+
betterHUDGUI::onHudRender
44+
);
45+
ClientTickEvents.START_CLIENT_TICK.register(betterHUDGUI);
46+
}
47+
}
Lines changed: 168 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,179 @@
11
package dsns.betterhud;
22

3+
import dsns.betterhud.util.BaseMod;
4+
import dsns.betterhud.util.CustomText;
5+
import dsns.betterhud.util.ModSettings;
6+
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
37
import java.util.ArrayList;
48
import java.util.List;
5-
6-
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
79
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
810
import net.minecraft.client.MinecraftClient;
911
import net.minecraft.client.gui.DrawContext;
1012
import net.minecraft.client.render.RenderTickCounter;
11-
import dsns.betterhud.mods.*;
12-
import dsns.betterhud.util.BaseMod;
13-
import dsns.betterhud.util.CustomText;
1413

1514
public class BetterHUDGUI implements ClientTickEvents.StartTick {
16-
private final MinecraftClient client = MinecraftClient.getInstance();
17-
private final List<CustomText> topLeftText = new ObjectArrayList<>();
18-
private final List<CustomText> topRightText = new ObjectArrayList<>();
19-
private final List<CustomText> bottomLeftList = new ObjectArrayList<>();
20-
private final List<CustomText> bottomRightText = new ObjectArrayList<>();
21-
private final List<CustomText> customPositionText = new ObjectArrayList<>();
22-
23-
@Override
24-
public void onStartTick(MinecraftClient client) {
25-
this.topLeftText.clear();
26-
this.topRightText.clear();
27-
this.bottomLeftList.clear();
28-
this.bottomRightText.clear();
29-
this.customPositionText.clear();
30-
31-
ArrayList<BaseMod> mods = new ArrayList<>();
32-
mods.add(new FPS());
33-
mods.add(new Ping());
34-
mods.add(new Momentum());
35-
mods.add(new Coordinates());
36-
mods.add(new Biome());
37-
mods.add(new Facing());
38-
mods.add(new Time());
39-
40-
for (BaseMod mod : mods) {
41-
ModSettings modSettings = Config.settings.get(mod.getModID());
42-
if (!modSettings.enabled)
43-
continue;
44-
45-
CustomText modText = mod.onStartTick(client);
46-
if (modText == null)
47-
continue;
48-
49-
if (modSettings.customPosition) {
50-
modText.customPosition = true;
51-
modText.customX = modSettings.customX;
52-
modText.customY = modSettings.customY;
53-
this.customPositionText.add(modText);
54-
} else if (modSettings.orientation.equals("top-left")) {
55-
this.topLeftText.add(modText);
56-
} else if (modSettings.orientation.equals("top-right")) {
57-
this.topRightText.add(modText);
58-
} else if (modSettings.orientation.equals("bottom-left")) {
59-
this.bottomLeftList.add(modText);
60-
} else if (modSettings.orientation.equals("bottom-right")) {
61-
this.bottomRightText.add(modText);
62-
}
63-
}
64-
}
65-
66-
public void onHudRender(DrawContext drawContext, RenderTickCounter tickCounter) {
67-
if (client.getDebugHud().shouldShowDebugHud())
68-
return;
69-
if (client.options.hudHidden)
70-
return;
71-
72-
int x = Config.horizontalMargin;
73-
int y = Config.verticalMargin;
74-
75-
for (CustomText text : topLeftText) {
76-
drawString(drawContext, text, x, y);
77-
78-
y += (client.textRenderer.fontHeight - 1) + (Config.verticalPadding * 2) + Config.lineHeight;
79-
}
80-
81-
y = client.getWindow().getScaledHeight() - Config.verticalMargin;
82-
83-
for (CustomText text : bottomLeftList) {
84-
y -= (client.textRenderer.fontHeight - 1) + (Config.verticalPadding * 2);
85-
drawString(drawContext, text, x, y);
86-
y -= Config.lineHeight;
87-
}
88-
89-
y = Config.verticalMargin;
90-
for (CustomText text : topRightText) {
91-
int offset = (client.textRenderer.getWidth(text.text) - 1) + (Config.horizontalPadding * 2)
92-
+ Config.horizontalMargin;
93-
x = client.getWindow().getScaledWidth() - offset;
94-
drawString(drawContext, text, x, y);
95-
96-
y += (client.textRenderer.fontHeight - 1) + (Config.verticalPadding * 2) + Config.lineHeight;
97-
}
98-
99-
y = client.getWindow().getScaledHeight() - Config.verticalMargin;
100-
for (CustomText text : bottomRightText) {
101-
int offset = (client.textRenderer.getWidth(text.text) - 1) + (Config.horizontalPadding * 2)
102-
+ Config.horizontalMargin;
103-
x = client.getWindow().getScaledWidth() - offset;
104-
105-
y -= (client.textRenderer.fontHeight - 1) + (Config.verticalPadding * 2);
106-
107-
drawString(drawContext, text, x, y);
108-
109-
y -= Config.lineHeight;
110-
}
111-
112-
for (CustomText text : customPositionText) {
113-
float xPercent = text.customX / 100.0f;
114-
float yPercent = text.customY / 100.0f;
115-
116-
int maxX = client.getWindow().getScaledWidth() - (Config.horizontalPadding * 2)
117-
- (client.textRenderer.getWidth(text.text) - 1);
118-
int maxY = client.getWindow().getScaledHeight() - (Config.verticalPadding * 2)
119-
- (client.textRenderer.fontHeight - 1);
120-
121-
int scaledX = (int) (xPercent * maxX);
122-
int scaledY = (int) (yPercent * maxY);
123-
124-
drawString(drawContext, text, scaledX, scaledY);
125-
}
126-
}
127-
128-
private void drawString(DrawContext drawContext, CustomText text, int x, int y) {
129-
drawContext.fill(x, y,
130-
x + (client.textRenderer.getWidth(text.text) - 1) + (Config.horizontalPadding * 2),
131-
y + (client.textRenderer.fontHeight - 1) + (Config.verticalPadding * 2), text.backgroundColor);
132-
133-
drawContext.drawText(client.textRenderer, text.text, x + Config.horizontalPadding, y + Config.verticalPadding,
134-
text.color, true);
135-
}
15+
16+
public static int verticalPadding = 4;
17+
public static int horizontalPadding = 4;
18+
19+
public static int verticalMargin = 1;
20+
public static int horizontalMargin = 1;
21+
22+
public static int lineHeight = 1;
23+
24+
private final MinecraftClient client = MinecraftClient.getInstance();
25+
private final List<CustomText> topLeftText = new ObjectArrayList<>();
26+
private final List<CustomText> topRightText = new ObjectArrayList<>();
27+
private final List<CustomText> bottomLeftList = new ObjectArrayList<>();
28+
private final List<CustomText> bottomRightText = new ObjectArrayList<>();
29+
private final List<CustomText> customPositionText = new ObjectArrayList<>();
30+
31+
private ArrayList<BaseMod> mods = new ArrayList<>();
32+
33+
public void instantiateMods(ArrayList<BaseMod> mods) {
34+
this.mods = mods;
35+
}
36+
37+
@Override
38+
public void onStartTick(MinecraftClient client) {
39+
this.topLeftText.clear();
40+
this.topRightText.clear();
41+
this.bottomLeftList.clear();
42+
this.bottomRightText.clear();
43+
this.customPositionText.clear();
44+
45+
for (BaseMod mod : mods) {
46+
ModSettings modSettings = mod.getModSettings();
47+
if (!modSettings.getSetting("enabled").getBooleanValue()) continue;
48+
49+
CustomText modText = mod.onStartTick(client);
50+
if (modText == null) continue;
51+
52+
String orientation = modSettings
53+
.getSetting("orientation")
54+
.getStringValue();
55+
56+
if (modSettings.getSetting("customPosition").getBooleanValue()) {
57+
modText.customPosition = true;
58+
modText.customX = modSettings
59+
.getSetting("customX")
60+
.getIntValue();
61+
modText.customY = modSettings
62+
.getSetting("customY")
63+
.getIntValue();
64+
this.customPositionText.add(modText);
65+
} else if (orientation.equals("top-left")) {
66+
this.topLeftText.add(modText);
67+
} else if (orientation.equals("top-right")) {
68+
this.topRightText.add(modText);
69+
} else if (orientation.equals("bottom-left")) {
70+
this.bottomLeftList.add(modText);
71+
} else if (orientation.equals("bottom-right")) {
72+
this.bottomRightText.add(modText);
73+
}
74+
}
75+
}
76+
77+
public void onHudRender(
78+
DrawContext drawContext,
79+
RenderTickCounter tickCounter
80+
) {
81+
if (client.getDebugHud().shouldShowDebugHud()) return;
82+
if (client.options.hudHidden) return;
83+
84+
int x = horizontalMargin;
85+
int y = verticalMargin;
86+
87+
for (CustomText text : topLeftText) {
88+
drawString(drawContext, text, x, y);
89+
90+
y +=
91+
(client.textRenderer.fontHeight - 1) +
92+
(verticalPadding * 2) +
93+
lineHeight;
94+
}
95+
96+
y = client.getWindow().getScaledHeight() - verticalMargin;
97+
98+
for (CustomText text : bottomLeftList) {
99+
y -= (client.textRenderer.fontHeight - 1) + (verticalPadding * 2);
100+
drawString(drawContext, text, x, y);
101+
y -= lineHeight;
102+
}
103+
104+
y = verticalMargin;
105+
for (CustomText text : topRightText) {
106+
int offset =
107+
(client.textRenderer.getWidth(text.text) - 1) +
108+
(horizontalPadding * 2) +
109+
horizontalMargin;
110+
x = client.getWindow().getScaledWidth() - offset;
111+
drawString(drawContext, text, x, y);
112+
113+
y +=
114+
(client.textRenderer.fontHeight - 1) +
115+
(verticalPadding * 2) +
116+
lineHeight;
117+
}
118+
119+
y = client.getWindow().getScaledHeight() - verticalMargin;
120+
for (CustomText text : bottomRightText) {
121+
int offset =
122+
(client.textRenderer.getWidth(text.text) - 1) +
123+
(horizontalPadding * 2) +
124+
horizontalMargin;
125+
x = client.getWindow().getScaledWidth() - offset;
126+
127+
y -= (client.textRenderer.fontHeight - 1) + (verticalPadding * 2);
128+
129+
drawString(drawContext, text, x, y);
130+
131+
y -= lineHeight;
132+
}
133+
134+
for (CustomText text : customPositionText) {
135+
float xPercent = text.customX / 100.0f;
136+
float yPercent = text.customY / 100.0f;
137+
138+
int maxX =
139+
client.getWindow().getScaledWidth() -
140+
(horizontalPadding * 2) -
141+
(client.textRenderer.getWidth(text.text) - 1);
142+
int maxY =
143+
client.getWindow().getScaledHeight() -
144+
(verticalPadding * 2) -
145+
(client.textRenderer.fontHeight - 1);
146+
147+
int scaledX = (int) (xPercent * maxX);
148+
int scaledY = (int) (yPercent * maxY);
149+
150+
drawString(drawContext, text, scaledX, scaledY);
151+
}
152+
}
153+
154+
private void drawString(
155+
DrawContext drawContext,
156+
CustomText text,
157+
int x,
158+
int y
159+
) {
160+
drawContext.fill(
161+
x,
162+
y,
163+
x +
164+
(client.textRenderer.getWidth(text.text) - 1) +
165+
(horizontalPadding * 2),
166+
y + (client.textRenderer.fontHeight - 1) + (verticalPadding * 2),
167+
text.backgroundColor
168+
);
169+
170+
drawContext.drawText(
171+
client.textRenderer,
172+
text.text,
173+
x + horizontalPadding,
174+
y + verticalPadding,
175+
text.color,
176+
true
177+
);
178+
}
136179
}

0 commit comments

Comments
 (0)