|
1 | 1 | package me.mini_bomba.streamchatmod; |
2 | 2 |
|
| 3 | +import com.github.twitch4j.helix.domain.User; |
3 | 4 | import me.mini_bomba.streamchatmod.commands.IDrawsChatOutline; |
4 | 5 | import me.mini_bomba.streamchatmod.events.LocalMessageEvent; |
| 6 | +import me.mini_bomba.streamchatmod.runnables.TwitchMessageHandler; |
5 | 7 | import me.mini_bomba.streamchatmod.tweaker.TransformerField; |
| 8 | +import me.mini_bomba.streamchatmod.utils.ChatComponentStreamEmote; |
6 | 9 | import net.minecraft.client.gui.GuiChat; |
7 | 10 | import net.minecraft.client.gui.GuiTextField; |
| 11 | +import net.minecraft.util.ChatComponentText; |
| 12 | +import net.minecraft.util.ChatComponentTranslation; |
8 | 13 | import net.minecraft.util.EnumChatFormatting; |
| 14 | +import net.minecraft.util.IChatComponent; |
| 15 | +import net.minecraftforge.client.event.ClientChatReceivedEvent; |
9 | 16 | import net.minecraftforge.client.event.GuiScreenEvent; |
10 | 17 | import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; |
11 | 18 | import net.minecraftforge.fml.common.gameevent.TickEvent; |
|
15 | 22 |
|
16 | 23 | import java.lang.reflect.Field; |
17 | 24 | import java.util.Arrays; |
| 25 | +import java.util.List; |
18 | 26 |
|
19 | 27 | public class StreamEvents { |
20 | 28 |
|
@@ -96,13 +104,50 @@ public void onLocalMinecraftMessage(LocalMessageEvent event) { |
96 | 104 | } |
97 | 105 | event.setCanceled(true); |
98 | 106 | if (mod.twitch == null || mod.twitchSender == null || !mod.config.twitchEnabled.getBoolean() || mod.twitchSender.getChat() == null) { |
99 | | - StreamUtils.addMessage(EnumChatFormatting.RED+"The message was not sent anywhere: Chat mode is set to 'Redirect to Twitch', but Twitch chat (or part of it) is disabled!"); |
| 107 | + StreamUtils.addMessage(EnumChatFormatting.RED + "The message was not sent anywhere: Chat mode is set to 'Redirect to Twitch', but Twitch chat (or part of it) is disabled!"); |
100 | 108 | return; |
101 | 109 | } |
102 | 110 | if (mod.config.twitchSelectedChannel.getString().length() == 0) { |
103 | | - StreamUtils.addMessage(EnumChatFormatting.RED+"The message was not sent anywhere: Chat mode is set to 'Redirect to Twitch', but no channel is selected!"); |
| 111 | + StreamUtils.addMessage(EnumChatFormatting.RED + "The message was not sent anywhere: Chat mode is set to 'Redirect to Twitch', but no channel is selected!"); |
104 | 112 | return; |
105 | 113 | } |
106 | 114 | mod.twitchSender.getChat().sendMessage(mod.config.twitchSelectedChannel.getString(), event.message); |
107 | 115 | } |
| 116 | + |
| 117 | + @SubscribeEvent |
| 118 | + public void onMessage(ClientChatReceivedEvent event) { |
| 119 | + String channel = mod.config.twitchSelectedChannel.getString(); |
| 120 | + if (channel.length() == 0) channel = mod.getTwitchUsername(); |
| 121 | + if (mod.config.showEmotesEverywhere.getBoolean()) { |
| 122 | + User user = channel == null ? null : mod.getTwitchUserByName(channel); |
| 123 | + event.message = transformComponent(event.message, user == null ? null : user.getId()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public IChatComponent transformComponent(IChatComponent component, String channelId) { |
| 128 | + IChatComponent newComponent; |
| 129 | + if (component instanceof ChatComponentText) { |
| 130 | + List<IChatComponent> components = TwitchMessageHandler.processEmotes(mod, component.getUnformattedTextForChat(), channelId); |
| 131 | + components.stream().filter(c -> !(c instanceof ChatComponentStreamEmote)).forEach(c -> c.setChatStyle(component.getChatStyle().createShallowCopy())); |
| 132 | + if (components.size() > 0) { |
| 133 | + newComponent = components.get(0); |
| 134 | + components.remove(0); |
| 135 | + for (IChatComponent c : components) newComponent.appendSibling(c); |
| 136 | + } else { |
| 137 | + newComponent = new ChatComponentText(""); |
| 138 | + newComponent.setChatStyle(component.getChatStyle().createShallowCopy()); |
| 139 | + } |
| 140 | + } else if (component instanceof ChatComponentTranslation) { |
| 141 | + ChatComponentTranslation castedComponent = (ChatComponentTranslation) component; |
| 142 | + newComponent = new ChatComponentTranslation(castedComponent.getKey(), Arrays.stream(castedComponent.getFormatArgs()).map(c -> c instanceof IChatComponent ? transformComponent((IChatComponent) c, channelId) : c).toArray()); |
| 143 | + newComponent.setChatStyle(castedComponent.getChatStyle().createShallowCopy()); |
| 144 | + } else { |
| 145 | + newComponent = component.createCopy(); |
| 146 | + newComponent.getSiblings().clear(); |
| 147 | + } |
| 148 | + for (IChatComponent sibling : component.getSiblings()) { |
| 149 | + newComponent.appendSibling(transformComponent(sibling, channelId)); |
| 150 | + } |
| 151 | + return newComponent; |
| 152 | + } |
108 | 153 | } |
0 commit comments