aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/xmrvizzy/skyblocker/skyblock/FancyStatusBars.java
blob: 852a16dfae057012013fc26cb533a0521041cb97 (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
package me.xmrvizzy.skyblocker.skyblock;

import com.mojang.blaze3d.systems.RenderSystem;
import me.xmrvizzy.skyblocker.SkyblockerMod;
import me.xmrvizzy.skyblocker.config.SkyblockerConfig;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FancyStatusBars extends DrawableHelper {
    private static final MinecraftClient client = MinecraftClient.getInstance();
    private static final Identifier BARS = new Identifier(SkyblockerMod.NAMESPACE,"textures/gui/bars.png");
    private static final Pattern ACTION_BAR_STATUS = Pattern.compile("^§[6c]([0-9]+)/([0-9]+)❤(?:\\+§c[0-9]+\\S)? {3,}(?:§a([0-9]+)§a❈ Defense|(\\S+(?: \\S+)*)) {3,}(?:§b([0-9]+)/([0-9]+)✎ (?:Mana|§3([0-9]+)ʬ)?|(\\S+(?: \\S+)*))(.*)$");
    private final Resource health;
    private final Resource mana;
    private int defense;

    public FancyStatusBars() {
        health = new Resource(100, 100);
        mana = new Resource(100, 100);
        defense = 0;
    }

    public boolean update(String actionBar) {
        if(!SkyblockerConfig.get().general.bars.enableBars)
            return false;
        Matcher matcher = ACTION_BAR_STATUS.matcher(actionBar);
        if(!matcher.matches())
            return false;
        health.set(matcher.group(1), matcher.group(2));
        if(matcher.group(3) != null)
            defense = Integer.parseInt(matcher.group(3));
        if(matcher.group(5) != null) {
            mana.set(matcher.group(5), matcher.group(6));
            if(matcher.group(7) != null)
                mana.add(Integer.parseInt(matcher.group(7)));
        }

        StringBuilder sb = new StringBuilder();
        appendIfNotNull(sb, matcher.group(4));
        appendIfNotNull(sb, matcher.group(8));
        appendIfNotNull(sb, matcher.group(9));

        if(!sb.isEmpty()) {
            assert client.player != null;
            client.player.sendMessage(Text.of(sb.toString()), true);
        }

        return true;
    }

    private void appendIfNotNull(StringBuilder sb, String str) {
        if(str == null)
            return;
        if(!sb.isEmpty())
            sb.append("    ");
        sb.append(str);
    }

    public boolean render(MatrixStack matrices, int scaledWidth, int scaledHeight) {
        if(!SkyblockerConfig.get().general.bars.enableBars)
            return false;
        int left = scaledWidth / 2 - 91;
        int top = scaledHeight - 35;

        int hpFillWidth = (int) (health.getFillLevel() * 33.0F);
        int hpOverflowWidth = (int) (health.getOverflow() * 33.0F);
        int manaFillWidth = (int) (mana.getFillLevel() * 33.0F);
        assert client.player != null;
        int xp = (int) (client.player.experienceProgress * 33.0F);
        int defenseFill = (int) (defense / (defense + 100.0) * 33.0);

        // Icons
//        this.client.getTextureManager().bindTexture(BARS);
        RenderSystem.setShaderTexture(0, BARS);
        this.drawTexture(matrices, left, top, 0, 0, 9, 9);
        this.drawTexture(matrices, left + 47, top, 9, 0, 7, 9);
        this.drawTexture(matrices, left + 92, top, 16, 0, 9, 9);
        this.drawTexture(matrices, left + 139, top, 25, 0, 9, 9);

        // Empty Bars
        this.drawTexture(matrices, left + 10, top + 1, 0, 9, 33, 7);
        this.drawTexture(matrices, left + 55, top + 1, 0, 9, 33, 7);
        this.drawTexture(matrices, left + 102, top + 1, 0, 9, 33, 7);
        this.drawTexture(matrices, left + 149, top + 1, 0, 9, 33, 7);

        // Progress Bars
        this.drawTexture(matrices, left + 10, top + 1, 0, 16, hpFillWidth, 7);
        this.drawTexture(matrices, left + 10, top + 1, 0, 44, hpOverflowWidth, 7);
        this.drawTexture(matrices, left + 55, top + 1, 0, 23, manaFillWidth, 7);
        this.drawTexture(matrices, left + 102, top + 1, 0, 30, defenseFill, 7);
        this.drawTexture(matrices, left + 149, top + 1, 0, 37, xp, 7);

        // Progress Texts
        renderText(matrices, health.getValue(), left + 11, top, 16733525);
        renderText(matrices, mana.getValue(), left + 56, top, 5636095);
        renderText(matrices, defense, left + 103, top, 12106180);
        renderText(matrices, client.player.experienceLevel, left + 150, top, 8453920);
        return true;
    }

    private void renderText(MatrixStack matrices, int value, int left, int top, int color) {
        TextRenderer textRenderer = client.textRenderer;
        String text = Integer.toString(value);
        int x = left + (33 - textRenderer.getWidth(text)) / 2;
        int y = top - 3;

        textRenderer.draw(matrices, text, (float) (x + 1), (float) y, 0);
        textRenderer.draw(matrices, text, (float) (x - 1), (float) y, 0);
        textRenderer.draw(matrices, text, (float) x, (float) (y + 1), 0);
        textRenderer.draw(matrices, text, (float) x, (float) (y - 1), 0);
        textRenderer.draw(matrices, text, (float) x, (float) y, color);
    }

    private static class Resource {
        private int value;
        private int max;
        public Resource(int value, int max) {
            this.value = value;
            this.max = max;
        }
        public void set(String value, String max) {
            this.value = Integer.parseInt(value);
            this.max = Integer.parseInt(max);
        }
        public void add(int value) {
            this.value += value;
        }
        public int getValue() {
            return value;
        }
        public double getFillLevel() {
            return Math.min(((double)value)/((double)max),  1);
        }
        public double getOverflow() {
            return Math.max(((double)value)/((double)max) - 1,  0);
        }
    }
}