Continued adding support for mcchat (#12)

And added more server ready questions
This commit is contained in:
Norbi Peti 2017-05-25 14:30:55 +02:00
parent 914cd60694
commit cb8eeb20c2
2 changed files with 61 additions and 34 deletions

View file

@ -1,6 +1,7 @@
package buttondevteam.discordplugin.listeners; package buttondevteam.discordplugin.listeners;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random; import java.util.Random;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
@ -26,6 +27,9 @@ public class CommandListener {
"Once the server stops screaming error messages.",// Ali "Once the server stops screaming error messages.",// Ali
}; };
private static final String[] serverReadyQuestions = new String[] { "when will the server be open",
"when will the server be ready", "when's the server ready", "when's the server open" };
private static final Random serverReadyRandom = new Random(); private static final Random serverReadyRandom = new Random();
private static final ArrayList<Short> usableServerReadyStrings = new ArrayList<Short>(serverReadyStrings.length) { private static final ArrayList<Short> usableServerReadyStrings = new ArrayList<Short>(serverReadyStrings.length) {
private static final long serialVersionUID = 2213771460909848770L; private static final long serialVersionUID = 2213771460909848770L;
@ -59,13 +63,9 @@ public class CommandListener {
public void handle(MessageReceivedEvent event) { public void handle(MessageReceivedEvent event) {
if (DiscordPlugin.SafeMode) if (DiscordPlugin.SafeMode)
return; return;
if (event.getMessage().getContent().toLowerCase().contains("when will the server be open?")) { final String msglowercase = event.getMessage().getContent().toLowerCase();
if (Arrays.stream(serverReadyQuestions).anyMatch(s -> msglowercase.contains(s))) {
int next; int next;
/*
* if (serverReadyStrings.length <= lastServerReadyStrings.size()) { next = lastServerReadyStrings.get(0); lastServerReadyStrings.clear(); } else { next = (short) serverReadyRandom
* .nextInt(serverReadyStrings.length - lastServerReadyStrings.size()); for (short i = 0; i < lastServerReadyStrings.size(); i++) { short j = lastServerReadyStrings.get(i); if
* (next == j) next++; if (next >= serverReadyStrings.length) next = 0; } lastServerReadyStrings.add(next); }
*/
if (usableServerReadyStrings.size() == 0) if (usableServerReadyStrings.size() == 0)
createUsableServerReadyStrings(usableServerReadyStrings); createUsableServerReadyStrings(usableServerReadyStrings);
next = usableServerReadyStrings.remove(serverReadyRandom.nextInt(usableServerReadyStrings.size())); next = usableServerReadyStrings.remove(serverReadyRandom.nextInt(usableServerReadyStrings.size()));

View file

@ -1,8 +1,10 @@
package buttondevteam.discordplugin.listeners; package buttondevteam.discordplugin.listeners;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -28,8 +30,6 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
return; return;
if (e.getSender() instanceof DiscordSender || e.getSender() instanceof DiscordPlayerSender) if (e.getSender() instanceof DiscordSender || e.getSender() instanceof DiscordPlayerSender)
return; return;
if (!e.getChannel().equals(Channel.GlobalChat))
return;
synchronized (this) { synchronized (this) {
final String authorPlayer = DiscordPlugin.sanitizeString(e.getSender() instanceof Player // final String authorPlayer = DiscordPlugin.sanitizeString(e.getSender() instanceof Player //
? ((Player) e.getSender()).getDisplayName() // ? ((Player) e.getSender()).getDisplayName() //
@ -40,22 +40,47 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
"https://minotar.net/avatar/" + ((Player) e.getSender()).getName() + "/32.png").build() "https://minotar.net/avatar/" + ((Player) e.getSender()).getName() + "/32.png").build()
: embed.build(); : embed.build();
final long nanoTime = System.nanoTime(); final long nanoTime = System.nanoTime();
if (lastmessage == null || lastmessage.isDeleted() Consumer<LastMsgData> doit = lastmsgdata -> {
|| !authorPlayer.equals(lastmessage.getEmbeds().get(0).getAuthor().getName()) if (lastmsgdata.message == null || lastmsgdata.message.isDeleted()
|| lastmsgtime / 1000000000f < nanoTime / 1000000000f - 120) { || !authorPlayer.equals(lastmsgdata.message.getEmbeds().get(0).getAuthor().getName())
lastmessage = DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, "", embedObject); || lastmsgdata.time / 1000000000f < nanoTime / 1000000000f - 120) {
lastmsgtime = nanoTime; lastmsgdata.message = DiscordPlugin.sendMessageToChannel(lastmsgdata.channel,
lastmsg = e.getMessage(); lastmsgdata.channel.isPrivate() ? e.getChannel().DisplayName : "", embedObject);
lastmsgdata.time = nanoTime;
} else } else
try { try {
lastmsg = embedObject.description = lastmsg + "\n" + embedObject.description; lastmsgdata.content = embedObject.description = lastmsgdata.content + "\n"
DiscordPlugin.perform(() -> lastmessage.edit("", embedObject)); + embedObject.description;// The message object doesn't get updated
final LastMsgData _lastmsgdata = lastmsgdata;
DiscordPlugin.perform(() -> _lastmsgdata.message.edit("", embedObject));
} catch (MissingPermissionsException | DiscordException e1) { } catch (MissingPermissionsException | DiscordException e1) {
TBMCCoreAPI.SendException("An error occured while editing chat message!", e1); TBMCCoreAPI.SendException("An error occured while editing chat message!", e1);
} }
};
if (e.getChannel().equals(Channel.GlobalChat))
doit.accept(lastmsgdata);
for (LastMsgData data : lastmsgPerUser) {
final DiscordPlayer user = DiscordPlayer.getUser(data.channel.getUsersHere().stream()
.filter(u -> u.getLongID() != u.getClient().getOurUser().getLongID()).findFirst().get()
.getStringID(), DiscordPlayer.class);
if (user.minecraftChat().get() && e.shouldSendTo()) // TODO!
doit.accept(data);
} // TODO: CHeck if user should get the message (get user from channel)
} // TODO: Author URL } // TODO: Author URL
} }
private static class LastMsgData {
public IMessage message;
public long time;
public String content;
public IChannel channel;
public LastMsgData(IChannel channel) {
this.channel = channel;
}
}
@EventHandler @EventHandler
public void onChatPreprocess(TBMCChatPreprocessEvent event) { public void onChatPreprocess(TBMCChatPreprocessEvent event) {
int start = -1; int start = -1;
@ -80,28 +105,30 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
private static final String[] UnconnectedCmds = new String[] { "list", "u", "shrug", "tableflip", "unflip", "mwiki", private static final String[] UnconnectedCmds = new String[] { "list", "u", "shrug", "tableflip", "unflip", "mwiki",
"yeehaw" }; "yeehaw" };
private static IMessage lastmessage = null; private static LastMsgData lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel);
private static long lastmsgtime = 0;
private static String lastmsg;
private static short lastlist = 0; private static short lastlist = 0;
private static short lastlistp = 0; private static short lastlistp = 0;
/**
* Used for messages in PMs (mcchat).
*/
private static ArrayList<LastMsgData> lastmsgPerUser = new ArrayList<LastMsgData>();
public static final HashMap<String, DiscordSender> UnconnectedSenders = new HashMap<>(); public static final HashMap<String, DiscordSender> UnconnectedSenders = new HashMap<>();
public static final HashMap<String, DiscordPlayerSender> ConnectedSenders = new HashMap<>(); public static final HashMap<String, DiscordPlayerSender> ConnectedSenders = new HashMap<>();
public static short ListC = 0; public static short ListC = 0;
public static void resetLastMessage() { public static void resetLastMessage() {
lastmessage = null; lastmsgdata.message = null; // Don't set the whole object to null, the player and channel information should be preserved
} }
@Override // Discord @Override // Discord
public void handle(sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent event) { public void handle(MessageReceivedEvent event) {
final IUser author = event.getMessage().getAuthor(); final IUser author = event.getMessage().getAuthor();
if (!event.getMessage().getChannel().getStringID().equals(DiscordPlugin.chatchannel.getStringID()) if (!event.getMessage().getChannel().getStringID().equals(DiscordPlugin.chatchannel.getStringID())
&& !(event.getMessage().getChannel().isPrivate() && !(event.getMessage().getChannel().isPrivate()
&& DiscordPlayer.getUser(author.getStringID(), DiscordPlayer.class).minecraftChat().get())) && DiscordPlayer.getUser(author.getStringID(), DiscordPlayer.class).minecraftChat().get()))
return; return;
lastmessage = null; resetLastMessage();
lastlist++; lastlist++;
if (author.isBot()) if (author.isBot())
return; return;