diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/events/discord/DiscordRelayEvent.java b/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/events/discord/DiscordRelayEvent.java index 565202b06..9cc2fdf22 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/events/discord/DiscordRelayEvent.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/events/discord/DiscordRelayEvent.java @@ -1,5 +1,6 @@ package net.essentialsx.api.v2.events.discord; +import net.ess3.api.IUser; import net.essentialsx.api.v2.services.discord.InteractionChannel; import net.essentialsx.api.v2.services.discord.InteractionMember; import org.bukkit.Bukkit; @@ -23,6 +24,7 @@ public class DiscordRelayEvent extends Event implements Cancellable { private final List groupNames; private final String rawMessage; private String formattedMessage; + private final List viewers; private boolean cancelled = false; /** @@ -31,14 +33,16 @@ public class DiscordRelayEvent extends Event implements Cancellable { * @param groupNames The message type keys which will be used to determine which player group the message should be sent to. * @param rawMessage The raw message sent from Discord. * @param formattedMessage The formatted message that will be sent to Minecraft. + * @param viewers The users that will see this relayed message. */ - public DiscordRelayEvent(final InteractionMember member, final InteractionChannel channel, final List groupNames, final String rawMessage, final String formattedMessage) { + public DiscordRelayEvent(final InteractionMember member, final InteractionChannel channel, final List groupNames, final String rawMessage, final String formattedMessage, final List viewers) { super(!Bukkit.isPrimaryThread()); this.member = member; this.channel = channel; this.groupNames = groupNames; this.rawMessage = rawMessage; this.formattedMessage = formattedMessage; + this.viewers = viewers; } /** @@ -89,6 +93,15 @@ public class DiscordRelayEvent extends Event implements Cancellable { this.formattedMessage = formattedMessage; } + /** + * Gets the users that will be sent the relayed message. + * The returned list is mutable. Removing a player from it will hide the message from them. + * @return The mutable list of users. + */ + public List getViewers() { + return viewers; + } + @Override public boolean isCancelled() { return cancelled; diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordListener.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordListener.java index 5f7a07d4a..bdc8aa6de 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordListener.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordListener.java @@ -19,6 +19,7 @@ import net.essentialsx.discord.util.MessageUtil; import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.logging.Level; @@ -105,11 +106,22 @@ public class DiscordListener extends ListenerAdapter { } } + final List viewers = new ArrayList<>(); + for (final IUser essUser : plugin.getPlugin().getEss().getOnlineUsers()) { + for (final String group : keys) { + final String perm = "essentials.discord.receive." + group; + final boolean primaryOverride = plugin.getSettings().isAlwaysReceivePrimary() && group.equalsIgnoreCase("primary"); + if (primaryOverride || (essUser.isPermissionSet(perm) && essUser.isAuthorized(perm))) { + viewers.add(essUser); + break; + } + } + } // Do not create the event specific objects if there are no listeners if (DiscordRelayEvent.getHandlerList().getRegisteredListeners().length != 0) { final DiscordRelayEvent relayEvent = new DiscordRelayEvent( new InteractionMemberImpl(member), new InteractionChannelImpl(event.getChannel()), - Collections.unmodifiableList(keys), event.getMessage().getContentRaw(), formattedMessage); + Collections.unmodifiableList(keys), event.getMessage().getContentRaw(), formattedMessage, viewers); Bukkit.getPluginManager().callEvent(relayEvent); if (relayEvent.isCancelled()) { return; @@ -117,15 +129,8 @@ public class DiscordListener extends ListenerAdapter { formattedMessage = relayEvent.getFormattedMessage(); } - for (IUser essUser : plugin.getPlugin().getEss().getOnlineUsers()) { - for (String group : keys) { - final String perm = "essentials.discord.receive." + group; - final boolean primaryOverride = plugin.getSettings().isAlwaysReceivePrimary() && group.equalsIgnoreCase("primary"); - if (primaryOverride || (essUser.isPermissionSet(perm) && essUser.isAuthorized(perm))) { - essUser.sendMessage(formattedMessage); - break; - } - } + for (final IUser essUser : viewers) { + essUser.sendMessage(formattedMessage); } } }