mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
ignore empty chats
This commit is contained in:
5 files changed
+37
-10
No files matched your search
@@ -309,9 +309,9 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
|
||||
final List<Audience> recipients,
|
||||
final String plainMessage
|
||||
) {
|
||||
final CarbonChatEvent chatEvent = this.prepareAndEmitChatEvent(sender, plainMessage, null, channel);
|
||||
final @Nullable CarbonChatEvent chatEvent = this.prepareAndEmitChatEvent(sender, plainMessage, null, channel);
|
||||
|
||||
if (chatEvent.cancelled()) {
|
||||
if (chatEvent == null || chatEvent.cancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
package net.draycia.carbon.common.listeners;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.event.CarbonEventHandler;
|
||||
@@ -35,8 +36,14 @@ import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.chat.SignedMessage;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.ComponentIteratorType;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public abstract class ChatListenerInternal {
|
||||
|
||||
private final ConfigFactory configFactory;
|
||||
@@ -53,7 +60,7 @@ public abstract class ChatListenerInternal {
|
||||
this.carbonEventHandler = carbonEventHandler;
|
||||
}
|
||||
|
||||
protected CarbonChatEvent prepareAndEmitChatEvent(final CarbonPlayer sender, final String messageContent, final SignedMessage signedMessage) {
|
||||
protected @Nullable CarbonChatEvent prepareAndEmitChatEvent(final CarbonPlayer sender, final String messageContent, final SignedMessage signedMessage) {
|
||||
final CarbonPlayer.ChannelMessage channelMessage = sender.channelForMessage(Component.text(messageContent));
|
||||
final ChatChannel channel = channelMessage.channel();
|
||||
final String message = PlainTextComponentSerializer.plainText().serialize(channelMessage.message());
|
||||
@@ -61,7 +68,7 @@ public abstract class ChatListenerInternal {
|
||||
return this.prepareAndEmitChatEvent(sender, message, signedMessage, channel);
|
||||
}
|
||||
|
||||
protected CarbonChatEvent prepareAndEmitChatEvent(final CarbonPlayer sender, final String messageContent, final SignedMessage signedMessage, final ChatChannel channel) {
|
||||
protected @Nullable CarbonChatEvent prepareAndEmitChatEvent(final CarbonPlayer sender, final String messageContent, final SignedMessage signedMessage, final ChatChannel channel) {
|
||||
String content = this.configFactory.primaryConfig().applyChatPlaceholders(messageContent);
|
||||
content = this.configFactory.primaryConfig().applyChatFilters(content);
|
||||
|
||||
@@ -77,6 +84,9 @@ public abstract class ChatListenerInternal {
|
||||
} else {
|
||||
message = WrappedCarbonPlayer.parseMessageTags(content, sender::hasPermission);
|
||||
}
|
||||
if (probablyBlank(message)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (sender.leftChannels().contains(channel.key())) {
|
||||
sender.joinChannel(channel);
|
||||
@@ -95,4 +105,20 @@ public abstract class ChatListenerInternal {
|
||||
return chatEvent;
|
||||
}
|
||||
|
||||
private static boolean probablyBlank(final Component component) {
|
||||
final Iterator<Component> it = component.iterator(ComponentIteratorType.DEPTH_FIRST);
|
||||
while (it.hasNext()) {
|
||||
final Component c = it.next();
|
||||
if (!(c instanceof TextComponent text)) {
|
||||
// Assume non-text components probably aren't blank
|
||||
return false;
|
||||
} else if (!text.content().isBlank()) {
|
||||
// Found some content, definitely not blank
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Likely blank
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user