allow channels to use recipients list

This commit is contained in:
Draycia committed 2020-08-17 03:12:58 -07:00
1 parent e01ab04877
commit 94078a40c9
4 files changed
+46 -8

No files matched your search

@@ -7,7 +7,9 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
public abstract class ChatChannel implements ForwardingAudience {
@@ -75,6 +77,8 @@ public abstract class ChatChannel implements ForwardingAudience {
public abstract Boolean primaryGroupOnly();
public abstract Boolean honorsRecipientList();
public abstract boolean testContext(ChatUser sender, ChatUser target);
public abstract Object getContext(String key);
@@ -102,6 +106,8 @@ public abstract class ChatChannel implements ForwardingAudience {
*/
public abstract void sendMessage(ChatUser user, String message, boolean fromBungee);
public abstract void sendMessage(ChatUser user, Collection<ChatUser> recipients, String message, boolean fromBungee);
public abstract void sendComponent(ChatUser user, Component component);
public String processPlaceholders(ChatUser user, String input) { return input; }
@@ -84,8 +84,7 @@ public class CarbonChatChannel extends ChatChannel {
}
}
@Override
public void sendMessage(ChatUser user, String message, boolean fromRemote) {
public void sendMessage(ChatUser user, Collection<ChatUser> recipients, String message, boolean fromRemote) {
updateUserNickname(user);
// Get player's formatting
@@ -106,7 +105,7 @@ public class CarbonChatChannel extends ChatChannel {
}
// Iterate through players who should receive messages in this channel
for (ChatUser target : audiences()) {
for (ChatUser target : recipients) {
// Call second format event. Used for relational stuff (placeholders etc)
ChatFormatEvent formatEvent = new ChatFormatEvent(user, target, this, preFormatEvent.getFormat(), preFormatEvent.getMessage());
Bukkit.getPluginManager().callEvent(formatEvent);
@@ -170,6 +169,11 @@ public class CarbonChatChannel extends ChatChannel {
}
}
@Override
public void sendMessage(ChatUser user, String message, boolean fromRemote) {
this.sendMessage(user, this.audiences(), message, fromRemote);
}
public String getFormat(ChatUser user) {
if (primaryGroupOnly()) {
return getPrimaryGroupFormat(user);
@@ -327,6 +331,11 @@ public class CarbonChatChannel extends ChatChannel {
return getBoolean("should-bungee");
}
@Override
public Boolean honorsRecipientList() {
return getBoolean("honors-recipient-list");
}
@Override
public String getName() {
String name = getString("name");
@@ -4,11 +4,17 @@ import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.channels.ChatChannel;
import net.draycia.carbon.storage.ChatUser;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class BukkitChatListener implements Listener {
private final CarbonChat carbonChat;
@@ -20,12 +26,12 @@ public class BukkitChatListener implements Listener {
// Chat messages
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerchat(AsyncPlayerChatEvent event) {
event.getRecipients().clear();
ChatUser user = carbonChat.getUserService().wrap(event.getPlayer());
ChatChannel channel = user.getSelectedChannel();
if (channel.shouldCancelChatEvent()) event.setCancelled(true);
if (channel.shouldCancelChatEvent()) {
event.setCancelled(true);
}
for (ChatChannel entry : carbonChat.getChannelManager().getRegistry().values()) {
if (entry.getMessagePrefix() == null) {
@@ -47,11 +53,25 @@ public class BukkitChatListener implements Listener {
return;
}
final Collection<ChatUser> recipients;
if (selectedChannel.honorsRecipientList()) {
recipients = new HashSet<>();
for (Player recipient : event.getRecipients()) {
recipients.add(carbonChat.getUserService().wrap(recipient));
}
} else {
recipients = (List<ChatUser>)selectedChannel.audiences();
}
event.getRecipients().clear();
if (event.isAsynchronous()) {
selectedChannel.sendMessage(user, event.getMessage(), false);
selectedChannel.sendMessage(user, recipients, event.getMessage(), false);
} else {
Bukkit.getScheduler().runTaskAsynchronously(carbonChat, () -> {
selectedChannel.sendMessage(user, event.getMessage(), false);
selectedChannel.sendMessage(user, recipients, event.getMessage(), false);
});
}
}
+3
View File
@@ -54,6 +54,9 @@ default:
# As such, setting this on private channels is recommended.
# For example, use this if DiscordSRV is sending messages when you don't want it to.
cancel-message-event: false
# For plugin compatibility. If true, this channel will use bukkit's list of players to send the messages to.
# It's advised to leave this off unless absolutely necessary.
honors-recipient-list: false
channels:
global: