diff --git a/api/src/main/java/com/discordsrv/api/discord/entity/message/DiscordMessageEmbed.java b/api/src/main/java/com/discordsrv/api/discord/entity/message/DiscordMessageEmbed.java index 45231f5f..40ad3431 100644 --- a/api/src/main/java/com/discordsrv/api/discord/entity/message/DiscordMessageEmbed.java +++ b/api/src/main/java/com/discordsrv/api/discord/entity/message/DiscordMessageEmbed.java @@ -48,6 +48,7 @@ public static Builder builder() { } private final Color color; + private final String unformattedColor; private final String authorName, authorUrl, authorImageUrl; private final String title, titleUrl; private final String description; @@ -55,6 +56,7 @@ public static Builder builder() { private final String thumbnailUrl; private final String imageUrl; private final OffsetDateTime timestamp; + private final String unformattedTimestamp; private final String footer, footerImageUrl; public DiscordMessageEmbed(MessageEmbed embed) { @@ -69,6 +71,7 @@ public DiscordMessageEmbed(MessageEmbed embed) { } this.color = new Color(embed.getColorRaw()); + this.unformattedColor = String.valueOf(embed.getColorRaw()); this.authorName = author != null ? author.getName(): null; this.authorUrl = author != null ? author.getUrl() : null; this.authorImageUrl = author != null ? author.getIconUrl() : null; @@ -79,14 +82,16 @@ public DiscordMessageEmbed(MessageEmbed embed) { this.thumbnailUrl = thumbnail != null ? thumbnail.getUrl() : null; this.imageUrl = image != null ? image.getUrl() : null; this.timestamp = embed.getTimestamp(); + this.unformattedTimestamp = embed.getTimestamp() != null ? embed.getTimestamp().toString() : null; this.footer = footer != null ? footer.getText() : null; this.footerImageUrl = footer != null ? footer.getIconUrl() : null; } - public DiscordMessageEmbed(Color color, String authorName, String authorUrl, String authorAvatarUrl, String title, + public DiscordMessageEmbed(Color color, String unformattedColor, String authorName, String authorUrl, String authorAvatarUrl, String title, String titleUrl, String description, List fields, String thumbnailUrl, - String imageUrl, OffsetDateTime timestamp, String footer, String footerImageUrl) { + String imageUrl, OffsetDateTime timestamp, String unformattedTimestamp, String footer, String footerImageUrl) { this.color = color; + this.unformattedColor = unformattedColor; this.authorName = authorName; this.authorUrl = authorUrl; this.authorImageUrl = authorAvatarUrl; @@ -97,6 +102,7 @@ public DiscordMessageEmbed(Color color, String authorName, String authorUrl, Str this.thumbnailUrl = thumbnailUrl; this.imageUrl = imageUrl; this.timestamp = timestamp; + this.unformattedTimestamp = unformattedTimestamp; this.footer = footer; this.footerImageUrl = footerImageUrl; } @@ -106,6 +112,11 @@ public Color getColor() { return color; } + @Nullable + public String getUnformattedColor() { + return unformattedColor; + } + @Nullable public String getAuthorName() { return authorName; @@ -156,6 +167,11 @@ public OffsetDateTime getTimestamp() { return timestamp; } + @Nullable + public String getUnformattedTimestamp() { + return unformattedTimestamp; + } + @Nullable public String getFooter() { return footer; @@ -221,6 +237,7 @@ public boolean isInline() { public static class Builder { private Color color; + private String unformattedColor; private String authorName, authorUrl, authorImageUrl; private String title, titleUrl; private String description; @@ -228,16 +245,18 @@ public static class Builder { private String thumbnailUrl; private String imageUrl; private OffsetDateTime timestamp; + private String unformattedTimestamp; private String footer, footerImageUrl; protected Builder() { this.fields = new ArrayList<>(); } - protected Builder(Color color, String authorName, String authorUrl, String authorImageUrl, String title, + protected Builder(Color color, String unformattedColor, String authorName, String authorUrl, String authorImageUrl, String title, String titleUrl, String description, List fields, String thumbnailUrl, String imageUrl, - OffsetDateTime timestamp, String footer, String footerImageUrl) { + OffsetDateTime timestamp, String unformattedTimestamp, String footer, String footerImageUrl) { this.color = color; + this.unformattedColor = unformattedColor; this.authorName = authorName; this.authorUrl = authorUrl; this.authorImageUrl = authorImageUrl; @@ -248,12 +267,14 @@ protected Builder(Color color, String authorName, String authorUrl, String autho this.thumbnailUrl = thumbnailUrl; this.imageUrl = imageUrl; this.timestamp = timestamp; + this.unformattedTimestamp = unformattedTimestamp; this.footer = footer; this.footerImageUrl = footerImageUrl; } protected Builder(DiscordMessageEmbed embed) { this.color = embed.getColor(); + this.unformattedColor = embed.getUnformattedColor(); this.authorName = embed.getAuthorName(); this.authorUrl = embed.getAuthorUrl(); this.authorImageUrl = embed.getAuthorImageUrl(); @@ -264,6 +285,7 @@ protected Builder(DiscordMessageEmbed embed) { this.thumbnailUrl = embed.getThumbnailUrl(); this.imageUrl = embed.getImageUrl(); this.timestamp = embed.getTimestamp(); + this.unformattedTimestamp = embed.getUnformattedTimestamp(); this.footer = embed.getFooter(); this.footerImageUrl = embed.getFooterImageUrl(); } @@ -284,6 +306,17 @@ public Builder setColor(int rgb) { return setColor(new Color(rgb)); } + @Nullable + public String getUnformattedColor() { + return unformattedColor; + } + + @NotNull + public Builder setUnformattedColor(String unformattedColor) { + this.unformattedColor = unformattedColor; + return this; + } + @NotNull public Builder setAuthor(@Nullable CharSequence authorName, @Nullable CharSequence authorUrl) { return setAuthor(authorName, authorUrl, null); @@ -425,6 +458,17 @@ public Builder setTimestamp(@Nullable OffsetDateTime timestamp) { return this; } + @Nullable + public String getUnformattedTimestamp() { + return unformattedTimestamp; + } + + @NotNull + public Builder setUnformattedTimestamp(String unformattedTimestamp) { + this.unformattedTimestamp = unformattedTimestamp; + return this; + } + @NotNull public Builder setFooter(@Nullable CharSequence footer, @Nullable CharSequence footerImageUrl) { this.footer = footer != null ? footer.toString() : null; @@ -456,16 +500,16 @@ public Builder setFooterImageUrl(@Nullable CharSequence footerImageUrl) { @NotNull public DiscordMessageEmbed build() { - return new DiscordMessageEmbed(color, authorName, authorUrl, authorImageUrl, title, titleUrl, description, - fields, thumbnailUrl, imageUrl, timestamp, footer, footerImageUrl); + return new DiscordMessageEmbed(color, unformattedColor, authorName, authorUrl, authorImageUrl, title, titleUrl, description, + fields, thumbnailUrl, imageUrl, timestamp, unformattedTimestamp, footer, footerImageUrl); } @SuppressWarnings({"MethodDoesntCallSuperMethod"}) @Override @NotNull public Builder clone() { - return new Builder(color, authorName, authorUrl, authorImageUrl, title, titleUrl, description, - fields, thumbnailUrl, imageUrl, timestamp, footer, footerImageUrl); + return new Builder(color, unformattedColor, authorName, authorUrl, authorImageUrl, title, titleUrl, description, + fields, thumbnailUrl, imageUrl, timestamp, unformattedTimestamp, footer, footerImageUrl); } } } diff --git a/api/src/main/java/com/discordsrv/api/discord/entity/message/impl/SendableDiscordMessageImpl.java b/api/src/main/java/com/discordsrv/api/discord/entity/message/impl/SendableDiscordMessageImpl.java index a3d56eda..e9a44b50 100644 --- a/api/src/main/java/com/discordsrv/api/discord/entity/message/impl/SendableDiscordMessageImpl.java +++ b/api/src/main/java/com/discordsrv/api/discord/entity/message/impl/SendableDiscordMessageImpl.java @@ -24,6 +24,7 @@ package com.discordsrv.api.discord.entity.message.impl; import com.discordsrv.api.DiscordSRVApi; +import com.discordsrv.api.color.Color; import com.discordsrv.api.discord.entity.interaction.component.actionrow.MessageActionRow; import com.discordsrv.api.discord.entity.message.AllowedMention; import com.discordsrv.api.discord.entity.message.DiscordMessageEmbed; @@ -38,6 +39,8 @@ import org.jetbrains.annotations.Nullable; import java.io.InputStream; +import java.time.OffsetDateTime; +import java.time.ZonedDateTime; import java.util.*; import java.util.function.Function; import java.util.regex.Matcher; @@ -420,6 +423,13 @@ private Function wrapFunction(Function functio for (DiscordMessageEmbed embed : embeds) { DiscordMessageEmbed.Builder embedBuilder = embed.toBuilder(); + Color color = embedBuilder.getColor(); + String formattedColorHex = placeholders.apply(embedBuilder.getUnformattedColor()); + try { + color = new Color(formattedColorHex); + } catch (Throwable ignored) {} + embedBuilder.setColor(color); + embedBuilder.setAuthor( cutToLength( placeholders.apply(embedBuilder.getAuthorName()), @@ -452,6 +462,12 @@ private Function wrapFunction(Function functio placeholders.apply(embedBuilder.getFooterImageUrl()) ); + embedBuilder.setTimestamp( + parseTimestamp( + placeholders.apply(embedBuilder.getUnformattedTimestamp()) + ) + ); + PlainPlaceholderFormat.with(PlainPlaceholderFormat.Formatting.DISCORD, () -> embedBuilder.setDescription( cutToLength( markdownPlaceholders.apply(embedBuilder.getDescription()), @@ -494,5 +510,17 @@ private String cutToLength(String input, int maxLength) { } return input; } + + private OffsetDateTime parseTimestamp(String timestamp) { + try { + return OffsetDateTime.parse(timestamp); + } catch (Throwable ignored) { + try { + return ZonedDateTime.parse(timestamp).toOffsetDateTime(); + } catch (Throwable ignoredAgain) { + return null; + } + } + } } } diff --git a/api/src/main/java/com/discordsrv/api/events/message/receive/game/AwardMessageReceiveEvent.java b/api/src/main/java/com/discordsrv/api/events/message/receive/game/AwardMessageReceiveEvent.java index 316c16d9..02f97240 100644 --- a/api/src/main/java/com/discordsrv/api/events/message/receive/game/AwardMessageReceiveEvent.java +++ b/api/src/main/java/com/discordsrv/api/events/message/receive/game/AwardMessageReceiveEvent.java @@ -24,6 +24,7 @@ package com.discordsrv.api.events.message.receive.game; import com.discordsrv.api.channel.GameChannel; +import com.discordsrv.api.color.Color; import com.discordsrv.api.component.MinecraftComponent; import com.discordsrv.api.eventbus.EventPriorities; import com.discordsrv.api.events.PlayerEvent; @@ -40,6 +41,8 @@ public class AwardMessageReceiveEvent extends AbstractGameMessageReceiveEvent im private final DiscordSRVPlayer player; private MinecraftComponent name; private MinecraftComponent title; + private MinecraftComponent description; + private AdvancementFrame frame; private GameChannel gameChannel; public AwardMessageReceiveEvent( @@ -47,6 +50,8 @@ public AwardMessageReceiveEvent( @NotNull DiscordSRVPlayer player, @Nullable MinecraftComponent name, @Nullable MinecraftComponent title, + @Nullable MinecraftComponent description, + @Nullable AdvancementFrame frame, @Nullable GameChannel gameChannel, boolean cancelled ) { @@ -54,6 +59,8 @@ public AwardMessageReceiveEvent( this.player = player; this.name = name; this.title = title; + this.description = description; + this.frame = frame; this.gameChannel = gameChannel; } @@ -81,6 +88,23 @@ public void setTitle(@Nullable MinecraftComponent title) { this.title = title; } + @Nullable + public MinecraftComponent getDescription() { + return description; + } + + public void setDescription(@Nullable MinecraftComponent description) { + this.description = description; + } + + @Nullable public AdvancementFrame getFrame() { + return frame; + } + + public void setFrame(@Nullable AdvancementFrame frame) { + this.frame = frame; + } + public GameChannel getGameChannel() { return gameChannel; } @@ -96,4 +120,26 @@ public String toString() { + "gameChannel=" + GameChannel.toString(gameChannel) + '}'; } + + public enum AdvancementFrame { + TASK("task", new Color(0x55FF55)), // Green + GOAL("goal", new Color(0x55FF55)), // Green + CHALLENGE("challenge", new Color(0xAA00AA)); // Dark Purple + + private final String name; + private final Color color; + + AdvancementFrame(String id, Color color) { + this.name = id; + this.color = color; + } + + public String id() { + return name; + } + + public Color color() { + return color; + } + } } diff --git a/bukkit/compat/bukkit1_12/src/main/java/com/discordsrv/bukkit/listener/BukkitLegacyAdvancementListener.java b/bukkit/compat/bukkit1_12/src/main/java/com/discordsrv/bukkit/listener/BukkitLegacyAdvancementListener.java index 64a314ab..13d237a6 100644 --- a/bukkit/compat/bukkit1_12/src/main/java/com/discordsrv/bukkit/listener/BukkitLegacyAdvancementListener.java +++ b/bukkit/compat/bukkit1_12/src/main/java/com/discordsrv/bukkit/listener/BukkitLegacyAdvancementListener.java @@ -96,6 +96,8 @@ protected void handleEvent(@NotNull PlayerAdvancementDoneEvent event, Void __) { title, null, null, + null, + null, false ) ); diff --git a/bukkit/compat/paper/src/main/java/com/discordsrv/bukkit/listener/PaperAdvancementListener.java b/bukkit/compat/paper/src/main/java/com/discordsrv/bukkit/listener/PaperAdvancementListener.java index 913ee244..6a4b9f67 100644 --- a/bukkit/compat/paper/src/main/java/com/discordsrv/bukkit/listener/PaperAdvancementListener.java +++ b/bukkit/compat/paper/src/main/java/com/discordsrv/bukkit/listener/PaperAdvancementListener.java @@ -43,6 +43,8 @@ public class PaperAdvancementListener extends AbstractBukkitListener ADVANCEMENT_DISPLAY_NAME_HANDLE = PaperComponentHandle.get(Advancement.class, "displayName"); + private static final PaperComponentHandle.Get ADVANCEMENT_DESCRIPTION_HANDLE + = PaperComponentHandle.get(AdvancementDisplay.class, "description"); public PaperAdvancementListener(BukkitDiscordSRV discordSRV) { super(discordSRV, new NamedLogger(discordSRV, "ADVANCEMENT_LISTENER")); @@ -70,15 +72,18 @@ protected void handleEvent(@NotNull PlayerAdvancementDoneEvent event, Void __) { } MinecraftComponent message = MESSAGE_HANDLE.getAPI(event); - MinecraftComponent displayName = ADVANCEMENT_DISPLAY_NAME_HANDLE.getAPI(advancement); + MinecraftComponent name = ADVANCEMENT_DISPLAY_NAME_HANDLE.getAPI(advancement); + MinecraftComponent description = ADVANCEMENT_DESCRIPTION_HANDLE.getAPI(display); IPlayer player = discordSRV.playerProvider().player(event.getPlayer()); discordSRV.eventBus().publish( new AwardMessageReceiveEvent( event, player, + name, message, - displayName, + description, + AwardMessageReceiveEvent.AdvancementFrame.valueOf(display.frame().toString()), null, message == null ) diff --git a/bukkit/compat/spigot/src/main/java/com/discordsrv/bukkit/listener/SpigotAdvancementListener.java b/bukkit/compat/spigot/src/main/java/com/discordsrv/bukkit/listener/SpigotAdvancementListener.java index 38fe1d93..9c5f81dc 100644 --- a/bukkit/compat/spigot/src/main/java/com/discordsrv/bukkit/listener/SpigotAdvancementListener.java +++ b/bukkit/compat/spigot/src/main/java/com/discordsrv/bukkit/listener/SpigotAdvancementListener.java @@ -64,7 +64,8 @@ protected void handleEvent(@NotNull PlayerAdvancementDoneEvent event, Void __) { return; } - MinecraftComponent title = ComponentUtil.toAPI(BukkitComponentSerializer.legacy().deserialize(display.getTitle())) ; + MinecraftComponent title = ComponentUtil.toAPI(BukkitComponentSerializer.legacy().deserialize(display.getTitle())); + MinecraftComponent description = ComponentUtil.toAPI(BukkitComponentSerializer.legacy().deserialize(display.getDescription())); IPlayer srvPlayer = discordSRV.playerProvider().player(event.getPlayer()); discordSRV.eventBus().publish( new AwardMessageReceiveEvent( @@ -72,6 +73,8 @@ protected void handleEvent(@NotNull PlayerAdvancementDoneEvent event, Void __) { srvPlayer, null, title, + description, + AwardMessageReceiveEvent.AdvancementFrame.valueOf(display.getType().toString()), null, false ) diff --git a/bukkit/shared/src/main/java/com/discordsrv/bukkit/listener/BukkitAchievementListener.java b/bukkit/shared/src/main/java/com/discordsrv/bukkit/listener/BukkitAchievementListener.java index e33c59db..599bee7b 100644 --- a/bukkit/shared/src/main/java/com/discordsrv/bukkit/listener/BukkitAchievementListener.java +++ b/bukkit/shared/src/main/java/com/discordsrv/bukkit/listener/BukkitAchievementListener.java @@ -56,6 +56,8 @@ protected void handleEvent(@NotNull PlayerAchievementAwardedEvent event, Void __ achievementName, null, null, + null, + null, event.isCancelled() ) ); diff --git a/common/src/main/java/com/discordsrv/common/config/configurate/serializer/DiscordMessageEmbedSerializer.java b/common/src/main/java/com/discordsrv/common/config/configurate/serializer/DiscordMessageEmbedSerializer.java index f2beeee3..2e98bf0d 100644 --- a/common/src/main/java/com/discordsrv/common/config/configurate/serializer/DiscordMessageEmbedSerializer.java +++ b/common/src/main/java/com/discordsrv/common/config/configurate/serializer/DiscordMessageEmbedSerializer.java @@ -21,7 +21,6 @@ import com.discordsrv.api.color.Color; import com.discordsrv.api.discord.entity.message.DiscordMessageEmbed; import com.discordsrv.common.config.configurate.manager.abstraction.ConfigurateConfigManager; -import net.dv8tion.jda.api.entities.Role; import org.jetbrains.annotations.Nullable; import org.spongepowered.configurate.ConfigurationNode; import org.spongepowered.configurate.serialize.SerializationException; @@ -29,6 +28,7 @@ import org.spongepowered.configurate.util.NamingScheme; import java.lang.reflect.Type; +import java.time.OffsetDateTime; import java.util.Collections; import java.util.List; @@ -57,7 +57,12 @@ public DiscordMessageEmbed.Builder deserialize(Type type, ConfigurationNode node DiscordMessageEmbed.Builder builder = DiscordMessageEmbed.builder(); Color color = node.node(map("Color")).get(Color.class); - builder.setColor(color != null ? color.rgb() : Role.DEFAULT_COLOR_RAW); + if (color == null) { + String unformattedColor = node.node(map("Color")).getString(); + builder.setUnformattedColor(unformattedColor); + } else { + builder.setColor(color); + } ConfigurationNode author = node.node(map("Author")); builder.setAuthor( @@ -78,7 +83,13 @@ public DiscordMessageEmbed.Builder deserialize(Type type, ConfigurationNode node builder.setThumbnailUrl(node.node(map("ThumbnailUrl")).getString()); builder.setImageUrl(node.node(map("ImageUrl")).getString()); - // TODO: timestamp + OffsetDateTime timestamp = node.node(map("Timestamp")).get(OffsetDateTime.class); + if (timestamp == null) { + String unformattedTimestamp = node.node(map("Timestamp")).getString(); + builder.setUnformattedTimestamp(unformattedTimestamp); + } else { + builder.setTimestamp(timestamp); + } ConfigurationNode footer = node.node(map("Footer")); builder.setFooter( @@ -100,7 +111,11 @@ public void serialize(Type type, DiscordMessageEmbed.@Nullable Builder obj, Conf return; } - node.node(map("Color")).set(obj.getColor()); + if (obj.getUnformattedColor() != null) { + node.node(map("Color")).set(obj.getUnformattedColor()); + } else { + node.node(map("Color")).set(obj.getColor()); + } ConfigurationNode author = node.node(map("Author")); author.node(map("Name")).set(obj.getAuthorName()); @@ -120,6 +135,12 @@ public void serialize(Type type, DiscordMessageEmbed.@Nullable Builder obj, Conf node.node(map("ThumbnailUrl")).set(obj.getThumbnailUrl()); node.node(map("ImageUrl")).set(obj.getImageUrl()); + if (obj.getUnformattedTimestamp() != null) { + node.node(map("Timestamp")).set(obj.getUnformattedTimestamp()); + } else { + node.node(map("Timestamp")).set(obj.getTimestamp()); + } + ConfigurationNode footer = node.node(map("Footer")); footer.node(map("Text")).set(obj.getFooter()); footer.node(map("ImageUrl")).set(obj.getFooterImageUrl()); diff --git a/common/src/main/java/com/discordsrv/common/config/main/channels/proxy/ServerSwitchMessageConfig.java b/common/src/main/java/com/discordsrv/common/config/main/channels/proxy/ServerSwitchMessageConfig.java index 453e4bbf..22135e7b 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/channels/proxy/ServerSwitchMessageConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/channels/proxy/ServerSwitchMessageConfig.java @@ -43,7 +43,7 @@ public ServerSwitchMessageConfig() { null, "%player_avatar_url%" ) - .setColor(0x5555FF) + .setColor(0xFFFF55) .build() ); diff --git a/common/src/main/java/com/discordsrv/common/config/main/channels/server/AwardMessageConfig.java b/common/src/main/java/com/discordsrv/common/config/main/channels/server/AwardMessageConfig.java index 266f3a52..cb805de4 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/channels/server/AwardMessageConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/channels/server/AwardMessageConfig.java @@ -43,7 +43,8 @@ public AwardMessageConfig() { null, "%player_avatar_url%" ) - .setColor(1) + .setDescription("%award_description%") + .setUnformattedColor("%award_color_hex%") .build() ); diff --git a/common/src/main/java/com/discordsrv/common/config/main/channels/server/DeathMessageConfig.java b/common/src/main/java/com/discordsrv/common/config/main/channels/server/DeathMessageConfig.java index f9b9cf03..4d0dc439 100644 --- a/common/src/main/java/com/discordsrv/common/config/main/channels/server/DeathMessageConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/main/channels/server/DeathMessageConfig.java @@ -18,6 +18,7 @@ package com.discordsrv.common.config.main.channels.server; +import com.discordsrv.api.color.Color; import com.discordsrv.api.discord.entity.message.DiscordMessageEmbed; import com.discordsrv.api.discord.entity.message.SendableDiscordMessage; import com.discordsrv.common.config.configurate.annotation.Untranslated; @@ -39,7 +40,7 @@ public DeathMessageConfig() { .addEmbed( DiscordMessageEmbed.builder() .setAuthor("%message%", null, "%player_avatar_url%") - .setColor(1) + .setColor(0xFF0000) .build() ); diff --git a/common/src/main/java/com/discordsrv/common/core/component/ComponentFactory.java b/common/src/main/java/com/discordsrv/common/core/component/ComponentFactory.java index f7bc0ae1..5963b019 100644 --- a/common/src/main/java/com/discordsrv/common/core/component/ComponentFactory.java +++ b/common/src/main/java/com/discordsrv/common/core/component/ComponentFactory.java @@ -57,9 +57,12 @@ import java.util.HashSet; import java.util.Set; +import java.util.regex.Pattern; public class ComponentFactory implements MinecraftComponentFactory { + private static final String TRANSLATION_KEY_REGEX = ".+\\..+"; + private static final Pattern TRANSLATION_KEY_PATTERN = Pattern.compile(TRANSLATION_KEY_REGEX); public static final Class UNRELOCATED_ADVENTURE_COMPONENT; static { @@ -122,6 +125,12 @@ private String provideTranslation(TranslatableComponent component) { Translation translation = translationRegistry.lookup(discordSRV.defaultLocale(), key); if (translation == null) { + // To support datapacks and other mods that don't provide translations but for some reason use the translation component + // We check if the key is following the pattern of a translation key. Which is "key.subkey" or "key.subkey.subsubkey" etc. + if (!TRANSLATION_KEY_PATTERN.matcher(key).matches()) { + return key; + } + return null; } diff --git a/common/src/main/java/com/discordsrv/common/core/placeholder/context/DateFormattingContext.java b/common/src/main/java/com/discordsrv/common/core/placeholder/context/DateFormattingContext.java index 6671e746..5486735c 100644 --- a/common/src/main/java/com/discordsrv/common/core/placeholder/context/DateFormattingContext.java +++ b/common/src/main/java/com/discordsrv/common/core/placeholder/context/DateFormattingContext.java @@ -25,6 +25,7 @@ import com.github.benmanes.caffeine.cache.LoadingCache; import java.time.DateTimeException; +import java.time.OffsetDateTime; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; @@ -73,4 +74,13 @@ public ZonedDateTime getStartDate() { return discordSRV.getInitializeTime(); } + @Placeholder(value = "now_date", relookup = "date") + public ZonedDateTime getNowDate() { + return ZonedDateTime.now(); + } + + @Placeholder(value = "now_time") + public OffsetDateTime getTimeNow() { + return OffsetDateTime.now(); + } } diff --git a/common/src/main/java/com/discordsrv/common/feature/messageforwarding/game/AwardMessageModule.java b/common/src/main/java/com/discordsrv/common/feature/messageforwarding/game/AwardMessageModule.java index 49bb7f50..e58094c8 100644 --- a/common/src/main/java/com/discordsrv/common/feature/messageforwarding/game/AwardMessageModule.java +++ b/common/src/main/java/com/discordsrv/common/feature/messageforwarding/game/AwardMessageModule.java @@ -19,6 +19,7 @@ package com.discordsrv.common.feature.messageforwarding.game; import com.discordsrv.api.channel.GameChannel; +import com.discordsrv.api.color.Color; import com.discordsrv.api.component.MinecraftComponent; import com.discordsrv.api.discord.entity.message.ReceivedDiscordMessageCluster; import com.discordsrv.api.discord.entity.message.SendableDiscordMessage; @@ -97,8 +98,15 @@ public void setPlaceholders(AwardMessageConfig config, AwardMessageReceiveEvent MinecraftComponent titleComponent = event.getTitle(); Component title = titleComponent != null ? ComponentUtil.fromAPI(titleComponent) : null; + MinecraftComponent descriptionComponent = event.getDescription(); + Component description = descriptionComponent != null ? ComponentUtil.fromAPI(descriptionComponent) : null; + + Color color = event.getFrame() != null ? event.getFrame().color() : null; + formatter .addPlaceholder("award_name", name) - .addPlaceholder("award_title", title); + .addPlaceholder("award_title", title) + .addPlaceholder("award_description", description) + .addPlaceholder("award_color", color, "color"); } } diff --git a/common/src/main/java/com/discordsrv/common/feature/messageforwarding/game/LeaveMessageModule.java b/common/src/main/java/com/discordsrv/common/feature/messageforwarding/game/LeaveMessageModule.java index ca411fc2..7c4a4b51 100644 --- a/common/src/main/java/com/discordsrv/common/feature/messageforwarding/game/LeaveMessageModule.java +++ b/common/src/main/java/com/discordsrv/common/feature/messageforwarding/game/LeaveMessageModule.java @@ -136,6 +136,7 @@ public void setPlaceholders( MinecraftComponent messageComponent = event.getMessage(); Component message = messageComponent != null ? ComponentUtil.fromAPI(messageComponent) : null; - formatter.addPlaceholder("message", message); + formatter + .addPlaceholder("message", message); } } diff --git a/fabric/src/main/java/com/discordsrv/fabric/module/AbstractFabricModule.java b/fabric/src/main/java/com/discordsrv/fabric/module/AbstractFabricModule.java index 478f8a48..1db3a9d8 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/module/AbstractFabricModule.java +++ b/fabric/src/main/java/com/discordsrv/fabric/module/AbstractFabricModule.java @@ -18,6 +18,7 @@ package com.discordsrv.fabric.module; +import com.discordsrv.common.core.logging.Logger; import com.discordsrv.common.core.module.type.AbstractModule; import com.discordsrv.fabric.FabricDiscordSRV; @@ -29,6 +30,10 @@ public AbstractFabricModule(FabricDiscordSRV discordSRV) { super(discordSRV); } + public AbstractFabricModule(FabricDiscordSRV discordSRV, Logger logger) { + super(discordSRV, logger); + } + @Override public void enable() { enabled = true; diff --git a/fabric/src/main/java/com/discordsrv/fabric/module/ban/FabricBanModule.java b/fabric/src/main/java/com/discordsrv/fabric/module/ban/FabricBanModule.java index bd8a22bf..118f0b9e 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/module/ban/FabricBanModule.java +++ b/fabric/src/main/java/com/discordsrv/fabric/module/ban/FabricBanModule.java @@ -22,6 +22,7 @@ import com.discordsrv.api.module.type.PunishmentModule; import com.discordsrv.api.punishment.Punishment; import com.discordsrv.common.abstraction.player.IPlayer; +import com.discordsrv.common.core.logging.NamedLogger; import com.discordsrv.common.feature.bansync.BanSyncModule; import com.discordsrv.common.util.ComponentUtil; import com.discordsrv.fabric.FabricDiscordSRV; @@ -48,7 +49,7 @@ public class FabricBanModule extends AbstractFabricModule implements PunishmentM private static FabricBanModule instance; public FabricBanModule(FabricDiscordSRV discordSRV) { - super(discordSRV); + super(discordSRV, new NamedLogger(discordSRV, "BAN_MODULE")); instance = this; } diff --git a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricAdvancementModule.java b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricAdvancementModule.java index dd35094c..9d21c801 100644 --- a/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricAdvancementModule.java +++ b/fabric/src/main/java/com/discordsrv/fabric/module/chat/FabricAdvancementModule.java @@ -21,20 +21,25 @@ import com.discordsrv.api.component.MinecraftComponent; import com.discordsrv.api.events.message.receive.game.AwardMessageReceiveEvent; import com.discordsrv.common.abstraction.player.IPlayer; +import com.discordsrv.common.core.logging.NamedLogger; import com.discordsrv.common.util.ComponentUtil; import com.discordsrv.fabric.FabricDiscordSRV; import com.discordsrv.fabric.module.AbstractFabricModule; import net.minecraft.advancement.Advancement; +import net.minecraft.advancement.AdvancementDisplay; import net.minecraft.advancement.AdvancementEntry; +import net.minecraft.advancement.AdvancementFrame; import net.minecraft.server.network.ServerPlayerEntity; +import java.util.Optional; + public class FabricAdvancementModule extends AbstractFabricModule { private static FabricAdvancementModule instance; private final FabricDiscordSRV discordSRV; public FabricAdvancementModule(FabricDiscordSRV discordSRV) { - super(discordSRV); + super(discordSRV, new NamedLogger(discordSRV, "ADVANCEMENT_MODULE")); this.discordSRV = discordSRV; instance = this; } @@ -44,20 +49,30 @@ public static void onGrant(AdvancementEntry advancementEntry, ServerPlayerEntity FabricDiscordSRV discordSRV = instance.discordSRV; Advancement advancement = advancementEntry.value(); - if (advancement.display().isEmpty() || advancement.name().isEmpty()) return; // Usually a crafting recipe. - MinecraftComponent advancementTitle = ComponentUtil.toAPI(discordSRV.getAdventure().asAdventure(advancement.display().get().getTitle())); - // TODO: Add description to the event. So we can explain how the player got the advancement. -// String description = Formatting.strip(advancement.display().get().getDescription().getString()); -// MinecraftComponent advancementDescription = ComponentUtil.fromPlain(description); + Optional displayOptional = advancement.display(); + if (displayOptional.isEmpty() || !displayOptional.get().shouldAnnounceToChat()) { + instance.logger().trace("Skipping advancement display of \"" + (advancement.name().isPresent() ? advancement.name().get() : advancement) + "\" for " + + owner + ": advancement display == null or does not broadcast to chat"); + return; + } + + AdvancementDisplay display = displayOptional.get(); + AdvancementFrame frame = display.getFrame(); + + MinecraftComponent name = ComponentUtil.toAPI(discordSRV.getAdventure().asAdventure(display.getTitle())); + MinecraftComponent title = ComponentUtil.toAPI(discordSRV.getAdventure().asAdventure(frame.getChatAnnouncementText(advancementEntry, owner))); + MinecraftComponent description = ComponentUtil.toAPI(discordSRV.getAdventure().asAdventure(display.getDescription())); IPlayer player = discordSRV.playerProvider().player(owner); discordSRV.eventBus().publish( new AwardMessageReceiveEvent( null, player, - advancementTitle, - null, + name, + title, + description, + AwardMessageReceiveEvent.AdvancementFrame.valueOf(frame.toString()), null, false )