Excluded list impr., MCCHAT FIX, API upd.

This commit is contained in:
Norbi Peti 2017-10-24 22:29:46 +02:00
parent 08fae9b73b
commit 2308ef4195
4 changed files with 40 additions and 28 deletions

View file

@ -30,6 +30,7 @@ import sx.blah.discord.api.*;
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.api.internal.json.objects.EmbedObject;
import sx.blah.discord.handle.impl.events.ReadyEvent; import sx.blah.discord.handle.impl.events.ReadyEvent;
import sx.blah.discord.handle.impl.obj.ReactionEmoji;
import sx.blah.discord.handle.obj.*; import sx.blah.discord.handle.obj.*;
import sx.blah.discord.util.*; import sx.blah.discord.util.*;
import sx.blah.discord.util.RequestBuffer.IRequest; import sx.blah.discord.util.RequestBuffer.IRequest;
@ -195,7 +196,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
public void onDisable() { public void onDisable() {
stop = true; stop = true;
for (val entry : MCChatListener.ConnectedSenders.entrySet()) for (val entry : MCChatListener.ConnectedSenders.entrySet())
MCListener.callEventExcluding(new PlayerQuitEvent(entry.getValue(), ""), "ProtocolLib"); MCListener.callEventExcludingSome(new PlayerQuitEvent(entry.getValue(), ""));
getConfig().set("lastannouncementtime", lastannouncementtime); getConfig().set("lastannouncementtime", lastannouncementtime);
getConfig().set("lastseentime", lastseentime); getConfig().set("lastseentime", lastseentime);
getConfig().set("gameroles", GameRoles); getConfig().set("gameroles", GameRoles);
@ -213,7 +214,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
private long lastannouncementtime = 0; private long lastannouncementtime = 0;
private long lastseentime = 0; private long lastseentime = 0;
public static final String DELIVERED_REACTION = ""; public static final ReactionEmoji DELIVERED_REACTION = ReactionEmoji.of("");
private void AnnouncementGetterThreadMethod() { private void AnnouncementGetterThreadMethod() {
while (!stop) { while (!stop) {
@ -362,15 +363,12 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
public static void updatePlayerList() { public static void updatePlayerList() {
perform(() -> { perform(() -> {
String[] s = chatchannel.getTopic().split("\\n----\\n"); String[] s = chatchannel.getTopic().split("\\n----\\n");
//System.out.println("Len: " + s.length);
//System.out.println("s0: " + s[0]);
if (s.length < 3) if (s.length < 3)
return; return;
s[0] = Bukkit.getOnlinePlayers().size() + " player" + (Bukkit.getOnlinePlayers().size() != 1 ? "s" : "") s[0] = Bukkit.getOnlinePlayers().size() + " player" + (Bukkit.getOnlinePlayers().size() != 1 ? "s" : "")
+ " online"; + " online";
s[s.length - 1] = "Players: " + Bukkit.getOnlinePlayers().stream() s[s.length - 1] = "Players: " + Bukkit.getOnlinePlayers().stream()
.map(p -> DiscordPlugin.sanitizeString(p.getDisplayName())).collect(Collectors.joining(", ")); .map(p -> DiscordPlugin.sanitizeString(p.getDisplayName())).collect(Collectors.joining(", "));
//System.out.println("s0 after: " + s[0]);
chatchannel.changeTopic(Arrays.stream(s).collect(Collectors.joining("\n----\n"))); chatchannel.changeTopic(Arrays.stream(s).collect(Collectors.joining("\n----\n")));
}); });
} }

View file

@ -1,8 +1,6 @@
package buttondevteam.discordplugin; package buttondevteam.discordplugin;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -30,8 +28,6 @@ public abstract class DiscordSenderBase implements IDiscordSender {
this.channel = channel; this.channel = channel;
} }
private static volatile List<String> broadcasts = new ArrayList<>();
private volatile String msgtosend = ""; private volatile String msgtosend = "";
private volatile BukkitTask sendtask; private volatile BukkitTask sendtask;
@ -52,13 +48,8 @@ public abstract class DiscordSenderBase implements IDiscordSender {
public void sendMessage(String message) { public void sendMessage(String message) {
try { try {
final boolean broadcast = new Exception().getStackTrace()[2].getMethodName().contains("broadcast"); final boolean broadcast = new Exception().getStackTrace()[2].getMethodName().contains("broadcast");
if (broadcast) { if (broadcast)
if (broadcasts.contains(message))
return; return;
if (broadcasts.size() > 10)
broadcasts.clear();
broadcasts.add(message);
}
final String sendmsg = DiscordPlugin.sanitizeString(message); final String sendmsg = DiscordPlugin.sanitizeString(message);
msgtosend += "\n" + sendmsg; msgtosend += "\n" + sendmsg;
if (sendtask == null) if (sendtask == null)

View file

@ -153,11 +153,11 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
val sender = new DiscordConnectedPlayer(user, channel, mcp.getUUID(), op.getName()); val sender = new DiscordConnectedPlayer(user, channel, mcp.getUUID(), op.getName());
ConnectedSenders.put(user.getStringID(), sender); ConnectedSenders.put(user.getStringID(), sender);
if (p == null)// Player is offline - If the player is online, that takes precedence if (p == null)// Player is offline - If the player is online, that takes precedence
MCListener.callEventExcluding(new PlayerJoinEvent(sender, ""), "ProtocolLib"); MCListener.callEventExcludingSome(new PlayerJoinEvent(sender, ""));
} else { } else {
val sender = ConnectedSenders.remove(user.getStringID()); val sender = ConnectedSenders.remove(user.getStringID());
if (p == null)// Player is offline - If the player is online, that takes precedence if (p == null)// Player is offline - If the player is online, that takes precedence
MCListener.callEventExcluding(new PlayerQuitEvent(sender, ""), "ProtocolLib"); MCListener.callEventExcludingSome(new PlayerQuitEvent(sender, ""));
} }
} }
return start // return start //
@ -332,9 +332,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
if (react) { if (react) {
event.getMessage().getChannel().getMessageHistory().stream().forEach(m -> { event.getMessage().getChannel().getMessageHistory().stream().forEach(m -> {
try { try {
final IReaction reaction = m.getReactionByUnicode(DiscordPlugin.DELIVERED_REACTION); final IReaction reaction = m.getReactionByEmoji(DiscordPlugin.DELIVERED_REACTION);
if (reaction != null) if (reaction != null)
DiscordPlugin.perform(() -> m.removeReaction(reaction)); DiscordPlugin.perform(() -> m.removeReaction(DiscordPlugin.dc.getOurUser(), reaction));
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e); TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e);
} }

View file

@ -12,7 +12,10 @@ import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.server.BroadcastMessageEvent;
import org.bukkit.event.server.ServerCommandEvent; import org.bukkit.event.server.ServerCommandEvent;
import org.bukkit.plugin.AuthorNagException; import org.bukkit.plugin.AuthorNagException;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -36,6 +39,17 @@ import sx.blah.discord.util.DiscordException;
import sx.blah.discord.util.MissingPermissionsException; import sx.blah.discord.util.MissingPermissionsException;
public class MCListener implements Listener { public class MCListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerLogin(PlayerLoginEvent e) {
if (e.getResult() != Result.ALLOWED)
return;
MCChatListener.ConnectedSenders.values().stream()
.filter(s -> s.getUniqueId().equals(e.getPlayer().getUniqueId())).findAny().ifPresent(dcp -> {
callEventExcludingSome(new PlayerQuitEvent(dcp, ""));
//dcp.sendMessage("Real login detected, logged fake player out.");
});
}
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(TBMCPlayerJoinEvent e) { public void onPlayerJoin(TBMCPlayerJoinEvent e) {
if (e.getPlayer() instanceof DiscordConnectedPlayer) if (e.getPlayer() instanceof DiscordConnectedPlayer)
@ -48,9 +62,6 @@ public class MCListener implements Listener {
new DiscordPlayerSender(user, user.getOrCreatePMChannel(), p)); new DiscordPlayerSender(user, user.getOrCreatePMChannel(), p));
MCChatListener.OnlineSenders.put("P" + dp.getDiscordID(), MCChatListener.OnlineSenders.put("P" + dp.getDiscordID(),
new DiscordPlayerSender(user, DiscordPlugin.chatchannel, p)); new DiscordPlayerSender(user, DiscordPlugin.chatchannel, p));
MCChatListener.ConnectedSenders.values().stream()
.filter(s -> s.getUniqueId().equals(e.getPlayer().getUniqueId())).findAny()
.ifPresent(dcp -> callEventExcluding(new PlayerQuitEvent(dcp, ""), "ProtocolLib"));
} }
if (ConnectCommand.WaitingToConnect.containsKey(e.GetPlayer().PlayerName().get())) { if (ConnectCommand.WaitingToConnect.containsKey(e.GetPlayer().PlayerName().get())) {
IUser user = DiscordPlugin.dc IUser user = DiscordPlugin.dc
@ -70,11 +81,12 @@ public class MCListener implements Listener {
return; // Only care about real users return; // Only care about real users
MCChatListener.OnlineSenders.entrySet() MCChatListener.OnlineSenders.entrySet()
.removeIf(entry -> entry.getValue().getUniqueId().equals(e.getPlayer().getUniqueId())); .removeIf(entry -> entry.getValue().getUniqueId().equals(e.getPlayer().getUniqueId()));
MCChatListener.ConnectedSenders.values().stream() Bukkit.getScheduler().runTask(DiscordPlugin.plugin,
() -> MCChatListener.ConnectedSenders.values().stream()
.filter(s -> s.getUniqueId().equals(e.getPlayer().getUniqueId())).findAny() .filter(s -> s.getUniqueId().equals(e.getPlayer().getUniqueId())).findAny()
.ifPresent(dcp -> callEventExcluding(new PlayerJoinEvent(dcp, ""), "ProtocolLib")); .ifPresent(dcp -> callEventExcludingSome(new PlayerJoinEvent(dcp, ""))));
MCChatListener.sendSystemMessageToChat(e.GetPlayer().PlayerName().get() + " left the game"); MCChatListener.sendSystemMessageToChat(e.GetPlayer().PlayerName().get() + " left the game");
DiscordPlugin.updatePlayerList(); Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin, DiscordPlugin::updatePlayerList, 5);
} }
@EventHandler @EventHandler
@ -138,6 +150,17 @@ public class MCListener implements Listener {
MCChatListener.sendSystemMessageToChat(event); MCChatListener.sendSystemMessageToChat(event);
} }
@EventHandler
public void onBroadcastMessage(BroadcastMessageEvent event) {
MCChatListener.sendSystemMessageToChat(event.getMessage());
}
private static final String[] EXCLUDED_PLUGINS = { "ProtocolLib", "LibsDisguises" };
public static void callEventExcludingSome(Event event) {
callEventExcluding(event, EXCLUDED_PLUGINS);
}
/** /**
* Calls an event with the given details. * Calls an event with the given details.
* <p> * <p>
@ -148,7 +171,7 @@ public class MCListener implements Listener {
* @param plugins * @param plugins
* The plugins to exclude. Not case sensitive. * The plugins to exclude. Not case sensitive.
*/ */
public static void callEventExcluding(Event event, String... plugins) { // Copied from Spigot-API and modified a bit private static void callEventExcluding(Event event, String... plugins) { // Copied from Spigot-API and modified a bit
if (event.isAsynchronous()) { if (event.isAsynchronous()) {
if (Thread.holdsLock(Bukkit.getPluginManager())) { if (Thread.holdsLock(Bukkit.getPluginManager())) {
throw new IllegalStateException( throw new IllegalStateException(