Skip to content

Commit

Permalink
Added Post/Delete message commands, more colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mason Frey authored and Mason Frey committed Jul 11, 2017
1 parent eb99e9d commit e6c8917
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
14 changes: 8 additions & 6 deletions src/main/java/com/google/youtube/gaming/chat/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -199,7 +200,7 @@ public void postMessage(final String message, final Consumer<String> 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();
}
});
Expand All @@ -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();
}
Expand Down Expand Up @@ -258,6 +259,7 @@ public void run() {
broadcastMessage(
message.getAuthorDetails(),
snippet.getSuperChatDetails(),
message.getId(),
snippet.getDisplayMessage());
}
System.out.println("POLL DELAY: " + response.getPollingIntervalMillis());
Expand All @@ -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);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/google/youtube/gaming/chat/YouTubeChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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";

Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,6 +35,7 @@ interface YouTubeChatMessageListener {
void onMessageReceived(
LiveChatMessageAuthorDetails author,
LiveChatSuperChatDetails superChatDetails,
String id,
String message);
}

Expand Down
42 changes: 30 additions & 12 deletions src/main/java/com/google/youtube/gaming/chat/YouTubeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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<String> 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);
}
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e6c8917

Please sign in to comment.