Skip to content

Commit e6c8917

Browse files
Mason FreyMason Frey
authored andcommitted
Added Post/Delete message commands, more colors
1 parent eb99e9d commit e6c8917

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

src/main/java/com/google/youtube/gaming/chat/ChatService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.minecraft.client.Minecraft;
2424
import net.minecraft.command.ICommandSender;
2525
import net.minecraft.util.ChatComponentText;
26+
import net.minecraft.util.EnumChatFormatting;
2627

2728
import java.util.ArrayList;
2829
import java.util.List;
@@ -128,7 +129,7 @@ public void start(
128129
} else {
129130
nextPoll = System.currentTimeMillis() + response.getPollingIntervalMillis();
130131
}
131-
showMessage("YTC Service started", sender);
132+
showMessage(EnumChatFormatting.RED + "[YTChat] " + EnumChatFormatting.GREEN + " Service started", sender);
132133
} catch (Throwable t) {
133134
showMessage(t.getMessage(), sender);
134135
t.printStackTrace();
@@ -144,7 +145,7 @@ public void stop(ICommandSender sender) {
144145
}
145146
liveChatId = null;
146147
isInitialized = false;
147-
showMessage("YTC Service stopped", sender);
148+
showMessage(EnumChatFormatting.RED + "[YTChat] " + EnumChatFormatting.DARK_RED + "Service stopped", sender);
148149
}
149150

150151
@Override
@@ -199,7 +200,7 @@ public void postMessage(final String message, final Consumer<String> onComplete)
199200
onComplete.accept(response.getId());
200201
} catch (Throwable t) {
201202
onComplete.accept(null);
202-
showMessage(t.getMessage(), Minecraft.getMinecraft().thePlayer.getCommandSenderEntity());
203+
showMessage(t.getMessage(), Minecraft.getMinecraft().thePlayer);
203204
t.printStackTrace();
204205
}
205206
});
@@ -219,7 +220,7 @@ public void deleteMessage(final String messageId, final Runnable onComplete) {
219220
liveChatDelete.execute();
220221
onComplete.run();
221222
} catch (Throwable t) {
222-
showMessage(t.getMessage(), Minecraft.getMinecraft().thePlayer.getCommandSenderEntity());
223+
showMessage(t.getMessage(), Minecraft.getMinecraft().thePlayer);
223224
t.printStackTrace();
224225
onComplete.run();
225226
}
@@ -258,6 +259,7 @@ public void run() {
258259
broadcastMessage(
259260
message.getAuthorDetails(),
260261
snippet.getSuperChatDetails(),
262+
message.getId(),
261263
snippet.getDisplayMessage());
262264
}
263265
System.out.println("POLL DELAY: " + response.getPollingIntervalMillis());
@@ -272,10 +274,10 @@ public void run() {
272274
}
273275

274276
void broadcastMessage(
275-
LiveChatMessageAuthorDetails author, LiveChatSuperChatDetails details, String message) {
277+
LiveChatMessageAuthorDetails author, LiveChatSuperChatDetails details, String id, String message) {
276278
for (YouTubeChatMessageListener listener :
277279
new ArrayList<>(listeners)) {
278-
listener.onMessageReceived(author, details, message);
280+
listener.onMessageReceived(author, details, id , message);
279281
}
280282
}
281283

src/main/java/com/google/youtube/gaming/chat/YouTubeChat.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import net.minecraftforge.fml.common.Mod;
2121
import net.minecraftforge.fml.common.Mod.EventHandler;
2222
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
23+
import tv.twitch.chat.Chat;
2324

