mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Cleanup chat listeners and deduplicate shared code (#260)
This commit is contained in:
1 parent
b8f1fa2d10
commit
fc1b597f03
11 files changed
+227
-209
No files matched your search
@@ -37,9 +37,11 @@ import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.event.CarbonEventHandlerImpl;
|
||||
import net.draycia.carbon.common.listeners.DeafenHandler;
|
||||
import net.draycia.carbon.common.listeners.HyperlinkHandler;
|
||||
import net.draycia.carbon.common.listeners.IgnoreHandler;
|
||||
import net.draycia.carbon.common.listeners.ItemLinkHandler;
|
||||
import net.draycia.carbon.common.listeners.Listener;
|
||||
import net.draycia.carbon.common.listeners.MessagePacketHandler;
|
||||
import net.draycia.carbon.common.listeners.MuteHandler;
|
||||
import net.draycia.carbon.common.listeners.PingHandler;
|
||||
import net.draycia.carbon.common.listeners.RadiusListener;
|
||||
@@ -134,8 +136,10 @@ public final class CarbonCommonModule extends AbstractModule {
|
||||
|
||||
final Multibinder<Listener> listeners = Multibinder.newSetBinder(this.binder(), Listener.class);
|
||||
listeners.addBinding().to(DeafenHandler.class);
|
||||
listeners.addBinding().to(HyperlinkHandler.class);
|
||||
listeners.addBinding().to(IgnoreHandler.class);
|
||||
listeners.addBinding().to(ItemLinkHandler.class);
|
||||
listeners.addBinding().to(MessagePacketHandler.class);
|
||||
listeners.addBinding().to(MuteHandler.class);
|
||||
listeners.addBinding().to(PingHandler.class);
|
||||
listeners.addBinding().to(RadiusListener.class);
|
||||
|
||||
@@ -56,13 +56,11 @@ import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.event.events.CarbonReloadEvent;
|
||||
import net.draycia.carbon.common.event.events.ChannelRegisterEventImpl;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.draycia.carbon.common.messaging.packets.ChatMessagePacket;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.registry.DefaultedRegistryGetter;
|
||||
import ninja.egg82.messenger.services.PacketService;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -77,7 +75,6 @@ import org.spongepowered.configurate.serialize.SerializationException;
|
||||
import org.spongepowered.configurate.transformation.ConfigurationTransformation;
|
||||
|
||||
import static net.draycia.carbon.api.util.KeyedRenderer.keyedRenderer;
|
||||
import static net.draycia.carbon.common.util.Strings.URL_REPLACEMENT_CONFIG;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
@@ -317,11 +314,7 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
final var renderers = new ArrayList<KeyedRenderer>();
|
||||
renderers.add(keyedRenderer(Key.key("carbon", "default"), channel));
|
||||
|
||||
Component eventMessage = ConfigChatChannel.parseMessageTags(sender, plainMessage);
|
||||
|
||||
if (sender.hasPermission("carbon.chatlinks")) {
|
||||
eventMessage = eventMessage.replaceText(URL_REPLACEMENT_CONFIG.get());
|
||||
}
|
||||
final Component eventMessage = ConfigChatChannel.parseMessageTags(sender, plainMessage);
|
||||
|
||||
final var chatEvent = new CarbonChatEvent(sender, eventMessage, recipients, renderers, channel, null);
|
||||
this.eventHandler.emit(chatEvent);
|
||||
@@ -349,19 +342,6 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
}
|
||||
recipient.sendMessage(renderedMessage);
|
||||
}
|
||||
|
||||
final @Nullable PacketService packetService = carbonChat.packetService();
|
||||
|
||||
if (packetService != null) {
|
||||
var renderedMessage = chatEvent.message();
|
||||
|
||||
for (final var renderer : chatEvent.renderers()) {
|
||||
renderedMessage = renderer.render(sender, sender, renderedMessage, chatEvent.message());
|
||||
}
|
||||
|
||||
packetService.queuePacket(new ChatMessagePacket(carbonChat.serverId(), sender.uuid(),
|
||||
channel.permission(), channel.key(), sender.username(), renderedMessage));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isPathEmpty(final Path path) {
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2023 Josua Parks (Vicarious)
|
||||
* Contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.draycia.carbon.common.listeners;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.event.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.common.channels.ConfigChatChannel;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.chat.SignedMessage;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public abstract class ChatListenerInternal {
|
||||
|
||||
private final ConfigFactory configFactory;
|
||||
private final CarbonMessages carbonMessages;
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
protected ChatListenerInternal(
|
||||
final CarbonChat carbonChat,
|
||||
final CarbonMessages carbonMessages,
|
||||
final ConfigFactory configFactory
|
||||
) {
|
||||
this.configFactory = configFactory;
|
||||
this.carbonMessages = carbonMessages;
|
||||
this.carbonChat = carbonChat;
|
||||
}
|
||||
|
||||
protected CarbonChatEvent prepareAndEmitChatEvent(final CarbonPlayer sender, final String messageContent, final SignedMessage signedMessage) {
|
||||
String content = messageContent;
|
||||
|
||||
for (final Map.Entry<String, String> placeholder : this.configFactory.primaryConfig().chatPlaceholders().entrySet()) {
|
||||
content = content.replace(placeholder.getKey(), placeholder.getValue());
|
||||
}
|
||||
|
||||
Component message = ConfigChatChannel.parseMessageTags(sender, content);
|
||||
|
||||
final CarbonPlayer.ChannelMessage channelMessage = sender.channelForMessage(message);
|
||||
final ChatChannel channel = channelMessage.channel();
|
||||
|
||||
message = channelMessage.message();
|
||||
|
||||
if (sender.leftChannels().contains(channel.key())) {
|
||||
sender.joinChannel(channel);
|
||||
this.carbonMessages.channelJoined(sender);
|
||||
}
|
||||
|
||||
final List<KeyedRenderer> renderers = new ArrayList<>();
|
||||
renderers.add(KeyedRenderer.keyedRenderer(Key.key("carbon", "default"), channel));
|
||||
|
||||
final List<Audience> recipients = channel.recipients(sender);
|
||||
|
||||
final CarbonChatEvent chatEvent = new CarbonChatEvent(sender, message, recipients, renderers, channel, signedMessage);
|
||||
|
||||
this.carbonChat.eventHandler().emit(chatEvent);
|
||||
|
||||
return chatEvent;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2023 Josua Parks (Vicarious)
|
||||
* Contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.draycia.carbon.common.listeners;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.event.CarbonEventHandler;
|
||||
import net.draycia.carbon.api.event.events.CarbonChatEvent;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static net.draycia.carbon.common.util.Strings.URL_REPLACEMENT_CONFIG;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class HyperlinkHandler implements Listener {
|
||||
|
||||
@Inject
|
||||
public HyperlinkHandler(final CarbonEventHandler events) {
|
||||
events.subscribe(CarbonChatEvent.class, 0, false, event -> {
|
||||
if (event.sender().hasPermission("carbon.chatlinks")) {
|
||||
event.message(event.message().replaceText(URL_REPLACEMENT_CONFIG.get()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2023 Josua Parks (Vicarious)
|
||||
* Contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.draycia.carbon.common.listeners;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.event.CarbonEventHandler;
|
||||
import net.draycia.carbon.api.event.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.CarbonChatInternal;
|
||||
import net.draycia.carbon.common.messaging.packets.ChatMessagePacket;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import ninja.egg82.messenger.services.PacketService;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class MessagePacketHandler implements Listener {
|
||||
|
||||
@Inject
|
||||
public MessagePacketHandler(final CarbonEventHandler events, final CarbonChat carbonChat) {
|
||||
|
||||
events.subscribe(CarbonChatEvent.class, 100, false, event -> {
|
||||
final @Nullable PacketService packetService = ((CarbonChatInternal<?>) carbonChat).packetService();
|
||||
final CarbonPlayer sender = event.sender();
|
||||
|
||||
if (packetService != null) {
|
||||
Component networkMessage = event.message();
|
||||
|
||||
for (final var renderer : event.renderers()) {
|
||||
networkMessage = renderer.render(sender, sender, networkMessage, event.originalMessage());
|
||||
}
|
||||
|
||||
packetService.queuePacket(new ChatMessagePacket(carbonChat.serverId(), sender.uuid(),
|
||||
event.chatChannel().permission(), event.chatChannel().key(), sender.username(), networkMessage));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,6 +40,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import static java.util.Objects.requireNonNullElse;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public abstract class WrappedCarbonPlayer implements CarbonPlayer {
|
||||
|
||||
@@ -166,7 +168,7 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
|
||||
final String text = PlainTextComponentSerializer.plainText().serialize(message);
|
||||
Component formattedMessage = message;
|
||||
|
||||
@Nullable ChatChannel channel = this.selectedChannel();
|
||||
ChatChannel channel = requireNonNullElse(this.selectedChannel(), this.carbonPlayerCommon().channelRegistry().defaultValue());
|
||||
|
||||
for (final ChatChannel chatChannel : this.carbonPlayerCommon.channelRegistry()) {
|
||||
final @MonotonicNonNull String prefix = chatChannel.quickPrefix();
|
||||
|
||||
Reference in new issue
Block a user