diff options
author | bowser0000 <bowser0000@gmail.com> | 2022-08-15 20:40:34 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-15 20:40:34 -0400 |
commit | 3aa11b859a2a22be30a5035f7273e1a604a4dde4 (patch) | |
tree | 8f428b29a6b56cc9d010b0441bf41b6b78373548 /src/main/java/me/Danker/features/Alerts.java | |
parent | 33710ac6d9f57fa59e8dfb19a81b053346f1b097 (diff) | |
parent | daceea1e42371c295c36f80b3246601a6ffb2cc5 (diff) | |
download | SkyblockMod-3aa11b859a2a22be30a5035f7273e1a604a4dde4.tar.gz SkyblockMod-3aa11b859a2a22be30a5035f7273e1a604a4dde4.tar.bz2 SkyblockMod-3aa11b859a2a22be30a5035f7273e1a604a4dde4.zip |
1.8.7
Diffstat (limited to 'src/main/java/me/Danker/features/Alerts.java')
-rw-r--r-- | src/main/java/me/Danker/features/Alerts.java | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/src/main/java/me/Danker/features/Alerts.java b/src/main/java/me/Danker/features/Alerts.java new file mode 100644 index 0000000..aa6197f --- /dev/null +++ b/src/main/java/me/Danker/features/Alerts.java @@ -0,0 +1,143 @@ +package me.Danker.features; + +import com.google.gson.GsonBuilder; +import me.Danker.commands.ToggleCommand; +import me.Danker.events.ModInitEvent; +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 init(ModInitEvent event) { + configFile = event.configDirectory + "/dsmalerts.json"; + } + + @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; + } + + } + +} |