Merge pull request #84 from TBMCPlugins/dev

Channelcon toggles, bunch of fixes and improvements and stuff
This commit is contained in:
Norbi Peti 2018-11-04 01:43:20 +01:00 committed by GitHub
commit cf35745e27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 274 additions and 200 deletions

View file

@ -94,6 +94,13 @@
<goals> <goal>testDelombok</goal> </goals> <configuration> <addOutputDirectory>false</addOutputDirectory> <goals> <goal>testDelombok</goal> </goals> <configuration> <addOutputDirectory>false</addOutputDirectory>
<sourceDirectory>src/test/java</sourceDirectory> </configuration> </execution> <sourceDirectory>src/test/java</sourceDirectory> </configuration> </execution>
</executions> </plugin> --> </executions> </plugin> -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false
</useSystemClassLoader> <!-- https://stackoverflow.com/a/53012553/2703239 -->
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>

View file

@ -0,0 +1,15 @@
package buttondevteam.discordplugin;
public enum ChannelconBroadcast {
JOINLEAVE,
AFK,
RESTART,
DEATH,
BROADCAST;
public final int flag;
ChannelconBroadcast() {
this.flag = 1 << this.ordinal();
}
}

View file

@ -9,6 +9,7 @@ import sx.blah.discord.api.internal.json.objects.EmbedObject;
import sx.blah.discord.handle.obj.IChannel; import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.util.EmbedBuilder; import sx.blah.discord.util.EmbedBuilder;
import javax.annotation.Nullable;
import java.awt.*; import java.awt.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -62,9 +63,10 @@ public class ChromaBot {
* *
* @param message The message to send, duh * @param message The message to send, duh
* @param embed Custom fancy stuff, use {@link EmbedBuilder} to create one * @param embed Custom fancy stuff, use {@link EmbedBuilder} to create one
* @param toggle The toggle type for channelcon
*/ */
public void sendMessageCustomAsWell(String message, EmbedObject embed) { public void sendMessageCustomAsWell(String message, EmbedObject embed, @Nullable ChannelconBroadcast toggle) {
MCChatListener.forCustomAndAllMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message, embed)); MCChatListener.forCustomAndAllMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message, embed), toggle, false);
} }
/** /**

View file

@ -38,6 +38,7 @@ import java.awt.*;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@ -149,30 +150,30 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
val mcch = Channel.getChannels().stream().filter(ch -> ch.ID.equals(chcon.getString("mcchid"))).findAny(); val mcch = Channel.getChannels().stream().filter(ch -> ch.ID.equals(chcon.getString("mcchid"))).findAny();
val ch = dc.getChannelByID(chcon.getLong("chid")); val ch = dc.getChannelByID(chcon.getLong("chid"));
val did = chcon.getLong("did"); val did = chcon.getLong("did");
val dp = DiscordPlayer.getUser(Long.toString(did), DiscordPlayer.class);
val user = dc.fetchUser(did); val user = dc.fetchUser(did);
val dcp = new DiscordConnectedPlayer(user, ch, UUID.fromString(chcon.getString("mcuid")), chcon.getString("mcname")); val dcp = new DiscordConnectedPlayer(user, ch, UUID.fromString(chcon.getString("mcuid")), chcon.getString("mcname"));
val groupid = chcon.getString("groupid"); val groupid = chcon.getString("groupid");
val toggles = chcon.getInt("toggles");
if (!mcch.isPresent() || ch == null || user == null || groupid == null) if (!mcch.isPresent() || ch == null || user == null || groupid == null)
continue; continue;
MCChatListener.addCustomChat(ch, groupid, mcch.get(), dp, user, dcp); MCChatListener.addCustomChat(ch, groupid, mcch.get(), user, dcp, toggles);
} }
} }
DiscordCommandBase.registerCommands(); DiscordCommandBase.registerCommands();
if (ResetMCCommand.resetting) if (ResetMCCommand.resetting)
ChromaBot.getInstance().sendMessageCustomAsWell("", 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(), ChannelconBroadcast.RESTART); //Really important to note the chat, hmm
else if (getConfig().getBoolean("serverup", false)) { else if (getConfig().getBoolean("serverup", false)) {
ChromaBot.getInstance().sendMessageCustomAsWell("", 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(), ChannelconBroadcast.RESTART);
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().sendMessageCustomAsWell("", 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(), ChannelconBroadcast.RESTART);
ResetMCCommand.resetting = false; //This is the last event handling this flag ResetMCCommand.resetting = false; //This is the last event handling this flag
@ -220,6 +221,12 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this); TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class); TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
TBMCCoreAPI.RegisterUserClass(DiscordPlayer.class); TBMCCoreAPI.RegisterUserClass(DiscordPlayer.class);
ChromaGamerBase.addConverter(sender -> {
//System.out.println("Discord converter queried: "+sender+" "+sender.getName()); - TODO: Remove
//System.out.println(((DiscordSenderBase) sender).getChromaUser().channel().get().ID); //TODO: TMP
return Optional.ofNullable(sender instanceof DiscordSenderBase
? ((DiscordSenderBase) sender).getChromaUser() : null);
});
new Thread(this::AnnouncementGetterThreadMethod).start(); new Thread(this::AnnouncementGetterThreadMethod).start();
setupProviders(); setupProviders();
} catch (Exception e) { } catch (Exception e) {
@ -261,31 +268,33 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
chconc.set("mcuid", chcon.dcp.getUniqueId().toString()); chconc.set("mcuid", chcon.dcp.getUniqueId().toString());
chconc.set("mcname", chcon.dcp.getName()); chconc.set("mcname", chcon.dcp.getName());
chconc.set("groupid", chcon.groupID); chconc.set("groupid", chcon.groupID);
chconc.set("toggles", chcon.toggles);
} }
saveConfig(); saveConfig();
EmbedObject embed;
if (ResetMCCommand.resetting)
embed = new EmbedBuilder().withColor(Color.ORANGE).withTitle("Discord plugin restarting").build();
else
embed = new EmbedBuilder().withColor(Restart ? Color.ORANGE : Color.RED)
.withTitle(Restart ? "Server restarting" : "Server stopping")
.withDescription(
Bukkit.getOnlinePlayers().size() > 0
? (DPUtils
.sanitizeString(Bukkit.getOnlinePlayers().stream()
.map(Player::getDisplayName).collect(Collectors.joining(", ")))
+ (Bukkit.getOnlinePlayers().size() == 1 ? " was " : " were ")
+ "kicked the hell out.") //TODO: Make configurable
: "") //If 'restart' is disabled then this isn't shown even if joinleave is enabled
.build();
MCChatListener.forCustomAndAllMCChat(ch -> { MCChatListener.forCustomAndAllMCChat(ch -> {
try { try {
if (ResetMCCommand.resetting)
DiscordPlugin.sendMessageToChannelWait(ch, "", DiscordPlugin.sendMessageToChannelWait(ch, "",
new EmbedBuilder().withColor(Color.ORANGE).withTitle("Discord plugin restarting").build()); embed, 5, TimeUnit.SECONDS);
else
DiscordPlugin.sendMessageToChannelWait(ch, "",
new EmbedBuilder().withColor(Restart ? Color.ORANGE : Color.RED)
.withTitle(Restart ? "Server restarting" : "Server stopping")
.withDescription(
Bukkit.getOnlinePlayers().size() > 0
? (DPUtils
.sanitizeString(Bukkit.getOnlinePlayers().stream()
.map(Player::getDisplayName).collect(Collectors.joining(", ")))
+ (Bukkit.getOnlinePlayers().size() == 1 ? " was " : " were ")
+ "asked *politely* to leave the server for a bit.")
: "")
.build(), 5, TimeUnit.SECONDS);
} catch (TimeoutException | InterruptedException e) { } catch (TimeoutException | InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
}); }, ChannelconBroadcast.RESTART, false);
ChromaBot.getInstance().updatePlayerList(); ChromaBot.getInstance().updatePlayerList();
try { try {
SafeMode = true; // Stop interacting with Discord SafeMode = true; // Stop interacting with Discord
@ -415,10 +424,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
.warning("Message was too long to send to discord and got truncated. In " + channel.getName()); .warning("Message was too long to send to discord and got truncated. In " + channel.getName());
} }
try { try {
if (channel == chatchannel) MCChatListener.resetLastMessage(channel); // If this is a chat message, it'll be set again
MCChatListener.resetLastMessage(); // If this is a chat message, it'll be set again
else if (channel.isPrivate())
MCChatListener.resetLastMessage(channel);
final String content = message; final String content = message;
RequestBuffer.IRequest<IMessage> r = () -> embed == null ? channel.sendMessage(content) RequestBuffer.IRequest<IMessage> r = () -> embed == null ? channel.sendMessage(content)
: channel.sendMessage(content, embed, false); : channel.sendMessage(content, embed, false);

View file

@ -16,10 +16,11 @@ import java.util.Set;
public class DiscordSender extends DiscordSenderBase implements CommandSender { public class DiscordSender extends DiscordSenderBase implements CommandSender {
private PermissibleBase perm = new PermissibleBase(this); private PermissibleBase perm = new PermissibleBase(this);
private String name = null; private String name;
public DiscordSender(IUser user, IChannel channel) { public DiscordSender(IUser user, IChannel channel) {
super(user, channel); super(user, channel);
name = user == null ? "Discord user" : user.getDisplayName(DiscordPlugin.mainServer);
} }
public DiscordSender(IUser user, IChannel channel, String name) { public DiscordSender(IUser user, IChannel channel, String name) {
@ -85,12 +86,12 @@ public class DiscordSender extends DiscordSenderBase implements CommandSender {
} }
@Override @Override
public boolean isOp() { // TODO: Connect with TBMC acc public boolean isOp() {
return false; return false;
} }
@Override @Override
public void setOp(boolean value) { // TODO: Connect with TBMC acc public void setOp(boolean value) {
} }
@Override @Override
@ -100,7 +101,7 @@ public class DiscordSender extends DiscordSenderBase implements CommandSender {
@Override @Override
public String getName() { public String getName() {
return name == null ? user == null ? "Discord user" : user.getDisplayName(DiscordPlugin.mainServer) : name; return name;
} }
@Override @Override

View file

@ -1,26 +1,18 @@
package buttondevteam.discordplugin; package buttondevteam.discordplugin;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.IDiscordSender; import buttondevteam.lib.chat.IDiscordSender;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import sx.blah.discord.handle.obj.IChannel; import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IUser; import sx.blah.discord.handle.obj.IUser;
import java.util.Arrays;
import java.util.stream.Collectors;
public abstract class DiscordSenderBase implements IDiscordSender { public abstract class DiscordSenderBase implements IDiscordSender {
/** /**
* May be null. * May be null.
*/ */
protected IUser user; protected IUser user;
protected IChannel channel; protected IChannel channel;
private @Getter @Setter @NonNull Channel mcchannel = Channel.GlobalChat;
protected DiscordSenderBase(IUser user, IChannel channel) { protected DiscordSenderBase(IUser user, IChannel channel) {
this.user = user; this.user = user;
@ -43,6 +35,18 @@ public abstract class DiscordSenderBase implements IDiscordSender {
return channel; return channel;
} }
private DiscordPlayer chromaUser;
/**
* Loads the user data on first query.
*
* @return A Chroma user of Discord or a Discord user of Chroma
*/
public DiscordPlayer getChromaUser() {
if (chromaUser == null) chromaUser = DiscordPlayer.getUser(user.getStringID(), DiscordPlayer.class);
return chromaUser;
}
@Override @Override
public void sendMessage(String message) { public void sendMessage(String message) {
try { try {
@ -65,6 +69,6 @@ public abstract class DiscordSenderBase implements IDiscordSender {
@Override @Override
public void sendMessage(String[] messages) { public void sendMessage(String[] messages) {
sendMessage(Arrays.stream(messages).collect(Collectors.joining("\n"))); sendMessage(String.join("\n", messages));
} }
} }

View file

@ -30,7 +30,7 @@ public class PlayerListWatcher extends DedicatedPlayerList {
if (packet instanceof PacketPlayOutChat) { if (packet instanceof PacketPlayOutChat) {
Field msgf = PacketPlayOutChat.class.getDeclaredField("a"); Field msgf = PacketPlayOutChat.class.getDeclaredField("a");
msgf.setAccessible(true); msgf.setAccessible(true);
MCChatListener.sendSystemMessageToChat(((IChatBaseComponent) msgf.get(packet)).toPlainText()); MCChatListener.forAllMCChat(MCChatListener.send(((IChatBaseComponent) msgf.get(packet)).toPlainText()));
} }
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Failed to broadcast message sent to all players - hacking failed.", e); TBMCCoreAPI.SendException("Failed to broadcast message sent to all players - hacking failed.", e);

View file

@ -1,9 +1,9 @@
package buttondevteam.discordplugin.commands; package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.ChannelconBroadcast;
import buttondevteam.discordplugin.DiscordConnectedPlayer; import buttondevteam.discordplugin.DiscordConnectedPlayer;
import buttondevteam.discordplugin.DiscordPlayer; import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.listeners.MCChatListener; import buttondevteam.discordplugin.listeners.MCChatListener;
import buttondevteam.lib.TBMCChannelConnectFakeEvent;
import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayer;
import lombok.val; import lombok.val;
@ -13,6 +13,8 @@ import sx.blah.discord.handle.obj.Permissions;
import sx.blah.discord.util.PermissionUtils; import sx.blah.discord.util.PermissionUtils;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class ChannelconCommand extends DiscordCommandBase { public class ChannelconCommand extends DiscordCommandBase {
@Override @Override
@ -29,13 +31,38 @@ public class ChannelconCommand extends DiscordCommandBase {
return true; return true;
} }
if (MCChatListener.hasCustomChat(message.getChannel())) { if (MCChatListener.hasCustomChat(message.getChannel())) {
if (args.equalsIgnoreCase("remove")) { if (args.toLowerCase().startsWith("remove")) {
if (MCChatListener.removeCustomChat(message.getChannel())) if (MCChatListener.removeCustomChat(message.getChannel()))
message.reply("channel connection removed."); message.reply("channel connection removed.");
else else
message.reply("wait what, couldn't remove channel connection."); message.reply("wait what, couldn't remove channel connection.");
return true; return true;
} }
if (args.toLowerCase().startsWith("toggle")) {
val cc = MCChatListener.getCustomChat(message.getChannel());
Supplier<String> togglesString = () -> Arrays.stream(ChannelconBroadcast.values()).map(t -> t.toString().toLowerCase() + ": " + ((cc.toggles & t.flag) == 0 ? "disabled" : "enabled")).collect(Collectors.joining("\n"));
String[] argsa = args.split(" ");
if (argsa.length < 2) {
message.reply("toggles:\n" + togglesString.get());
return true;
}
String arg = argsa[1].toUpperCase();
val b = Arrays.stream(ChannelconBroadcast.values()).filter(t -> t.toString().equals(arg)).findAny();
if (!b.isPresent()) {
message.reply("cannot find toggle. Toggles:\n" + togglesString.get());
return true;
}
//A B | F
//------- A: original - B: mask - F: new
//0 0 | 0
//0 1 | 1
//1 0 | 1
//1 1 | 0
// XOR
cc.toggles ^= b.get().flag;
message.reply("'" + b.get().toString().toLowerCase() + "' " + ((cc.toggles & b.get().flag) == 0 ? "disabled" : "enabled"));
return true;
}
message.reply("this channel is already connected to a Minecraft channel. Use `@ChromaBot channelcon remove` to remove it."); message.reply("this channel is already connected to a Minecraft channel. Use `@ChromaBot channelcon remove` to remove it.");
return true; return true;
} }
@ -51,18 +78,17 @@ public class ChannelconCommand extends DiscordCommandBase {
return true; return true;
} }
DiscordConnectedPlayer dcp = new DiscordConnectedPlayer(message.getAuthor(), message.getChannel(), chp.getUUID(), Bukkit.getOfflinePlayer(chp.getUUID()).getName()); DiscordConnectedPlayer dcp = new DiscordConnectedPlayer(message.getAuthor(), message.getChannel(), chp.getUUID(), Bukkit.getOfflinePlayer(chp.getUUID()).getName());
val ev = new TBMCChannelConnectFakeEvent(dcp, chan.get());
//Using a fake player with no login/logout, should be fine for this event //Using a fake player with no login/logout, should be fine for this event
String groupid = ev.getGroupID(ev.getSender()); //We're not trying to send in a specific group, we want to know which group the user belongs to (so not getGroupID()) String groupid = chan.get().getGroupID(dcp);
if (groupid == null) { if (groupid == null) {
message.reply("sorry, that didn't work. You cannot use that Minecraft channel."); message.reply("sorry, that didn't work. You cannot use that Minecraft channel.");
return true; return true;
} }
if (MCChatListener.getCustomChats().stream().anyMatch(cc -> cc.groupID.equals(groupid) && cc.mcchannel.ID.equals(chan.get().ID))) { /*if (MCChatListener.getCustomChats().stream().anyMatch(cc -> cc.groupID.equals(groupid) && cc.mcchannel.ID.equals(chan.get().ID))) {
message.reply("sorry, this MC chat is already connected to a different channel, multiple channels are not supported atm."); message.reply("sorry, this MC chat is already connected to a different channel, multiple channels are not supported atm.");
return true; return true;
} }*/ //TODO: "Channel admins" that can connect channels?
MCChatListener.addCustomChat(message.getChannel(), groupid, ev.getChannel(), dp, message.getAuthor(), dcp); MCChatListener.addCustomChat(message.getChannel(), groupid, chan.get(), message.getAuthor(), dcp, 0);
message.reply("alright, connection made to group `" + groupid + "`!"); message.reply("alright, connection made to group `" + groupid + "`!");
return true; return true;
} }

View file

@ -59,19 +59,19 @@ public class RoleCommand extends DiscordCommandBase {
DiscordPlugin.sendMessageToChannel(message.getChannel(), usage + "\nUsage: " + argsa[0] + " <rolename>"); DiscordPlugin.sendMessageToChannel(message.getChannel(), usage + "\nUsage: " + argsa[0] + " <rolename>");
return null; return null;
} }
String rolename = argsa[1]; StringBuilder rolename = new StringBuilder(argsa[1]);
for (int i = 2; i < argsa.length; i++) for (int i = 2; i < argsa.length; i++)
rolename += " " + argsa[i]; rolename.append(" ").append(argsa[i]);
if (!DiscordPlugin.GameRoles.contains(rolename)) { if (!DiscordPlugin.GameRoles.contains(rolename.toString())) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "That game role cannot be found."); DiscordPlugin.sendMessageToChannel(message.getChannel(), "That game role cannot be found.");
listRoles(message); listRoles(message);
return null; return null;
} }
final List<IRole> roles = DiscordPlugin.mainServer.getRolesByName(rolename); final List<IRole> roles = DiscordPlugin.mainServer.getRolesByName(rolename.toString());
if (roles.size() == 0) { if (roles.size() == 0) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), DiscordPlugin.sendMessageToChannel(message.getChannel(),
"The specified role cannot be found on Discord! Removing from the list."); "The specified role cannot be found on Discord! Removing from the list.");
DiscordPlugin.GameRoles.remove(rolename); DiscordPlugin.GameRoles.remove(rolename.toString());
return null; return null;
} }
if (roles.size() > 1) { if (roles.size() > 1) {

View file

@ -1,6 +1,5 @@
package buttondevteam.discordplugin.listeners; package buttondevteam.discordplugin.listeners;
import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase; import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
@ -99,8 +98,7 @@ public class CommandListener {
&& !(event.getMessage().getContent().startsWith("/") && !(event.getMessage().getContent().startsWith("/")
&& event.getChannel().getStringID().equals(DiscordPlugin.botchannel.getStringID()))) // && event.getChannel().getStringID().equals(DiscordPlugin.botchannel.getStringID()))) //
return; return;
if (DiscordPlayer.getUser(event.getAuthor().getStringID(), DiscordPlayer.class) if (MCChatListener.isMinecraftChatEnabled(event.getAuthor().toString()))
.isMinecraftChatEnabled())
if (!event.getMessage().getContent().equalsIgnoreCase("mcchat")) if (!event.getMessage().getContent().equalsIgnoreCase("mcchat"))
return; return;
if (event.getMessage().getAuthor().isBot()) if (event.getMessage().getAuthor().isBot())

View file

@ -2,7 +2,10 @@ package buttondevteam.discordplugin.listeners;
import buttondevteam.discordplugin.*; import buttondevteam.discordplugin.*;
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener; import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
import buttondevteam.lib.*; import buttondevteam.lib.TBMCChatEvent;
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;
@ -33,6 +36,7 @@ import sx.blah.discord.util.DiscordException;
import sx.blah.discord.util.EmbedBuilder; import sx.blah.discord.util.EmbedBuilder;
import sx.blah.discord.util.MissingPermissionsException; import sx.blah.discord.util.MissingPermissionsException;
import javax.annotation.Nullable;
import java.awt.*; import java.awt.*;
import java.time.Instant; import java.time.Instant;
import java.util.*; import java.util.*;
@ -124,14 +128,14 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
Predicate<IChannel> isdifferentchannel = ch -> !(e.getSender() instanceof DiscordSenderBase) Predicate<IChannel> isdifferentchannel = ch -> !(e.getSender() instanceof DiscordSenderBase)
|| ((DiscordSenderBase) e.getSender()).getChannel().getLongID() != ch.getLongID(); || ((DiscordSenderBase) e.getSender()).getChannel().getLongID() != ch.getLongID();
if ((e.getChannel() == Channel.GlobalChat || e.getChannel().ID.equals("rp")) if (e.getChannel().isGlobal()
&& (e.isFromcmd() || isdifferentchannel.test(DiscordPlugin.chatchannel))) && (e.isFromcmd() || isdifferentchannel.test(DiscordPlugin.chatchannel)))
doit.accept(lastmsgdata == null doit.accept(lastmsgdata == null
? lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel, null, null) ? lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel, null)
: lastmsgdata); : lastmsgdata);
for (LastMsgData data : lastmsgPerUser) { for (LastMsgData data : lastmsgPerUser) {
if (data.dp.isMinecraftChatEnabled() && (e.isFromcmd() || isdifferentchannel.test(data.channel)) if ((e.isFromcmd() || isdifferentchannel.test(data.channel))
&& e.shouldSendTo(getSender(data.channel, data.user))) && e.shouldSendTo(getSender(data.channel, data.user)))
doit.accept(data); doit.accept(data);
} }
@ -166,20 +170,21 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
public final IChannel channel; public final IChannel channel;
public Channel mcchannel; public Channel mcchannel;
public final IUser user; public final IUser user;
public final DiscordPlayer dp;
} }
public static class CustomLMD extends LastMsgData { public static class CustomLMD extends LastMsgData {
public final String groupID; public final String groupID;
public final Channel mcchannel; public final Channel mcchannel;
public final DiscordConnectedPlayer dcp; public final DiscordConnectedPlayer dcp;
public int toggles;
public CustomLMD(@NonNull IChannel channel, @NonNull IUser user, @NonNull DiscordPlayer dp, private CustomLMD(@NonNull IChannel channel, @NonNull IUser user,
@NonNull String groupid, @NonNull Channel mcchannel, @NonNull DiscordConnectedPlayer dcp) { @NonNull String groupid, @NonNull Channel mcchannel, @NonNull DiscordConnectedPlayer dcp, int toggles) {
super(channel, user, dp); super(channel, user);
groupID = groupid; groupID = groupid;
this.mcchannel = mcchannel; this.mcchannel = mcchannel;
this.dcp = dcp; this.dcp = dcp;
this.toggles = toggles;
} }
} }
@ -238,7 +243,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
if (!start) if (!start)
lastmsgfromd.remove(channel.getLongID()); lastmsgfromd.remove(channel.getLongID());
return start // return start //
? lastmsgPerUser.add(new LastMsgData(channel, user, dp)) // Doesn't support group DMs ? lastmsgPerUser.add(new LastMsgData(channel, user)) // Doesn't support group DMs
: lastmsgPerUser.removeIf(lmd -> lmd.channel.getLongID() == channel.getLongID()); : lastmsgPerUser.removeIf(lmd -> lmd.channel.getLongID() == channel.getLongID());
} }
@ -293,8 +298,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
.anyMatch(lmd -> ((IPrivateChannel) lmd.channel).getRecipient().getStringID().equals(did)); .anyMatch(lmd -> ((IPrivateChannel) lmd.channel).getRecipient().getStringID().equals(did));
} }
public static void addCustomChat(IChannel channel, String groupid, Channel mcchannel, DiscordPlayer dp, IUser user, DiscordConnectedPlayer dcp) { public static void addCustomChat(IChannel channel, String groupid, Channel mcchannel, IUser user, DiscordConnectedPlayer dcp, int toggles) {
val lmd = new CustomLMD(channel, user, dp, groupid, mcchannel, dcp); val lmd = new CustomLMD(channel, user, groupid, mcchannel, dcp, toggles);
lastmsgCustom.add(lmd); lastmsgCustom.add(lmd);
} }
@ -302,9 +307,10 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
return lastmsgCustom.stream().anyMatch(lmd -> lmd.channel.getLongID() == channel.getLongID()); return lastmsgCustom.stream().anyMatch(lmd -> lmd.channel.getLongID() == channel.getLongID());
} }
public static CustomLMD getCustomChat(IChannel channel) { @Nullable
return lastmsgCustom.stream().filter(lmd -> lmd.channel.getLongID() == channel.getLongID()).findAny().orElse(null); public static CustomLMD getCustomChat(IChannel channel) {
} return lastmsgCustom.stream().filter(lmd -> lmd.channel.getLongID() == channel.getLongID()).findAny().orElse(null);
}
public static boolean removeCustomChat(IChannel channel) { public static boolean removeCustomChat(IChannel channel) {
lastmsgfromd.remove(channel.getLongID()); lastmsgfromd.remove(channel.getLongID());
@ -326,33 +332,25 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
public static final HashMap<String, HashMap<IChannel, DiscordPlayerSender>> OnlineSenders = new HashMap<>(); public static final HashMap<String, HashMap<IChannel, DiscordPlayerSender>> OnlineSenders = new HashMap<>();
public static short ListC = 0; public static short ListC = 0;
public static void resetLastMessage() {
(lastmsgdata == null ? lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel, null, null)
: lastmsgdata).message = null;
} // Don't set the whole object to null, the player and channel information should be preserved
public static void resetLastMessage(IChannel channel) {
for (LastMsgData data : lastmsgPerUser)
if (data.channel.getLongID() == channel.getLongID())
data.message = null; // Since only private channels are stored, only those will work anyways
}
public static void resetLastMessageCustom(IChannel channel) {
for (LastMsgData data : lastmsgCustom)
if (data.channel.getLongID() == channel.getLongID())
data.message = null;
}
/** /**
* This overload sends it to the global chat. * Resets the last message, so it will start a new one instead of appending to it.
* This is used when someone (even the bot) sends a message to the channel.
*
* @param channel The channel to reset in - the process is slightly different for the public, private and custom chats
*/ */
public static void sendSystemMessageToChat(String msg) { public static void resetLastMessage(IChannel channel) {
forAllMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, DPUtils.sanitizeString(msg))); if (channel.getLongID() == DiscordPlugin.chatchannel.getLongID()) {
} (lastmsgdata == null ? lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel, null)
: lastmsgdata).message = null;
public static void sendSystemMessageToChat(TBMCSystemChatEvent event) { return;
forAllowedMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, DPUtils.sanitizeString(event.getMessage())), } // Don't set the whole object to null, the player and channel information should be preserved
event); for (LastMsgData data : channel.isPrivate() ? lastmsgPerUser : lastmsgCustom) {
if (data.channel.getLongID() == channel.getLongID()) {
data.message = null;
return;
}
}
//If it gets here, it's sending a message to a non-chat channel
} }
public static void forAllMCChat(Consumer<IChannel> action) { public static void forAllMCChat(Consumer<IChannel> action) {
@ -362,27 +360,70 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
// lastmsgCustom.forEach(cc -> action.accept(cc.channel)); - Only send relevant messages to custom chat // lastmsgCustom.forEach(cc -> action.accept(cc.channel)); - Only send relevant messages to custom chat
} }
public static void forCustomAndAllMCChat(Consumer<IChannel> action) { /**
forAllMCChat(action); * For custom and all MC chat
lastmsgCustom.forEach(cc -> action.accept(cc.channel)); *
* @param action The action to act
* @param toggle The toggle to check
* @param hookmsg Whether the message is also sent from the hook
*/
public static void forCustomAndAllMCChat(Consumer<IChannel> action, @Nullable ChannelconBroadcast toggle, boolean hookmsg) {
if (!DiscordPlugin.hooked || !hookmsg)
forAllMCChat(action);
final Consumer<CustomLMD> customLMDConsumer = cc -> action.accept(cc.channel);
if (toggle == null)
lastmsgCustom.forEach(customLMDConsumer);
else
lastmsgCustom.stream().filter(cc -> (cc.toggles & toggle.flag) != 0).forEach(customLMDConsumer);
} }
public static void forAllowedCustomMCChat(Consumer<IChannel> action, CommandSender sender) { /**
* Do the {@code action} for each custom chat the {@code sender} have access to and has that broadcast type enabled.
*
* @param action The action to do
* @param sender The sender to check perms of or null to send to all that has it toggled
* @param toggle The toggle to check or null to send to all allowed
*/
public static void forAllowedCustomMCChat(Consumer<IChannel> action, @Nullable CommandSender sender, @Nullable ChannelconBroadcast toggle) {
lastmsgCustom.stream().filter(clmd -> { lastmsgCustom.stream().filter(clmd -> {
//new TBMCChannelConnectFakeEvent(sender, clmd.mcchannel).shouldSendTo(clmd.dcp) - Thought it was this simple hehe - Wait, it *should* be this simple //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); if (toggle != null && (clmd.toggles & toggle.flag) == 0)
return clmd.groupID.equals(e.getGroupID(sender)); return false; //If null then allow
}).forEach(cc -> action.accept(cc.channel)); //TODO: Use getScore and getGroupID in fake event constructor - This should also send error messages on channel connect if (sender == null)
return true;
return clmd.groupID.equals(clmd.mcchannel.getGroupID(sender));
}).forEach(cc -> action.accept(cc.channel)); //TODO: Send error messages on channel connect
} }
private static void forAllowedMCChat(Consumer<IChannel> action, TBMCSystemChatEvent event) { /**
if (Channel.GlobalChat.ID.equals(event.getChannel().ID)) * Do the {@code action} for each custom chat the {@code sender} have access to and has that broadcast type enabled.
*
* @param action The action to do
* @param sender The sender to check perms of or null to send to all that has it toggled
* @param toggle The toggle to check or null to send to all allowed
* @param hookmsg Whether the message is also sent from the hook
*/
public static void forAllowedCustomAndAllMCChat(Consumer<IChannel> action, @Nullable CommandSender sender, @Nullable ChannelconBroadcast toggle, boolean hookmsg) {
if (!DiscordPlugin.hooked || !hookmsg)
forAllMCChat(action);
forAllowedCustomMCChat(action, sender, toggle);
}
public static Consumer<IChannel> send(String message) {
return ch -> DiscordPlugin.sendMessageToChannel(ch, DPUtils.sanitizeString(message));
}
public static void forAllowedMCChat(Consumer<IChannel> action, TBMCSystemChatEvent event) {
if (event.getChannel().isGlobal())
action.accept(DiscordPlugin.chatchannel); action.accept(DiscordPlugin.chatchannel);
for (LastMsgData data : lastmsgPerUser) for (LastMsgData data : lastmsgPerUser)
if (event.shouldSendTo(getSender(data.channel, data.user))) if (event.shouldSendTo(getSender(data.channel, data.user)))
action.accept(data.channel); action.accept(data.channel);
lastmsgCustom.stream().filter(data -> event.shouldSendTo(data.dcp)) lastmsgCustom.stream().filter(clmd -> {
.map(data -> data.channel).forEach(action); if ((clmd.toggles & ChannelconBroadcast.BROADCAST.flag) == 0)
return false;
return event.shouldSendTo(clmd.dcp);
}).map(clmd -> clmd.channel).forEach(action);
} }
/** /**
@ -438,12 +479,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
return; // Race condition: If it gets here after it enabled mcchat it says it - I might as well allow disabling with this (CommandListener) return; // Race condition: If it gets here after it enabled mcchat it says it - I might as well allow disabling with this (CommandListener)
if (CommandListener.runCommand(ev.getMessage(), true)) if (CommandListener.runCommand(ev.getMessage(), true))
return; return;
if (!ev.getMessage().getChannel().isPrivate()) resetLastMessage(ev.getChannel());
resetLastMessage();
else if (hasCustomChat)
resetLastMessageCustom(ev.getChannel());
else
resetLastMessage(ev.getMessage().getChannel());
lastlist++; lastlist++;
recevents.add(ev); recevents.add(ev);
if (rectask != null) if (rectask != null)
@ -467,10 +503,10 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
return; return;
} }
val sender = event.getMessage().getAuthor(); val sender = event.getMessage().getAuthor();
val user = DiscordPlayer.getUser(sender.getStringID(), DiscordPlayer.class);
String dmessage = event.getMessage().getContent(); String dmessage = event.getMessage().getContent();
try { try {
final DiscordSenderBase dsender = getSender(event.getMessage().getChannel(), sender); final DiscordSenderBase dsender = getSender(event.getMessage().getChannel(), sender);
val user = dsender.getChromaUser();
for (IUser u : event.getMessage().getMentions()) { for (IUser u : event.getMessage().getMentions()) {
dmessage = dmessage.replace(u.mention(false), "@" + u.getName()); // TODO: IG Formatting dmessage = dmessage.replace(u.mention(false), "@" + u.getName()); // TODO: IG Formatting
@ -486,7 +522,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
Function<String, String> getChatMessage = msg -> // Function<String, String> getChatMessage = msg -> //
msg + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage() msg + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage()
.getAttachments().stream().map(a -> a.getUrl()).collect(Collectors.joining("\n")) .getAttachments().stream().map(IMessage.Attachment::getUrl).collect(Collectors.joining("\n"))
: ""); : "");
CustomLMD clmd = getCustomChat(event.getChannel()); CustomLMD clmd = getCustomChat(event.getChannel());
@ -498,7 +534,6 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
if (!event.getMessage().isDeleted() && !event.getChannel().isPrivate()) if (!event.getMessage().isDeleted() && !event.getChannel().isPrivate())
event.getMessage().delete(); event.getMessage().delete();
}); });
//preprocessChat(dsender, dmessage); - Same is done below
final String cmd = dmessage.substring(1); final String cmd = dmessage.substring(1);
final String cmdlowercased = cmd.toLowerCase(); final String cmdlowercased = cmd.toLowerCase();
if (dsender instanceof DiscordSender && Arrays.stream(UnconnectedCmds) if (dsender instanceof DiscordSender && Arrays.stream(UnconnectedCmds)
@ -530,38 +565,51 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
.filter(c -> c.ID.equalsIgnoreCase(topcmd) .filter(c -> c.ID.equalsIgnoreCase(topcmd)
|| (c.IDs != null && c.IDs.length > 0 || (c.IDs != null && c.IDs.length > 0
&& Arrays.stream(c.IDs).anyMatch(id -> id.equalsIgnoreCase(topcmd)))).findAny(); && Arrays.stream(c.IDs).anyMatch(id -> id.equalsIgnoreCase(topcmd)))).findAny();
if (!ch.isPresent()) if (!ch.isPresent()) //TODO: What if talking in the public chat while we have it on a different one
Bukkit.getScheduler().runTask(DiscordPlugin.plugin, Bukkit.getScheduler().runTask(DiscordPlugin.plugin, //Commands need to be run sync
() -> { () -> { //TODO: Better handling...
val channel = user.channel();
val chtmp = channel.get();
//System.out.println("1: "+chtmp.ID);
//System.out.println("clmd: "+clmd);
if (clmd != null) {
channel.set(clmd.mcchannel); //Hack to send command in the channel
//System.out.println("clmd chan: "+clmd.mcchannel.ID);
} //TODO: Permcheck isn't implemented for commands
//System.out.println("2: "+channel.get().ID);
VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd); VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd);
Bukkit.getLogger().info(dsender.getName() + " issued command from Discord: /" + cmdlowercased); Bukkit.getLogger().info(dsender.getName() + " issued command from Discord: /" + cmdlowercased);
if (clmd != null)
channel.set(chtmp);
//System.out.println("3: "+channel.get().ID); - TODO: Remove
}); });
else { else {
Channel chc = ch.get(); Channel chc = ch.get();
if (!chc.ID.equals(Channel.GlobalChat.ID) && !chc.ID.equals("rp") && !event.getMessage().getChannel().isPrivate()) if (!chc.isGlobal() && !event.getMessage().getChannel().isPrivate())
dsender.sendMessage( dsender.sendMessage(
"You can only talk in global in the public chat. DM `mcchat` to enable private chat to talk in the other channels."); "You can only talk in a public chat here. DM `mcchat` to enable private chat to talk in the other channels.");
else { else {
if (spi == -1) // Switch channels if (spi == -1) // Switch channels
{ {
val oldch = dsender.getMcchannel(); val channel = dsender.getChromaUser().channel();
val oldch = channel.get();
if (oldch instanceof ChatRoom) if (oldch instanceof ChatRoom)
((ChatRoom) oldch).leaveRoom(dsender); ((ChatRoom) oldch).leaveRoom(dsender);
if (!oldch.ID.equals(chc.ID)) { if (!oldch.ID.equals(chc.ID)) {
dsender.setMcchannel(chc); channel.set(chc);
if (chc instanceof ChatRoom) if (chc instanceof ChatRoom)
((ChatRoom) chc).joinRoom(dsender); ((ChatRoom) chc).joinRoom(dsender);
} else } else
dsender.setMcchannel(Channel.GlobalChat); channel.set(Channel.GlobalChat);
dsender.sendMessage("You're now talking in: " dsender.sendMessage("You're now talking in: "
+ DPUtils.sanitizeString(dsender.getMcchannel().DisplayName)); + DPUtils.sanitizeString(channel.get().DisplayName));
} else { // Send single message } else { // Send single message
final String msg = cmd.substring(spi + 1); final String msg = cmd.substring(spi + 1);
val cmb = ChatMessage.builder(chc, dsender, user, getChatMessage.apply(msg)).fromCommand(true); val cmb = ChatMessage.builder(dsender, user, getChatMessage.apply(msg)).fromCommand(true);
if (clmd == null) if (clmd == null)
TBMCChatAPI.SendChatMessage(cmb.build()); TBMCChatAPI.SendChatMessage(cmb.build(), chc);
else else
TBMCChatAPI.SendChatMessage(cmb.permCheck(clmd.dcp).build()); TBMCChatAPI.SendChatMessage(cmb.permCheck(clmd.dcp).build(), chc);
react = true; react = true;
} }
} }
@ -570,14 +618,16 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
lastlistp = (short) Bukkit.getOnlinePlayers().size(); lastlistp = (short) Bukkit.getOnlinePlayers().size();
} else {// Not a command } else {// Not a command
if (dmessage.length() == 0 && event.getMessage().getAttachments().size() == 0 if (dmessage.length() == 0 && event.getMessage().getAttachments().size() == 0
&& !event.getChannel().isPrivate() && event.getMessage().isSystemMessage()) && !event.getChannel().isPrivate() && event.getMessage().isSystemMessage()) {
TBMCChatAPI.SendSystemMessage(Channel.GlobalChat, 0, "everyone", val rtr = clmd != null ? clmd.mcchannel.filteranderrormsg.apply(clmd.dcp) : dsender.getChromaUser().channel().get().filteranderrormsg.apply(dsender);
(dsender instanceof Player ? ((Player) dsender).getDisplayName() TBMCChatAPI.SendSystemMessage(clmd != null ? clmd.mcchannel : dsender.getChromaUser().channel().get(), rtr.score, rtr.groupID,
: dsender.getName()) + " pinned a message on Discord."); (dsender instanceof Player ? ((Player) dsender).getDisplayName()
: dsender.getName()) + " pinned a message on Discord.");
}
else { else {
val cmb = ChatMessage.builder(dsender.getMcchannel(), dsender, user, getChatMessage.apply(dmessage)).fromCommand(false); val cmb = ChatMessage.builder(dsender, user, getChatMessage.apply(dmessage)).fromCommand(false);
if (clmd != null) if (clmd != null)
TBMCChatAPI.SendChatMessage(cmb.channel(clmd.mcchannel).permCheck(clmd.dcp).build()); TBMCChatAPI.SendChatMessage(cmb.permCheck(clmd.dcp).build(), clmd.mcchannel);
else else
TBMCChatAPI.SendChatMessage(cmb.build()); TBMCChatAPI.SendChatMessage(cmb.build());
react = true; react = true;
@ -601,55 +651,17 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
} }
} }
private boolean preprocessChat(DiscordSenderBase dsender, String dmessage) {
if (dmessage.length() < 2)
return false;
int index = dmessage.indexOf(" ");
String cmd;
if (index == -1) { // Only the command is run
cmd = dmessage;
for (Channel channel : Channel.getChannels()) {
if (cmd.equalsIgnoreCase(channel.ID) || (channel.IDs != null && Arrays.stream(channel.IDs).anyMatch(cmd::equalsIgnoreCase))) {
Channel oldch = dsender.getMcchannel();
if (oldch instanceof ChatRoom)
((ChatRoom) oldch).leaveRoom(dsender);
if (oldch.equals(channel))
dsender.setMcchannel(Channel.GlobalChat);
else {
dsender.setMcchannel(channel);
if (channel instanceof ChatRoom)
((ChatRoom) channel).joinRoom(dsender);
}
dsender.sendMessage("You are now talking in: " + dsender.getMcchannel().DisplayName);
return true;
}
}
} else { // We have arguments
cmd = dmessage.substring(0, index);
for (Channel channel : Channel.getChannels()) {
if (cmd.equalsIgnoreCase(channel.ID) || (channel.IDs != null && Arrays.stream(channel.IDs).anyMatch(cmd::equalsIgnoreCase))) {
val dp = DiscordPlayer.getUser(dsender.getUser().getStringID(), DiscordPlayer.class);
TBMCChatAPI.SendChatMessage(ChatMessage.builder(channel, dsender, dp, dmessage.substring(index + 1)).build());
return true;
}
}
// TODO: Target selectors
}
return false;
}
/** /**
* This method will find the best sender to use: if the player is online, use that, if not but connected then use that etc. * This method will find the best sender to use: if the player is online, use that, if not but connected then use that etc.
*/ */
private static DiscordSenderBase getSender(IChannel channel, final IUser author) { private static DiscordSenderBase getSender(IChannel channel, final IUser author) {
val key = author.getStringID(); //noinspection OptionalGetWithoutIsPresent
return Stream.<Supplier<Optional<DiscordSenderBase>>>of( // https://stackoverflow.com/a/28833677/2703239 return Stream.<Supplier<Optional<DiscordSenderBase>>>of( // https://stackoverflow.com/a/28833677/2703239
() -> Optional.ofNullable(getSender(OnlineSenders, channel, author)), // Find first non-null () -> Optional.ofNullable(getSender(OnlineSenders, channel, author)), // Find first non-null
() -> Optional.ofNullable(getSender(ConnectedSenders, channel, author)), // This doesn't support the public chat, but it'll always return null for it () -> Optional.ofNullable(getSender(ConnectedSenders, channel, author)), // This doesn't support the public chat, but it'll always return null for it
() -> Optional.ofNullable(getSender(OnlineSenders, channel, author)), () -> { () -> Optional.ofNullable(getSender(OnlineSenders, channel, author)), //
return Optional.of(addSender(UnconnectedSenders, author, () -> Optional.of(addSender(UnconnectedSenders, author,
new DiscordSender(author, channel))); new DiscordSender(author, channel)))).map(Supplier::get).filter(Optional::isPresent).map(Optional::get).findFirst().get();
}).map(Supplier::get).filter(Optional::isPresent).map(Optional::get).findFirst().get();
} }
@FunctionalInterface @FunctionalInterface

