From e6c8917bd1af87feac1c3409fc0799097433845e Mon Sep 17 00:00:00 2001 From: Mason Frey Date: Tue, 11 Jul 2017 14:50:09 -0400 Subject: [PATCH] Added Post/Delete message commands, more colors --- .../youtube/gaming/chat/ChatService.java | 14 ++++--- .../youtube/gaming/chat/YouTubeChat.java | 6 ++- .../youtube/gaming/chat/YouTubeChatMock.java | 2 +- .../gaming/chat/YouTubeChatService.java | 2 + .../youtube/gaming/chat/YouTubeCommand.java | 42 +++++++++++++------ 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/google/youtube/gaming/chat/ChatService.java b/src/main/java/com/google/youtube/gaming/chat/ChatService.java index 3fb73bd..eaf12dd 100755 --- a/src/main/java/com/google/youtube/gaming/chat/ChatService.java +++ b/src/main/java/com/google/youtube/gaming/chat/ChatService.java @@ -23,6 +23,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.command.ICommandSender; import net.minecraft.util.ChatComponentText; +import net.minecraft.util.EnumChatFormatting; import java.util.ArrayList; import java.util.List; @@ -128,7 +129,7 @@ public void start( } else { nextPoll = System.currentTimeMillis() + response.getPollingIntervalMillis(); } - showMessage("YTC Service started", sender); + showMessage(EnumChatFormatting.RED + "[YTChat] " + EnumChatFormatting.GREEN + " Service started", sender); } catch (Throwable t) { showMessage(t.getMessage(), sender); t.printStackTrace(); @@ -144,7 +145,7 @@ public void stop(ICommandSender sender) { } liveChatId = null; isInitialized = false; - showMessage("YTC Service stopped", sender); + showMessage(EnumChatFormatting.RED + "[YTChat] " + EnumChatFormatting.DARK_RED + "Service stopped", sender); } @Override @@ -199,7 +200,7 @@ public void postMessage(final String message, final Consumer onComplete) onComplete.accept(response.getId()); } catch (Throwable t) { onComplete.accept(null); - showMessage(t.getMessage(), Minecraft.getMinecraft().thePlayer.getCommandSenderEntity()); + showMessage(t.getMessage(), Minecraft.getMinecraft().thePlayer); t.printStackTrace(); } }); @@ -219,7 +220,7 @@ public void deleteMessage(final String messageId, final Runnable onComplete) { liveChatDelete.execute(); onComplete.run(); } catch (Throwable t) { - showMessage(t.getMessage(), Minecraft.getMinecraft().thePlayer.getCommandSenderEntity()); + showMessage(t.getMessage(), Minecraft.getMinecraft().thePlayer); t.printStackTrace(); onComplete.run(); } @@ -258,6 +259,7 @@ public void run() { broadcastMessage( message.getAuthorDetails(), snippet.getSuperChatDetails(), + message.getId(), snippet.getDisplayMessage()); } System.out.println("POLL DELAY: " + response.getPollingIntervalMillis()); @@ -272,10 +274,10 @@ public void run() { } void broadcastMessage( - LiveChatMessageAuthorDetails author, LiveChatSuperChatDetails details, String message) { + LiveChatMessageAuthorDetails author, LiveChatSuperChatDetails details, String id, String message) { for (YouTubeChatMessageListener listener : new ArrayList<>(listeners)) { - listener.onMessageReceived(author, details, message); + listener.onMessageReceived(author, details, id , message); } } diff --git a/src/main/java/com/google/youtube/gaming/chat/YouTubeChat.java b/src/main/java/com/google/youtube/gaming/chat/YouTubeChat.java index a492ea5..7e3e846 100755 --- a/src/main/java/com/google/youtube/gaming/chat/YouTubeChat.java +++ b/src/main/java/com/google/youtube/gaming/chat/YouTubeChat.java @@ -20,6 +20,7 @@ import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import tv.twitch.chat.Chat; /** * Main entry point for YouTube Chat. Provides the chat service API to other mods, e.g. @@ -30,7 +31,7 @@ public class YouTubeChat { public static final String MODID = "ytchat"; public static final String APPNAME = "YouTube Chat"; - public static final String VERSION = "1.2.1_1.8.9"; + public static final String VERSION = "1.3.0_1.8.9"; public static final String GUI_FACTORY = "com.google.youtube.gaming.chat.YouTubeConfigurationGuiFactory"; @@ -48,7 +49,8 @@ public static synchronized YouTubeChatService getService() { public void preInit(FMLPreInitializationEvent event) { YouTubeConfiguration.initialize(event.getSuggestedConfigurationFile()); - ClientCommandHandler.instance.registerCommand(new YouTubeCommand(new ChatService())); + ClientCommandHandler.instance.registerCommand(new YouTubeCommand((ChatService) getService())); ClientCommandHandler.instance.registerCommand(new YouTubeChatMock()); + ClientCommandHandler.instance.registerCommand(new YouTubeDeleteCommand((ChatService) getService())); } } diff --git a/src/main/java/com/google/youtube/gaming/chat/YouTubeChatMock.java b/src/main/java/com/google/youtube/gaming/chat/YouTubeChatMock.java index 62a2622..59d7378 100755 --- a/src/main/java/com/google/youtube/gaming/chat/YouTubeChatMock.java +++ b/src/main/java/com/google/youtube/gaming/chat/YouTubeChatMock.java @@ -77,7 +77,7 @@ public void processCommand(ICommandSender sender, String[] args) throws CommandE authorDetails.setDisplayName(author); authorDetails.setChannelId(author); ((ChatService) YouTubeChat.getService()).broadcastMessage( - authorDetails, new LiveChatSuperChatDetails(), message); + authorDetails, new LiveChatSuperChatDetails(), "MOCK" , message); } @Override diff --git a/src/main/java/com/google/youtube/gaming/chat/YouTubeChatService.java b/src/main/java/com/google/youtube/gaming/chat/YouTubeChatService.java index 7d9b9fc..6f71844 100755 --- a/src/main/java/com/google/youtube/gaming/chat/YouTubeChatService.java +++ b/src/main/java/com/google/youtube/gaming/chat/YouTubeChatService.java @@ -16,6 +16,7 @@ package com.google.youtube.gaming.chat; +import com.google.api.services.youtube.model.LiveChatMessage; import com.google.api.services.youtube.model.LiveChatMessageAuthorDetails; import com.google.api.services.youtube.model.LiveChatSuperChatDetails; @@ -34,6 +35,7 @@ interface YouTubeChatMessageListener { void onMessageReceived( LiveChatMessageAuthorDetails author, LiveChatSuperChatDetails superChatDetails, + String id, String message); } diff --git a/src/main/java/com/google/youtube/gaming/chat/YouTubeCommand.java b/src/main/java/com/google/youtube/gaming/chat/YouTubeCommand.java index b9909ed..e0ce3ec 100755 --- a/src/main/java/com/google/youtube/gaming/chat/YouTubeCommand.java +++ b/src/main/java/com/google/youtube/gaming/chat/YouTubeCommand.java @@ -16,18 +16,21 @@ package com.google.youtube.gaming.chat; -import com.google.api.services.youtube.model.LiveChatMessageAuthorDetails; -import com.google.api.services.youtube.model.LiveChatSuperChatDetails; +import com.google.api.services.youtube.model.*; import com.google.youtube.gaming.chat.YouTubeChatService.YouTubeChatMessageListener; import net.minecraft.client.Minecraft; import net.minecraft.command.CommandBase; import net.minecraft.command.ICommandSender; +import net.minecraft.event.ClickEvent; +import net.minecraft.event.HoverEvent; import net.minecraft.util.ChatComponentText; +import net.minecraft.util.ChatStyle; import net.minecraft.util.EnumChatFormatting; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; /** * An in-game command for managing the YouTube Chat service. Usage: @@ -49,7 +52,7 @@ public String getCommandName() { @Override public String getCommandUsage(ICommandSender sender) { - return getCommandName() + " [start|stop|logout|echoStart|echoStop]"; + return getCommandName() + " [start|stop|logout|echoStart|echoStop|post"; } @Override @@ -84,22 +87,32 @@ public void processCommand(ICommandSender sender, String[] args) { } catch (IOException e) { showMessage(e.getMessage(), sender); } - } else { - if (args[0].equalsIgnoreCase("echoStart")) { + } else if (args[0].equalsIgnoreCase("echoStart")) + { if (!service.isInitialized()) { showMessage("Service is not initialized", sender); showUsage(sender); } else { service.subscribe(this); } - } else if (args[0].equalsIgnoreCase("echoStop")) { + } else if (args[0].equalsIgnoreCase("echoStop")) { service.unsubscribe(this); - } else { + } else if(args[0].equalsIgnoreCase("post")) { + StringBuilder message = new StringBuilder(); + for (String arg: args) { + if(!arg.equalsIgnoreCase("post")) { + message.append(arg).append(" "); + } + } + Consumer id = i -> showMessage(EnumChatFormatting.RED + "[YTChat] " + + EnumChatFormatting.GREEN + "Message Posted.", sender); + service.postMessage(message.toString(), id); + } else { showUsage(sender); - } } } + private void showUsage(ICommandSender sender) { showMessage("Usage: " + getCommandUsage(sender), sender); } @@ -108,9 +121,13 @@ private void showMessage(String message, ICommandSender sender) { sender.addChatMessage(new ChatComponentText(message)); } - private void showStreamMessage(LiveChatMessageAuthorDetails author, String message, ICommandSender sender) { - sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "[YTChat] " + (author.getIsChatModerator() ? EnumChatFormatting.BLUE : EnumChatFormatting.WHITE) + - (author.getIsChatOwner() ? EnumChatFormatting.GOLD : EnumChatFormatting.WHITE) + author.getDisplayName() + EnumChatFormatting.WHITE + ": " + message)); + private void showStreamMessage(LiveChatMessageAuthorDetails author, String message, String id, ICommandSender sender) { + ChatStyle chatStyle = new ChatStyle(); + sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "[YTChat] " + (author.getIsChatModerator() ? EnumChatFormatting.BLUE : EnumChatFormatting.WHITE) + + (author.getIsChatOwner() ? EnumChatFormatting.GOLD : EnumChatFormatting.WHITE) + author.getDisplayName() + EnumChatFormatting.WHITE + ": " + message) + .setChatStyle(chatStyle.setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "del " + id))) + .setChatStyle(chatStyle.setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(EnumChatFormatting.RED + "Click to delete this message!")))) + ); } @Override @@ -129,10 +146,11 @@ public boolean isUsernameIndex(String[] args, int index) { public void onMessageReceived( LiveChatMessageAuthorDetails author, LiveChatSuperChatDetails superChatDetails, + String id, String message) { if(!YouTubeConfiguration.getInstance().getSuperOnly()) { - showStreamMessage(author, message, Minecraft.getMinecraft().thePlayer); + showStreamMessage(author, message, id, Minecraft.getMinecraft().thePlayer); } if (superChatDetails != null