From fabf10cfaa11577c21fee7e592749f04544c07d9 Mon Sep 17 00:00:00 2001 From: Yui Date: Sun, 1 Dec 2024 20:23:12 -0300 Subject: [PATCH] feat: made timestamp optional thru config --- .../java/moe/yuyui/mcstreamerbot/Config.java | 4 ++++ .../events/custom/OnChatMessage.java | 17 ++++++++++++---- .../gui/MCStreamerBotGuiConfig.java | 20 +++++++++++++++---- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/main/java/moe/yuyui/mcstreamerbot/Config.java b/src/main/java/moe/yuyui/mcstreamerbot/Config.java index fd19c06..bec04bc 100644 --- a/src/main/java/moe/yuyui/mcstreamerbot/Config.java +++ b/src/main/java/moe/yuyui/mcstreamerbot/Config.java @@ -8,9 +8,13 @@ public class Config { public static int portNumber = 8069; public static String ipAddress = "127.0.0.1"; public static String authToken = ""; + public static boolean addTimestamp = false; public static Configuration configuration; public static void synchronizeConfiguration() { + addTimestamp = configuration + .getBoolean("Add timestamp", Configuration.CATEGORY_GENERAL, addTimestamp, "Add timestamp to messages"); + configuration.addCustomCategoryComment(CATEGORY_WEBSOCKET, "Configuration for the websocket"); portNumber = configuration .getInt("Port", CATEGORY_WEBSOCKET, portNumber, 1024, 65535, "The Port that the websocket will listen on"); diff --git a/src/main/java/moe/yuyui/mcstreamerbot/events/custom/OnChatMessage.java b/src/main/java/moe/yuyui/mcstreamerbot/events/custom/OnChatMessage.java index dc767df..c77deab 100644 --- a/src/main/java/moe/yuyui/mcstreamerbot/events/custom/OnChatMessage.java +++ b/src/main/java/moe/yuyui/mcstreamerbot/events/custom/OnChatMessage.java @@ -9,12 +9,14 @@ import net.minecraft.client.Minecraft; import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumChatFormatting; +import moe.yuyui.mcstreamerbot.Config; import moe.yuyui.mcstreamerbot.common.beans.MessageBean; import moe.yuyui.mcstreamerbot.common.beans.UserBean; public final class OnChatMessage { - private static final String _messageFormat = "<%s> %s%s[%s]%s: %s"; + private static final String _messageFormat = "%s%s[%s]%s: %s"; + private static final String _timestampedMessageFormat = "<%s>"; private static String buildMessage(List messageParts) { StringBuilder finalMessage = new StringBuilder(); @@ -58,19 +60,26 @@ public final class OnChatMessage { return; } final String message = buildMessage(messageParts); - final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a"); + final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("hh:mm a"); final String formattedTime = Instant.ofEpochSecond(timestamp) .atZone(ZoneId.systemDefault()) .format(dateFormatter); - ChatComponentText chatComponentText = new ChatComponentText( + StringBuilder chatMessage = new StringBuilder(); + + if (Config.addTimestamp) { + chatMessage.append(String.format(_timestampedMessageFormat, formattedTime)); + } + + chatMessage.append( String.format( _messageFormat, - formattedTime, getUserColor(user), getUserBadge(user), user.getName(), EnumChatFormatting.RESET.toString(), message.replace('ยง', ' '))); + + ChatComponentText chatComponentText = new ChatComponentText(chatMessage.toString()); Minecraft.getMinecraft().ingameGUI.getChatGUI() .printChatMessage(chatComponentText); } diff --git a/src/main/java/moe/yuyui/mcstreamerbot/gui/MCStreamerBotGuiConfig.java b/src/main/java/moe/yuyui/mcstreamerbot/gui/MCStreamerBotGuiConfig.java index f499f09..9982c30 100644 --- a/src/main/java/moe/yuyui/mcstreamerbot/gui/MCStreamerBotGuiConfig.java +++ b/src/main/java/moe/yuyui/mcstreamerbot/gui/MCStreamerBotGuiConfig.java @@ -1,10 +1,15 @@ package moe.yuyui.mcstreamerbot.gui; +import java.util.List; +import java.util.stream.Collectors; + import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraftforge.common.config.ConfigElement; +import net.minecraftforge.common.config.Configuration; import cpw.mods.fml.client.config.GuiConfig; +import cpw.mods.fml.client.config.IConfigElement; import moe.yuyui.mcstreamerbot.Config; import moe.yuyui.mcstreamerbot.MCStreamerBot; @@ -13,12 +18,11 @@ public class MCStreamerBotGuiConfig extends GuiConfig { public MCStreamerBotGuiConfig(GuiScreen parent) { super( parent, - new ConfigElement<>(Config.configuration.getCategory(Config.CATEGORY_WEBSOCKET)).getChildElements(), + getConfigElements(), MCStreamerBot.MODID, - true, false, - "Configure the Websocket."); - titleLine2 = "Make sure you know what you're doing ;)"; + false, + "Configure the Streamer.bot integration."); } @Override @@ -35,4 +39,12 @@ public class MCStreamerBotGuiConfig extends GuiConfig { protected void actionPerformed(GuiButton button) { super.actionPerformed(button); } + + private static List getConfigElements() { + Configuration config = Config.configuration; + return config.getCategoryNames() + .stream() + .map(name -> new ConfigElement<>(config.getCategory(name))) + .collect(Collectors.toList()); + } }