aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hype/bbsentials/chat/Message.java
blob: 9ff05a16928a0aa41902bfacc162325abbe0fdf9 (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
package de.hype.bbsentials.chat;

import de.hype.bbsentials.client.BBsentials;
import net.minecraft.text.Text;

public class Message {
    public Text text;
    private String unformattedString = null;
    private String playerName = null;

    public Message(Text text) {
        this.text = text;
    }

    public Text getText() {
        return text;
    }

    public String getString() {
        return text.getString();
    }

    public String getUnformattedString() {
        if (unformattedString != null) return unformattedString;
        unformattedString = text.getString().replaceAll("§.", "").trim();
        return unformattedString;
    }

    public String getMessageContent() {
        if (isServerMessage()) return unformattedString;
        return getUnformattedString().split(":", 1)[1];
    }

    Boolean guild = null;

    public boolean isFromGuild() {
        if (guild != null) return guild;
        guild = getUnformattedString().startsWith("Guild >");
        return guild;
    }

    Boolean party = null;

    public boolean isFromParty() {
        if (party != null) return party;
        party = getUnformattedString().startsWith("Party >");
        return party;
    }

    Boolean msg = null;

    public boolean isMsg() {
        if (msg != null) return msg;
        msg = getUnformattedString().startsWith("From") || getUnformattedString().startsWith("To");
        return msg;
    }

    Boolean server = null;

    public boolean isServerMessage() {
        if (server != null) return server;
        int space = getUnformattedString().indexOf(" ");
        int doublepoint = getUnformattedString().indexOf(":");
        return ((space + 2 < doublepoint)&&doublepoint!=-1&&space!=-1);
    }

    public String getPlayerName() {
        if (playerName != null) return playerName;
        playerName = getUnformattedString();
        if (!playerName.contains(":")) {
            playerName = "";
            return "";
        }
        playerName = playerName.split(":", 1)[0];
        if (isMsg()) {
            playerName = playerName.replaceFirst("From", "").replace("To", "").trim();
        }
        playerName = playerName.replaceFirst("\\[[^\\]]*\\](?:\\s?\\[[^\\]]*\\])*", "").trim();
        if (playerName.matches("^[a-zA-Z0-9_-]+$")) playerName = "";
        return playerName;
    }

    public void replaceInJson(String replace, String replaceWith) {
        String textString = toJson();
        if (textString.contains(replace)) {
            textString = textString.replaceAll("\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}", "");
        }
        textString = textString.replace(replace, replaceWith);
        text = Text.Serializer.fromJson(textString);
    }

    public static Message fromJson(String json) {
        return new Message(Text.Serializer.fromJson(json));
    }

    public String toJson() {
        return Text.Serializer.toJson(text);
    }

    public boolean isFromReportedUser() {
        return BBsentials.config.alreadyReported.contains(getPlayerName()) && !getPlayerName().isEmpty();
    }

    public boolean contains(String string) {
        return getUnformattedString().contains(string);
    }

    public boolean startsWith(String string) {
        return getUnformattedString().startsWith(string);
    }
}