aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java
blob: 36f4b908127156ba973b8020894ddbec2539f339 (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
package io.github.moulberry.notenoughupdates.miscfeatures;

import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import net.minecraft.entity.item.EntityArmorStand;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;

import java.text.NumberFormat;
import java.util.HashMap;

public class DamageCommas {

    private static final HashMap<Integer, ChatComponentText> replacementMap = new HashMap<>();

    public static void tick() {
        replacementMap.clear();
    }

    public static IChatComponent replaceName(IChatComponent name) {
        if(!NotEnoughUpdates.INSTANCE.config.misc.damageCommas) return name;

        String formatted = name.getFormattedText();
        int hashCode = formatted.hashCode();

        if(replacementMap.containsKey(hashCode)) {
            ChatComponentText component = replacementMap.get(hashCode);
            if(component == null) return name;
            return component;
        }

        if(formatted.length() >= 7 && formatted.startsWith("\u00A7f\u2727") &&
                formatted.endsWith("\u2727\u00a7r")) {

            StringBuilder builder = new StringBuilder();
            boolean numLast = false;
            boolean colLast = false;
            boolean colLastLast;
            int numCount = 0;
            for(int i=formatted.length()-4; i>=3; i--) {
                char c = formatted.charAt(i);
                colLastLast = colLast;

                if(c == '\u00a7') {
                    if(numLast) numCount--;
                    numLast = false;
                    colLast = true;
                } else if(c >= '0' && c <= '9') {
                    numLast = true;
                    colLast = false;
                    numCount++;
                } else {
                    if(colLast) {
                        replacementMap.put(hashCode, null);
                        return name;
                    }
                    numLast = false;
                }

                if(colLastLast && numLast && numCount > 1 && (numCount-1) % 3 == 0) builder.append(',');
                builder.append(c);
            }

            ChatComponentText ret = new ChatComponentText("\u00A7f\u2727"+builder.reverse().toString()+"\u2727\u00a7r");
            replacementMap.put(hashCode, ret);
            return ret;
        }

        if(formatted.length() >= 5 && formatted.startsWith(EnumChatFormatting.GRAY.toString()) &&
                formatted.endsWith(EnumChatFormatting.RESET.toString())) {
            String damageS = formatted.substring(2, formatted.length()-2);

            for(int i=0; i<damageS.length(); i++) {
                char c = damageS.charAt(i);
                if(c < '0' || c > '9') {
                    replacementMap.put(hashCode, null);
                    return name;
                }
            }

            try {
                int damage = Integer.parseInt(damageS);

                String damageFormatted = NumberFormat.getIntegerInstance().format(damage);

                ChatComponentText ret = new ChatComponentText(EnumChatFormatting.GRAY+damageFormatted+EnumChatFormatting.RESET);
                replacementMap.put(hashCode, ret);
                return ret;
            } catch(Exception e) {
                replacementMap.put(hashCode, null);
                return name;
            }
        }
        replacementMap.put(hashCode, null);
        return name;
    }

}