View file

@ -63,9 +63,7 @@ public class MCListener implements Listener {
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"; final String message = e.GetPlayer().PlayerName().get() + " joined the game";
if (!DiscordPlugin.hooked) MCChatListener.forAllowedCustomAndAllMCChat(MCChatListener.send(message), e.getPlayer(), ChannelconBroadcast.JOINLEAVE, true);
MCChatListener.sendSystemMessageToChat(message);
MCChatListener.forAllowedCustomMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message), e.getPlayer());
//System.out.println("Does this appear more than once?"); //No //System.out.println("Does this appear more than once?"); //No
MCChatListener.ListC = 0; MCChatListener.ListC = 0;
ChromaBot.getInstance().updatePlayerList(); ChromaBot.getInstance().updatePlayerList();
@ -85,16 +83,14 @@ public class MCListener implements Listener {
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"; final String message = e.GetPlayer().PlayerName().get() + " left the game";
if (!DiscordPlugin.hooked) MCChatListener.forAllowedCustomAndAllMCChat(MCChatListener.send(message), e.getPlayer(), ChannelconBroadcast.JOINLEAVE, true);
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)
public void onPlayerKick(PlayerKickEvent e) { public void onPlayerKick(PlayerKickEvent e) {
if (!DiscordPlugin.hooked && !e.getReason().equals("The server is restarting") /*if (!DiscordPlugin.hooked && !e.getReason().equals("The server is restarting")
&& !e.getReason().equals("Server closed")) // The leave messages errored with the previous setup, I could make it wait since I moved it here, but instead I have a special && !e.getReason().equals("Server closed")) // The leave messages errored with the previous setup, I could make it wait since I moved it here, but instead I have a special
MCChatListener.sendSystemMessageToChat(e.getPlayer().getName() + " left the game"); // message for this - Oh wait this doesn't even send normally because of the hook MCChatListener.forAllowedCustomAndAllMCChat(e.getPlayer().getName() + " left the game"); // message for this - Oh wait this doesn't even send normally because of the hook*/
} }
@EventHandler @EventHandler
@ -113,16 +109,17 @@ public class MCListener implements Listener {
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.LOW)
public void onPlayerDeath(PlayerDeathEvent e) { public void onPlayerDeath(PlayerDeathEvent e) {
if (!DiscordPlugin.hooked) MCChatListener.forAllowedCustomAndAllMCChat(MCChatListener.send(e.getDeathMessage()), e.getEntity(), ChannelconBroadcast.DEATH, true);
MCChatListener.sendSystemMessageToChat(e.getDeathMessage());
} }
@EventHandler @EventHandler
public void onPlayerAFK(AfkStatusChangeEvent e) { //TODO: Add AFK to custom chats? public void onPlayerAFK(AfkStatusChangeEvent e) {
if (e.isCancelled() || !e.getAffected().getBase().isOnline()) final Player base = e.getAffected().getBase();
if (e.isCancelled() || !base.isOnline())
return; return;
MCChatListener.sendSystemMessageToChat(e.getAffected().getBase().getDisplayName() final String msg = base.getDisplayName()
+ " is " + (e.getValue() ? "now" : "no longer") + " AFK."); + " is " + (e.getValue() ? "now" : "no longer") + " AFK.";
MCChatListener.forAllowedCustomAndAllMCChat(MCChatListener.send(msg), base, ChannelconBroadcast.AFK, false);
} }
@EventHandler @EventHandler
@ -140,7 +137,7 @@ public class MCListener implements Listener {
return; return;
final IUser user = DiscordPlugin.dc.getUserByID( final IUser user = DiscordPlugin.dc.getUserByID(
Long.parseLong(TBMCPlayerBase.getPlayer(source.getPlayer().getUniqueId(), TBMCPlayer.class) Long.parseLong(TBMCPlayerBase.getPlayer(source.getPlayer().getUniqueId(), TBMCPlayer.class)
.getAs(DiscordPlayer.class).getDiscordID())); // TODO: Use long .getAs(DiscordPlayer.class).getDiscordID()));
if (e.getValue()) if (e.getValue())
user.addRole(role); user.addRole(role);
else else
@ -154,18 +151,21 @@ public class MCListener implements Listener {
@EventHandler @EventHandler
public void onChatSystemMessage(TBMCSystemChatEvent event) { public void onChatSystemMessage(TBMCSystemChatEvent event) {
MCChatListener.sendSystemMessageToChat(event); MCChatListener.forAllowedMCChat(MCChatListener.send(event.getMessage()), event);
} }
@EventHandler @EventHandler
public void onBroadcastMessage(BroadcastMessageEvent event) { public void onBroadcastMessage(BroadcastMessageEvent event) {
MCChatListener.sendSystemMessageToChat(event.getMessage()); MCChatListener.forCustomAndAllMCChat(MCChatListener.send(event.getMessage()), ChannelconBroadcast.BROADCAST, false);
} }
/*@EventHandler @EventHandler
public void onYEEHAW(TBMCYEEHAWEvent event) { public void onYEEHAW(TBMCYEEHAWEvent event) { //TODO: Inherit from the chat event base to have channel support
MCChatListener.forAllowedCustomMCChat();event.getSender().getName()+" <:YEEHAW:"+DiscordPlugin.mainServer.getEmojiByName("YEEHAW").getStringID()+">s"//TODO: :YEEHAW:s - Change from broadcastMessage() in ButtonChat String name = event.getSender() instanceof Player ? ((Player) event.getSender()).getDisplayName()
}*/ : event.getSender().getName();
//Channel channel = ChromaGamerBase.getFromSender(event.getSender()).channel().get(); - TODO
MCChatListener.forAllMCChat(MCChatListener.send(name + " <:YEEHAW:" + DiscordPlugin.mainServer.getEmojiByName("YEEHAW").getStringID() + ">s"));
}
private static final String[] EXCLUDED_PLUGINS = {"ProtocolLib", "LibsDisguises"}; private static final String[] EXCLUDED_PLUGINS = {"ProtocolLib", "LibsDisguises"};

View file

@ -1,6 +1,7 @@
package buttondevteam.discordplugin.mccommands; package buttondevteam.discordplugin.mccommands;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.DiscordSenderBase;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.TBMCCommandBase; import buttondevteam.lib.chat.TBMCCommandBase;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -15,9 +16,11 @@ public class ResetMCCommand extends TBMCCommandBase { //Not player-only, so not
resetting = true; //Turned off after sending enable message (ReadyEvent) resetting = true; //Turned off after sending enable message (ReadyEvent)
sender.sendMessage("§bDisabling DiscordPlugin..."); sender.sendMessage("§bDisabling DiscordPlugin...");
Bukkit.getPluginManager().disablePlugin(DiscordPlugin.plugin); Bukkit.getPluginManager().disablePlugin(DiscordPlugin.plugin);
sender.sendMessage("§bEnabling DiscordPlugin..."); if (!(sender instanceof DiscordSenderBase)) //Sending to Discord errors
sender.sendMessage("§bEnabling DiscordPlugin...");
Bukkit.getPluginManager().enablePlugin(DiscordPlugin.plugin); Bukkit.getPluginManager().enablePlugin(DiscordPlugin.plugin);
sender.sendMessage("§bReset finished!"); if (!(sender instanceof DiscordSenderBase)) //Sending to Discord errors
sender.sendMessage("§bReset finished!");
}); });
return true; return true;
} }

View file

@ -46,13 +46,13 @@ public class DiscordInventory implements Inventory {
@Override @Override
public HashMap<Integer, ItemStack> addItem(ItemStack... items) throws IllegalArgumentException { // Can't add anything public HashMap<Integer, ItemStack> addItem(ItemStack... items) throws IllegalArgumentException { // Can't add anything
return new HashMap<>( return new HashMap<>(
IntStream.range(0, items.length).mapToObj(i -> i).collect(Collectors.toMap(i -> i, i -> items[i]))); IntStream.range(0, items.length).boxed().collect(Collectors.toMap(i -> i, i -> items[i])));
} }
@Override @Override
public HashMap<Integer, ItemStack> removeItem(ItemStack... items) throws IllegalArgumentException { public HashMap<Integer, ItemStack> removeItem(ItemStack... items) throws IllegalArgumentException {
return new HashMap<>( return new HashMap<>(
IntStream.range(0, items.length).mapToObj(i -> i).collect(Collectors.toMap(i -> i, i -> items[i]))); IntStream.range(0, items.length).boxed().collect(Collectors.toMap(i -> i, i -> items[i])));
} }
@Override @Override