aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/Danker/features/Alerts.java
blob: 8f45423787218d0dc16fcf29a1278a20fb1ce3ac (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
package me.Danker.features;

import com.google.gson.GsonBuilder;
import javafx.scene.control.Alert;
import me.Danker.commands.ToggleCommand;
import me.Danker.utils.Utils;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StringUtils;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.awt.*;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Alerts {

    public static List<Alert> alerts = new ArrayList<>();
    public static HashMap<Alert, Pattern> patterns = new HashMap<>();
    public static String configFile;

    @SubscribeEvent
    public void onChat(ClientChatReceivedEvent event) {
        if (!ToggleCommand.alerts || event.type == 2) return;

        String message = StringUtils.stripControlCodes(event.message.getUnformattedText());

        for (Alert alert : alerts) {
            if (!alert.toggled) continue;

            boolean location;
            switch (alert.location) {
                case "Skyblock":
                    location = Utils.inSkyblock;
                    break;
                case "Dungeons":
                    location = Utils.inDungeons;
                    break;
                default:
                    location = true;
            }
            if (!location) continue;

            if (alert.mode.equals("Regex")) {
                Matcher matcher = patterns.get(alert).matcher(message);
                if (matcher.matches()) {
                    matcher.reset();
                    String alertText = alert.alert;

                    int i = 0;
                    while (matcher.find()) {
                        for (int j = 0; j <= matcher.groupCount(); j++) {
                            alertText = alertText.replace("$$" + i + "$$", matcher.group(j));
                            i++;
                        }
                    }

                    Utils.createTitle(EnumChatFormatting.RED + alertText.replace("&", "§"), 2);
                    if (alert.desktop) Utils.desktopNotification("Alert", alertText, message, TrayIcon.MessageType.INFO);

                    return;
                }
            } else {
                boolean trigger;
                switch (alert.mode) {
                    case "Starts With":
                        trigger = message.startsWith(alert.message);
                        break;
                    case "Contains":
                        trigger = message.contains(alert.message);
                        break;
                    case "Ends With":
                        trigger = message.endsWith(alert.message);
                        break;
                    default:
                        continue;
                }

                if (trigger) {
                    Utils.createTitle(EnumChatFormatting.RED + alert.alert.replace("&", "§"), 2);
                    if (alert.desktop) Utils.desktopNotification("Alert", alert.alert, message, TrayIcon.MessageType.INFO);

                    return;
                }
            }
        }
    }

    public static void save() {
        for (Alert alert : alerts) {
            if (alert.mode.equals("Regex")) {
                Pattern pattern = Pattern.compile(alert.message);
                patterns.put(alert, pattern);
            }
        }

        try (FileWriter writer = new FileWriter(configFile)) {
            new GsonBuilder().create().toJson(alerts, writer);
            writer.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static class Alert {

        public String mode;
        public String location;
        public String message;
        public String alert;
        public boolean desktop;
        public boolean toggled;

        public Alert(String mode, String location, String message, String alert, boolean desktop, boolean toggled) {
            this.mode = mode;
            this.location = location;
            this.message = message;
            this.alert = alert;
            this.desktop = desktop;
            this.toggled = toggled;
        }

        public void toggle() {
            toggled = !toggled;
        }

        public void toggleDesktop() {
            desktop = !desktop;
        }

    }

}