NameAlert: Save messages to file

This commit is contained in:
noil 2020-12-23 14:59:27 -05:00
parent 71d1919bf2
commit b1f9a692ed
2 changed files with 46 additions and 5 deletions

View File

@ -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<Boolean> saveToFile = new Value<Boolean>("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<String> 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);
}
}

View File

@ -41,7 +41,7 @@ import java.util.logging.Level;
public final class StorageAlertModule extends Module {
public final Value<Mode> mode = new Value<Mode>("Mode", new String[]{"Mode", "M"}, "Change between alert modes.", Mode.CHAT);
public final Value<Boolean> saveToFile = new Value<Boolean>("SaveToFile", new String[]{"Save", "Saves"}, "Saves the alert to a file in your Seppuku directory.", false);
public final Value<Boolean> saveToFile = new Value<Boolean>("SaveToFile", new String[]{"Save", "Saves"}, "Saves the alert to a file in your Seppuku 'Config' directory.", false);
public final Value<Boolean> chests = new Value<Boolean>("Chests", new String[]{"Chests", "chest"}, "Count chests.", true);
public final Value<Boolean> echests = new Value<Boolean>("EnderChests", new String[]{"EnderChests", "echest", "echest"}, "Count ender chests.", false);
public final Value<Boolean> shulkers = new Value<Boolean>("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);
}
}