diff --git a/api/src/main/java/com/discordsrv/api/discord/entity/channel/DiscordGuildChannel.java b/api/src/main/java/com/discordsrv/api/discord/entity/channel/DiscordGuildChannel.java index 59bdf073..b70c38ce 100644 --- a/api/src/main/java/com/discordsrv/api/discord/entity/channel/DiscordGuildChannel.java +++ b/api/src/main/java/com/discordsrv/api/discord/entity/channel/DiscordGuildChannel.java @@ -30,7 +30,7 @@ import com.discordsrv.api.placeholder.annotation.PlaceholderPrefix; import org.jetbrains.annotations.NotNull; @PlaceholderPrefix("channel_") -public interface DiscordGuildChannel extends Snowflake { +public interface DiscordGuildChannel extends DiscordChannel, Snowflake { /** * Gets the name of the channel. diff --git a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/AbstractGameMessageForwardedEvent.java b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/AbstractGameMessageForwardedEvent.java index 60da281b..6bdddbc7 100644 --- a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/AbstractGameMessageForwardedEvent.java +++ b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/AbstractGameMessageForwardedEvent.java @@ -23,18 +23,29 @@ package com.discordsrv.api.event.events.message.forward.game; +import com.discordsrv.api.channel.GameChannel; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessageCluster; import com.discordsrv.api.event.events.Event; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public abstract class AbstractGameMessageForwardedEvent implements Event { + private final GameChannel originGameChannel; private final ReceivedDiscordMessageCluster discordMessage; - public AbstractGameMessageForwardedEvent(@NotNull ReceivedDiscordMessageCluster discordMessage) { + public AbstractGameMessageForwardedEvent( + @Nullable GameChannel originGameChannel, + @NotNull ReceivedDiscordMessageCluster discordMessage + ) { + this.originGameChannel = originGameChannel; this.discordMessage = discordMessage; } + public GameChannel getOriginGameChannel() { + return originGameChannel; + } + /** * Gets the {@link ReceivedDiscordMessageCluster} containing the sent message(s). * @return the message cluster diff --git a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/AwardMessageForwardedEvent.java b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/AwardMessageForwardedEvent.java index 5a6f65d7..dcf94692 100644 --- a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/AwardMessageForwardedEvent.java +++ b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/AwardMessageForwardedEvent.java @@ -23,15 +23,20 @@ package com.discordsrv.api.event.events.message.forward.game; +import com.discordsrv.api.channel.GameChannel; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessageCluster; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Indicates that an advancement or achievement message was forwarded to Discord. */ public class AwardMessageForwardedEvent extends AbstractGameMessageForwardedEvent { - public AwardMessageForwardedEvent(@NotNull ReceivedDiscordMessageCluster discordMessage) { - super(discordMessage); + public AwardMessageForwardedEvent( + @Nullable GameChannel originGameChannel, + @NotNull ReceivedDiscordMessageCluster discordMessage + ) { + super(originGameChannel, discordMessage); } } diff --git a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/DeathMessageForwardedEvent.java b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/DeathMessageForwardedEvent.java index 54a66a62..86ef0969 100644 --- a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/DeathMessageForwardedEvent.java +++ b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/DeathMessageForwardedEvent.java @@ -23,15 +23,20 @@ package com.discordsrv.api.event.events.message.forward.game; +import com.discordsrv.api.channel.GameChannel; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessageCluster; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Indicates that a death message was forwarded to Discord. */ public class DeathMessageForwardedEvent extends AbstractGameMessageForwardedEvent { - public DeathMessageForwardedEvent(@NotNull ReceivedDiscordMessageCluster discordMessage) { - super(discordMessage); + public DeathMessageForwardedEvent( + @Nullable GameChannel originGameChannel, + @NotNull ReceivedDiscordMessageCluster discordMessage + ) { + super(originGameChannel, discordMessage); } } diff --git a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/GameChatMessageForwardedEvent.java b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/GameChatMessageForwardedEvent.java index f4b54233..dcd31c38 100644 --- a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/GameChatMessageForwardedEvent.java +++ b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/GameChatMessageForwardedEvent.java @@ -23,15 +23,20 @@ package com.discordsrv.api.event.events.message.forward.game; +import com.discordsrv.api.channel.GameChannel; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessageCluster; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Indicates that a chat message was forwarded to Discord. */ public class GameChatMessageForwardedEvent extends AbstractGameMessageForwardedEvent { - public GameChatMessageForwardedEvent(@NotNull ReceivedDiscordMessageCluster discordMessage) { - super(discordMessage); + public GameChatMessageForwardedEvent( + @Nullable GameChannel originGameChannel, + @NotNull ReceivedDiscordMessageCluster discordMessage + ) { + super(originGameChannel, discordMessage); } } diff --git a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/JoinMessageForwardedEvent.java b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/JoinMessageForwardedEvent.java index 69db0826..f1749541 100644 --- a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/JoinMessageForwardedEvent.java +++ b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/JoinMessageForwardedEvent.java @@ -23,15 +23,20 @@ package com.discordsrv.api.event.events.message.forward.game; +import com.discordsrv.api.channel.GameChannel; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessageCluster; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Indicates that a join message was forwarded to Discord. */ public class JoinMessageForwardedEvent extends AbstractGameMessageForwardedEvent { - public JoinMessageForwardedEvent(@NotNull ReceivedDiscordMessageCluster discordMessage) { - super(discordMessage); + public JoinMessageForwardedEvent( + @Nullable GameChannel originGameChannel, + @NotNull ReceivedDiscordMessageCluster discordMessage + ) { + super(originGameChannel, discordMessage); } } diff --git a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/LeaveMessageForwardedEvent.java b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/LeaveMessageForwardedEvent.java index b6a0f138..247dbda9 100644 --- a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/LeaveMessageForwardedEvent.java +++ b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/LeaveMessageForwardedEvent.java @@ -23,15 +23,20 @@ package com.discordsrv.api.event.events.message.forward.game; +import com.discordsrv.api.channel.GameChannel; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessageCluster; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Indicates that a leave message was forwarded to Discord. */ public class LeaveMessageForwardedEvent extends AbstractGameMessageForwardedEvent { - public LeaveMessageForwardedEvent(@NotNull ReceivedDiscordMessageCluster discordMessage) { - super(discordMessage); + public LeaveMessageForwardedEvent( + @Nullable GameChannel originGameChannel, + @NotNull ReceivedDiscordMessageCluster discordMessage + ) { + super(originGameChannel, discordMessage); } } diff --git a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/ServerSwitchMessageForwardedEvent.java b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/ServerSwitchMessageForwardedEvent.java index 3c088980..e1bd3610 100644 --- a/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/ServerSwitchMessageForwardedEvent.java +++ b/api/src/main/java/com/discordsrv/api/event/events/message/forward/game/ServerSwitchMessageForwardedEvent.java @@ -23,15 +23,20 @@ package com.discordsrv.api.event.events.message.forward.game; +import com.discordsrv.api.channel.GameChannel; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessageCluster; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Indicates that a server switch message was forwarded to Discord. */ public class ServerSwitchMessageForwardedEvent extends AbstractGameMessageForwardedEvent { - public ServerSwitchMessageForwardedEvent(@NotNull ReceivedDiscordMessageCluster discordMessage) { - super(discordMessage); + public ServerSwitchMessageForwardedEvent( + @Nullable GameChannel originGameChannel, + @NotNull ReceivedDiscordMessageCluster discordMessage + ) { + super(originGameChannel, discordMessage); } } diff --git a/common/src/main/java/com/discordsrv/common/config/main/generic/DestinationConfig.java b/common/src/main/java/com/discordsrv/common/config/main/generic/DestinationConfig.java index 201d0ead..ed931ed4 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/generic/DestinationConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/generic/DestinationConfig.java @@ -18,6 +18,8 @@ package com.discordsrv.common.config.main.generic; +import com.discordsrv.api.discord.entity.channel.DiscordChannel; +import com.discordsrv.api.discord.entity.channel.DiscordThreadChannel; import com.discordsrv.common.config.configurate.annotation.Constants; import org.apache.commons.lang3.StringUtils; import org.spongepowered.configurate.objectmapping.ConfigSerializable; @@ -73,6 +75,21 @@ public class DestinationConfig { } } + public boolean contains(DiscordChannel channel) { + if (channel instanceof DiscordThreadChannel) { + long parentId = ((DiscordThreadChannel) channel).getParentChannel().getId(); + String threadName = ((DiscordThreadChannel) channel).getName(); + for (ThreadConfig thread : threads) { + if (thread.channelId == parentId && thread.threadName.equals(threadName)) { + return true; + } + } + return false; + } + + return channelIds.contains(channel.getId()); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/common/src/main/java/com/discordsrv/common/console/SingleConsoleHandler.java b/common/src/main/java/com/discordsrv/common/console/SingleConsoleHandler.java index 0672b47a..a4c0a5a9 100644 --- a/common/src/main/java/com/discordsrv/common/console/SingleConsoleHandler.java +++ b/common/src/main/java/com/discordsrv/common/console/SingleConsoleHandler.java @@ -22,7 +22,6 @@ import com.discordsrv.api.discord.entity.DiscordUser; import com.discordsrv.api.discord.entity.channel.DiscordGuildChannel; import com.discordsrv.api.discord.entity.channel.DiscordGuildMessageChannel; import com.discordsrv.api.discord.entity.channel.DiscordMessageChannel; -import com.discordsrv.api.discord.entity.channel.DiscordThreadChannel; import com.discordsrv.api.discord.entity.guild.DiscordGuildMember; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessage; import com.discordsrv.api.discord.entity.message.SendableDiscordMessage; @@ -105,23 +104,7 @@ public class SingleConsoleHandler { } DestinationConfig.Single destination = config.channel; - String threadName = destination.thread.threadName; - - DiscordGuildChannel checkChannel; - if (StringUtils.isNotEmpty(threadName)) { - if (!(channel instanceof DiscordThreadChannel)) { - return; - } - - if (!channel.getName().equals(threadName)) { - return; - } - - checkChannel = ((DiscordThreadChannel) channel).getParentChannel(); - } else { - checkChannel = channel; - } - if (checkChannel.getId() != destination.channelId) { + if (!destination.asDestination().contains(channel)) { return; } diff --git a/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java b/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java index 1656a523..66c13e6a 100644 --- a/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java +++ b/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java @@ -330,12 +330,28 @@ public class DiscordMessageMirroringModule extends AbstractModule { public void onGameMessageForwarded(AbstractGameMessageForwardedEvent event) { Set messages = event.getDiscordMessage().getMessages(); + GameChannel gameChannel = event.getOriginGameChannel(); + BaseChannelConfig gameChannelConfig = gameChannel != null ? discordSRV.channelConfig().get(gameChannel) : null; + Map references = new LinkedHashMap<>(); for (ReceivedDiscordMessage message : messages) { - DiscordMessageChannel channel = message.getChannel(); + BaseChannelConfig channelConfig = gameChannelConfig; + if (channelConfig == null) { + DiscordMessageChannel channel = message.getChannel(); - MirroringConfig config = discordSRV.channelConfig().resolve(channel).values().iterator().next().mirroring; // TODO: add channel to event - MessageReference reference = getReference(message, config); + channelConfig = discordSRV.channelConfig() + .resolve(channel) + .values() + .stream() + .filter(config -> config instanceof IChannelConfig && ((IChannelConfig) config).destination().contains(channel)) + .findAny() + .orElse(null); + } + if (channelConfig == null) { + continue; + } + + MessageReference reference = getReference(message, channelConfig.mirroring); references.put(message, reference); } diff --git a/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java b/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java index 9f0cc4ac..bfbcbe86 100644 --- a/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java +++ b/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java @@ -54,11 +54,11 @@ import java.util.concurrent.CompletableFuture; *

* Order of operations: * - Event (E generic) is received, implementation calls {@link #process(AbstractGameMessageReceiveEvent, DiscordSRVPlayer, GameChannel)} - * - {@link IPlayer} and {@link BaseChannelConfig} (uses {@link #mapConfig(AbstractGameMessageReceiveEvent, BaseChannelConfig)} are resolved, then {@link #forwardToChannel(AbstractGameMessageReceiveEvent, IPlayer, BaseChannelConfig)} is called + * - {@link IPlayer} and {@link BaseChannelConfig} (uses {@link #mapConfig(AbstractGameMessageReceiveEvent, BaseChannelConfig)} are resolved, then {@link #forwardToChannel(AbstractGameMessageReceiveEvent, IPlayer, BaseChannelConfig, GameChannel)} is called * - Destinations are looked up and {@link #sendMessageToChannels} gets called * - {@link #setPlaceholders(IMessageConfig, AbstractGameMessageReceiveEvent, SendableDiscordMessage.Formatter)} is called to set any additional placeholders * - {@link #sendMessageToChannel(DiscordGuildMessageChannel, SendableDiscordMessage)} is called (once per channel) to send messages to individual channels - * - {@link #postClusterToEventBus(ReceivedDiscordMessageCluster)} is called with all messages that were sent (if any messages were sent) + * - {@link #postClusterToEventBus(GameChannel, ReceivedDiscordMessageCluster)} is called with all messages that were sent (if any messages were sent) * * @param config model * @param the event indicating a message was received from in-game, of type {@link AbstractGameMessageReceiveEvent} @@ -84,7 +84,7 @@ public abstract class AbstractGameMessageModule process( @Nullable E event, @@ -100,7 +100,7 @@ public abstract class AbstractGameMessageModule> futures = new ArrayList<>(); for (BaseChannelConfig channelConfig : discordSRV.channelConfig().getAllChannels()) { - CompletableFuture future = forwardToChannel(event, srvPlayer, channelConfig); + CompletableFuture future = forwardToChannel(event, srvPlayer, channelConfig, null); if (future != null) { futures.add(future); } @@ -113,14 +113,15 @@ public abstract class AbstractGameMessageModule CompletableFuture forwardToChannel( @Nullable E event, @Nullable IPlayer player, - @NotNull BaseChannelConfig config + @NotNull BaseChannelConfig config, + @Nullable GameChannel channel ) { T moduleConfig = mapConfig(event, config); if (!moduleConfig.enabled()) { @@ -158,7 +159,7 @@ public abstract class AbstractGameMessageModule { discordSRV.logger().error("Failed to publish to event bus", t); TestHelper.fail(t); diff --git a/common/src/main/java/com/discordsrv/common/messageforwarding/game/AwardMessageModule.java b/common/src/main/java/com/discordsrv/common/messageforwarding/game/AwardMessageModule.java index 11b134f4..985a0ea0 100644 --- a/common/src/main/java/com/discordsrv/common/messageforwarding/game/AwardMessageModule.java +++ b/common/src/main/java/com/discordsrv/common/messageforwarding/game/AwardMessageModule.java @@ -18,6 +18,7 @@ package com.discordsrv.common.messageforwarding.game; +import com.discordsrv.api.channel.GameChannel; import com.discordsrv.api.component.MinecraftComponent; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessageCluster; import com.discordsrv.api.discord.entity.message.SendableDiscordMessage; @@ -33,6 +34,8 @@ import com.discordsrv.common.config.main.channels.base.server.ServerBaseChannelC import com.discordsrv.common.config.main.channels.server.AwardMessageConfig; import com.github.benmanes.caffeine.cache.Cache; import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -81,8 +84,8 @@ public class AwardMessageModule extends AbstractGameMessageModule { @@ -53,8 +55,8 @@ public class DeathMessageModule extends AbstractGameMessageModule forwardToChannel( @Nullable JoinMessageReceiveEvent event, @Nullable IPlayer player, - @NotNull BaseChannelConfig config + @NotNull BaseChannelConfig config, + @Nullable GameChannel channel ) { if (config.joinMessages().enableSilentPermission && player != null && player.hasPermission(Permission.SILENT_JOIN)) { logger().info(player.username() + " is joining silently, join message will not be sent"); @@ -79,7 +81,7 @@ public class JoinMessageModule extends AbstractGameMessageModule completableFuture = new CompletableFuture<>(); synchronized (delayedTasks) { CompletableFuture future = discordSRV.scheduler() - .supplyLater(() -> super.forwardToChannel(event, player, config), Duration.ofMillis(delay)) + .supplyLater(() -> super.forwardToChannel(event, player, config, channel), Duration.ofMillis(delay)) .thenCompose(r -> r) .whenComplete((v, t) -> delayedTasks.remove(playerUUID)); @@ -87,7 +89,7 @@ public class JoinMessageModule extends AbstractGameMessageModule forwardToChannel( @Nullable LeaveMessageReceiveEvent event, @Nullable IPlayer player, - @NotNull BaseChannelConfig config + @NotNull BaseChannelConfig config, + @Nullable GameChannel channel ) { if (player != null) { Pair> pair = playersJoinedRecently.remove(player.uniqueId()); @@ -105,7 +107,7 @@ public class LeaveMessageModule extends AbstractGameMessageModule { @@ -54,8 +56,8 @@ public class ServerSwitchMessageModule extends AbstractGameMessageModule> sendMessageToChannels( diff --git a/common/src/main/java/com/discordsrv/common/messageforwarding/game/StopMessageModule.java b/common/src/main/java/com/discordsrv/common/messageforwarding/game/StopMessageModule.java index 301990f9..e5c4b916 100644 --- a/common/src/main/java/com/discordsrv/common/messageforwarding/game/StopMessageModule.java +++ b/common/src/main/java/com/discordsrv/common/messageforwarding/game/StopMessageModule.java @@ -18,6 +18,7 @@ package com.discordsrv.common.messageforwarding.game; +import com.discordsrv.api.channel.GameChannel; import com.discordsrv.api.discord.entity.channel.DiscordGuildMessageChannel; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessage; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessageCluster; @@ -27,6 +28,7 @@ import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.config.main.channels.StopMessageConfig; import com.discordsrv.common.config.main.channels.base.BaseChannelConfig; import com.discordsrv.common.player.IPlayer; +import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.Collections; @@ -53,7 +55,7 @@ public class StopMessageModule extends AbstractGameMessageModule> sendMessageToChannels( diff --git a/common/src/main/java/com/discordsrv/common/messageforwarding/game/minecrafttodiscord/MinecraftToDiscordChatModule.java b/common/src/main/java/com/discordsrv/common/messageforwarding/game/minecrafttodiscord/MinecraftToDiscordChatModule.java index 7634b164..839092e2 100644 --- a/common/src/main/java/com/discordsrv/common/messageforwarding/game/minecrafttodiscord/MinecraftToDiscordChatModule.java +++ b/common/src/main/java/com/discordsrv/common/messageforwarding/game/minecrafttodiscord/MinecraftToDiscordChatModule.java @@ -43,6 +43,7 @@ import com.discordsrv.common.player.IPlayer; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Role; import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.NotNull; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -73,8 +74,8 @@ public class MinecraftToDiscordChatModule extends AbstractGameMessageModule