Add mutable "viewers" list to DiscordRelayEvent (#5080)

Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
This commit is contained in:
Noah van der Aa 2022-10-15 00:26:01 +02:00 committed by GitHub
parent 57529c4b7d
commit 4aa93a43db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 11 deletions

View File

@ -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<String> groupNames;
private final String rawMessage;
private String formattedMessage;
private final List<IUser> 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<String> groupNames, final String rawMessage, final String formattedMessage) {
public DiscordRelayEvent(final InteractionMember member, final InteractionChannel channel, final List<String> groupNames, final String rawMessage, final String formattedMessage, final List<IUser> 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<IUser> getViewers() {
return viewers;
}
@Override
public boolean isCancelled() {
return cancelled;

View File

@ -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<IUser> 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);
}
}
}