Only sending join/leave events to custom chats

Also sending server start/stop messages still
#57
I need to look over this freshly
This commit is contained in:
Norbi Peti 2018-07-22 00:26:22 +02:00
parent 4f6612c21b
commit d22b6dec80
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
4 changed files with 39 additions and 13 deletions

View file

@ -57,6 +57,16 @@ public class ChromaBot {
MCChatListener.forAllMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message, embed)); MCChatListener.forAllMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message, embed));
} }
/**
* Send a message to the chat channels, private chats and custom chats.
*
* @param message The message to send, duh
* @param embed Custom fancy stuff, use {@link EmbedBuilder} to create one
*/
public void sendMessageCustomAsWell(String message, EmbedObject embed) {
MCChatListener.forCustomAndAllMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message, embed));
}
/** /**
* Send a message to an arbitrary channel. This will not send it to the private chats. * Send a message to an arbitrary channel. This will not send it to the private chats.
* *

View file

@ -158,17 +158,17 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
DiscordCommandBase.registerCommands(); DiscordCommandBase.registerCommands();
if (ResetMCCommand.resetting) if (ResetMCCommand.resetting)
ChromaBot.getInstance().sendMessage("", new EmbedBuilder().withColor(Color.CYAN) ChromaBot.getInstance().sendMessageCustomAsWell("", new EmbedBuilder().withColor(Color.CYAN)
.withTitle("Discord plugin restarted - chat connected.").build()); //Really important to note the chat, hmm .withTitle("Discord plugin restarted - chat connected.").build()); //Really important to note the chat, hmm
else if (getConfig().getBoolean("serverup", false)) { else if (getConfig().getBoolean("serverup", false)) {
ChromaBot.getInstance().sendMessage("", new EmbedBuilder().withColor(Color.YELLOW) ChromaBot.getInstance().sendMessageCustomAsWell("", new EmbedBuilder().withColor(Color.YELLOW)
.withTitle("Server recovered from a crash - chat connected.").build()); .withTitle("Server recovered from a crash - chat connected.").build());
val thr = new Throwable( val thr = new Throwable(
"The server shut down unexpectedly. See the log of the previous run for more details."); "The server shut down unexpectedly. See the log of the previous run for more details.");
thr.setStackTrace(new StackTraceElement[0]); thr.setStackTrace(new StackTraceElement[0]);
TBMCCoreAPI.SendException("The server crashed!", thr); TBMCCoreAPI.SendException("The server crashed!", thr);
} else } else
ChromaBot.getInstance().sendMessage("", new EmbedBuilder().withColor(Color.GREEN) ChromaBot.getInstance().sendMessageCustomAsWell("", new EmbedBuilder().withColor(Color.GREEN)
.withTitle("Server started - chat connected.").build()); .withTitle("Server started - chat connected.").build());
ResetMCCommand.resetting = false; //This is the last event handling this flag ResetMCCommand.resetting = false; //This is the last event handling this flag
@ -261,7 +261,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
} }
saveConfig(); saveConfig();
MCChatListener.forAllMCChat(ch -> { MCChatListener.forCustomAndAllMCChat(ch -> {
try { try {
if (ResetMCCommand.resetting) if (ResetMCCommand.resetting)
DiscordPlugin.sendMessageToChannelWait(ch, "", DiscordPlugin.sendMessageToChannelWait(ch, "",

View file

@ -2,10 +2,7 @@ package buttondevteam.discordplugin.listeners;
import buttondevteam.discordplugin.*; import buttondevteam.discordplugin.*;
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener; import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
import buttondevteam.lib.TBMCChatEvent; import buttondevteam.lib.*;
import buttondevteam.lib.TBMCChatPreprocessEvent;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.ChatMessage; import buttondevteam.lib.chat.ChatMessage;
import buttondevteam.lib.chat.ChatRoom; import buttondevteam.lib.chat.ChatRoom;
@ -17,6 +14,7 @@ import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.val; import lombok.val;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -255,8 +253,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
// The maps may not contain the senders for UnconnectedSenders // The maps may not contain the senders for UnconnectedSenders
public static boolean isMinecraftChatEnabled(DiscordPlayer dp) { public static boolean isMinecraftChatEnabled(DiscordPlayer dp) {
return lastmsgPerUser.stream().anyMatch( return isMinecraftChatEnabled(dp.getDiscordID());
lmd -> ((IPrivateChannel) lmd.channel).getRecipient().getStringID().equals(dp.getDiscordID()));
} }
public static boolean isMinecraftChatEnabled(String did) { // Don't load the player data just for this public static boolean isMinecraftChatEnabled(String did) { // Don't load the player data just for this
@ -330,9 +327,22 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
action.accept(DiscordPlugin.chatchannel); action.accept(DiscordPlugin.chatchannel);
for (LastMsgData data : lastmsgPerUser) for (LastMsgData data : lastmsgPerUser)
action.accept(data.channel); action.accept(data.channel);
// lastmsgCustom.forEach(cc -> action.accept(cc.channel)); - Only send relevant messages to custom chat
}
public static void forCustomAndAllMCChat(Consumer<IChannel> action) {
forAllMCChat(action);
lastmsgCustom.forEach(cc -> action.accept(cc.channel)); lastmsgCustom.forEach(cc -> action.accept(cc.channel));
} }
public static void forAllowedCustomMCChat(Consumer<IChannel> action, CommandSender sender) {
lastmsgCustom.stream().filter(clmd -> {
//new TBMCChannelConnectFakeEvent(sender, clmd.mcchannel).shouldSendTo(clmd.dcp) - Thought it was this simple hehe - Wait, it *should* be this simple
val e = new TBMCChannelConnectFakeEvent(sender, clmd.mcchannel);
return clmd.groupID.equals(e.getGroupID(sender));
}).forEach(cc -> action.accept(cc.channel)); //TODO: Use getScore and getGroupID in fake event constructor - This should also send error messages on channel connect
}
private static void forAllowedMCChat(Consumer<IChannel> action, TBMCSystemChatEvent event) { private static void forAllowedMCChat(Consumer<IChannel> action, TBMCSystemChatEvent event) {
if (Channel.GlobalChat.ID.equals(event.getChannel().ID)) if (Channel.GlobalChat.ID.equals(event.getChannel().ID))
action.accept(DiscordPlugin.chatchannel); action.accept(DiscordPlugin.chatchannel);

View file

@ -62,8 +62,10 @@ public class MCListener implements Listener {
+ " do /discord accept"); + " do /discord accept");
p.sendMessage("§bIf it wasn't you, do /discord decline"); p.sendMessage("§bIf it wasn't you, do /discord decline");
} }
final String message = e.GetPlayer().PlayerName().get() + " joined the game";
if (!DiscordPlugin.hooked) if (!DiscordPlugin.hooked)
MCChatListener.sendSystemMessageToChat(e.GetPlayer().PlayerName().get() + " joined the game"); MCChatListener.sendSystemMessageToChat(message);
MCChatListener.forAllowedCustomMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message), e.getPlayer());
MCChatListener.ListC = 0; MCChatListener.ListC = 0;
ChromaBot.getInstance().updatePlayerList(); ChromaBot.getInstance().updatePlayerList();
}); });
@ -81,6 +83,10 @@ public class MCListener implements Listener {
.ifPresent(dcp -> callEventExcludingSome(new PlayerJoinEvent(dcp, "")))); .ifPresent(dcp -> callEventExcludingSome(new PlayerJoinEvent(dcp, ""))));
Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin, Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin,
ChromaBot.getInstance()::updatePlayerList, 5); ChromaBot.getInstance()::updatePlayerList, 5);
final String message = e.GetPlayer().PlayerName().get() + " left the game";
if (!DiscordPlugin.hooked)
MCChatListener.sendSystemMessageToChat(message); //TODO: Probably double sends if kicked and unhooked
MCChatListener.forAllowedCustomMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message), e.getPlayer());
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
@ -111,7 +117,7 @@ public class MCListener implements Listener {
} }
@EventHandler @EventHandler
public void onPlayerAFK(AfkStatusChangeEvent e) { public void onPlayerAFK(AfkStatusChangeEvent e) { //TODO: Add AFK to custom chats?
if (e.isCancelled() || !e.getAffected().getBase().isOnline()) if (e.isCancelled() || !e.getAffected().getBase().isOnline())
return; return;
MCChatListener.sendSystemMessageToChat(DPUtils.sanitizeString(e.getAffected().getBase().getDisplayName()) MCChatListener.sendSystemMessageToChat(DPUtils.sanitizeString(e.getAffected().getBase().getDisplayName())