IT WORKS! (See commit body)
- Added message grouping - Moved listener and other registrations to the Discord ready event, so people can't do things before they should - Spent lot of time on message grouping
This commit is contained in:
parent
2f76583a87
commit
6c1b2e39c4
3 changed files with 45 additions and 27 deletions
|
@ -56,15 +56,6 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8));
|
cb.withToken(Files.readFirstLine(new File("TBMC", "Token.txt"), StandardCharsets.UTF_8));
|
||||||
dc = cb.login();
|
dc = cb.login();
|
||||||
dc.getDispatcher().registerListener(this);
|
dc.getDispatcher().registerListener(this);
|
||||||
for (IListener<?> listener : CommandListener.getListeners())
|
|
||||||
dc.getDispatcher().registerListener(listener);
|
|
||||||
MCChatListener mcchat = new MCChatListener();
|
|
||||||
dc.getDispatcher().registerListener(mcchat);
|
|
||||||
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this);
|
|
||||||
dc.getDispatcher().registerListener(new AutoUpdaterListener());
|
|
||||||
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
|
|
||||||
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
|
|
||||||
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Bukkit.getPluginManager().disablePlugin(this);
|
Bukkit.getPluginManager().disablePlugin(this);
|
||||||
|
@ -112,6 +103,17 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
coffeechannel = botchannel; // bot-room
|
coffeechannel = botchannel; // bot-room
|
||||||
dc.changeStatus(Status.game("testing"));
|
dc.changeStatus(Status.game("testing"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (IListener<?> listener : CommandListener.getListeners())
|
||||||
|
dc.getDispatcher().registerListener(listener);
|
||||||
|
MCChatListener mcchat = new MCChatListener();
|
||||||
|
dc.getDispatcher().registerListener(mcchat);
|
||||||
|
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this);
|
||||||
|
dc.getDispatcher().registerListener(new AutoUpdaterListener());
|
||||||
|
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
|
||||||
|
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
|
||||||
|
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
|
||||||
|
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(this, () -> sendMessageToChannel(chatchannel, "",
|
Bukkit.getScheduler().runTaskAsynchronously(this, () -> sendMessageToChannel(chatchannel, "",
|
||||||
new EmbedBuilder().withColor(Color.GREEN).withTitle("Server started - chat connected.").build()));
|
new EmbedBuilder().withColor(Color.GREEN).withTitle("Server started - chat connected.").build()));
|
||||||
Runnable r = new Runnable() {
|
Runnable r = new Runnable() {
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package buttondevteam.discordplugin.listeners;
|
package buttondevteam.discordplugin.listeners;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -11,17 +10,14 @@ import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
|
||||||
import buttondevteam.discordplugin.*;
|
import buttondevteam.discordplugin.*;
|
||||||
import buttondevteam.lib.TBMCChatEvent;
|
import buttondevteam.lib.*;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
|
||||||
import buttondevteam.lib.TBMCPlayer;
|
|
||||||
import buttondevteam.lib.chat.Channel;
|
import buttondevteam.lib.chat.Channel;
|
||||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||||
import sx.blah.discord.api.events.IListener;
|
import sx.blah.discord.api.events.IListener;
|
||||||
|
import sx.blah.discord.api.internal.json.objects.EmbedObject;
|
||||||
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
|
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
|
||||||
import sx.blah.discord.handle.obj.IReaction;
|
import sx.blah.discord.handle.obj.*;
|
||||||
import sx.blah.discord.handle.obj.IUser;
|
import sx.blah.discord.util.*;
|
||||||
import sx.blah.discord.util.EmbedBuilder;
|
|
||||||
import sx.blah.discord.util.RateLimitException;
|
|
||||||
|
|
||||||
public class MCChatListener implements Listener, IListener<MessageReceivedEvent> {
|
public class MCChatListener implements Listener, IListener<MessageReceivedEvent> {
|
||||||
@EventHandler // Minecraft
|
@EventHandler // Minecraft
|
||||||
|
@ -31,21 +27,41 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
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)) {
|
if (e.getChannel().equals(Channel.GlobalChat)) {
|
||||||
final EmbedBuilder embed = new EmbedBuilder()
|
final String authorPlayer = DiscordPlugin.sanitizeString(e.getSender() instanceof Player //
|
||||||
.withAuthorName(DiscordPlugin.sanitizeString(e.getSender() instanceof Player //
|
? ((Player) e.getSender()).getDisplayName() //
|
||||||
? ((Player) e.getSender()).getDisplayName() //
|
: e.getSender().getName());
|
||||||
: e.getSender().getName()))
|
final EmbedBuilder embed = new EmbedBuilder().withAuthorName(authorPlayer).withDescription(e.getMessage());
|
||||||
.withDescription(e.getMessage());
|
final EmbedObject embedObject = e.getSender() instanceof Player
|
||||||
DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, "",
|
? embed.withAuthorIcon(
|
||||||
e.getSender() instanceof Player ? embed
|
"https://minotar.net/avatar/" + ((Player) e.getSender()).getName() + "/32.png").build()
|
||||||
.withAuthorIcon("https://minotar.net/avatar/" + ((Player) e.getSender()).getName() + "/32.png")
|
: embed.build();
|
||||||
.build() : embed.build());
|
final long nanoTime = System.nanoTime();
|
||||||
|
if (lastmessage == null || lastmessage.isDeleted()
|
||||||
|
|| !authorPlayer.equals(lastmessage.getEmbedded().get(0).getAuthor().getName())
|
||||||
|
|| lastmsgtime / 1000000000f < nanoTime / 1000000000f - 120) {
|
||||||
|
/*System.out.println("lastmsgtime: " + lastmsgtime);
|
||||||
|
System.out.println("Current: " + nanoTime);
|
||||||
|
System.out.println("2 mins before: " + (nanoTime - 120 * 1000000000));
|
||||||
|
System.out.println("Diff: " + (nanoTime - (nanoTime - 120 * 1000000000)));*/
|
||||||
|
lastmessage = DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, "", embedObject);
|
||||||
|
lastmsgtime = nanoTime;
|
||||||
|
} else
|
||||||
|
try {
|
||||||
|
embedObject.description = lastmessage.getEmbedded().get(0).getDescription() + "\n"
|
||||||
|
+ embedObject.description;
|
||||||
|
lastmessage.edit("", embedObject);
|
||||||
|
} catch (MissingPermissionsException | RateLimitException | DiscordException e1) {
|
||||||
|
TBMCCoreAPI.SendException("An error occured while editing chat message!", e1);
|
||||||
|
}
|
||||||
} // TODO: Author URL
|
} // TODO: Author URL
|
||||||
}
|
}
|
||||||
|
|
||||||
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 long lastmsgtime = 0;
|
||||||
|
|
||||||
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<>();
|
||||||
|
|
||||||
|
@ -57,6 +73,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
if (!event.getMessage().getChannel().getID().equals(DiscordPlugin.chatchannel.getID())
|
if (!event.getMessage().getChannel().getID().equals(DiscordPlugin.chatchannel.getID())
|
||||||
/* && !(event.getMessage().getChannel().isPrivate() && privatechat) */)
|
/* && !(event.getMessage().getChannel().isPrivate() && privatechat) */)
|
||||||
return;
|
return;
|
||||||
|
lastmessage = null;
|
||||||
if (CommandListener.runCommand(event.getMessage(), true))
|
if (CommandListener.runCommand(event.getMessage(), true))
|
||||||
return;
|
return;
|
||||||
String dmessage = event.getMessage().getContent();
|
String dmessage = event.getMessage().getContent();
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package buttondevteam.discordplugin.listeners;
|
package buttondevteam.discordplugin.listeners;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
|
|
Loading…
Reference in a new issue