Add channel whisper functionality (#91)

* ChatChannel -> TextChannel, WhisperChannel, GroupChannel

* Add basic whisper channel support

* Move WhisperPings into WhisperOptions

* Add defaults to WhisperOptions

* Various deprecation and refactor fixes

* Test and fux bugs
This commit is contained in:
Josh (Vicarious) authored and GitHub committed 2020-10-02 02:23:25 -07:00
1 parent 14dfcfbf38
commit f9cac355b6
44 files changed
+797 -304

No files matched your search

@@ -4,7 +4,7 @@ import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
import com.intellectualsites.commands.paper.PaperCommandManager;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.adventure.CarbonTranslations;
import net.draycia.carbon.api.config.CarbonTranslations;
import net.draycia.carbon.api.adventure.MessageProcessor;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.commands.settings.CommandSettingsRegistry;
@@ -24,7 +24,7 @@ import net.draycia.carbon.api.config.SQLCredentials;
import net.draycia.carbon.common.messaging.RedisMessageService;
import net.draycia.carbon.listeners.contexts.DistanceContext;
import net.draycia.carbon.listeners.contexts.EconomyContext;
import net.draycia.carbon.listeners.contexts.FilterContext;
import net.draycia.carbon.common.contexts.FilterContext;
import net.draycia.carbon.listeners.contexts.TownyContext;
import net.draycia.carbon.listeners.contexts.WorldGuardContext;
import net.draycia.carbon.listeners.contexts.mcMMOContext;
@@ -57,6 +57,8 @@ import net.draycia.carbon.util.Metrics;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.serializer.craftbukkit.BukkitComponentSerializer;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
@@ -121,7 +123,8 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
this.reloadConfig();
// Setup Adventure
this.messageProcessor = new AdventureManager(BukkitAudiences.create(this), FormatType.MINIMESSAGE); // TODO: get format type from config
final BukkitAudiences audiences = BukkitAudiences.create(this);
this.messageProcessor = new AdventureManager(audiences, FormatType.MINIMESSAGE); // TODO: get format type from config
// Initialize managers
this.channelManager = new ChannelManager(this);
@@ -402,4 +405,9 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
return this.getDataFolder().toPath();
}
@Override
public @NonNull GsonComponentSerializer gsonSerializer() {
return BukkitComponentSerializer.gson();
}
}
@@ -1,6 +1,7 @@
package net.draycia.carbon.listeners.contexts;
import net.draycia.carbon.CarbonChatBukkit;
import net.draycia.carbon.api.channels.TextChannel;
import net.draycia.carbon.api.events.misc.CarbonEvents;
import net.draycia.carbon.api.events.PreChatFormatEvent;
import net.draycia.carbon.api.Context;
@@ -21,7 +22,12 @@ public class EconomyContext {
this.economy = this.carbonChat.getServer().getServicesManager().getRegistration(Economy.class).getProvider();
CarbonEvents.register(PreChatFormatEvent.class, event -> {
final Context context = event.channel().context("vault-balance");
if (!(event.channel() instanceof TextChannel)) {
return;
}
final TextChannel channel = (TextChannel) event.channel();
final Context context = channel.context("vault-balance");
if (context == null) {
return;
@@ -30,7 +36,7 @@ public class EconomyContext {
final Double requiredBal;
if (context.isNumber()) {
requiredBal = context.asNumber().doubleValue();;
requiredBal = context.asNumber().doubleValue();
} else {
return;
}
@@ -50,7 +56,12 @@ public class EconomyContext {
});
CarbonEvents.register(PreChatFormatEvent.class, event -> {
final Context context = event.channel().context("vault-cost");
if (!(event.channel() instanceof TextChannel)) {
return;
}
final TextChannel channel = (TextChannel) event.channel();
final Context context = channel.context("vault-cost");
if (context == null) {
return;
@@ -1,74 +0,0 @@
package net.draycia.carbon.listeners.contexts;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.events.misc.CarbonEvents;
import net.draycia.carbon.api.events.PreChatFormatEvent;
import net.draycia.carbon.api.Context;
import net.kyori.event.PostOrders;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FilterContext {
@NonNull
private final CarbonChat carbonChat;
public FilterContext(final @NonNull CarbonChat carbonChat) {
this.carbonChat = carbonChat;
CarbonEvents.register(PreChatFormatEvent.class, PostOrders.FIRST, false, event -> {
if (!carbonChat.moderationSettings().filters().enabled()) {
return;
}
if (event.user().permissible() && event.user().hasPermission("carbonchat.filter.exempt")) {
return;
}
if (!this.channelUsesFilter(event.channel())) {
return;
}
String message = event.message();
for (final Map.Entry<String, List<Pattern>> entry :
this.carbonChat.moderationSettings().filters().replacements().entrySet()) {
for (final Pattern pattern : entry.getValue()) {
final Matcher matcher = pattern.matcher(message);
if (entry.getKey().equals("_")) {
message = matcher.replaceAll("");
} else {
message = matcher.replaceAll(entry.getKey());
}
}
}
event.message(message);
for (final Pattern blockedWord : this.carbonChat.moderationSettings().filters().blockedPatterns()) {
if (blockedWord.matcher(event.message()).find()) {
event.cancelled(true);
break;
}
}
});
}
private boolean channelUsesFilter(final @NonNull ChatChannel chatChannel) {
final Context context = chatChannel.context("filter");
if (context == null) {
return false;
}
return context.isBoolean() && context.asBoolean();
}
}
@@ -4,8 +4,9 @@ import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Resident;
import net.draycia.carbon.CarbonChatBukkit;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.channels.TextChannel;
import net.draycia.carbon.api.events.misc.CarbonEvents;
import net.draycia.carbon.api.events.ChannelSwitchEvent;
import net.draycia.carbon.api.events.MessageContextEvent;
@@ -25,9 +26,9 @@ public final class TownyContext implements Listener {
private static final String KEY = "towny-town";
@NonNull
private final CarbonChatBukkit carbonChat;
private final CarbonChat carbonChat;
public TownyContext(final @NonNull CarbonChatBukkit carbonChat) {
public TownyContext(final @NonNull CarbonChat carbonChat) {
this.carbonChat = carbonChat;
CarbonEvents.register(ReceiverContextEvent.class, event -> {
@@ -39,11 +40,16 @@ public final class TownyContext implements Listener {
});
CarbonEvents.register(ChannelSwitchEvent.class, event -> {
final Context context = event.channel().context(KEY);
if (!(event.channel() instanceof TextChannel)) {
return;
}
final TextChannel channel = (TextChannel) event.channel();
final Context context = channel.context(KEY);
if (context != null && context.isBoolean() && context.asBoolean()) {
if (!this.isInTown(event.user())) {
event.failureMessage(carbonChat.getConfig().getString("contexts.Towny.cancellation-message"));
//event.failureMessage(carbonChat.getConfig().getString("contexts.Towny.cancellation-message"));
event.cancelled(true);
}
}
@@ -67,11 +73,12 @@ public final class TownyContext implements Listener {
final ChatUser user = this.carbonChat.userService().wrap(Bukkit.getPlayer(name).getUniqueId());
final ChatChannel channel = user.selectedChannel();
if (channel == null) {
if (!(channel instanceof TextChannel)) {
return;
}
final Context context = channel.context(KEY);
final TextChannel textChannel = (TextChannel) channel;
final Context context = textChannel.context(KEY);
if (context != null && context.isBoolean() && context.asBoolean()) {
user.clearSelectedChannel();
@@ -83,11 +90,12 @@ public final class TownyContext implements Listener {
final ChatUser user = this.carbonChat.userService().wrap(event.getPlayer().getUniqueId());
final ChatChannel channel = user.selectedChannel();
if (channel == null) {
if (!(channel instanceof TextChannel)) {
return;
}
final Context context = channel.context(KEY);
final TextChannel textChannel = (TextChannel) channel;
final Context context = textChannel.context(KEY);
if (context != null && context.isBoolean() && context.asBoolean() && !this.isInTown(user)) {
user.clearSelectedChannel();
@@ -2,8 +2,9 @@ package net.draycia.carbon.listeners.contexts;
import com.gmail.nossr50.api.PartyAPI;
import com.gmail.nossr50.events.party.McMMOPartyChangeEvent;
import net.draycia.carbon.CarbonChatBukkit;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.channels.TextChannel;
import net.draycia.carbon.api.events.misc.CarbonEvents;
import net.draycia.carbon.api.events.ChannelSwitchEvent;
import net.draycia.carbon.api.events.MessageContextEvent;
@@ -21,12 +22,12 @@ import org.checkerframework.checker.nullness.qual.NonNull;
public final class mcMMOContext implements Listener {
@NonNull
private final CarbonChatBukkit carbonChat;
private final CarbonChat carbonChat;
@NonNull
private static final String KEY = "mcmmo-party";
public mcMMOContext(final @NonNull CarbonChatBukkit carbonChat) {
public mcMMOContext(final @NonNull CarbonChat carbonChat) {
this.carbonChat = carbonChat;
CarbonEvents.register(ReceiverContextEvent.class, PostOrders.NORMAL, false, event -> {
@@ -39,11 +40,16 @@ public final class mcMMOContext implements Listener {
});
CarbonEvents.register(ChannelSwitchEvent.class, PostOrders.NORMAL, false, event -> {
final Context context = event.channel().context(KEY);
if (!(event.channel() instanceof TextChannel)) {
return;
}
final TextChannel channel = (TextChannel) event.channel();
final Context context = channel.context(KEY);
if (context != null && context.isBoolean() && context.asBoolean()) {
if (!this.isInParty(event.user())) {
event.failureMessage(carbonChat.getConfig().getString("contexts.mcMMO.cancellation-message"));
//event.failureMessage(carbonChat.carbonSettings().getString("contexts.mcMMO.cancellation-message"));
event.cancelled(true);
}
}
@@ -79,11 +85,12 @@ public final class mcMMOContext implements Listener {
final ChatUser user = this.carbonChat.userService().wrap(player.getUniqueId());
final ChatChannel channel = user.selectedChannel();
if (channel == null) {
if (!(channel instanceof TextChannel)) {
return;
}
final Context context = channel.context(KEY);
final TextChannel textChannel = (TextChannel) channel;
final Context context = textChannel.context(KEY);
if (context != null && context.isBoolean() && context.asBoolean() && !this.isInParty(user)) {
user.clearSelectedChannel();
@@ -1,21 +1,18 @@
package net.draycia.carbon.listeners.events;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.channels.TextChannel;
import net.draycia.carbon.api.users.ChatUser;
import net.draycia.carbon.CarbonChatBukkit;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
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 org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
public class BukkitChatListener implements Listener {
@@ -41,18 +38,24 @@ public class BukkitChatListener implements Listener {
channel = this.carbonChat.channelRegistry().defaultChannel();
}
if (channel.shouldCancelChatEvent()) {
event.setCancelled(true);
}
// if (channel.shouldCancelChatEvent()) {
// event.setCancelled(true);
// }
for (final ChatChannel entry : this.carbonChat.channelRegistry()) {
if (entry.messagePrefix() == null || entry.messagePrefix().isEmpty()) {
if (!(entry instanceof TextChannel)) {
continue;
}
if (event.getMessage().startsWith(entry.messagePrefix())) {
final TextChannel textChannel = (TextChannel) entry;
if (textChannel.messagePrefix() == null || textChannel.messagePrefix().isEmpty()) {
continue;
}
if (event.getMessage().startsWith(textChannel.messagePrefix())) {
if (entry.canPlayerUse(user)) {
event.setMessage(event.getMessage().substring(entry.messagePrefix().length()));
event.setMessage(event.getMessage().substring(textChannel.messagePrefix().length()));
channel = entry;
break;
}
@@ -65,26 +68,14 @@ public class BukkitChatListener implements Listener {
return;
}
final Collection<ChatUser> recipients;
if (selectedChannel.honorsRecipientList()) {
recipients = new HashSet<>();
for (final Player recipient : event.getRecipients()) {
recipients.add(this.carbonChat.userService().wrap(recipient.getUniqueId()));
}
} else {
recipients = selectedChannel.audiences();
}
event.getRecipients().clear();
if (event.isAsynchronous()) {
final Map<ChatUser, Component> messages =
selectedChannel.parseMessage(user, recipients, event.getMessage(), false);
selectedChannel.parseMessage(user, event.getMessage(), false);
for (final Map.Entry<ChatUser, Component> entry : messages.entrySet()) {
if (entry.getValue().equals(TextComponent.empty())) {
if (entry.getValue().equals(Component.empty())) {
continue;
}
@@ -98,13 +89,13 @@ public class BukkitChatListener implements Listener {
} else {
Bukkit.getScheduler().runTaskAsynchronously(this.carbonChat, () -> {
selectedChannel.sendComponentsAndLog(
selectedChannel.parseMessage(user, recipients, event.getMessage(), false));
selectedChannel.parseMessage(user, event.getMessage(), false));
if (this.carbonChat.getConfig().getBoolean("show-tips")) {
this.carbonChat.logger().info("Tip: Sync chat event! I cannot set the message format due to this. :(");
this.carbonChat.logger().info("Tip: To 'solve' this, do a binary search and see which plugin is triggering");
this.carbonChat.logger().info("Tip: sync chat events and causing this, and let that plugin author know.");
}
// if (this.carbonChat.getConfig().getBoolean("show-tips")) {
// this.carbonChat.logger().info("Tip: Sync chat event! I cannot set the message format due to this. :(");
// this.carbonChat.logger().info("Tip: To 'solve' this, do a binary search and see which plugin is triggering");
// this.carbonChat.logger().info("Tip: sync chat events and causing this, and let that plugin author know.");
// }
});
}
}
@@ -28,17 +28,17 @@ public class ItemLinkHandler {
final Component itemComponent = CarbonUtils.createComponent(player);
if (itemComponent.equals(TextComponent.empty())) {
if (itemComponent.equals(Component.empty())) {
return;
}
for (final Pattern pattern : event.channel().itemLinkPatterns()) {
final String patternContent = pattern.toString().replace("\\Q", "").replace("\\E", "");
final String patternContent = pattern.toString().replace("\\Q", "")
.replace("\\E", "");
if (event.originalMessage().contains(patternContent)) {
final TextComponent component = (TextComponent) event.component().replaceFirstText(pattern, input -> {
return TextComponent.builder().append(itemComponent);
});
final TextComponent component = (TextComponent) event.component()
.replaceFirstText(pattern, input -> itemComponent);
event.component(component);
break;
@@ -11,8 +11,6 @@ import net.draycia.carbon.api.events.ChannelSwitchEvent;
import net.draycia.carbon.api.events.PrivateMessageEvent;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
@@ -45,7 +43,7 @@ public class BukkitChatUser implements ChatUser, ForwardingAudience {
@MonotonicNonNull // @NonNull but not initialised in all constructors.
private UUID uuid;
@Nullable
private String selectedChannel = null;
private String selectedChannelKey = null;
private boolean muted = false;
private boolean shadowMuted = false;
private boolean spyingWhispers = false;
@@ -53,6 +51,9 @@ public class BukkitChatUser implements ChatUser, ForwardingAudience {
@Nullable
private String nickname = null;
@Nullable
private transient ChatChannel selectedChannel = null;
@Nullable
private transient UUID replyTarget = null;
@@ -115,9 +116,9 @@ public class BukkitChatUser implements ChatUser, ForwardingAudience {
onlinePlayer.setDisplayName(newNickname);
if (this.carbonChat.getConfig().getBoolean("nicknames-set-tab-name")) {
onlinePlayer.setPlayerListName(newNickname);
}
// if (this.carbonChat.getConfig().getBoolean("nicknames-set-tab-name")) {
// onlinePlayer.setPlayerListName(newNickname);
// }
}
if (!fromRemote) {
@@ -158,11 +159,15 @@ public class BukkitChatUser implements ChatUser, ForwardingAudience {
@Override
@Nullable
public ChatChannel selectedChannel() {
if (this.selectedChannel == null) {
return null;
if (this.selectedChannel != null) {
return this.selectedChannel;
}
return this.carbonChat.channelRegistry().channelOrDefault(this.selectedChannel);
if (this.selectedChannelKey != null) {
return this.carbonChat.channelRegistry().channelOrDefault(this.selectedChannelKey);
}
return null;
}
@Override
@@ -180,7 +185,8 @@ public class BukkitChatUser implements ChatUser, ForwardingAudience {
return;
}
this.selectedChannel = chatChannel.key();
this.selectedChannelKey = chatChannel.key();
this.selectedChannel = chatChannel;
if (!fromRemote) {
this.carbonChat.messageManager().sendMessage("selected-channel", this.uuid(), byteArray -> {
@@ -344,7 +350,7 @@ public class BukkitChatUser implements ChatUser, ForwardingAudience {
}
@Override
public boolean spyingwhispers() {
public boolean spyingWhispers() {
return this.spyingWhispers;
}
@@ -354,8 +360,8 @@ public class BukkitChatUser implements ChatUser, ForwardingAudience {
return;
}
final String toPlayerFormat = this.carbonChat.translations().messageToOther();
final String fromPlayerFormat = this.carbonChat.translations().messageFromOther();
final String toPlayerFormat = this.carbonChat.carbonSettings().whisperOptions().senderFormat();
final String fromPlayerFormat = this.carbonChat.carbonSettings().whisperOptions().receiverFormat();
final OfflinePlayer offlineSender = Bukkit.getOfflinePlayer(sender.uuid());
String senderName = offlineSender.getName();
@@ -403,15 +409,6 @@ public class BukkitChatUser implements ChatUser, ForwardingAudience {
sender.replyTarget(this);
this.replyTarget(sender);
if (this.carbonChat.getConfig().getBoolean("pings.on-whisper")) {
final Key key = Key.of(this.carbonChat.getConfig().getString("pings.sound"));
final Sound.Source source = Sound.Source.valueOf(this.carbonChat.getConfig().getString("pings.source"));
final float volume = (float) this.carbonChat.getConfig().getDouble("pings.volume");
final float pitch = (float) this.carbonChat.getConfig().getDouble("pings.pitch");
this.playSound(Sound.of(key, source, volume, pitch));
}
}
} else if (offlineSender.isOnline()) {
final String targetNameFinal = targetName;
@@ -453,7 +450,7 @@ public class BukkitChatUser implements ChatUser, ForwardingAudience {
this.carbonChat.messageManager().sendMessage("whisper-component", sender.uuid(), byteArray -> {
byteArray.writeLong(this.uuid().getMostSignificantBits());
byteArray.writeLong(this.uuid().getLeastSignificantBits());
byteArray.writeUTF(this.carbonChat.messageProcessor().audiences().gsonSerializer().serialize(fromPlayerComponent));
byteArray.writeUTF(this.carbonChat.gsonSerializer().serialize(fromPlayerComponent));
});
});
}
@@ -461,7 +458,7 @@ public class BukkitChatUser implements ChatUser, ForwardingAudience {
for (final Player player : Bukkit.getOnlinePlayers()) {
final ChatUser user = this.carbonChat.userService().wrap(player.getUniqueId());
if (!user.spyingwhispers()) {
if (!user.spyingWhispers()) {
continue;
}
@@ -22,7 +22,7 @@ package net.draycia.carbon.util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.bungeecord.BungeeCordComponentSerializer;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
@@ -58,13 +58,13 @@ public final class CarbonUtils {
@NonNull
public static Component createComponent(final @NonNull Player player) {
if (!FunctionalityConstants.HAS_HOVER_EVENT_METHOD) {
return net.kyori.adventure.text.TextComponent.empty();
return net.kyori.adventure.text.Component.empty();
}
final ItemStack itemStack = player.getInventory().getItemInMainHand();
if (itemStack.getType().isAir()) {
return net.kyori.adventure.text.TextComponent.empty();
return net.kyori.adventure.text.Component.empty();
}
final Content content = Bukkit.getItemFactory().hoverContentOf(itemStack);
@@ -86,7 +86,7 @@ public final class CarbonUtils {
component.color(ChatColor.WHITE).append("]");
return BungeeCordComponentSerializer.get().deserialize(component.create());
return BungeeComponentSerializer.get().deserialize(component.create());
}
}