2425
/**
2526
* Main entry point for YouTube Chat. Provides the chat service API to other mods, e.g.
@@ -30,7 +31,7 @@
3031
public class YouTubeChat {
3132
public static final String MODID = "ytchat";
3233
public static final String APPNAME = "YouTube Chat";
33-
public static final String VERSION = "1.2.1_1.8.9";
34+
public static final String VERSION = "1.3.0_1.8.9";
3435
public static final String GUI_FACTORY =
3536
"com.google.youtube.gaming.chat.YouTubeConfigurationGuiFactory";
3637

@@ -48,7 +49,8 @@ public static synchronized YouTubeChatService getService() {
4849
public void preInit(FMLPreInitializationEvent event) {
4950
YouTubeConfiguration.initialize(event.getSuggestedConfigurationFile());
5051

51-
ClientCommandHandler.instance.registerCommand(new YouTubeCommand(new ChatService()));
52+
ClientCommandHandler.instance.registerCommand(new YouTubeCommand((ChatService) getService()));
5253
ClientCommandHandler.instance.registerCommand(new YouTubeChatMock());
54+
ClientCommandHandler.instance.registerCommand(new YouTubeDeleteCommand((ChatService) getService()));
5355
}
5456
}

src/main/java/com/google/youtube/gaming/chat/YouTubeChatMock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE
7777
authorDetails.setDisplayName(author);
7878
authorDetails.setChannelId(author);
7979
((ChatService) YouTubeChat.getService()).broadcastMessage(
80-
authorDetails, new LiveChatSuperChatDetails(), message);
80+
authorDetails, new LiveChatSuperChatDetails(), "MOCK" , message);
8181
}
8282

8383
@Override

src/main/java/com/google/youtube/gaming/chat/YouTubeChatService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.youtube.gaming.chat;
1818

19+
import com.google.api.services.youtube.model.LiveChatMessage;
1920
import com.google.api.services.youtube.model.LiveChatMessageAuthorDetails;
2021
import com.google.api.services.youtube.model.LiveChatSuperChatDetails;
2122

@@ -34,6 +35,7 @@ interface YouTubeChatMessageListener {
3435
void onMessageReceived(
3536
LiveChatMessageAuthorDetails author,
3637
LiveChatSuperChatDetails superChatDetails,
38+
String id,
3739
String message);
3840
}
3941

src/main/java/com/google/youtube/gaming/chat/YouTubeCommand.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616

1717
package com.google.youtube.gaming.chat;
1818

19-
import com.google.api.services.youtube.model.LiveChatMessageAuthorDetails;
20-
import com.google.api.services.youtube.model.LiveChatSuperChatDetails;
19+
import com.google.api.services.youtube.model.*;
2120
import com.google.youtube.gaming.chat.YouTubeChatService.YouTubeChatMessageListener;
2221
import net.minecraft.client.Minecraft;
2322
import net.minecraft.command.CommandBase;
2423
import net.minecraft.command.ICommandSender;
24+
import net.minecraft.event.ClickEvent;
25+
import net.minecraft.event.HoverEvent;
2526
import net.minecraft.util.ChatComponentText;
27+
import net.minecraft.util.ChatStyle;
2628
import net.minecraft.util.EnumChatFormatting;
2729

2830
import java.io.IOException;
2931
import java.util.ArrayList;
3032
import java.util.List;
33+
import java.util.function.Consumer;
3134

3235
/**
3336
* An in-game command for managing the YouTube Chat service. Usage:
@@ -49,7 +52,7 @@ public String getCommandName() {
4952

5053
@Override
5154
public String getCommandUsage(ICommandSender sender) {
52-
return getCommandName() + " [start|stop|logout|echoStart|echoStop]";
55+
return getCommandName() + " [start|stop|logout|echoStart|echoStop|post";
5356
}
5457

5558
@Override
@@ -84,22 +87,32 @@ public void processCommand(ICommandSender sender, String[] args) {
8487
} catch (IOException e) {
8588
showMessage(e.getMessage(), sender);
8689
}
87-
} else {
88-
if (args[0].equalsIgnoreCase("echoStart")) {
90+
} else if (args[0].equalsIgnoreCase("echoStart"))
91+
{
8992
if (!service.isInitialized()) {
9093
showMessage("Service is not initialized", sender);
9194
showUsage(sender);
9295
} else {
9396
service.subscribe(this);
9497
}
95-
} else if (args[0].equalsIgnoreCase("echoStop")) {
98+
} else if (args[0].equalsIgnoreCase("echoStop")) {
9699
service.unsubscribe(this);
97-
} else {
100+
} else if(args[0].equalsIgnoreCase("post")) {
101+
StringBuilder message = new StringBuilder();
102+
for (String arg: args) {
103+
if(!arg.equalsIgnoreCase("post")) {
104+
message.append(arg).append(" ");
105+
}
106+
}
107+
Consumer<String> id = i -> showMessage(EnumChatFormatting.RED + "[YTChat] "
108+
+ EnumChatFormatting.GREEN + "Message Posted.", sender);
109+
service.postMessage(message.toString(), id);
110+
} else {
98111
showUsage(sender);
99-
}
100112
}
101113
}
102114

115+
103116
private void showUsage(ICommandSender sender) {
104117
showMessage("Usage: " + getCommandUsage(sender), sender);
105118
}
@@ -108,9 +121,13 @@ private void showMessage(String message, ICommandSender sender) {
108121
sender.addChatMessage(new ChatComponentText(message));
109122
}
110123

111-
private void showStreamMessage(LiveChatMessageAuthorDetails author, String message, ICommandSender sender) {
112-
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "[YTChat] " + (author.getIsChatModerator() ? EnumChatFormatting.BLUE : EnumChatFormatting.WHITE) +
113-
(author.getIsChatOwner() ? EnumChatFormatting.GOLD : EnumChatFormatting.WHITE) + author.getDisplayName() + EnumChatFormatting.WHITE + ": " + message));
124+
private void showStreamMessage(LiveChatMessageAuthorDetails author, String message, String id, ICommandSender sender) {
125+
ChatStyle chatStyle = new ChatStyle();
126+
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "[YTChat] " + (author.getIsChatModerator() ? EnumChatFormatting.BLUE : EnumChatFormatting.WHITE) +
127+
(author.getIsChatOwner() ? EnumChatFormatting.GOLD : EnumChatFormatting.WHITE) + author.getDisplayName() + EnumChatFormatting.WHITE + ": " + message)
128+
.setChatStyle(chatStyle.setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "del " + id)))
129+
.setChatStyle(chatStyle.setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(EnumChatFormatting.RED + "Click to delete this message!"))))
130+
);
114131
}
115132

116133
@Override
@@ -129,10 +146,11 @@ public boolean isUsernameIndex(String[] args, int index) {
129146
public void onMessageReceived(
130147
LiveChatMessageAuthorDetails author,
131148
LiveChatSuperChatDetails superChatDetails,
149+
String id,
132150
String message) {
133151

134152
if(!YouTubeConfiguration.getInstance().getSuperOnly()) {
135-
showStreamMessage(author, message, Minecraft.getMinecraft().thePlayer);
153+
showStreamMessage(author, message, id, Minecraft.getMinecraft().thePlayer);
136154
}
137155

138156
if (superChatDetails != null

0 commit comments

Comments
 (0)