From b1f9a692edb0f087c923d07358fb336f2cb849f7 Mon Sep 17 00:00:00 2001 From: noil Date: Wed, 23 Dec 2020 14:59:27 -0500 Subject: [PATCH] NameAlert: Save messages to file --- .../impl/module/misc/NameAlertModule.java | 43 ++++++++++++++++++- .../impl/module/misc/StorageAlertModule.java | 8 ++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/rigamortis/seppuku/impl/module/misc/NameAlertModule.java b/src/main/java/me/rigamortis/seppuku/impl/module/misc/NameAlertModule.java index 40dcfb7..633da54 100644 --- a/src/main/java/me/rigamortis/seppuku/impl/module/misc/NameAlertModule.java +++ b/src/main/java/me/rigamortis/seppuku/impl/module/misc/NameAlertModule.java @@ -5,11 +5,22 @@ import me.rigamortis.seppuku.Seppuku; import me.rigamortis.seppuku.api.event.EventStageable; import me.rigamortis.seppuku.api.event.network.EventReceivePacket; import me.rigamortis.seppuku.api.module.Module; +import me.rigamortis.seppuku.api.util.FileUtil; +import me.rigamortis.seppuku.api.value.Value; import net.minecraft.client.Minecraft; import net.minecraft.network.play.server.SPacketChat; import net.minecraft.util.StringUtils; +import net.minecraft.util.math.Vec2f; import team.stiff.pomelo.impl.annotated.handler.annotation.Listener; +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -18,11 +29,23 @@ import java.util.regex.Pattern; */ public final class NameAlertModule extends Module { + public final Value saveToFile = new Value("SaveToFile", new String[]{"Save", "Saves"}, "Saves the alert to a file in your Seppuku 'Config' directory.", false); + //private final String REGEX_NAME = "(?<=<).*?(?=>)"; private final String REGEX_NAME = "<(\\S+)\\s*(\\S+?)?>\\s(.*)"; + private final File messagesFile; + public NameAlertModule() { super("NameAlert", new String[]{"NameAlert", "SayMyName", "WhoSaid"}, "Alerts you when someone says your name in chat via a notification.", "NONE", -1, ModuleType.MISC); + + this.messagesFile = new File(Seppuku.INSTANCE.getConfigManager().getConfigDir(), "NameAlerts.txt"); + try { + if (!this.messagesFile.exists()) + this.messagesFile.createNewFile(); + } catch (IOException e) { + Seppuku.INSTANCE.getLogger().log(Level.WARNING, "Couldn't create NameAlert messages file."); + } } @Listener @@ -33,8 +56,12 @@ public final class NameAlertModule extends Module { String text = packetChat.getChatComponent().getFormattedText(); final String localUsername = Minecraft.getMinecraft().getSession().getUsername(); - if (text.contains(":") && text.toLowerCase().contains(ChatFormatting.LIGHT_PURPLE + "from")) { + if ((text.contains(":") && text.toLowerCase().contains(ChatFormatting.LIGHT_PURPLE + "from")) || + (text.toLowerCase().contains(ChatFormatting.GRAY + "") && StringUtils.stripControlCodes(text).contains("whispers to you"))) { Seppuku.INSTANCE.getNotificationManager().addNotification("Whisper", "Someone whispered to you."); + if (this.saveToFile.getValue()) { + this.saveMessageToFile("Whisper", StringUtils.stripControlCodes(text)); + } return; } @@ -48,10 +75,24 @@ public final class NameAlertModule extends Module { String username = chatUsernameMatcher.group(1).replaceAll(">", ""); if (!username.equals(localUsername)) { Seppuku.INSTANCE.getNotificationManager().addNotification("Public Chat", String.format("Someone mentioned you in chat. <%s>", username)); + if (this.saveToFile.getValue()) { + this.saveMessageToFile(username, text); + } } } } } } } + + public void saveMessageToFile(String fromWho, String messageContent) { + final String time = new SimpleDateFormat().format(new Date()); + final String host = Minecraft.getMinecraft().getCurrentServerData() != null ? Minecraft.getMinecraft().getCurrentServerData().serverIP : "localhost"; + + final List linesToAdd = new ArrayList<>(); + final String data = String.format("server: %s, date: %s, from: %s, message: %s", host, time, fromWho, messageContent); + linesToAdd.add(data); + + FileUtil.write(this.messagesFile, linesToAdd, false); + } } diff --git a/src/main/java/me/rigamortis/seppuku/impl/module/misc/StorageAlertModule.java b/src/main/java/me/rigamortis/seppuku/impl/module/misc/StorageAlertModule.java index e54af05..4fbb9f7 100644 --- a/src/main/java/me/rigamortis/seppuku/impl/module/misc/StorageAlertModule.java +++ b/src/main/java/me/rigamortis/seppuku/impl/module/misc/StorageAlertModule.java @@ -41,7 +41,7 @@ import java.util.logging.Level; public final class StorageAlertModule extends Module { public final Value mode = new Value("Mode", new String[]{"Mode", "M"}, "Change between alert modes.", Mode.CHAT); - public final Value saveToFile = new Value("SaveToFile", new String[]{"Save", "Saves"}, "Saves the alert to a file in your Seppuku directory.", false); + public final Value saveToFile = new Value("SaveToFile", new String[]{"Save", "Saves"}, "Saves the alert to a file in your Seppuku 'Config' directory.", false); public final Value chests = new Value("Chests", new String[]{"Chests", "chest"}, "Count chests.", true); public final Value echests = new Value("EnderChests", new String[]{"EnderChests", "echest", "echest"}, "Count ender chests.", false); public final Value shulkers = new Value("ShulkerBoxes", new String[]{"ShulkerBoxes", "shul"}, "Count shulkers.", false); @@ -62,8 +62,8 @@ public final class StorageAlertModule extends Module { this.locationsFile = new File(Seppuku.INSTANCE.getConfigManager().getConfigDir(), "StorageAlerts.txt"); try { - if (!locationsFile.exists()) - locationsFile.createNewFile(); + if (!this.locationsFile.exists()) + this.locationsFile.createNewFile(); } catch (IOException e) { Seppuku.INSTANCE.getLogger().log(Level.WARNING, "Couldn't create StorageAlert locations file."); } @@ -129,6 +129,6 @@ public final class StorageAlertModule extends Module { linesToAdd.add(data); } - FileUtil.write(locationsFile, linesToAdd, false); + FileUtil.write(this.locationsFile, linesToAdd, false); } }