Make channels ForwardingAudiences

This commit is contained in:
Draycia committed 2020-08-10 18:07:45 -07:00
1 parent aeb15ff0f1
commit 24aff39e11
2 files changed
+31 -8

No files matched your search

@@ -2,6 +2,7 @@ package net.draycia.carbon.channels;
import net.draycia.carbon.events.PreChatFormatEvent;
import net.draycia.carbon.storage.ChatUser;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.jetbrains.annotations.Nullable;
@@ -9,7 +10,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.regex.Pattern;
public abstract class ChatChannel {
public abstract class ChatChannel implements ForwardingAudience {
/**
* @return The color that represents this channel. Optionally used in formatting.
@@ -83,8 +84,6 @@ public abstract class ChatChannel {
public abstract Boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying);
public abstract List<ChatUser> getAudience(ChatUser user);
/**
* @return If the channel should forward its formatting / formatted message to other servers
*/
@@ -46,12 +46,14 @@ public class CarbonChatChannel extends ChatChannel {
}
@Override
public List<ChatUser> getAudience(ChatUser user) {
public List<ChatUser> audiences() {
List<ChatUser> audience = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
if (canPlayerSee(user, carbonChat.getUserService().wrap(player), true)) {
audience.add(carbonChat.getUserService().wrap(player));
ChatUser playerUser = carbonChat.getUserService().wrap(player);
if (canPlayerSee(playerUser, true)) {
audience.add(playerUser);
}
}
@@ -93,7 +95,9 @@ public class CarbonChatChannel extends ChatChannel {
}
// Iterate through players who should receive messages in this channel
for (ChatUser target : getAudience(user)) {
for (ChatUser target : audiences()) {
// TODO: listener to cancel events when Target ignores Sender
// 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);
@@ -218,7 +222,7 @@ public class CarbonChatChannel extends ChatChannel {
@Override
public void sendComponent(ChatUser player, Component component) {
for (ChatUser user : getAudience(player)) {
for (ChatUser user : audiences()) {
if (!user.isIgnoringUser(player)) {
user.sendMessage(component);
}
@@ -235,6 +239,26 @@ public class CarbonChatChannel extends ChatChannel {
carbonChat.getPluginMessageManager().sendMessage(this, player, message);
}
public Boolean canPlayerSee(ChatUser target, boolean checkSpying) {
Player targetPlayer = target.asPlayer();
if (checkSpying && targetPlayer.hasPermission("carbonchat.spy." + getName())) {
if (target.getChannelSettings(this).isSpying()) {
return true;
}
}
if (!targetPlayer.hasPermission("carbonchat.channels." + getName() + ".see")) {
return false;
}
if (isIgnorable()) {
return !target.getChannelSettings(this).isIgnored();
}
return true;
}
public Boolean canPlayerSee(ChatUser sender, ChatUser target, boolean checkSpying) {
Player targetPlayer = target.asPlayer();