aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/skyblock/dwarven/DwarvenHud.java
blob: 4446c1a7efbb7f22867f54f9567521053e136563 (plain)
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
package de.hysky.skyblocker.skyblock.dwarven;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.skyblock.tabhud.util.Colors;
import de.hysky.skyblocker.skyblock.tabhud.widget.hud.HudCommsWidget;
import de.hysky.skyblocker.skyblock.tabhud.widget.hud.HudPowderWidget;
import de.hysky.skyblocker.utils.Utils;
import de.hysky.skyblocker.utils.scheduler.Scheduler;
import it.unimi.dsi.fastutil.Pair;
import it.unimi.dsi.fastutil.ints.IntIntPair;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DwarvenHud {

    public static final MinecraftClient client = MinecraftClient.getInstance();
    public static List<Commission> commissionList = new ArrayList<>();

    public static String mithrilPowder = "0";
    public static String gemStonePowder = "0";

    public static final List<Pattern> COMMISSIONS = Stream.of(
                    "(?:Titanium|Mithril|Hard Stone) Miner",
                    "(?:Ice Walker|Golden Goblin|(?<!Golden )Goblin|Goblin Raid|Automaton|Sludge|Team Treasurite Member|Yog|Boss Corleone|Thyst) Slayer",
                    "(?:Lava Springs|Cliffside Veins|Rampart's Quarry|Upper Mines|Royal Mines) Mithril",
                    "(?:Lava Springs|Cliffside Veins|Rampart's Quarry|Upper Mines|Royal Mines) Titanium",
                    "Goblin Raid",
                    "(?:Powder Ghast|Star Sentry) Puncher",
                    "(?<!Lucky )Raffle",
                    "Lucky Raffle",
                    "2x Mithril Powder Collector",
                    "(?:Ruby|Amber|Sapphire|Jade|Amethyst|Topaz) Gemstone Collector",
                    "(?:Amber|Sapphire|Jade|Amethyst|Topaz) Crystal Hunter",
                    "Chest Looter").map(s -> Pattern.compile("^.*(" + s + "): (\\d+\\.?\\d*%|DONE)"))
            .collect(Collectors.toList());
    public static final Pattern MITHRIL_PATTERN = Pattern.compile("Mithril Powder: [0-9,]+");
    public static final Pattern GEMSTONE_PATTERN = Pattern.compile("Gemstone Powder: [0-9,]+");

    public static void init() {
        ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(ClientCommandManager.literal("skyblocker")
                .then(ClientCommandManager.literal("hud")
                        .then(ClientCommandManager.literal("dwarven")
                                .executes(Scheduler.queueOpenScreenCommand(DwarvenHudConfigScreen::new))))));

        HudRenderCallback.EVENT.register((context, tickDelta) -> {
            if ((!SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enabledCommissions && !SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enabledPowder)
                    || client.options.playerListKey.isPressed()
                    || client.player == null
                    || (!Utils.isInDwarvenMines() && !Utils.isInCrystalHollows())) {
                return;
            }

            render(HudCommsWidget.INSTANCE, HudPowderWidget.INSTANCE, context,
                    SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.x,
                    SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.y,
                    SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.powderX,
                    SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.powderY,
                    commissionList);
        });
    }

    /**
     * Gets the dimensions (width, height) for the commissions hud and the powder hud
     * @param commissions what commissions to get the dimensions for
     * @return a {@link Pair} of {@link IntIntPair} with the first pair being for the commissions hud and the second pair being for the powder hud
     */
    public static Pair<IntIntPair,IntIntPair> getDimForConfig(List<Commission> commissions) {
        return switch (SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.style) {
            case SIMPLE -> {
                HudCommsWidget.INSTANCE_CFG.updateData(commissions, false);
                yield Pair.of(
                        IntIntPair.of(
                                HudCommsWidget.INSTANCE_CFG.getWidth(),
                                HudCommsWidget.INSTANCE_CFG.getHeight()),
                        IntIntPair.of(
                                HudPowderWidget.INSTANCE_CFG.getWidth(),
                                HudPowderWidget.INSTANCE_CFG.getHeight())
                );
            }
            case FANCY -> {
                HudCommsWidget.INSTANCE_CFG.updateData(commissions, true);
                yield Pair.of(
                        IntIntPair.of(
                                HudCommsWidget.INSTANCE_CFG.getWidth(),
                                HudCommsWidget.INSTANCE_CFG.getHeight()),
                        IntIntPair.of(
                                HudPowderWidget.INSTANCE_CFG.getWidth(),
                                HudPowderWidget.INSTANCE_CFG.getHeight())
                );
            }
            default -> Pair.of(
                    IntIntPair.of(
                            200,
                            20 * commissions.size()),
                    IntIntPair.of(
                            200,
                           40)
            );
        };
    }

    public static void render(HudCommsWidget hcw, HudPowderWidget hpw, DrawContext context, int comHudX, int comHudY, int powderHudX, int powderHudY, List<Commission> commissions) {

        switch (SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.style) {
            case SIMPLE -> renderSimple(hcw,hpw, context, comHudX, comHudY,powderHudX,powderHudY, commissions);
            case FANCY -> renderFancy(hcw,hpw, context, comHudX, comHudY,powderHudX,powderHudY, commissions);
            case CLASSIC -> renderClassic(context, comHudX, comHudY,powderHudX,powderHudY, commissions);
        }
    }

    /**
     * Renders hud to window without using the widget rendering
     * @param context DrawContext to draw the hud to
     * @param comHudX X coordinate of the commissions hud
     * @param comHudY Y coordinate of the commissions hud
     * @param powderHudX X coordinate of the powder hud
     * @param powderHudY Y coordinate of the powder hud
     * @param commissions the commissions to render to the commissions hud
     */
    public static void renderClassic(DrawContext context, int comHudX, int comHudY, int powderHudX, int powderHudY, List<Commission> commissions) {
        if (SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enableBackground) {
            context.fill(comHudX, comHudY, comHudX + 200, comHudY + (20 * commissions.size()), 0x64000000);
            context.fill(powderHudX, powderHudY, powderHudX + 200, powderHudY + 40, 0x64000000);
        }
        if (SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enabledCommissions) {
            int y = 0;
            for (Commission commission : commissions) {
                float percentage;
                if (!commission.progression().contains("DONE")) {
                    percentage = Float.parseFloat(commission.progression().substring(0, commission.progression().length() - 1));
                } else {
                    percentage = 100f;
                }

                context
                        .drawTextWithShadow(client.textRenderer,
                                Text.literal(commission.commission + ": ").formatted(Formatting.AQUA)
                                        .append(Text.literal(commission.progression).formatted(Colors.hypixelProgressColor(percentage))),
                                comHudX + 5, comHudY + y + 5, 0xFFFFFFFF);
                y += 20;
            }
        }
        if(SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enabledPowder) {
            //render mithril powder then gemstone
            context
                    .drawTextWithShadow(client.textRenderer,
                            Text.literal("Mithril: " + mithrilPowder).formatted(Formatting.AQUA),
                            powderHudX + 5, powderHudY + 5, 0xFFFFFFFF);
            context
                    .drawTextWithShadow(client.textRenderer,
                            Text.literal("Gemstone: " + gemStonePowder).formatted(Formatting.DARK_PURPLE),
                            powderHudX + 5, powderHudY + 25, 0xFFFFFFFF);
        }
    }

    public static void renderSimple(HudCommsWidget hcw, HudPowderWidget hpw, DrawContext context, int comHudX, int comHudY, int powderHudX, int powderHudY, List<Commission> commissions) {
        if (SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enabledCommissions) {
            hcw.updateData(commissions, false);
            hcw.update();
            hcw.setX(comHudX);
            hcw.setY(comHudY);
            hcw.render(context,
                    SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enableBackground);
        }
        if (SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enabledPowder) {
            hpw.update();
            hpw.setX(powderHudX);
            hpw.setY(powderHudY);
            hpw.render(context,
                    SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enableBackground);
        }
    }

    public static void renderFancy(HudCommsWidget hcw, HudPowderWidget hpw, DrawContext context, int comHudX, int comHudY, int powderHudX, int powderHudY, List<Commission> commissions) {
        if (SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enabledCommissions) {
            hcw.updateData(commissions, true);
            hcw.update();
            hcw.setX(comHudX);
            hcw.setY(comHudY);
            hcw.render(context,
                    SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enableBackground);
        }
        if (SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enabledPowder) {
            hpw.update();
            hpw.setX(powderHudX);
            hpw.setY(powderHudY);
            hpw.render(context,
                    SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enableBackground);
        }
    }

    public static void update() {
        if (client.player == null || client.getNetworkHandler() == null || !SkyblockerConfigManager.get().locations.dwarvenMines.dwarvenHud.enabledCommissions || (!Utils.isInCrystalHollows()
                && !Utils.isInDwarvenMines()))
            return;

        commissionList = new ArrayList<>();

        client.getNetworkHandler().getPlayerList().forEach(playerListEntry -> {
            if (playerListEntry.getDisplayName() != null) {
                //find commissions
                for (Pattern pattern : COMMISSIONS) {
                    Matcher matcher = pattern.matcher(playerListEntry.getDisplayName().getString());
                    if (matcher.find()) {
                        commissionList.add(new Commission(matcher.group(1), matcher.group(2)));
                    }

                }
                //find powder
                Matcher mithrilMatcher = MITHRIL_PATTERN.matcher(playerListEntry.getDisplayName().getString());
                if (mithrilMatcher.find()){
                    mithrilPowder = mithrilMatcher.group(0).split(": ")[1];
                }
                Matcher gemstoneMatcher = GEMSTONE_PATTERN.matcher(playerListEntry.getDisplayName().getString());
                if (gemstoneMatcher.find()){
                    gemStonePowder = gemstoneMatcher.group(0).split(": ")[1];
                }
            }
        });
    }

    // steamroller tactics to get visibility from outside classes (HudCommsWidget)
    public record Commission(String commission, String progression) {
    }
}